6 precision mediump float;
11 // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#pbrmetallicroughnessmetallicroughnesstexture
17 #endif //GLTF_CHANNELS
21 uniform sampler2D sAlbedoAlpha;
22 uniform sampler2D sMetalRoughness;
23 uniform sampler2D sNormal;
26 uniform float uAlphaThreshold;
30 uniform sampler2D sAlbedoMetal;
31 uniform sampler2D sNormalRoughness;
35 uniform sampler2D sOcclusion;
36 uniform float uOcclusionStrength;
40 uniform sampler2D sEmissive;
41 uniform vec3 uEmissiveFactor;
44 uniform samplerCube sDiffuse;
45 uniform samplerCube sSpecular;
47 // Number of mip map levels in the texture
48 uniform float uMaxLOD;
50 // Transformation matrix of the cubemap texture
51 uniform mat4 uCubeMatrix;
54 uniform float uMetallicFactor;
55 uniform float uRoughnessFactor;
58 uniform float uIblIntensity;
60 // TODO: Multiple texture coordinate will be supported.
68 // Functions for BRDF calculation come from
69 // https://www.unrealengine.com/blog/physically-based-shading-on-mobile
70 // Based on the paper by Dimitar Lazarov
71 // http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
72 vec3 EnvBRDFApprox( vec3 SpecularColor, float Roughness, float NoV )
74 const vec4 c0 = vec4( -1.0, -0.0275, -0.572, 0.022 );
75 const vec4 c1 = vec4( 1.0, 0.0425, 1.04, -0.04 );
76 vec4 r = Roughness * c0 + c1;
77 float a004 = min( r.x * r.x, exp2( -9.28 * NoV ) ) * r.x + r.y;
78 vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;
80 return SpecularColor * AB.x + AB.y;
85 // We get information from the maps (albedo, normal map, roughness, metalness
86 // I access the maps in the order they will be used
88 vec4 albedoAlpha = texture(sAlbedoAlpha, vUV.st);
89 float alpha = albedoAlpha.a;
91 if (alpha <= uAlphaThreshold)
96 vec3 albedoColor = albedoAlpha.rgb * uColor.rgb;
98 vec4 metalRoughness = texture(sMetalRoughness, vUV.st);
99 float metallic = metalRoughness.METALLIC * uMetallicFactor;
100 float roughness = metalRoughness.ROUGHNESS * uRoughnessFactor;
102 vec3 normalMap = texture(sNormal, vUV.st).rgb;
104 vec4 albedoMetal = texture(sAlbedoMetal, vUV.st);
105 vec3 albedoColor = albedoMetal.rgb * uColor.rgb;
106 float metallic = albedoMetal.a * uMetallicFactor;
108 vec4 normalRoughness = texture(sNormalRoughness, vUV.st);
109 vec3 normalMap = normalRoughness.rgb;
110 float roughness = normalRoughness.a * uRoughnessFactor;
113 vec3 normal = normalize(vNormal);
114 vec3 tangent = normalize(vTangent);
116 // NOTE: normal and tangent have to be orthogonal for the result of the cross()
117 // product to be a unit vector. We might find that we need to normalize().
118 vec3 bitangent = cross(normal, tangent);
120 vec3 viewVec = normalize(vViewVec);
122 // Create Inverse Local to world matrix
123 mat3 vInvTBN = mat3(tangent, bitangent, normal);
125 // Get normal map info in world space
126 normalMap = normalize(normalMap - 0.5);
127 vec3 newNormal = vInvTBN * normalMap.rgb;
129 // Calculate normal dot view vector
130 float NoV = max(dot(newNormal, -viewVec), 0.0);
133 vec3 reflectionVec = reflect(viewVec, newNormal);
135 //transform it now to environment coordinates (used when the environment rotates)
136 vec3 reflecCube = (uCubeMatrix * vec4( reflectionVec, 0.0 ) ).xyz;
137 reflecCube = normalize( reflecCube );
139 //transform it now to environment coordinates
140 vec3 normalCube = ( uCubeMatrix * vec4( newNormal, 0.0 ) ).xyz;
141 normalCube = normalize( normalCube );
143 // Get irradiance from diffuse cubemap
144 vec3 irradiance = texture( sDiffuse, normalCube ).rgb;
146 // Access reflection color using roughness value
147 float finalLod = mix( 0.0, uMaxLOD - 2.0, roughness);
148 vec3 reflectionColor = textureLod(sSpecular, reflecCube, finalLod).rgb;
150 // We are supposed to be using DielectricColor (0.04) of a plastic (almost everything)
151 // http://blog.selfshadow.com/publications/s2014-shading-course/hoffman/s2014_pbs_physics_math_slides.pdf
152 // however that seems to prevent achieving very dark tones (i.e. get dark gray blacks).
153 vec3 DiffuseColor = albedoColor - albedoColor * metallic; // 1 mad
154 vec3 SpecularColor = mix( vec3(0.04), albedoColor, metallic); // 2 mad
156 // Calculate specular color using Magic Function (takes original roughness and normal dot view).
157 vec3 specColor = reflectionColor.rgb * EnvBRDFApprox(SpecularColor, roughness, NoV );
159 // Multiply the result by albedo texture and do energy conservation
160 vec3 diffuseColor = irradiance * DiffuseColor;
162 // Final color is the sum of the diffuse and specular term
163 vec3 finalColor = diffuseColor + specColor;
165 finalColor = sqrt( finalColor ) * uIblIntensity;
169 float ao = texture(sOcclusion, vUV.st).r;
170 finalColor = mix( finalColor, finalColor * ao, uOcclusionStrength );
174 vec3 emissive = texture( sEmissive, vUV.st ).rgb * uEmissiveFactor;
175 finalColor += emissive;
179 FragColor = vec4( finalColor, alpha );
181 FragColor = vec4( finalColor, 1.0 );