mirror of
https://github.com/ZDoom/gzdoom-gles.git
synced 2024-11-25 05:31:00 +00:00
- add PBR materials to main.fp
This commit is contained in:
parent
30af6d38b3
commit
a6da1d356a
1 changed files with 156 additions and 0 deletions
|
@ -384,6 +384,158 @@ float spotLightAttenuation(vec4 lightpos, vec3 spotdir, float lightCosInnerAngle
|
|||
return smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir);
|
||||
}
|
||||
|
||||
#if defined(PBR)
|
||||
|
||||
const float PI = 3.14159265359;
|
||||
|
||||
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
||||
{
|
||||
float a = roughness * roughness;
|
||||
float a2 = a * a;
|
||||
float NdotH = max(dot(N, H), 0.0);
|
||||
float NdotH2 = NdotH*NdotH;
|
||||
|
||||
float nom = a2;
|
||||
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
|
||||
denom = PI * denom * denom;
|
||||
|
||||
return nom / denom;
|
||||
}
|
||||
|
||||
float GeometrySchlickGGX(float NdotV, float roughness)
|
||||
{
|
||||
float r = (roughness + 1.0);
|
||||
float k = (r * r) / 8.0;
|
||||
|
||||
float nom = NdotV;
|
||||
float denom = NdotV * (1.0 - k) + k;
|
||||
|
||||
return nom / denom;
|
||||
}
|
||||
|
||||
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
||||
{
|
||||
float NdotV = max(dot(N, V), 0.0);
|
||||
float NdotL = max(dot(N, L), 0.0);
|
||||
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
||||
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
||||
return ggx1 * ggx2;
|
||||
}
|
||||
|
||||
vec3 fresnelSchlick(float cosTheta, vec3 F0)
|
||||
{
|
||||
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
|
||||
}
|
||||
|
||||
vec3 applyLight(vec3 albedo, vec3 ambientLight)
|
||||
{
|
||||
vec3 worldpos = pixelpos.xyz;
|
||||
|
||||
albedo = pow(albedo, vec3(2.2)); // sRGB to linear
|
||||
ambientLight = pow(ambientLight, vec3(2.2));
|
||||
|
||||
vec3 normal = ApplyNormalMap();
|
||||
float metallic = texture(metallictexture, vTexCoord.st).r;
|
||||
float roughness = texture(roughnesstexture, vTexCoord.st).r;
|
||||
float ao = texture(aotexture, vTexCoord.st).r;
|
||||
|
||||
vec3 N = normalize(normal);
|
||||
vec3 V = normalize(uCameraPos.xyz - worldpos);
|
||||
|
||||
vec3 F0 = mix(vec3(0.04), albedo, metallic);
|
||||
|
||||
vec3 Lo = uDynLightColor.rgb;
|
||||
|
||||
#if defined NUM_UBO_LIGHTS || defined SHADER_STORAGE_LIGHTS
|
||||
if (uLightIndex >= 0)
|
||||
{
|
||||
ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1);
|
||||
if (lightRange.z > lightRange.x)
|
||||
{
|
||||
//
|
||||
// modulated lights
|
||||
//
|
||||
for(int i=lightRange.x; i<lightRange.y; i+=4)
|
||||
{
|
||||
vec4 lightpos = lights[i];
|
||||
vec4 lightcolor = lights[i+1];
|
||||
vec4 lightspot1 = lights[i+2];
|
||||
vec4 lightspot2 = lights[i+3];
|
||||
|
||||
vec3 L = normalize(lightpos.xyz - worldpos);
|
||||
vec3 H = normalize(V + L);
|
||||
//float distance = length(lightpos.xyz - worldpos);
|
||||
//float attenuation = 1.0 / (distance * distance);
|
||||
float attenuation = pointLightAttenuation(lightpos, lightcolor.a).x;
|
||||
if (lightspot1.w == 1.0)
|
||||
attenuation *= spotLightAttenuation(lightpos, lightspot1.xyz, lightspot2.x, lightspot2.y);
|
||||
|
||||
vec3 radiance = lightcolor.rgb * attenuation;
|
||||
|
||||
// cook-torrance brdf
|
||||
float NDF = DistributionGGX(N, H, roughness);
|
||||
float G = GeometrySmith(N, V, L, roughness);
|
||||
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
|
||||
|
||||
vec3 kS = F;
|
||||
vec3 kD = (vec3(1.0) - kS) * (1.0 - metallic);
|
||||
|
||||
vec3 nominator = NDF * G * F;
|
||||
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
|
||||
vec3 specular = nominator / max(denominator, 0.001);
|
||||
|
||||
float NdotL = max(dot(N, L), 0.0);
|
||||
Lo += (kD * albedo / PI + specular) * radiance * NdotL;
|
||||
}
|
||||
//
|
||||
// subtractive lights
|
||||
//
|
||||
for(int i=lightRange.y; i<lightRange.z; i+=4)
|
||||
{
|
||||
vec4 lightpos = lights[i];
|
||||
vec4 lightcolor = lights[i+1];
|
||||
vec4 lightspot1 = lights[i+2];
|
||||
vec4 lightspot2 = lights[i+3];
|
||||
|
||||
vec3 L = normalize(lightpos.xyz - worldpos);
|
||||
vec3 H = normalize(V + L);
|
||||
//float distance = length(lightpos.xyz - worldpos);
|
||||
//float attenuation = 1.0 / (distance * distance);
|
||||
float attenuation = pointLightAttenuation(lightpos, lightcolor.a).x;
|
||||
if (lightspot1.w == 1.0)
|
||||
attenuation *= spotLightAttenuation(lightpos, lightspot1.xyz, lightspot2.x, lightspot2.y);
|
||||
|
||||
vec3 radiance = lightcolor.rgb * attenuation;
|
||||
|
||||
// cook-torrance brdf
|
||||
float NDF = DistributionGGX(N, H, roughness);
|
||||
float G = GeometrySmith(N, V, L, roughness);
|
||||
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
|
||||
|
||||
vec3 kS = F;
|
||||
vec3 kD = (vec3(1.0) - kS) * (1.0 - metallic);
|
||||
|
||||
vec3 nominator = NDF * G * F;
|
||||
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0);
|
||||
vec3 specular = nominator / max(denominator, 0.001);
|
||||
|
||||
float NdotL = max(dot(N, L), 0.0);
|
||||
Lo -= (kD * albedo / PI + specular) * radiance * NdotL;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
vec3 ambient = ambientLight * albedo * ao;
|
||||
vec3 color = ambient + Lo;
|
||||
|
||||
// Tonemap (reinhard) and apply sRGB gamma
|
||||
color = color / (color + vec3(1.0));
|
||||
return pow(color, vec3(1.0 / 2.2));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
//===========================================================================
|
||||
//
|
||||
// Calculate light
|
||||
|
@ -439,6 +591,9 @@ vec4 getLightColor(vec4 material, vec4 materialSpec, float fogdist, float fogfac
|
|||
//
|
||||
color = ProcessLight(color);
|
||||
|
||||
#if defined(PBR)
|
||||
return vec4(applyLight(material.rgb, color.rgb), color.a * vColor.a);
|
||||
#else
|
||||
//
|
||||
// apply dynamic lights (except additive)
|
||||
//
|
||||
|
@ -492,6 +647,7 @@ vec4 getLightColor(vec4 material, vec4 materialSpec, float fogdist, float fogfac
|
|||
|
||||
// prevent any unintentional messing around with the alpha.
|
||||
return vec4(material.rgb * color.rgb + materialSpec.rgb * specular.rgb, material.a * vColor.a);
|
||||
#endif
|
||||
}
|
||||
|
||||
//===========================================================================
|
||||
|
|
Loading…
Reference in a new issue