1//
2// Copyright 2019 Pixar
3//
4// Licensed under the Apache License, Version 2.0 (the "Apache License")
5// with the following modification; you may not use this file except in
6// compliance with the Apache License and the following modification to it:
7// Section 6. Trademarks. is deleted and replaced with:
8//
9// 6. Trademarks. This License does not grant permission to use the trade
10//    names, trademarks, service marks, or product names of the Licensor
11//    and its affiliates, except as required to comply with Section 4(c) of
12//    the License and to reproduce the content of the NOTICE file.
13//
14// You may obtain a copy of the Apache License at
15//
16//     http://www.apache.org/licenses/LICENSE-2.0
17//
18// Unless required by applicable law or agreed to in writing, software
19// distributed under the Apache License with the above modification is
20// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21// KIND, either express or implied. See the Apache License for the specific
22// language governing permissions and limitations under the Apache License.
23//
24shader UsdPreviewSurfaceParameters
25(
26    // Inputs
27    color diffuseColor = color(0.18, 0.18, 0.18),
28    color emissiveColor = color(0.0, 0.0, 0.0),
29    int useSpecularWorkflow = 0,
30    color specularColor = color(0.0, 0.0, 0.0),
31    float metallic = 0.0,
32    float roughness = 0.5,
33    float clearcoat = 0.0,
34    float clearcoatRoughness = 0.01,
35    float opacity = 1.0,
36    float opacityThreshold = 0.0,
37    float ior = 1.5,
38    normal normalIn = normal(0.0, 0.0, 1.0),
39    float displacement = 0.0,
40    float occlusion = 1.0,
41
42    // Diffuse outputs
43    output float diffuseGainOut = 0.0
44        [[ string widget = "null" ]],
45    output color diffuseColorOut = color(0.0,0.0,0.0)
46        [[ string widget = "null" ]],
47
48    // Specular outputs
49    output color specularFaceColorOut = color(0.0,0.0,0.0)
50        [[ string widget = "null" ]],
51    output color specularEdgeColorOut = color(0.0,0.0,0.0)
52        [[ string widget = "null" ]],
53    output float specularRoughnessOut = 0.0
54        [[ string widget = "null" ]],
55    output color specularIorOut = color(0.0)
56        [[ string widget = "null" ]],
57
58    // Clearcoat outputs
59    output color clearcoatFaceColorOut = color(0.0,0.0,0.0)
60        [[ string widget = "null" ]],
61    output color clearcoatEdgeColorOut = color(0.0,0.0,0.0)
62        [[ string widget = "null" ]],
63    output float clearcoatRoughnessOut = 0.0
64        [[ string widget = "null" ]],
65
66    // Emissive outputs
67    output float glowGainOut = 0.0
68        [[ string widget = "null" ]],
69    output color glowColorOut = color(0.0,0.0,0.0)
70        [[ string widget = "null" ]],
71
72    // Normal outputs
73    output normal bumpNormalOut = normal(0.0,0.0,0.0)
74        [[ string widget = "null" ]],
75
76    // Opacity
77    output float refractionGainOut = 0.0
78        [[ string widget = "null" ]],
79    output float glassIorOut= 0.0
80        [[ string widget = "null" ]],
81
82    // Presence output
83    output float presenceOut = 1.0
84        [[ string widget = "null" ]],
85
86    // Displacement output
87    output float dispAmountOut = 0.0
88        [[ string widget = "null" ]],
89    output float dispScalarOut = 0.0
90        [[ string widget = "null" ]],
91)
92{
93    // Diffuse
94    if (diffuseColor[0] > 0.0 ||
95        diffuseColor[1] > 0.0 ||
96        diffuseColor[2] > 0.0)
97    {
98        diffuseGainOut = 1.0;
99        diffuseColorOut = diffuseColor;
100    }
101
102    // Opacity Threshold
103    float opacityAdjusted = opacity;
104    if (opacityThreshold > 0.0) {
105        if (opacity < opacityThreshold) {
106            presenceOut = 0.0;
107        } else {
108            opacityAdjusted = 1.0;
109        }
110    }
111
112    // Opacity
113    if (opacityAdjusted < 1.0) {
114        glassIorOut = ior;
115        refractionGainOut = 1.0 - opacityAdjusted;
116        diffuseGainOut *= opacityAdjusted;
117    }
118
119    // Specular
120    // Assumes "Physical" for both Specular and Rough Specular
121    float r = (1.0 - ior) / (1.0 + ior);
122    if (useSpecularWorkflow) {
123        specularFaceColorOut = specularColor;
124        specularEdgeColorOut = color(1.0,1.0,1.0);
125    } else {
126        float metal = clamp(metallic, 0.0, 1.0);
127        color spec = mix(color(1.0,1.0,1.0), diffuseColor, metal);
128        specularFaceColorOut = mix(r * r * spec, spec, metal);
129        specularEdgeColorOut = spec;
130
131        diffuseGainOut *= 1.0 - metal;
132    }
133    specularIorOut = color(ior);
134    specularRoughnessOut = roughness;
135
136    // Clearcoat
137    if (clearcoat > 0.0) {
138        color clearcoatColor = color(1.0, 1.0, 1.0);
139        clearcoatFaceColorOut = clearcoat * r * r * clearcoatColor;
140        clearcoatEdgeColorOut = clearcoat * clearcoatColor;
141        clearcoatRoughnessOut = clearcoatRoughness;
142    }
143
144    // Emissive
145    if (emissiveColor[0] > 0.0 ||
146        emissiveColor[1] > 0.0 ||
147        emissiveColor[2] > 0.0)
148    {
149        glowGainOut = 1.0;
150        glowColorOut = emissiveColor;
151    }
152
153    // Normal map
154    // Convert tangent space normalIn to bumpNormalOut
155    {
156        vector Tn = vector (1.0,0.0,0.0);
157        getattribute("builtin", "Tn", Tn);
158        vector Bn = normalize(cross(N, Tn));
159
160        // Convert from tangent-space normal to current space
161        bumpNormalOut = Tn * normalIn[0] +
162                        Bn * normalIn[1] +
163                         N * normalIn[2];
164        bumpNormalOut = normalize(bumpNormalOut);
165
166        normal Ngn = N;
167        getattribute("builtin", "Ngn", Ngn);
168
169        // Is it backwards from the geometric normal
170        if (dot(Ngn, bumpNormalOut) < 0.0) {
171            bumpNormalOut = -bumpNormalOut;
172        }
173    }
174
175    // Displacement
176    if (displacement != 0.0)
177    {
178        dispAmountOut = 0.1;
179        dispScalarOut = displacement;
180    }
181}
182