Skip to content

Commit

Permalink
Support general reflectance
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralhalo committed May 15, 2022
1 parent 3cb7860 commit f215100
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
3 changes: 1 addition & 2 deletions assets/lumi/shaders/forward/main.frag
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ void frx_pipelineFragment()
float ao = (frx_fragEnableAo && frx_modelOriginRegion) ? frx_fragLight.z : 1.0;

float roughness = max(0.01, frx_fragRoughness); // TODO: use white clear color and stop doing this
float metalness = frx_fragReflectance > 0.5 ? frx_fragReflectance : 0.0; // TODO: do something
float disableDiffuse = 1.0 - float(frx_fragEnableDiffuse);

// put water flag last because it makes the material buffer looks blue :D easier to debug
Expand All @@ -121,7 +120,7 @@ void frx_pipelineFragment()
fragColor[1] = vec4(frx_fragLight.xy, frx_fragEmissive, 1.0);
fragColor[2] = vec4(frx_vertexNormal, 1.0);
fragColor[3] = vec4(frx_fragNormal, 1.0);
fragColor[4] = vec4(roughness, metalness, ao, 1.0);
fragColor[4] = vec4(roughness, frx_fragReflectance, ao, 1.0);
fragColor[5] = vec4(frx_normalizeMappedUV(frx_texcoord), bitFlags, 1.0);
}

Expand Down
16 changes: 8 additions & 8 deletions assets/lumi/shaders/prog/shading.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,20 @@ float diffuseNdL(float NdotL, float alpha, float disableDiffuse)
return diffuseNdotL;
}

void lightPbr(vec3 albedo, float alpha, vec3 radiance, float roughness, float metallic, vec3 f0, vec3 toLight, vec3 toEye, vec3 normal, float disableDiffuse)
void lightPbr(vec3 albedo, float alpha, vec3 radiance, float roughness, vec3 f0, vec3 toLight, vec3 toEye, vec3 normal, float disableDiffuse)
{
vec3 halfway = normalize(toEye + toLight);
vec3 fresnel = pbr_fresnelSchlick(pbr_dot(toEye, halfway), f0);
float rawNdL = dot(normal, toLight);
float NdotL = clamp(rawNdL, 0.0, 1.0);

//fake metallic diffuse
metallic = min(0.5, metallic);
// darken diffuse on conductive materials
float conductiveFac = 1.0 - l2_max3(f0);

float diffuseNdotL = diffuseNdL(NdotL, alpha, disableDiffuse);

shading0.specular = pbr_specularBRDF(roughness, radiance, halfway, toLight, toEye, normal, fresnel, NdotL);
shading0.diffuse = albedo * radiance * diffuseNdotL * (1.0 - fresnel * step(0.0, rawNdL)) * (1.0 - metallic) / PI;
shading0.diffuse = albedo * radiance * diffuseNdotL * (1.0 - fresnel * step(0.0, rawNdL)) * conductiveFac / PI;
}

void prepare(vec4 color, sampler2D natureTexture, vec3 eyePos, float vertexNormaly, bool isUnderwater, inout vec4 light)
Expand Down Expand Up @@ -297,7 +297,7 @@ vec4 shading(vec4 color, sampler2D natureTexture, vec4 light, float ao, vec2 mat

prepare(color, natureTexture, eyePos, vertexNormal.y, isUnderwater, light);

vec3 f0 = mix(vec3(0.01), albedo, material.y);
vec3 f0 = albedo * material.y;
vec3 toEye = -normalize(eyePos);

vec3 baseLight, blockLight, hlLight, skyLight;
Expand All @@ -310,16 +310,16 @@ vec4 shading(vec4 color, sampler2D natureTexture, vec4 light, float ao, vec2 mat
// vanilla-ish style diffuse
float dotUpNorth = l2_max3(abs(normal * vec3(0.6, 1.0, 0.8)));
// perfect diffuse light
vec3 shaded = albedo * (baseLight + blockLight * (1.0 - blF)) * dotUpNorth * max(1.0 - material.y, 0.5) / PI;
vec3 shaded = albedo * (baseLight + blockLight * (1.0 - blF)) * dotUpNorth * max(1.0 - f0, vec3(0.5)) / PI;
// block light specular
vec3 specular = pbr_specularBRDF(max(material.x, 0.5 * material.y), blockLight, blH, normal, toEye, normal, blF, 1.0);
shaded += specular;

lightPbr(albedo, color.a, hlLight, material.x, material.y, f0, toEye, toEye, normal, disableDiffuse);
lightPbr(albedo, color.a, hlLight, material.x, f0, toEye, toEye, normal, disableDiffuse);
shaded += shading0.specular + shading0.diffuse;
specular += shading0.specular;

lightPbr(albedo, color.a, skyLight, material.x, material.y, f0, frx_skyLightVector, toEye, normal, disableDiffuse);
lightPbr(albedo, color.a, skyLight, material.x, f0, frx_skyLightVector, toEye, normal, disableDiffuse);
shaded += shading0.specular + shading0.diffuse;
specular += shading0.specular;

Expand Down

0 comments on commit f215100

Please sign in to comment.