Reviewed shaders formating to follow raylib coding conventions
This commit is contained in:
parent
49b905077d
commit
8e450e4446
6 changed files with 151 additions and 148 deletions
|
@ -54,23 +54,22 @@ uniform vec3 viewPos;
|
||||||
uniform vec3 ambientColor;
|
uniform vec3 ambientColor;
|
||||||
uniform float ambient;
|
uniform float ambient;
|
||||||
|
|
||||||
// refl in range 0 to 1
|
// Reflectivity in range 0.0 to 1.0
|
||||||
// returns base reflectivity to 1
|
// NOTE: Reflectivity is increased when surface view at larger angle
|
||||||
// incrase reflectivity when surface view at larger angle
|
vec3 SchlickFresnel(float hDotV,vec3 refl)
|
||||||
vec3 schlickFresnel(float hDotV,vec3 refl)
|
|
||||||
{
|
{
|
||||||
return refl + (1.0 - refl)*pow(1.0 - hDotV,5.0);
|
return refl + (1.0 - refl)*pow(1.0 - hDotV, 5.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
float ggxDistribution(float nDotH, float roughness)
|
float GgxDistribution(float nDotH,float roughness)
|
||||||
{
|
{
|
||||||
float a = roughness*roughness*roughness*roughness;
|
float a = roughness*roughness*roughness*roughness;
|
||||||
float d = nDotH*nDotH*(a - 1.0) + 1.0;
|
float d = nDotH*nDotH*(a - 1.0) + 1.0;
|
||||||
d = PI*d*d;
|
d = PI*d*d;
|
||||||
return a/max(d,0.0000001);
|
return (a/max(d,0.0000001));
|
||||||
}
|
}
|
||||||
|
|
||||||
float geomSmith(float nDotV, float nDotL, float roughness)
|
float GeomSmith(float nDotV,float nDotL,float roughness)
|
||||||
{
|
{
|
||||||
float r = roughness + 1.0;
|
float r = roughness + 1.0;
|
||||||
float k = r*r/8.0;
|
float k = r*r/8.0;
|
||||||
|
@ -80,7 +79,7 @@ float geomSmith(float nDotV, float nDotL, float roughness)
|
||||||
return ggx1*ggx2;
|
return ggx1*ggx2;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 pbr()
|
vec3 ComputePBR()
|
||||||
{
|
{
|
||||||
vec3 albedo = texture2D(albedoMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb;
|
vec3 albedo = texture2D(albedoMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb;
|
||||||
albedo = vec3(albedoColor.x*albedo.x, albedoColor.y*albedo.y, albedoColor.z*albedo.z);
|
albedo = vec3(albedoColor.x*albedo.x, albedoColor.y*albedo.y, albedoColor.z*albedo.z);
|
||||||
|
@ -107,20 +106,20 @@ vec3 pbr()
|
||||||
|
|
||||||
vec3 V = normalize(viewPos - fragPosition);
|
vec3 V = normalize(viewPos - fragPosition);
|
||||||
|
|
||||||
vec3 e = vec3(0);
|
vec3 emissive = vec3(0);
|
||||||
e = (texture2D(emissiveMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb).g*emissiveColor.rgb*emissivePower*float(useTexEmissive);
|
emissive = (texture2D(emissiveMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb).g*emissiveColor.rgb*emissivePower*float(useTexEmissive);
|
||||||
|
|
||||||
// return N;//vec3(metallic,metallic,metallic);
|
// return N;//vec3(metallic,metallic,metallic);
|
||||||
// If dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity
|
// If dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity
|
||||||
vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic);
|
vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic);
|
||||||
vec3 Lo = vec3(0.0); // Acumulate lighting lum
|
vec3 lightAccum = vec3(0.0); // Acumulate lighting lum
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
vec3 L = normalize(lights[i].position - fragPosition); // Compute light vector
|
vec3 L = normalize(lights[i].position - fragPosition); // Compute light vector
|
||||||
vec3 H = normalize(V + L); // Compute halfway bisecting vector
|
vec3 H = normalize(V + L); // Compute halfway bisecting vector
|
||||||
float dist = length(lights[i].position - fragPosition); // Compute distance to light
|
float dist = length(lights[i].position - fragPosition); // Compute distance to light
|
||||||
float attenuation = 1.0/(dist*dist*0.23); // Compute attenuation
|
float attenuation = 1.0/(dist*dist*0.23); // Compute attenuation
|
||||||
vec3 radiance = lights[i].color.rgb*lights[i].intensity*attenuation; // Compute input radiance, light energy comming in
|
vec3 radiance = lights[i].color.rgb*lights[i].intensity*attenuation; // Compute input radiance, light energy comming in
|
||||||
|
|
||||||
// Cook-Torrance BRDF distribution function
|
// Cook-Torrance BRDF distribution function
|
||||||
|
@ -128,9 +127,9 @@ vec3 pbr()
|
||||||
float nDotL = max(dot(N,L), 0.0000001);
|
float nDotL = max(dot(N,L), 0.0000001);
|
||||||
float hDotV = max(dot(H,V), 0.0);
|
float hDotV = max(dot(H,V), 0.0);
|
||||||
float nDotH = max(dot(N,H), 0.0);
|
float nDotH = max(dot(N,H), 0.0);
|
||||||
float D = ggxDistribution(nDotH, roughness); // Larger the more micro-facets aligned to H
|
float D = GgxDistribution(nDotH, roughness); // Larger the more micro-facets aligned to H
|
||||||
float G = geomSmith(nDotV, nDotL, roughness); // Smaller the more micro-facets shadow
|
float G = GeomSmith(nDotV, nDotL, roughness); // Smaller the more micro-facets shadow
|
||||||
vec3 F = schlickFresnel(hDotV, baseRefl); // Fresnel proportion of specular reflectance
|
vec3 F = SchlickFresnel(hDotV, baseRefl); // Fresnel proportion of specular reflectance
|
||||||
|
|
||||||
vec3 spec = (D*G*F)/(4.0*nDotV*nDotL);
|
vec3 spec = (D*G*F)/(4.0*nDotV*nDotL);
|
||||||
|
|
||||||
|
@ -138,25 +137,25 @@ vec3 pbr()
|
||||||
// kD = 1.0 - kS diffuse component is equal 1.0 - spec comonent
|
// kD = 1.0 - kS diffuse component is equal 1.0 - spec comonent
|
||||||
vec3 kD = vec3(1.0) - F;
|
vec3 kD = vec3(1.0) - F;
|
||||||
|
|
||||||
// Mult kD by the inverse of metallnes , only non-metals should have diffuse light
|
// Mult kD by the inverse of metallnes, only non-metals should have diffuse light
|
||||||
kD *= 1.0 - metallic;
|
kD *= 1.0 - metallic;
|
||||||
Lo += ((kD*albedo.rgb/PI + spec)*radiance*nDotL)*float(lights[i].enabled); // Angle of light has impact on result
|
lightAccum += ((kD*albedo.rgb/PI + spec)*radiance*nDotL)*float(lights[i].enabled); // Angle of light has impact on result
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 ambientFinal = (ambientColor + albedo)*ambient*0.5;
|
vec3 ambientFinal = (ambientColor + albedo)*ambient*0.5;
|
||||||
|
|
||||||
return (ambientFinal + Lo*ao + e);
|
return (ambientFinal + lightAccum*ao + emissive);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec3 color = pbr();
|
vec3 color = ComputePBR();
|
||||||
|
|
||||||
// HDR tonemapping
|
// HDR tonemapping
|
||||||
color = pow(color,color + vec3(1.0));
|
color = pow(color, color + vec3(1.0));
|
||||||
|
|
||||||
// Gamma correction
|
// Gamma correction
|
||||||
color = pow(color,vec3(1.0/2.2));
|
color = pow(color, vec3(1.0/2.2));
|
||||||
|
|
||||||
gl_FragColor = vec4(color,1.0);
|
gl_FragColor = vec4(color,1.0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,16 +51,16 @@ mat3 transpose(mat3 m)
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// calc binormal from vertex normal and tangent
|
// Compute binormal from vertex normal and tangent
|
||||||
vec3 vertexBinormal = cross(vertexNormal, vertexTangent);
|
vec3 vertexBinormal = cross(vertexNormal, vertexTangent);
|
||||||
// calc fragment normal based on normal transformations
|
|
||||||
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
|
||||||
// calc fragment position based on model transformations
|
|
||||||
|
|
||||||
|
// Compute fragment normal based on normal transformations
|
||||||
|
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
||||||
|
|
||||||
|
// Compute fragment position based on model transformations
|
||||||
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
||||||
|
|
||||||
fragTexCoord = vertexTexCoord*2.0;
|
fragTexCoord = vertexTexCoord*2.0;
|
||||||
|
|
||||||
fragNormal = normalize(normalMatrix*vertexNormal);
|
fragNormal = normalize(normalMatrix*vertexNormal);
|
||||||
vec3 fragTangent = normalize(normalMatrix*vertexTangent);
|
vec3 fragTangent = normalize(normalMatrix*vertexTangent);
|
||||||
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal);
|
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal);
|
||||||
|
@ -70,5 +70,5 @@ void main()
|
||||||
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
|
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
|
||||||
|
|
||||||
// Calculate final vertex position
|
// Calculate final vertex position
|
||||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
gl_Position = mvp*vec4(vertexPosition, 1.0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,6 @@ varying vec3 fragNormal;
|
||||||
varying vec4 shadowPos;
|
varying vec4 shadowPos;
|
||||||
varying mat3 TBN;
|
varying mat3 TBN;
|
||||||
|
|
||||||
|
|
||||||
// Input uniform values
|
// Input uniform values
|
||||||
uniform int numOfLights;
|
uniform int numOfLights;
|
||||||
uniform sampler2D albedoMap;
|
uniform sampler2D albedoMap;
|
||||||
|
@ -53,102 +52,108 @@ uniform vec3 viewPos;
|
||||||
uniform vec3 ambientColor;
|
uniform vec3 ambientColor;
|
||||||
uniform float ambient;
|
uniform float ambient;
|
||||||
|
|
||||||
// refl in range 0 to 1
|
// Reflectivity in range 0.0 to 1.0
|
||||||
// returns base reflectivity to 1
|
// NOTE: Reflectivity is increased when surface view at larger angle
|
||||||
// incrase reflectivity when surface view at larger angle
|
vec3 SchlickFresnel(float hDotV,vec3 refl)
|
||||||
vec3 schlickFresnel(float hDotV,vec3 refl)
|
|
||||||
{
|
{
|
||||||
return refl + (1.0 - refl) * pow(1.0 - hDotV,5.0);
|
return refl + (1.0 - refl)*pow(1.0 - hDotV, 5.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
float ggxDistribution(float nDotH,float roughness)
|
float GgxDistribution(float nDotH,float roughness)
|
||||||
{
|
{
|
||||||
float a = roughness * roughness * roughness * roughness;
|
float a = roughness*roughness*roughness*roughness;
|
||||||
float d = nDotH * nDotH * (a - 1.0) + 1.0;
|
float d = nDotH*nDotH*(a - 1.0) + 1.0;
|
||||||
d = PI * d * d;
|
d = PI*d*d;
|
||||||
return a / max(d,0.0000001);
|
return (a/max(d,0.0000001));
|
||||||
}
|
}
|
||||||
|
|
||||||
float geomSmith(float nDotV,float nDotL,float roughness)
|
float GeomSmith(float nDotV,float nDotL,float roughness)
|
||||||
{
|
{
|
||||||
float r = roughness + 1.0;
|
float r = roughness + 1.0;
|
||||||
float k = r * r / 8.0;
|
float k = r*r/8.0;
|
||||||
float ik = 1.0 - k;
|
float ik = 1.0 - k;
|
||||||
float ggx1 = nDotV / (nDotV * ik + k);
|
float ggx1 = nDotV/(nDotV*ik + k);
|
||||||
float ggx2 = nDotL / (nDotL * ik + k);
|
float ggx2 = nDotL/(nDotL*ik + k);
|
||||||
return ggx1 * ggx2;
|
return ggx1*ggx2;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 pbr(){
|
vec3 ComputePBR()
|
||||||
vec3 albedo = texture2D(albedoMap,vec2(fragTexCoord.x*tiling.x+offset.x,fragTexCoord.y*tiling.y+offset.y)).rgb;
|
{
|
||||||
albedo = vec3(albedoColor.x*albedo.x,albedoColor.y*albedo.y,albedoColor.z*albedo.z);
|
vec3 albedo = texture2D(albedoMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb;
|
||||||
float metallic = clamp(metallicValue,0.0,1.0);
|
albedo = vec3(albedoColor.x*albedo.x, albedoColor.y*albedo.y, albedoColor.z*albedo.z);
|
||||||
float roughness = clamp(roughnessValue,0.0,1.0);
|
|
||||||
float ao = clamp(aoValue,0.0,1.0);
|
|
||||||
if(useTexMRA == 1) {
|
|
||||||
vec4 mra = texture2D(mraMap, vec2(fragTexCoord.x * tiling.x + offset.x, fragTexCoord.y * tiling.y + offset.y));
|
|
||||||
metallic = clamp(mra.r+metallicValue,0.04,1.0);
|
|
||||||
roughness = clamp(mra.g+roughnessValue,0.04,1.0);
|
|
||||||
ao = (mra.b+aoValue)*0.5;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
float metallic = clamp(metallicValue, 0.0, 1.0);
|
||||||
|
float roughness = clamp(roughnessValue, 0.0, 1.0);
|
||||||
|
float ao = clamp(aoValue, 0.0, 1.0);
|
||||||
|
|
||||||
|
if (useTexMRA == 1)
|
||||||
|
{
|
||||||
|
vec4 mra = texture2D(mraMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y));
|
||||||
|
metallic = clamp(mra.r + metallicValue, 0.04, 1.0);
|
||||||
|
roughness = clamp(mra.g + roughnessValue, 0.04, 1.0);
|
||||||
|
ao = (mra.b + aoValue)*0.5;
|
||||||
|
}
|
||||||
|
|
||||||
vec3 N = normalize(fragNormal);
|
vec3 N = normalize(fragNormal);
|
||||||
if(useTexNormal == 1) {
|
if (useTexNormal == 1)
|
||||||
N = texture2D(normalMap, vec2(fragTexCoord.x * tiling.x + offset.y, fragTexCoord.y * tiling.y + offset.y)).rgb;
|
{
|
||||||
N = normalize(N * 2.0 - 1.0);
|
N = texture2D(normalMap, vec2(fragTexCoord.x*tiling.x + offset.y, fragTexCoord.y*tiling.y + offset.y)).rgb;
|
||||||
N = normalize(N * TBN);
|
N = normalize(N*2.0 - 1.0);
|
||||||
}
|
N = normalize(N*TBN);
|
||||||
|
}
|
||||||
|
|
||||||
vec3 V = normalize(viewPos - fragPosition);
|
vec3 V = normalize(viewPos - fragPosition);
|
||||||
|
|
||||||
vec3 e = vec3(0);
|
vec3 emissive = vec3(0);
|
||||||
e = (texture2D(emissiveMap, vec2(fragTexCoord.x*tiling.x+offset.x, fragTexCoord.y*tiling.y+offset.y)).rgb).g * emissiveColor.rgb*emissivePower * float(useTexEmissive);
|
emissive = (texture2D(emissiveMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb).g*emissiveColor.rgb*emissivePower*float(useTexEmissive);
|
||||||
|
|
||||||
//return N;//vec3(metallic,metallic,metallic);
|
// return N;//vec3(metallic,metallic,metallic);
|
||||||
//if dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity
|
// If dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity
|
||||||
vec3 baseRefl = mix(vec3(0.04),albedo.rgb,metallic);
|
vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic);
|
||||||
vec3 Lo = vec3(0.0); // acumulate lighting lum
|
vec3 lightAccum = vec3(0.0); // Acumulate lighting lum
|
||||||
|
|
||||||
for(int i=0;i<numOfLights;++i){
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
vec3 L = normalize(lights[i].position - fragPosition); // Compute light vector
|
||||||
|
vec3 H = normalize(V + L); // Compute halfway bisecting vector
|
||||||
|
float dist = length(lights[i].position - fragPosition); // Compute distance to light
|
||||||
|
float attenuation = 1.0/(dist*dist*0.23); // Compute attenuation
|
||||||
|
vec3 radiance = lights[i].color.rgb*lights[i].intensity*attenuation; // Compute input radiance, light energy comming in
|
||||||
|
|
||||||
vec3 L = normalize(lights[i].position - fragPosition); // calc light vector
|
// Cook-Torrance BRDF distribution function
|
||||||
vec3 H = normalize(V + L); // calc halfway bisecting vector
|
float nDotV = max(dot(N,V), 0.0000001);
|
||||||
float dist = length(lights[i].position - fragPosition); // calc distance to light
|
float nDotL = max(dot(N,L), 0.0000001);
|
||||||
float attenuation = 1.0 / (dist * dist * 0.23); // calc attenuation
|
float hDotV = max(dot(H,V), 0.0);
|
||||||
vec3 radiance = lights[i].color.rgb * lights[i].intensity * attenuation; // calc input radiance,light energy comming in
|
float nDotH = max(dot(N,H), 0.0);
|
||||||
|
float D = GgxDistribution(nDotH, roughness); // Larger the more micro-facets aligned to H
|
||||||
|
float G = GeomSmith(nDotV, nDotL, roughness); // Smaller the more micro-facets shadow
|
||||||
|
vec3 F = SchlickFresnel(hDotV, baseRefl); // Fresnel proportion of specular reflectance
|
||||||
|
|
||||||
//Cook-Torrance BRDF distribution function
|
vec3 spec = (D*G*F)/(4.0*nDotV*nDotL);
|
||||||
float nDotV = max(dot(N,V),0.0000001);
|
|
||||||
float nDotL = max(dot(N,L),0.0000001);
|
|
||||||
float hDotV = max(dot(H,V),0.0);
|
|
||||||
float nDotH = max(dot(N,H),0.0);
|
|
||||||
float D = ggxDistribution(nDotH,roughness); // larger the more micro-facets aligned to H
|
|
||||||
float G = geomSmith(nDotV,nDotL,roughness); // smaller the more micro-facets shadow
|
|
||||||
vec3 F = schlickFresnel(hDotV, baseRefl); // fresnel proportion of specular reflectance
|
|
||||||
|
|
||||||
vec3 spec = (D * G * F) / (4.0 * nDotV * nDotL);
|
// Difuse and spec light can't be above 1.0
|
||||||
// difuse and spec light can't be above 1.0
|
// kD = 1.0 - kS diffuse component is equal 1.0 - spec comonent
|
||||||
// kD = 1.0 - kS diffuse component is equal 1.0 - spec comonent
|
vec3 kD = vec3(1.0) - F;
|
||||||
vec3 kD = vec3(1.0) - F;
|
|
||||||
//mult kD by the inverse of metallnes , only non-metals should have diffuse light
|
// Mult kD by the inverse of metallnes, only non-metals should have diffuse light
|
||||||
kD *= 1.0 - metallic;
|
kD *= 1.0 - metallic;
|
||||||
Lo += ((kD * albedo.rgb / PI + spec) * radiance * nDotL)*float(lights[i].enabled); // angle of light has impact on result
|
lightAccum += ((kD*albedo.rgb/PI + spec)*radiance*nDotL)*float(lights[i].enabled); // Angle of light has impact on result
|
||||||
}
|
}
|
||||||
vec3 ambient_final = (ambientColor + albedo)* ambient * 0.5;
|
|
||||||
return ambient_final+Lo*ao+e;
|
vec3 ambientFinal = (ambientColor + albedo)*ambient*0.5;
|
||||||
|
|
||||||
|
return (ambientFinal + lightAccum*ao + emissive);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
vec3 color = pbr();
|
vec3 color = ComputePBR();
|
||||||
|
|
||||||
//HDR tonemapping
|
// HDR tonemapping
|
||||||
color = pow(color,color + vec3(1.0));
|
color = pow(color, color + vec3(1.0));
|
||||||
//gamma correction
|
|
||||||
color = pow(color,vec3(1.0/2.2));
|
|
||||||
|
|
||||||
gl_FragColor = vec4(color,1.0);
|
// Gamma correction
|
||||||
|
color = pow(color, vec3(1.0/2.2));
|
||||||
|
|
||||||
|
gl_FragColor = vec4(color,1.0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#version 120
|
#version 120
|
||||||
|
|
||||||
// Input vertex attributes
|
// Input vertex attributes
|
||||||
attribute vec3 vertexPosition;
|
attribute vec3 vertexPosition;
|
||||||
attribute vec2 vertexTexCoord;
|
attribute vec2 vertexTexCoord;
|
||||||
attribute vec3 vertexNormal;
|
attribute vec3 vertexNormal;
|
||||||
attribute vec3 vertexTangent;
|
attribute vec3 vertexTangent;
|
||||||
attribute vec4 vertexColor;
|
attribute vec4 vertexColor;
|
||||||
|
|
||||||
// Input uniform values
|
// Input uniform values
|
||||||
uniform mat4 mvp;
|
uniform mat4 mvp;
|
||||||
|
@ -26,17 +26,17 @@ const float normalOffset = 0.1;
|
||||||
// https://github.com/glslify/glsl-inverse
|
// https://github.com/glslify/glsl-inverse
|
||||||
mat3 inverse(mat3 m)
|
mat3 inverse(mat3 m)
|
||||||
{
|
{
|
||||||
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
|
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
|
||||||
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
|
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
|
||||||
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
|
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
|
||||||
|
|
||||||
float b01 = a22*a11 - a12*a21;
|
float b01 = a22*a11 - a12*a21;
|
||||||
float b11 = -a22*a10 + a12*a20;
|
float b11 = -a22*a10 + a12*a20;
|
||||||
float b21 = a21*a10 - a11*a20;
|
float b21 = a21*a10 - a11*a20;
|
||||||
|
|
||||||
float det = a00*b01 + a01*b11 + a02*b21;
|
float det = a00*b01 + a01*b11 + a02*b21;
|
||||||
|
|
||||||
return mat3(b01, (-a22*a01 + a02*a21), (a12*a01 - a02*a11),
|
return mat3(b01, (-a22*a01 + a02*a21), (a12*a01 - a02*a11),
|
||||||
b11, (a22*a00 - a02*a20), (-a12*a00 + a02*a10),
|
b11, (a22*a00 - a02*a20), (-a12*a00 + a02*a10),
|
||||||
b21, (-a21*a00 + a01*a20), (a11*a00 - a01*a10))/det;
|
b21, (-a21*a00 + a01*a20), (a11*a00 - a01*a10))/det;
|
||||||
}
|
}
|
||||||
|
@ -44,24 +44,23 @@ mat3 inverse(mat3 m)
|
||||||
// https://github.com/glslify/glsl-transpose
|
// https://github.com/glslify/glsl-transpose
|
||||||
mat3 transpose(mat3 m)
|
mat3 transpose(mat3 m)
|
||||||
{
|
{
|
||||||
return mat3(m[0][0], m[1][0], m[2][0],
|
return mat3(m[0][0], m[1][0], m[2][0],
|
||||||
m[0][1], m[1][1], m[2][1],
|
m[0][1], m[1][1], m[2][1],
|
||||||
m[0][2], m[1][2], m[2][2]);
|
m[0][2], m[1][2], m[2][2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
// Compute binormal from vertex normal and tangent
|
||||||
// calc binormal from vertex normal and tangent
|
|
||||||
vec3 vertexBinormal = cross(vertexNormal, vertexTangent);
|
vec3 vertexBinormal = cross(vertexNormal, vertexTangent);
|
||||||
// calc fragment normal based on normal transformations
|
|
||||||
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
|
||||||
// calc fragment position based on model transformations
|
|
||||||
|
|
||||||
|
// Compute fragment normal based on normal transformations
|
||||||
|
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
||||||
|
|
||||||
|
// Compute fragment position based on model transformations
|
||||||
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
||||||
|
|
||||||
fragTexCoord = vertexTexCoord*2.0;
|
fragTexCoord = vertexTexCoord*2.0;
|
||||||
|
|
||||||
fragNormal = normalize(normalMatrix*vertexNormal);
|
fragNormal = normalize(normalMatrix*vertexNormal);
|
||||||
vec3 fragTangent = normalize(normalMatrix*vertexTangent);
|
vec3 fragTangent = normalize(normalMatrix*vertexTangent);
|
||||||
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal);
|
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal);
|
||||||
|
@ -71,5 +70,5 @@ void main()
|
||||||
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
|
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
|
||||||
|
|
||||||
// Calculate final vertex position
|
// Calculate final vertex position
|
||||||
gl_Position = mvp * vec4(vertexPosition, 1.0);
|
gl_Position = mvp*vec4(vertexPosition, 1.0);
|
||||||
}
|
}
|
|
@ -64,16 +64,16 @@ vec3 SchlickFresnel(float hDotV,vec3 refl)
|
||||||
|
|
||||||
float GgxDistribution(float nDotH,float roughness)
|
float GgxDistribution(float nDotH,float roughness)
|
||||||
{
|
{
|
||||||
float a = roughness * roughness * roughness * roughness;
|
float a = roughness*roughness*roughness*roughness;
|
||||||
float d = nDotH * nDotH * (a - 1.0) + 1.0;
|
float d = nDotH*nDotH*(a - 1.0) + 1.0;
|
||||||
d = PI * d * d;
|
d = PI*d*d;
|
||||||
return a / max(d,0.0000001);
|
return (a/max(d,0.0000001));
|
||||||
}
|
}
|
||||||
|
|
||||||
float GeomSmith(float nDotV,float nDotL,float roughness)
|
float GeomSmith(float nDotV,float nDotL,float roughness)
|
||||||
{
|
{
|
||||||
float r = roughness + 1.0;
|
float r = roughness + 1.0;
|
||||||
float k = r*r / 8.0;
|
float k = r*r/8.0;
|
||||||
float ik = 1.0 - k;
|
float ik = 1.0 - k;
|
||||||
float ggx1 = nDotV/(nDotV*ik + k);
|
float ggx1 = nDotV/(nDotV*ik + k);
|
||||||
float ggx2 = nDotL/(nDotL*ik + k);
|
float ggx2 = nDotL/(nDotL*ik + k);
|
||||||
|
@ -91,7 +91,7 @@ vec3 ComputePBR()
|
||||||
|
|
||||||
if (useTexMRA == 1)
|
if (useTexMRA == 1)
|
||||||
{
|
{
|
||||||
vec4 mra = texture(mraMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y))*useTexMRA;
|
vec4 mra = texture(mraMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y));
|
||||||
metallic = clamp(mra.r + metallicValue, 0.04, 1.0);
|
metallic = clamp(mra.r + metallicValue, 0.04, 1.0);
|
||||||
roughness = clamp(mra.g + roughnessValue, 0.04, 1.0);
|
roughness = clamp(mra.g + roughnessValue, 0.04, 1.0);
|
||||||
ao = (mra.b + aoValue)*0.5;
|
ao = (mra.b + aoValue)*0.5;
|
||||||
|
@ -108,10 +108,10 @@ vec3 ComputePBR()
|
||||||
vec3 V = normalize(viewPos - fragPosition);
|
vec3 V = normalize(viewPos - fragPosition);
|
||||||
|
|
||||||
vec3 emissive = vec3(0);
|
vec3 emissive = vec3(0);
|
||||||
emissive = (texture(emissiveMap, vec2(fragTexCoord.x*tiling.x+offset.x, fragTexCoord.y*tiling.y+offset.y)).rgb).g * emissiveColor.rgb*emissivePower * useTexEmissive;
|
emissive = (texture(emissiveMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb).g*emissiveColor.rgb*emissivePower*useTexEmissive;
|
||||||
|
|
||||||
// return N;//vec3(metallic,metallic,metallic);
|
// return N;//vec3(metallic,metallic,metallic);
|
||||||
// if dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity
|
// If dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity
|
||||||
vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic);
|
vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic);
|
||||||
vec3 lightAccum = vec3(0.0); // Acumulate lighting lum
|
vec3 lightAccum = vec3(0.0); // Acumulate lighting lum
|
||||||
|
|
||||||
|
@ -145,7 +145,7 @@ vec3 ComputePBR()
|
||||||
|
|
||||||
vec3 ambientFinal = (ambientColor + albedo)*ambient*0.5;
|
vec3 ambientFinal = (ambientColor + albedo)*ambient*0.5;
|
||||||
|
|
||||||
return ambientFinal + lightAccum*ao + emissive;
|
return (ambientFinal + lightAccum*ao + emissive);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
|
|
|
@ -32,7 +32,7 @@ void main()
|
||||||
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
|
||||||
|
|
||||||
// Compute fragment position based on model transformations
|
// Compute fragment position based on model transformations
|
||||||
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0f));
|
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
||||||
|
|
||||||
fragTexCoord = vertexTexCoord*2.0;
|
fragTexCoord = vertexTexCoord*2.0;
|
||||||
fragNormal = normalize(normalMatrix*vertexNormal);
|
fragNormal = normalize(normalMatrix*vertexNormal);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue