|
@@ -93,6 +93,7 @@ uniform vec3 Material[6];
|
|
|
// It should be called for each texture index. It uses two externally defined variables:
|
|
// It should be called for each texture index. It uses two externally defined variables:
|
|
|
// vec4 texColor
|
|
// vec4 texColor
|
|
|
// vec4 texMixed
|
|
// vec4 texMixed
|
|
|
|
|
+// TODO alpha blending (dont use mix) see panel shader
|
|
|
#define MIX_TEXTURE(i) \
|
|
#define MIX_TEXTURE(i) \
|
|
|
if (MatTexVisible(i)) { \
|
|
if (MatTexVisible(i)) { \
|
|
|
texColor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i)); \
|
|
texColor = texture(MatTexture[i], FragTexcoord * MatTexRepeat(i) + MatTexOffset(i)); \
|
|
@@ -536,22 +537,21 @@ precision highp float;
|
|
|
//#endif
|
|
//#endif
|
|
|
|
|
|
|
|
#ifdef HAS_BASECOLORMAP
|
|
#ifdef HAS_BASECOLORMAP
|
|
|
-uniform sampler2D u_BaseColorSampler;
|
|
|
|
|
|
|
+uniform sampler2D uBaseColorSampler;
|
|
|
|
|
+#endif
|
|
|
|
|
+#ifdef HAS_METALROUGHNESSMAP
|
|
|
|
|
+uniform sampler2D uMetallicRoughnessSampler;
|
|
|
#endif
|
|
#endif
|
|
|
#ifdef HAS_NORMALMAP
|
|
#ifdef HAS_NORMALMAP
|
|
|
-uniform sampler2D u_NormalSampler;
|
|
|
|
|
-uniform float u_NormalScale;
|
|
|
|
|
|
|
+uniform sampler2D uNormalSampler;
|
|
|
|
|
+//uniform float uNormalScale;
|
|
|
#endif
|
|
#endif
|
|
|
#ifdef HAS_EMISSIVEMAP
|
|
#ifdef HAS_EMISSIVEMAP
|
|
|
-uniform sampler2D u_EmissiveSampler;
|
|
|
|
|
-uniform vec3 u_EmissiveFactor;
|
|
|
|
|
-#endif
|
|
|
|
|
-#ifdef HAS_METALROUGHNESSMAP
|
|
|
|
|
-uniform sampler2D u_MetallicRoughnessSampler;
|
|
|
|
|
|
|
+uniform sampler2D uEmissiveSampler;
|
|
|
#endif
|
|
#endif
|
|
|
#ifdef HAS_OCCLUSIONMAP
|
|
#ifdef HAS_OCCLUSIONMAP
|
|
|
-uniform sampler2D u_OcclusionSampler;
|
|
|
|
|
-uniform float u_OcclusionStrength;
|
|
|
|
|
|
|
+uniform sampler2D uOcclusionSampler;
|
|
|
|
|
+uniform float uOcclusionStrength;
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
// Material parameters uniform array
|
|
// Material parameters uniform array
|
|
@@ -576,13 +576,17 @@ out vec4 FragColor;
|
|
|
// Encapsulate the various inputs used by the various functions in the shading equation
|
|
// Encapsulate the various inputs used by the various functions in the shading equation
|
|
|
// We store values in this struct to simplify the integration of alternative implementations
|
|
// We store values in this struct to simplify the integration of alternative implementations
|
|
|
// of the shading terms, outlined in the Readme.MD Appendix.
|
|
// of the shading terms, outlined in the Readme.MD Appendix.
|
|
|
-struct PBRInfo
|
|
|
|
|
|
|
+struct PBRLightInfo
|
|
|
{
|
|
{
|
|
|
float NdotL; // cos angle between normal and light direction
|
|
float NdotL; // cos angle between normal and light direction
|
|
|
float NdotV; // cos angle between normal and view direction
|
|
float NdotV; // cos angle between normal and view direction
|
|
|
float NdotH; // cos angle between normal and half vector
|
|
float NdotH; // cos angle between normal and half vector
|
|
|
float LdotH; // cos angle between light direction and half vector
|
|
float LdotH; // cos angle between light direction and half vector
|
|
|
float VdotH; // cos angle between view direction and half vector
|
|
float VdotH; // cos angle between view direction and half vector
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+struct PBRInfo
|
|
|
|
|
+{
|
|
|
float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)
|
|
float perceptualRoughness; // roughness value, as authored by the model creator (input to shader)
|
|
|
float metalness; // metallic value at the surface
|
|
float metalness; // metallic value at the surface
|
|
|
vec3 reflectance0; // full reflectance color (normal incidence angle)
|
|
vec3 reflectance0; // full reflectance color (normal incidence angle)
|
|
@@ -600,12 +604,12 @@ vec4 SRGBtoLINEAR(vec4 srgbIn) {
|
|
|
// #ifdef SRGB_FAST_APPROXIMATION
|
|
// #ifdef SRGB_FAST_APPROXIMATION
|
|
|
// vec3 linOut = pow(srgbIn.xyz,vec3(2.2));
|
|
// vec3 linOut = pow(srgbIn.xyz,vec3(2.2));
|
|
|
// #else //SRGB_FAST_APPROXIMATION
|
|
// #else //SRGB_FAST_APPROXIMATION
|
|
|
-// vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
|
|
|
|
|
-// vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
|
|
|
|
|
|
|
+ vec3 bLess = step(vec3(0.04045),srgbIn.xyz);
|
|
|
|
|
+ vec3 linOut = mix( srgbIn.xyz/vec3(12.92), pow((srgbIn.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), bLess );
|
|
|
// #endif //SRGB_FAST_APPROXIMATION
|
|
// #endif //SRGB_FAST_APPROXIMATION
|
|
|
-// return vec4(linOut,srgbIn.w);
|
|
|
|
|
|
|
+ return vec4(linOut,srgbIn.w);
|
|
|
//#else //MANUAL_SRGB
|
|
//#else //MANUAL_SRGB
|
|
|
- return srgbIn;
|
|
|
|
|
|
|
+// return srgbIn;
|
|
|
//#endif //MANUAL_SRGB
|
|
//#endif //MANUAL_SRGB
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -621,11 +625,11 @@ vec3 getNormal()
|
|
|
vec3 tex_dy = dFdy(vec3(FragTexcoord, 0.0));
|
|
vec3 tex_dy = dFdy(vec3(FragTexcoord, 0.0));
|
|
|
vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);
|
|
vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);
|
|
|
|
|
|
|
|
-#ifdef HAS_NORMALS
|
|
|
|
|
|
|
+//#ifdef HAS_NORMALS
|
|
|
vec3 ng = normalize(Normal);
|
|
vec3 ng = normalize(Normal);
|
|
|
-#else
|
|
|
|
|
- vec3 ng = cross(pos_dx, pos_dy);
|
|
|
|
|
-#endif
|
|
|
|
|
|
|
+//#else
|
|
|
|
|
+// vec3 ng = cross(pos_dx, pos_dy);
|
|
|
|
|
+//#endif
|
|
|
|
|
|
|
|
t = normalize(t - ng * dot(ng, t));
|
|
t = normalize(t - ng * dot(ng, t));
|
|
|
vec3 b = normalize(cross(ng, t));
|
|
vec3 b = normalize(cross(ng, t));
|
|
@@ -635,6 +639,7 @@ vec3 getNormal()
|
|
|
//#endif
|
|
//#endif
|
|
|
|
|
|
|
|
#ifdef HAS_NORMALMAP
|
|
#ifdef HAS_NORMALMAP
|
|
|
|
|
+ float uNormalScale = 1.0;
|
|
|
vec3 n = texture2D(uNormalSampler, FragTexcoord).rgb;
|
|
vec3 n = texture2D(uNormalSampler, FragTexcoord).rgb;
|
|
|
n = normalize(tbn * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
|
|
n = normalize(tbn * ((2.0 * n - 1.0) * vec3(uNormalScale, uNormalScale, 1.0)));
|
|
|
#else
|
|
#else
|
|
@@ -645,32 +650,32 @@ vec3 getNormal()
|
|
|
return n;
|
|
return n;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-//// Calculation of the lighting contribution from an optional Image Based Light source.
|
|
|
|
|
-//// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].
|
|
|
|
|
-//// See our README.md on Environment Maps [3] for additional discussion.
|
|
|
|
|
-//vec3 getIBLContribution(PBRInfo pbrInputs, vec3 n, vec3 reflection)
|
|
|
|
|
-//{
|
|
|
|
|
-// float mipCount = 9.0; // resolution of 512x512
|
|
|
|
|
-// float lod = (pbrInputs.perceptualRoughness * mipCount);
|
|
|
|
|
-// // retrieve a scale and bias to F0. See [1], Figure 3
|
|
|
|
|
-// vec3 brdf = SRGBtoLINEAR(texture2D(u_brdfLUT, vec2(pbrInputs.NdotV, 1.0 - pbrInputs.perceptualRoughness))).rgb;
|
|
|
|
|
-// vec3 diffuseLight = SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb;
|
|
|
|
|
-//
|
|
|
|
|
-////#ifdef USE_TEX_LOD
|
|
|
|
|
-//// vec3 specularLight = SRGBtoLINEAR(textureCubeLodEXT(u_SpecularEnvSampler, reflection, lod)).rgb;
|
|
|
|
|
-////#else
|
|
|
|
|
-// vec3 specularLight = SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb;
|
|
|
|
|
-////#endif
|
|
|
|
|
-//
|
|
|
|
|
-// vec3 diffuse = diffuseLight * pbrInputs.diffuseColor;
|
|
|
|
|
-// vec3 specular = specularLight * (pbrInputs.specularColor * brdf.x + brdf.y);
|
|
|
|
|
-//
|
|
|
|
|
-// // For presentation, this allows us to disable IBL terms
|
|
|
|
|
|
|
+// Calculation of the lighting contribution from an optional Image Based Light source.
|
|
|
|
|
+// Precomputed Environment Maps are required uniform inputs and are computed as outlined in [1].
|
|
|
|
|
+// See our README.md on Environment Maps [3] for additional discussion.
|
|
|
|
|
+vec3 getIBLContribution(PBRInfo pbrInputs, PBRLightInfo pbrLight, vec3 n, vec3 reflection)
|
|
|
|
|
+{
|
|
|
|
|
+ float mipCount = 9.0; // resolution of 512x512
|
|
|
|
|
+ float lod = (pbrInputs.perceptualRoughness * mipCount);
|
|
|
|
|
+ // retrieve a scale and bias to F0. See [1], Figure 3
|
|
|
|
|
+ vec3 brdf = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(texture2D(u_brdfLUT, vec2(pbrLight.NdotV, 1.0 - pbrInputs.perceptualRoughness))).rgb;
|
|
|
|
|
+ vec3 diffuseLight = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(textureCube(u_DiffuseEnvSampler, n)).rgb;
|
|
|
|
|
+
|
|
|
|
|
+//#ifdef USE_TEX_LOD
|
|
|
|
|
+// vec3 specularLight = SRGBtoLINEAR(textureCubeLodEXT(u_SpecularEnvSampler, reflection, lod)).rgb;
|
|
|
|
|
+//#else
|
|
|
|
|
+ vec3 specularLight = vec3(0.5,0.5,0.5);//SRGBtoLINEAR(textureCube(u_SpecularEnvSampler, reflection)).rgb;
|
|
|
|
|
+//#endif
|
|
|
|
|
+
|
|
|
|
|
+ vec3 diffuse = diffuseLight * pbrInputs.diffuseColor;
|
|
|
|
|
+ vec3 specular = specularLight * (pbrInputs.specularColor * brdf.x + brdf.y);
|
|
|
|
|
+
|
|
|
|
|
+ // For presentation, this allows us to disable IBL terms
|
|
|
// diffuse *= u_ScaleIBLAmbient.x;
|
|
// diffuse *= u_ScaleIBLAmbient.x;
|
|
|
// specular *= u_ScaleIBLAmbient.y;
|
|
// specular *= u_ScaleIBLAmbient.y;
|
|
|
-//
|
|
|
|
|
-// return diffuse + specular;
|
|
|
|
|
-//}
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return diffuse + specular;
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
// Basic Lambertian diffuse
|
|
// Basic Lambertian diffuse
|
|
|
// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog
|
|
// Implementation from Lambert's Photometria https://archive.org/details/lambertsphotome00lambgoog
|
|
@@ -682,19 +687,19 @@ vec3 diffuse(PBRInfo pbrInputs)
|
|
|
|
|
|
|
|
// The following equation models the Fresnel reflectance term of the spec equation (aka F())
|
|
// The following equation models the Fresnel reflectance term of the spec equation (aka F())
|
|
|
// Implementation of fresnel from [4], Equation 15
|
|
// Implementation of fresnel from [4], Equation 15
|
|
|
-vec3 specularReflection(PBRInfo pbrInputs)
|
|
|
|
|
|
|
+vec3 specularReflection(PBRInfo pbrInputs, PBRLightInfo pbrLight)
|
|
|
{
|
|
{
|
|
|
- return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrInputs.VdotH, 0.0, 1.0), 5.0);
|
|
|
|
|
|
|
+ return pbrInputs.reflectance0 + (pbrInputs.reflectance90 - pbrInputs.reflectance0) * pow(clamp(1.0 - pbrLight.VdotH, 0.0, 1.0), 5.0);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// This calculates the specular geometric attenuation (aka G()),
|
|
// This calculates the specular geometric attenuation (aka G()),
|
|
|
// where rougher material will reflect less light back to the viewer.
|
|
// where rougher material will reflect less light back to the viewer.
|
|
|
// This implementation is based on [1] Equation 4, and we adopt their modifications to
|
|
// This implementation is based on [1] Equation 4, and we adopt their modifications to
|
|
|
// alphaRoughness as input as originally proposed in [2].
|
|
// alphaRoughness as input as originally proposed in [2].
|
|
|
-float geometricOcclusion(PBRInfo pbrInputs)
|
|
|
|
|
|
|
+float geometricOcclusion(PBRInfo pbrInputs, PBRLightInfo pbrLight)
|
|
|
{
|
|
{
|
|
|
- float NdotL = pbrInputs.NdotL;
|
|
|
|
|
- float NdotV = pbrInputs.NdotV;
|
|
|
|
|
|
|
+ float NdotL = pbrLight.NdotL;
|
|
|
|
|
+ float NdotV = pbrLight.NdotV;
|
|
|
float r = pbrInputs.alphaRoughness;
|
|
float r = pbrInputs.alphaRoughness;
|
|
|
|
|
|
|
|
float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
|
|
float attenuationL = 2.0 * NdotL / (NdotL + sqrt(r * r + (1.0 - r * r) * (NdotL * NdotL)));
|
|
@@ -705,13 +710,52 @@ float geometricOcclusion(PBRInfo pbrInputs)
|
|
|
// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())
|
|
// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D())
|
|
|
// Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
|
|
// Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz
|
|
|
// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.
|
|
// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3.
|
|
|
-float microfacetDistribution(PBRInfo pbrInputs)
|
|
|
|
|
|
|
+float microfacetDistribution(PBRInfo pbrInputs, PBRLightInfo pbrLight)
|
|
|
{
|
|
{
|
|
|
float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;
|
|
float roughnessSq = pbrInputs.alphaRoughness * pbrInputs.alphaRoughness;
|
|
|
- float f = (pbrInputs.NdotH * roughnessSq - pbrInputs.NdotH) * pbrInputs.NdotH + 1.0;
|
|
|
|
|
|
|
+ float f = (pbrLight.NdotH * roughnessSq - pbrLight.NdotH) * pbrLight.NdotH + 1.0;
|
|
|
return roughnessSq / (M_PI * f * f);
|
|
return roughnessSq / (M_PI * f * f);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+vec3 pbrModel(PBRInfo pbrInputs, vec3 lightColor, vec3 lightDir) {
|
|
|
|
|
+
|
|
|
|
|
+// vec3 lightDir = lightPos - vec3(Position);
|
|
|
|
|
+
|
|
|
|
|
+ vec3 n = getNormal(); // normal at surface point
|
|
|
|
|
+ vec3 v = normalize(CamDir); // Vector from surface point to camera
|
|
|
|
|
+ vec3 l = normalize(lightDir); // Vector from surface point to light
|
|
|
|
|
+ vec3 h = normalize(l+v); // Half vector between both l and v
|
|
|
|
|
+ vec3 reflection = -normalize(reflect(v, n));
|
|
|
|
|
+
|
|
|
|
|
+ float NdotL = clamp(dot(n, l), 0.001, 1.0);
|
|
|
|
|
+ float NdotV = abs(dot(n, v)) + 0.001;
|
|
|
|
|
+ float NdotH = clamp(dot(n, h), 0.0, 1.0);
|
|
|
|
|
+ float LdotH = clamp(dot(l, h), 0.0, 1.0);
|
|
|
|
|
+ float VdotH = clamp(dot(v, h), 0.0, 1.0);
|
|
|
|
|
+
|
|
|
|
|
+ PBRLightInfo pbrLight = PBRLightInfo(
|
|
|
|
|
+ NdotL,
|
|
|
|
|
+ NdotV,
|
|
|
|
|
+ NdotH,
|
|
|
|
|
+ LdotH,
|
|
|
|
|
+ VdotH
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // Calculate the shading terms for the microfacet specular shading model
|
|
|
|
|
+ vec3 F = specularReflection(pbrInputs, pbrLight);
|
|
|
|
|
+ float G = geometricOcclusion(pbrInputs, pbrLight);
|
|
|
|
|
+ float D = microfacetDistribution(pbrInputs, pbrLight);
|
|
|
|
|
+
|
|
|
|
|
+ // Calculation of analytical lighting contribution
|
|
|
|
|
+ vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);
|
|
|
|
|
+ vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);
|
|
|
|
|
+ // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
|
|
|
|
|
+ vec3 color = NdotL * lightColor * (diffuseContrib + specContrib);
|
|
|
|
|
+
|
|
|
|
|
+ return color;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
void main() {
|
|
void main() {
|
|
|
|
|
|
|
|
float perceptualRoughness = uRoughnessFactor;
|
|
float perceptualRoughness = uRoughnessFactor;
|
|
@@ -741,39 +785,24 @@ void main() {
|
|
|
vec3 f0 = vec3(0.04);
|
|
vec3 f0 = vec3(0.04);
|
|
|
vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);
|
|
vec3 diffuseColor = baseColor.rgb * (vec3(1.0) - f0);
|
|
|
diffuseColor *= 1.0 - metallic;
|
|
diffuseColor *= 1.0 - metallic;
|
|
|
|
|
+
|
|
|
|
|
+// vec3 AmbientLight = vec3(0.5);
|
|
|
|
|
+// diffuseColor.rgb += AmbientLight;
|
|
|
|
|
+// diffuseColor *= baseColor.rgb;
|
|
|
|
|
+// diffuseColor = max(diffuseColor, 0.0);
|
|
|
|
|
+
|
|
|
vec3 specularColor = mix(f0, baseColor.rgb, uMetallicFactor);
|
|
vec3 specularColor = mix(f0, baseColor.rgb, uMetallicFactor);
|
|
|
|
|
|
|
|
// Compute reflectance.
|
|
// Compute reflectance.
|
|
|
float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
|
|
float reflectance = max(max(specularColor.r, specularColor.g), specularColor.b);
|
|
|
|
|
|
|
|
// For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
|
|
// For typical incident reflectance range (between 4% to 100%) set the grazing reflectance to 100% for typical fresnel effect.
|
|
|
- // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflecance to 0%.
|
|
|
|
|
|
|
+ // For very low reflectance range on highly diffuse objects (below 4%), incrementally reduce grazing reflectance to 0%.
|
|
|
float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
|
|
float reflectance90 = clamp(reflectance * 25.0, 0.0, 1.0);
|
|
|
vec3 specularEnvironmentR0 = specularColor.rgb;
|
|
vec3 specularEnvironmentR0 = specularColor.rgb;
|
|
|
vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
|
|
vec3 specularEnvironmentR90 = vec3(1.0, 1.0, 1.0) * reflectance90;
|
|
|
|
|
|
|
|
- // TODO These are not currently uniforms - and need to support all kinds of lights!
|
|
|
|
|
- vec3 u_LightColor = vec3(1,1,1);
|
|
|
|
|
- vec3 u_LightDirection = vec3(1,0,0);
|
|
|
|
|
-
|
|
|
|
|
- vec3 n = getNormal(); // normal at surface point
|
|
|
|
|
- vec3 v = normalize(CamDir); // Vector from surface point to camera
|
|
|
|
|
- vec3 l = normalize(u_LightDirection); // Vector from surface point to light
|
|
|
|
|
- vec3 h = normalize(l+v); // Half vector between both l and v
|
|
|
|
|
- vec3 reflection = -normalize(reflect(v, n));
|
|
|
|
|
-
|
|
|
|
|
- float NdotL = clamp(dot(n, l), 0.001, 1.0);
|
|
|
|
|
- float NdotV = abs(dot(n, v)) + 0.001;
|
|
|
|
|
- float NdotH = clamp(dot(n, h), 0.0, 1.0);
|
|
|
|
|
- float LdotH = clamp(dot(l, h), 0.0, 1.0);
|
|
|
|
|
- float VdotH = clamp(dot(v, h), 0.0, 1.0);
|
|
|
|
|
-
|
|
|
|
|
PBRInfo pbrInputs = PBRInfo(
|
|
PBRInfo pbrInputs = PBRInfo(
|
|
|
- NdotL,
|
|
|
|
|
- NdotV,
|
|
|
|
|
- NdotH,
|
|
|
|
|
- LdotH,
|
|
|
|
|
- VdotH,
|
|
|
|
|
perceptualRoughness,
|
|
perceptualRoughness,
|
|
|
metallic,
|
|
metallic,
|
|
|
specularEnvironmentR0,
|
|
specularEnvironmentR0,
|
|
@@ -783,16 +812,103 @@ void main() {
|
|
|
specularColor
|
|
specularColor
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
- // Calculate the shading terms for the microfacet specular shading model
|
|
|
|
|
- vec3 F = specularReflection(pbrInputs);
|
|
|
|
|
- float G = geometricOcclusion(pbrInputs);
|
|
|
|
|
- float D = microfacetDistribution(pbrInputs);
|
|
|
|
|
|
|
+// vec3 normal = getNormal();
|
|
|
|
|
+ vec3 color = vec3(0.0);
|
|
|
|
|
|
|
|
- // Calculation of analytical lighting contribution
|
|
|
|
|
- vec3 diffuseContrib = (1.0 - F) * diffuse(pbrInputs);
|
|
|
|
|
- vec3 specContrib = F * G * D / (4.0 * NdotL * NdotV);
|
|
|
|
|
- // Obtain final intensity as reflectance (BRDF) scaled by the energy of the light (cosine law)
|
|
|
|
|
- vec3 color = NdotL * u_LightColor * (diffuseContrib + specContrib);
|
|
|
|
|
|
|
+#if AMB_LIGHTS>0
|
|
|
|
|
+ // Ambient lights
|
|
|
|
|
+ for (int i = 0; i < AMB_LIGHTS; i++) {
|
|
|
|
|
+ color += AmbientLightColor[i] * pbrInputs.diffuseColor;
|
|
|
|
|
+ }
|
|
|
|
|
+#endif
|
|
|
|
|
+
|
|
|
|
|
+#if DIR_LIGHTS>0
|
|
|
|
|
+ // Directional lights
|
|
|
|
|
+ for (int i = 0; i < DIR_LIGHTS; i++) {
|
|
|
|
|
+ // Diffuse reflection
|
|
|
|
|
+ // DirLightPosition is the direction of the current light
|
|
|
|
|
+ vec3 lightDirection = normalize(DirLightPosition(i));
|
|
|
|
|
+ // PBR
|
|
|
|
|
+ color += pbrModel(pbrInputs, DirLightColor(i), lightDirection);
|
|
|
|
|
+ }
|
|
|
|
|
+#endif
|
|
|
|
|
+
|
|
|
|
|
+#if POINT_LIGHTS>0
|
|
|
|
|
+ // Point lights
|
|
|
|
|
+ for (int i = 0; i < POINT_LIGHTS; i++) {
|
|
|
|
|
+ // Common calculations
|
|
|
|
|
+ // Calculates the direction and distance from the current vertex to this point light.
|
|
|
|
|
+ vec3 lightDirection = PointLightPosition(i) - vec3(Position);
|
|
|
|
|
+ float lightDistance = length(lightDirection);
|
|
|
|
|
+ // Normalizes the lightDirection
|
|
|
|
|
+ lightDirection = lightDirection / lightDistance;
|
|
|
|
|
+ // Calculates the attenuation due to the distance of the light
|
|
|
|
|
+ float attenuation = 1.0 / (1.0 + PointLightLinearDecay(i) * lightDistance +
|
|
|
|
|
+ PointLightQuadraticDecay(i) * lightDistance * lightDistance);
|
|
|
|
|
+ vec3 attenuatedColor = PointLightColor(i) * attenuation;
|
|
|
|
|
+ // PBR
|
|
|
|
|
+ color += pbrModel(pbrInputs, attenuatedColor, lightDirection);
|
|
|
|
|
+ }
|
|
|
|
|
+#endif
|
|
|
|
|
+
|
|
|
|
|
+#if SPOT_LIGHTS>0
|
|
|
|
|
+// for (int i = 0; i < SPOT_LIGHTS; i++) {
|
|
|
|
|
+// // Calculates the direction and distance from the current vertex to this spot light.
|
|
|
|
|
+// vec3 lightDirection = SpotLightPosition(i) - vec3(Position);
|
|
|
|
|
+// float lightDistance = length(lightDirection);
|
|
|
|
|
+// lightDirection = lightDirection / lightDistance;
|
|
|
|
|
+//
|
|
|
|
|
+// // Calculates the attenuation due to the distance of the light
|
|
|
|
|
+// float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance +
|
|
|
|
|
+// SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
|
|
|
|
|
+//
|
|
|
|
|
+// // Calculates the angle between the vertex direction and spot direction
|
|
|
|
|
+// // If this angle is greater than the cutoff the spotlight will not contribute
|
|
|
|
|
+// // to the final color.
|
|
|
|
|
+// float angle = acos(dot(-lightDirection, SpotLightDirection(i)));
|
|
|
|
|
+// float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
|
|
|
|
|
+//
|
|
|
|
|
+// if (angle < cutoff) {
|
|
|
|
|
+// float spotFactor = pow(dot(-lightDirection, SpotLightDirection(i)), SpotLightAngularDecay(i));
|
|
|
|
|
+//
|
|
|
|
|
+// // Diffuse reflection
|
|
|
|
|
+// float dotNormal = max(dot(lightDirection, normal), 0.0);
|
|
|
|
|
+// color += SpotLightColor(i) * dotNormal * attenuation * spotFactor;
|
|
|
|
|
+//
|
|
|
|
|
+// // Specular reflection
|
|
|
|
|
+// vec3 ref = reflect(-lightDirection, normal);
|
|
|
|
|
+// if (dotNormal > 0.0) {
|
|
|
|
|
+// color += SpotLightColor(i) * pow(max(dot(ref, CamDir), 0.0), 5) * attenuation * spotFactor;
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+// TODO
|
|
|
|
|
+ for (int i = 0; i < SPOT_LIGHTS; i++) {
|
|
|
|
|
+
|
|
|
|
|
+ // Calculates the direction and distance from the current vertex to this spot light.
|
|
|
|
|
+ vec3 lightDirection = SpotLightPosition(i) - vec3(Position);
|
|
|
|
|
+ float lightDistance = length(lightDirection);
|
|
|
|
|
+ lightDirection = lightDirection / lightDistance;
|
|
|
|
|
+
|
|
|
|
|
+ // Calculates the attenuation due to the distance of the light
|
|
|
|
|
+ float attenuation = 1.0 / (1.0 + SpotLightLinearDecay(i) * lightDistance +
|
|
|
|
|
+ SpotLightQuadraticDecay(i) * lightDistance * lightDistance);
|
|
|
|
|
+
|
|
|
|
|
+ // Calculates the angle between the vertex direction and spot direction
|
|
|
|
|
+ // If this angle is greater than the cutoff the spotlight will not contribute
|
|
|
|
|
+ // to the final color.
|
|
|
|
|
+ float angle = acos(dot(-lightDirection, SpotLightDirection(i)));
|
|
|
|
|
+ float cutoff = radians(clamp(SpotLightCutoffAngle(i), 0.0, 90.0));
|
|
|
|
|
+
|
|
|
|
|
+ if (angle < cutoff) {
|
|
|
|
|
+ float spotFactor = pow(dot(-lightDirection, SpotLightDirection(i)), SpotLightAngularDecay(i));
|
|
|
|
|
+ vec3 attenuatedColor = SpotLightColor(i) * attenuation * spotFactor;
|
|
|
|
|
+ // PBR
|
|
|
|
|
+ color += pbrModel(pbrInputs, attenuatedColor, lightDirection);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
// Calculate lighting contribution from image based lighting source (IBL)
|
|
// Calculate lighting contribution from image based lighting source (IBL)
|
|
|
//#ifdef USE_IBL
|
|
//#ifdef USE_IBL
|
|
@@ -801,18 +917,47 @@ void main() {
|
|
|
|
|
|
|
|
// Apply optional PBR terms for additional (optional) shading
|
|
// Apply optional PBR terms for additional (optional) shading
|
|
|
#ifdef HAS_OCCLUSIONMAP
|
|
#ifdef HAS_OCCLUSIONMAP
|
|
|
- float ao = texture2D(uOcclusionSampler, v_UV).r;
|
|
|
|
|
- color = mix(color, color * ao, u_OcclusionStrength);
|
|
|
|
|
|
|
+ float ao = texture2D(uOcclusionSampler, FragTexcoord).r;
|
|
|
|
|
+ color = mix(color, color * ao, 1.0);//, uOcclusionStrength);
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_EMISSIVEMAP
|
|
#ifdef HAS_EMISSIVEMAP
|
|
|
- vec3 emissive = SRGBtoLINEAR(texture2D(u_EmissiveSampler, v_UV)).rgb * u_EmissiveFactor;
|
|
|
|
|
|
|
+ vec3 emissive = SRGBtoLINEAR(texture2D(uEmissiveSampler, FragTexcoord)).rgb * vec3(uEmissiveColor);
|
|
|
color += emissive;
|
|
color += emissive;
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
|
|
+ // Base Color
|
|
|
|
|
+// FragColor = baseColor;
|
|
|
|
|
+
|
|
|
|
|
+ // Normal
|
|
|
|
|
+// FragColor = vec4(n, 1.0);
|
|
|
|
|
+
|
|
|
|
|
+ // Emissive Color
|
|
|
|
|
+// FragColor = vec4(emissive, 1.0);
|
|
|
|
|
+
|
|
|
|
|
+ // F
|
|
|
|
|
+// color = F;
|
|
|
|
|
+
|
|
|
|
|
+ // G
|
|
|
|
|
+// color = vec3(G);
|
|
|
|
|
+
|
|
|
|
|
+ // D
|
|
|
|
|
+// color = vec3(D);
|
|
|
|
|
+
|
|
|
|
|
+ // Specular
|
|
|
|
|
+// color = specContrib;
|
|
|
|
|
+
|
|
|
|
|
+ // Diffuse
|
|
|
|
|
+// color = diffuseContrib;
|
|
|
|
|
+
|
|
|
|
|
+ // Roughness
|
|
|
|
|
+// color = vec3(perceptualRoughness);
|
|
|
|
|
+
|
|
|
|
|
+ // Metallic
|
|
|
|
|
+// color = vec3(metallic);
|
|
|
|
|
+
|
|
|
// Final fragment color
|
|
// Final fragment color
|
|
|
FragColor = vec4(pow(color,vec3(1.0/2.2)), baseColor.a);
|
|
FragColor = vec4(pow(color,vec3(1.0/2.2)), baseColor.a);
|
|
|
-
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|