3 // https://github.com/KhronosGroup/glTF-Sample-Viewer/blob/glTF-WebGL-PBR/shaders/pbr-frag.glsl
4 // Commit dc84b5e374fb3d23153d2248a338ef88173f9eb6
6 // This fragment shader defines a reference implementation for Physically Based Shading of
7 // a microfacet surface material defined by a glTF model.For the DamagedHelmet.gltf and its Assets
10 // [1] Real Shading in Unreal Engine 4
11 // http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
12 // [2] Physically Based Shading at Disney
13 // http://blog.selfshadow.com/publications/s2012-shading-course/burley/s2012_pbs_disney_brdf_notes_v3.pdf
14 // [3] README.md - Environment Maps
15 // https://github.com/KhronosGroup/glTF-Sample-Viewer/#environment-maps
16 // [4] \"An Inexpensive BRDF Model for Physically based Rendering\" by Christophe Schlick
17 // https://www.cs.virginia.edu/~jdl/bib/appearance/analytic%20models/schlick94b.pdf
20 precision highp float;
22 precision mediump float;
31 #endif //GLTF_CHANNELS
33 uniform lowp vec4 uColor; // Color from SceneGraph
34 uniform lowp vec4 uColorFactor; // Color from material
35 uniform lowp float uMetallicFactor;
36 uniform lowp float uRoughnessFactor;
37 uniform lowp float uDielectricSpecular;
41 uniform sampler2D sAlbedoAlpha;
42 uniform float uBaseColorTextureTransformAvailable;
43 uniform mat3 uBaseColorTextureTransform;
44 #endif // BASECOLOR_TEX
45 #ifdef METALLIC_ROUGHNESS_TEX
46 uniform sampler2D sMetalRoughness;
47 uniform float uMetalRoughnessTextureTransformAvailable;
48 uniform mat3 uMetalRoughnessTextureTransform;
49 #endif // METALLIC_ROUGHNESS_TEX
51 uniform sampler2D sNormal;
52 uniform float uNormalTextureTransformAvailable;
53 uniform mat3 uNormalTextureTransform;
54 uniform float uNormalScale;
57 uniform sampler2D sAlbedoMetal;
58 uniform float uBaseColorTextureTransformAvailable;
59 uniform mat3 uBaseColorTextureTransform;
60 uniform sampler2D sNormalRoughness;
61 uniform float uNormalRoughnessTextureTransformAvailable;
62 uniform mat3 uNormalRoughnessTextureTransform;
68 uniform sampler2D sOcclusion;
69 uniform float uOcclusionTextureTransformAvailable;
70 uniform mat3 uOcclusionTextureTransform;
71 uniform float uOcclusionStrength;
74 #ifdef EMISSIVE_TEXTURE
75 uniform sampler2D sEmissive;
76 uniform float uEmissiveTextureTransformAvailable;
77 uniform mat3 uEmissiveTextureTransform;
79 uniform vec3 uEmissiveFactor;
81 uniform float uSpecularFactor;
82 uniform vec3 uSpecularColorFactor;
83 #ifdef MATERIAL_SPECULAR_TEXTURE
84 uniform sampler2D sSpecular;
86 #ifdef MATERIAL_SPECULAR_COLOR_TEXTURE
87 uniform sampler2D sSpecularColor;
90 // For Light (Currently Directional Only)
92 uniform mediump int uLightCount;
93 uniform mediump vec3 uLightDirection[MAX_LIGHTS];
94 uniform mediump vec3 uLightColor[MAX_LIGHTS];
97 uniform lowp int uIsShadowEnabled;
98 uniform sampler2D sShadowMap;
99 #ifdef GLSL_VERSION_1_0
100 uniform int uShadowMapWidth;
101 uniform int uShadowMapHeight;
103 INPUT highp vec3 positionFromLightView;
106 uniform sampler2D sbrdfLUT;
107 uniform samplerCube sDiffuseEnvSampler;
108 uniform samplerCube sSpecularEnvSampler;
109 uniform float uIblIntensity;
110 uniform mediump vec3 uYDirection;
111 uniform float uMaxLOD;
114 uniform lowp float uOpaque;
115 uniform lowp float uMask;
116 uniform lowp float uAlphaThreshold;
118 // TODO: Multiple texture coordinate will be supported.
119 INPUT mediump vec2 vUV;
120 INPUT lowp mat3 vTBN;
121 INPUT lowp vec4 vColor;
122 INPUT highp vec3 vPositionToCamera;
124 const float c_MinRoughness = 0.04;
125 const float M_PI = 3.141592653589793;
127 // These properties can be used for circular sampling for PCF
129 // Percentage Closer Filtering to mitigate the banding artifacts.
130 const int kPcfSampleCount = 9;
132 const float kPi = 3.141592653589;
133 const float kInvSampleCount = 1.0 / float(kPcfSampleCount);
134 const float kPcfTheta = 2.0 * kPi * kInvSampleCount;
135 const float kSinPcfTheta = sin(kPcfTheta);
136 const float kCosPcfTheta = cos(kPcfTheta);
138 uniform lowp int uEnableShadowSoftFiltering;
139 uniform mediump float uShadowIntensity;
140 uniform highp float uShadowBias;
142 vec3 linear(vec3 color)
144 return pow(color, vec3(2.2));
147 vec2 computeTextureTransform(vec2 texCoord, mat3 textureTransform)
149 return vec2(textureTransform * vec3(texCoord, 1.0));
154 // Metallic and Roughness material properties are packed together
155 // In glTF, these factors can be specified by fixed scalar values
156 // or from a metallic-roughness map
157 // Roughness is stored in the 'g' channel, metallic is stored in the 'b' channel.
158 // This layout intentionally reserves the 'r' channel for (optional) occlusion map data
159 lowp float metallic = uMetallicFactor;
160 lowp float perceptualRoughness = uRoughnessFactor;
161 // If there isn't normal texture, use surface normal
162 highp vec3 n = normalize(vTBN[2].xyz);
165 // The albedo may be defined from a base texture or a flat color
167 mediump vec2 baseColorTexCoords = mix(vUV, computeTextureTransform(vUV, uBaseColorTextureTransform), uBaseColorTextureTransformAvailable);
168 lowp vec4 baseColor = TEXTURE(sAlbedoAlpha, baseColorTexCoords);
169 baseColor = vColor * vec4(linear(baseColor.rgb), baseColor.w) * uColorFactor;
170 #else // BASECOLOR_TEX
171 lowp vec4 baseColor = vColor * uColorFactor;
172 #endif // BASECOLOR_TEX
174 #ifdef METALLIC_ROUGHNESS_TEX
175 mediump vec2 metalRoughnessTexCoords = mix(vUV, computeTextureTransform(vUV, uMetalRoughnessTextureTransform), uMetalRoughnessTextureTransformAvailable);
176 lowp vec4 metrou = TEXTURE(sMetalRoughness, metalRoughnessTexCoords);
177 metallic = metrou.METALLIC * metallic;
178 perceptualRoughness = metrou.ROUGHNESS * perceptualRoughness;
179 #endif // METALLIC_ROUGHNESS_TEX
182 mediump vec2 normalTexCoords = mix(vUV, computeTextureTransform(vUV, uNormalTextureTransform), uNormalTextureTransformAvailable);
183 n = TEXTURE(sNormal, normalTexCoords).rgb;
184 n = normalize(vTBN * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
187 mediump vec2 baseColorTexCoords = mix(vUV, computeTextureTransform(vUV, uBaseColorTextureTransform), uBaseColorTextureTransformAvailable);
188 vec4 albedoMetal = TEXTURE(sAlbedoMetal, baseColorTexCoords);
189 lowp vec4 baseColor = vec4(linear(albedoMetal.rgb), 1.0) * vColor * uColorFactor;
191 metallic = albedoMetal.METALLIC * metallic;
193 mediump vec2 normalRoughnessTexCoords = mix(vUV, computeTextureTransform(vUV, uNormalRoughnessTextureTransform), uNormalRoughnessTextureTransformAvailable);
194 vec4 normalRoughness = TEXTURE(sNormalRoughness, normalRoughnessTexCoords);
195 perceptualRoughness = normalRoughness.ROUGHNESS * perceptualRoughness;
197 n = normalRoughness.rgb;
198 n = normalize(vTBN * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
201 // The value of uOpaque and uMask can be 0.0 or 1.0.
202 // If uMask is 1.0, a Pixel that has bigger alpha than uAlphaThreashold becomes fully opaque,
203 // and, a pixel that has smaller alpha than uAlphaThreashold becomes fully transparent.
204 // If uOpaque is 1.0, alpha value of final color is 1.0;
205 // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#_material_alphamode
206 if(uMask > 0.5 && baseColor.a < uAlphaThreshold)
210 baseColor.a = mix(baseColor.a, 1.0, uOpaque);
212 metallic = clamp(metallic, 0.0, 1.0);
213 // Roughness is authored as perceptual roughness; as is convention,
214 // convert to material roughness by squaring the perceptual roughness [2].
215 perceptualRoughness = clamp(perceptualRoughness, c_MinRoughness, 1.0);
218 lowp vec3 f0 = vec3(uDielectricSpecular);
221 float specularWeight = 1.0;
222 vec4 materialSpecularTexture = vec4(1.0);
223 #ifdef MATERIAL_SPECULAR_TEXTURE
224 materialSpecularTexture.a = TEXTURE(sSpecular, vUV).a;
226 #ifdef MATERIAL_SPECULAR_COLOR_TEXTURE
227 materialSpecularTexture.rgb = TEXTURE(sSpecularColor, vUV).rgb;
229 specularWeight = uSpecularFactor * materialSpecularTexture.a;
230 f0 = min(f0 * uSpecularColorFactor * materialSpecularTexture.rgb, vec3(1.0));
231 f0 = mix(f0, baseColor.rgb, metallic);
233 mediump vec3 v = normalize(vPositionToCamera); // Vector from surface point to camera
234 mediump float NdotV = clamp(abs(dot(n, v)), 0.001, 1.0);
235 mediump vec3 reflection = -normalize(reflect(v, n));
236 lowp vec3 brdf = TEXTURE(sbrdfLUT, vec2(NdotV, 1.0 - perceptualRoughness)).rgb;
237 vec3 Fr = max(vec3(1.0 - perceptualRoughness), f0) - f0;
238 vec3 k_S = f0 + Fr * pow(1.0 - NdotV, 5.0);
239 vec3 FssEss = specularWeight * (k_S * brdf.x + brdf.y);
242 // uMaxLOD that means mipmap level of specular texture is used for bluring of reflection of specular following roughness.
243 float lod = perceptualRoughness * (uMaxLOD - 1.0);
244 #ifdef GLSL_VERSION_1_0
245 // glsl 1.0 doesn't support textureLod. Let we just use textureCube instead.
246 lowp vec3 specularLight = linear(textureCube(sSpecularEnvSampler, reflection * uYDirection).rgb);
248 lowp vec3 specularLight = linear(textureLod(sSpecularEnvSampler, reflection * uYDirection, lod).rgb);
250 lowp vec3 specular = specularLight * FssEss;
253 lowp vec3 diffuseColor = mix(baseColor.rgb, vec3(0), metallic);
254 #ifdef GLSL_VERSION_1_0
255 lowp vec3 irradiance = linear(textureCube(sDiffuseEnvSampler, n * uYDirection).rgb);
257 lowp vec3 irradiance = linear(TEXTURE(sDiffuseEnvSampler, n * uYDirection).rgb);
259 float Ems = (1.0 - (brdf.x + brdf.y));
260 vec3 F_avg = specularWeight * (f0 + (1.0 - f0) / 21.0);
261 vec3 FmsEms = Ems * FssEss * F_avg / (1.0 - F_avg * Ems);
262 vec3 k_D = diffuseColor * (1.0 - FssEss + FmsEms);
263 lowp vec3 diffuse = (FmsEms + k_D) * irradiance;
265 lowp vec3 color = (diffuse + specular) * uIblIntensity;
270 // Compute reflectance.
271 lowp float reflectance = max(max(f0.r, f0.g), f0.b);
272 lowp float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
273 lowp float r = perceptualRoughness * perceptualRoughness;
274 lowp float attenuationV = 2.0 * NdotV / (NdotV + sqrt(r * r + (1.0 - r * r) * (NdotV * NdotV)));
275 mediump float roughnessSq = r * r;
276 lowp vec3 diffuseColorPunctual = baseColor.rgb * (vec3(1.0) - f0);
277 diffuseColorPunctual *= ( 1.0 - metallic );
279 for(int i = 0; i < uLightCount; ++i)
281 highp vec3 l = normalize(-uLightDirection[i]); // Vector from surface point to light
282 mediump vec3 h = normalize(l+v); // Half vector between both l and v
283 mediump float VdotH = dot(v, h);
284 lowp vec3 specularReflection = f0 + (reflectance90 - f0) * pow(clamp(1.0 - VdotH, 0.0, 1.0), 5.0);
286 mediump float NdotL = clamp(dot(n, l), 0.001, 1.0);
287 lowp float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
288 lowp float geometricOcclusion = attenuationL * attenuationV;
290 highp float NdotH = dot(n, h);
291 highp float f = (NdotH * roughnessSq - NdotH) * NdotH + 1.0;
292 lowp float microfacetDistribution = roughnessSq / (M_PI * f * f);;
294 // Calculation of analytical lighting contribution
295 lowp vec3 diffuseContrib = ( 1.0 - specularReflection ) * ( diffuseColorPunctual / M_PI );
296 lowp vec3 specContrib = specularReflection * geometricOcclusion * microfacetDistribution / ( 4.0 * NdotL * NdotV );
298 // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
299 color += NdotL * uLightColor[i] * (diffuseContrib + specContrib);
303 if(float(uIsShadowEnabled) * uShadowIntensity > 0.0)
305 mediump float exposureFactor = 0.0;
306 if(uEnableShadowSoftFiltering > 0)
308 #ifdef GLSL_VERSION_1_0
309 ivec2 texSize = ivec2(uShadowMapWidth, uShadowMapHeight);
311 ivec2 texSize = textureSize(sShadowMap, 0);
313 mediump vec2 texelSize = vec2(1.0) / vec2(texSize.x, texSize.y);
314 mediump vec2 pcfSample = vec2(1.0, 0.0);
315 for (int i = 0; i < kPcfSampleCount; ++i)
317 pcfSample = vec2(kCosPcfTheta * pcfSample.x - kSinPcfTheta * pcfSample.y,
318 kSinPcfTheta * pcfSample.x + kCosPcfTheta * pcfSample.y);
319 lowp float depthValue = TEXTURE(sShadowMap, positionFromLightView.xy + pcfSample * texelSize).r;
320 exposureFactor += (depthValue < positionFromLightView.z - uShadowBias) ? 0.0 : 1.0;
322 exposureFactor *= kInvSampleCount;
326 mediump float depthValue = TEXTURE(sShadowMap, positionFromLightView.xy).r;
327 exposureFactor = (depthValue < positionFromLightView.z - uShadowBias) ? 0.0 : 1.0;
329 color *= (1.0 - (1.0 - exposureFactor) * uShadowIntensity);
333 mediump vec2 occlusionTexCoords = mix(vUV, computeTextureTransform(vUV, uOcclusionTextureTransform), uOcclusionTextureTransformAvailable);
334 lowp float ao = TEXTURE(sOcclusion, occlusionTexCoords).r;
335 color = mix(color, color * ao, uOcclusionStrength);
338 #ifdef EMISSIVE_TEXTURE
339 mediump vec2 emissiveTexCoords = mix(vUV, computeTextureTransform(vUV, uEmissiveTextureTransform), uEmissiveTextureTransformAvailable);
340 lowp vec3 emissive = linear(TEXTURE(sEmissive, emissiveTexCoords).rgb) * uEmissiveFactor;
342 lowp vec3 emissive = uEmissiveFactor;
343 #endif // EMISSIVE_TEXTURE
346 OUT_COLOR = vec4(pow(color, vec3(1.0 / 2.2)), baseColor.a) * uColor;