diff options
| author | Ray <[email protected]> | 2023-12-23 14:07:45 +0100 |
|---|---|---|
| committer | Ray <[email protected]> | 2023-12-23 14:07:45 +0100 |
| commit | e039a221a3cf45d115aa9d9489e7d7e03d92b91e (patch) | |
| tree | d0f0976f430860e7d41bbf5be7535c4ad5d9eb37 /examples/shaders | |
| parent | 706f74bce056c13ba32729c88bb7b30ceb6df34b (diff) | |
| download | raylib-e039a221a3cf45d115aa9d9489e7d7e03d92b91e.tar.gz raylib-e039a221a3cf45d115aa9d9489e7d7e03d92b91e.zip | |
Review formating
Diffstat (limited to 'examples/shaders')
| -rw-r--r-- | examples/shaders/resources/shaders/glsl330/pbr.fs | 177 | ||||
| -rw-r--r-- | examples/shaders/resources/shaders/glsl330/pbr.vs | 13 |
2 files changed, 96 insertions, 94 deletions
diff --git a/examples/shaders/resources/shaders/glsl330/pbr.fs b/examples/shaders/resources/shaders/glsl330/pbr.fs index 5375d449..d7544d31 100644 --- a/examples/shaders/resources/shaders/glsl330/pbr.fs +++ b/examples/shaders/resources/shaders/glsl330/pbr.fs @@ -24,8 +24,8 @@ in mat3 TBN; // Output fragment color out vec4 finalColor; -// Input uniform values +// Input uniform values uniform int numOfLights; uniform sampler2D albedoMap; uniform sampler2D mraMap; @@ -55,105 +55,108 @@ uniform vec3 viewPos; uniform vec3 ambientColor; uniform float ambient; -// refl in range 0 to 1 -// returns base reflectivity to 1 -// incrase reflectivity when surface view at larger angle -vec3 schlickFresnel(float hDotV,vec3 refl) +// Reflectivity in range 0.0 to 1.0 +// NOTE: Reflectivity is increased when surface view at larger angle +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 d = nDotH * nDotH * (a - 1.0) + 1.0; - d = PI * d * d; - return a / max(d,0.0000001); + float a = roughness * roughness * roughness * roughness; + float d = nDotH * nDotH * (a - 1.0) + 1.0; + d = PI * d * d; + 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 k = r * r / 8.0; - float ik = 1.0 - k; - float ggx1 = nDotV / (nDotV * ik + k); - float ggx2 = nDotL / (nDotL * ik + k); - return ggx1 * ggx2; + float r = roughness + 1.0; + float k = r*r / 8.0; + float ik = 1.0 - k; + float ggx1 = nDotV/(nDotV*ik + k); + float ggx2 = nDotL/(nDotL*ik + k); + return ggx1*ggx2; } -vec3 pbr(){ - vec3 albedo = texture(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); - 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 = texture(mraMap, vec2(fragTexCoord.x * tiling.x + offset.x, fragTexCoord.y * tiling.y + offset.y)) * useTexMRA; - 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); - if(useTexNormal == 1) { - N = texture(normalMap, vec2(fragTexCoord.x * tiling.x + offset.y, fragTexCoord.y * tiling.y + offset.y)).rgb; - N = normalize(N * 2.0 - 1.0); - N = normalize(N * TBN); - } - - vec3 V = normalize(viewPos - fragPosition); - - vec3 e = vec3(0); - e = (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); - //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 Lo = vec3(0.0); // acumulate lighting lum - - for(int i=0;i<numOfLights;++i){ - - vec3 L = normalize(lights[i].position - fragPosition); // calc light vector - vec3 H = normalize(V + L); // calc halfway bisecting vector - float dist = length(lights[i].position - fragPosition); // calc distance to light - float attenuation = 1.0 / (dist * dist * 0.23); // calc attenuation - vec3 radiance = lights[i].color.rgb * lights[i].intensity * attenuation; // calc input radiance,light energy comming in - - //Cook-Torrance BRDF distribution function - 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 - // kD = 1.0 - kS diffuse component is equal 1.0 - spec comonent - vec3 kD = vec3(1.0) - F; - //mult kD by the inverse of metallnes , only non-metals should have diffuse light - kD *= 1.0 - metallic; - Lo += ((kD * albedo.rgb / PI + spec) * radiance * nDotL)*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 ComputePBR() +{ + vec3 albedo = texture(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); + + 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 = texture(mraMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y))*useTexMRA; + 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); + if (useTexNormal == 1) + { + N = texture(normalMap, vec2(fragTexCoord.x*tiling.x + offset.y, fragTexCoord.y*tiling.y + offset.y)).rgb; + N = normalize(N*2.0 - 1.0); + N = normalize(N*TBN); + } + + vec3 V = normalize(viewPos - fragPosition); + + 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; + + // 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 + vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic); + vec3 lightAccum = vec3(0.0); // Acumulate lighting lum + + for (int i = 0; i < numOfLights; 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 + + // Cook-Torrance BRDF distribution function + 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 + // kD = 1.0 - kS diffuse component is equal 1.0 - spec comonent + vec3 kD = vec3(1.0) - F; + + // Mult kD by the inverse of metallnes, only non-metals should have diffuse light + kD *= 1.0 - metallic; + lightAccum += ((kD*albedo.rgb/PI + spec)*radiance*nDotL)*lights[i].enabled; // Angle of light has impact on result + } + + vec3 ambientFinal = (ambientColor + albedo)*ambient*0.5; + + return ambientFinal + lightAccum*ao + emissive; } void main() { - vec3 color = pbr(); + vec3 color = ComputePBR(); - //HDR tonemapping - color = pow(color,color + vec3(1.0)); - //gamma correction - color = pow(color,vec3(1.0/2.2)); + // HDR tonemapping + color = pow(color, color + vec3(1.0)); + + // Gamma correction + color = pow(color, vec3(1.0/2.2)); - finalColor = vec4(color,1.0); + finalColor = vec4(color, 1.0); }
\ No newline at end of file diff --git a/examples/shaders/resources/shaders/glsl330/pbr.vs b/examples/shaders/resources/shaders/glsl330/pbr.vs index a03898cb..94b0062c 100644 --- a/examples/shaders/resources/shaders/glsl330/pbr.vs +++ b/examples/shaders/resources/shaders/glsl330/pbr.vs @@ -25,17 +25,16 @@ const float normalOffset = 0.1; void main() { - - // calc binormal from vertex normal and tangent + // Compute binormal from vertex normal and tangent vec3 vertexBinormal = cross(vertexNormal, vertexTangent); - // calc fragment normal based on normal transformations + + // Compute fragment normal based on normal transformations mat3 normalMatrix = transpose(inverse(mat3(matModel))); - // calc fragment position based on model transformations - + + // Compute fragment position based on model transformations fragPosition = vec3(matModel*vec4(vertexPosition, 1.0f)); fragTexCoord = vertexTexCoord*2.0; - fragNormal = normalize(normalMatrix*vertexNormal); vec3 fragTangent = normalize(normalMatrix*vertexTangent); fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal); @@ -45,5 +44,5 @@ void main() TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal)); // Calculate final vertex position - gl_Position = mvp * vec4(vertexPosition, 1.0); + gl_Position = mvp*vec4(vertexPosition, 1.0); }
\ No newline at end of file |
