mirror of
https://github.com/raysan5/raylib.git
synced 2026-02-02 04:09:18 -05:00
Reviewed shader formating
This commit is contained in:
@ -80,23 +80,26 @@ float geomSmith(float nDotV,float nDotL,float roughness)
|
|||||||
return ggx1*ggx2;
|
return ggx1*ggx2;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 pbr(){
|
vec3 pbr()
|
||||||
|
{
|
||||||
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);
|
||||||
|
|
||||||
float metallic = clamp(metallicValue, 0.0, 1.0);
|
float metallic = clamp(metallicValue, 0.0, 1.0);
|
||||||
float roughness = clamp(roughnessValue, 0.0, 1.0);
|
float roughness = clamp(roughnessValue, 0.0, 1.0);
|
||||||
float ao = clamp(aoValue, 0.0, 1.0);
|
float ao = clamp(aoValue, 0.0, 1.0);
|
||||||
if(useTexMRA == 1) {
|
|
||||||
|
if (useTexMRA == 1)
|
||||||
|
{
|
||||||
vec4 mra = texture2D(mraMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y));
|
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);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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 = texture2D(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*2.0 - 1.0);
|
||||||
N = normalize(N*TBN);
|
N = normalize(N*TBN);
|
||||||
@ -108,37 +111,41 @@ vec3 pbr(){
|
|||||||
e = (texture2D(emissiveMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb).g*emissiveColor.rgb*emissivePower*float(useTexEmissive);
|
e = (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 Lo = 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); // calc light vector
|
vec3 L = normalize(lights[i].position - fragPosition); // Compute light vector
|
||||||
vec3 H = normalize(V + L); // calc halfway bisecting vector
|
vec3 H = normalize(V + L); // Compute halfway bisecting vector
|
||||||
float dist = length(lights[i].position - fragPosition); // calc distance to light
|
float dist = length(lights[i].position - fragPosition); // Compute distance to light
|
||||||
float attenuation = 1.0 / (dist * dist * 0.23); // calc attenuation
|
float attenuation = 1.0/(dist*dist*0.23); // Compute attenuation
|
||||||
vec3 radiance = lights[i].color.rgb * lights[i].intensity * attenuation; // calc 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
|
||||||
float nDotV = max(dot(N,V), 0.0000001);
|
float nDotV = max(dot(N,V), 0.0000001);
|
||||||
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);
|
||||||
// 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
|
Lo += ((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 + Lo*ao + e);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
@ -147,9 +154,9 @@ void main()
|
|||||||
|
|
||||||
// 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,7 +51,6 @@ mat3 transpose(mat3 m)
|
|||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
|
||||||
// calc 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
|
// calc fragment normal based on normal transformations
|
||||||
|
|||||||
Reference in New Issue
Block a user