Skip to content

Commit

Permalink
Add absorption
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdorra committed Apr 19, 2021
1 parent 79291f0 commit 5ad44ca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/shader/material.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ void configure_material(const in uint matIdx, inout RenderState rs, out Material
unpackMaterialTexInfo(matIdx, matTexInfo);

vec4 albedo = evaluateMaterialTextureValue(matTexInfo.albedoTexture, uv);
c.albedo = matData.albedo * pow(albedo.xyz, vec3(2.2));
c.albedo = matData.albedo * to_linear_rgb(albedo.xyz);

if (length(vertexColor) > 0.0) {
c.albedo *= vertexColor.xyz;
Expand Down Expand Up @@ -226,12 +226,15 @@ void configure_material(const in uint matIdx, inout RenderState rs, out Material
c.specular_f90 = vec3((1.0 - c.metallic) * c.specular + c.metallic);

vec3 emission = evaluateMaterialTextureValue(matTexInfo.emissionTexture, uv).xyz;
c.emission = matData.emission * pow(emission, vec3(2.2));
c.emission = matData.emission * to_linear_rgb(emission);

vec4 clearcoat = evaluateMaterialTextureValue(matTexInfo.clearcoatTexture, uv);
c.clearcoat = matData.clearcoat * clearcoat.y;
vec4 clearcoatRoughness = evaluateMaterialTextureValue(matTexInfo.clearcoatRoughnessTexture, uv);
float clearcoat_alpha =
matData.clearcoatRoughness * matData.clearcoatRoughness * clearcoatRoughness.x * clearcoatRoughness.x;
c.clearcoat_alpha = max(clearcoat_alpha, MINIMUM_ROUGHNESS);

c.attenuationColor = matData.attenuationColor;
c.attenuationDistance = matData.attenuationDistance;
}
10 changes: 10 additions & 0 deletions lib/shader/pt.frag
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ struct MaterialClosure {
float clearcoat;
float clearcoat_alpha;
bool thin_walled;
float attenuationDistance;
vec3 attenuationColor;
float ior;
bool backside;
vec3 n;
Expand Down Expand Up @@ -209,6 +211,13 @@ int sampleBSDFBounce(inout RenderState rs, inout vec3 pathWeight, out int eventT
HitInfo hit;
if (intersectScene_Nearest(r, hit)) {
fillRenderState(r, hit, rs);

// Absorption
if(rs.closure.backside) {
vec3 absorptionCoefficient = -log(rs.closure.attenuationColor) / rs.closure.attenuationDistance;
pathWeight *= exp(-absorptionCoefficient*hit.tfar);
}

return 1;
}
}
Expand Down Expand Up @@ -354,6 +363,7 @@ vec4 trace(const Ray r) {
contrib += sampleIBL(rs.wo) * pathWeight;
break;
}

// All clear. next sample has properly been generated and intersection was found.
// Render state contains new intersection info.
i++;
Expand Down
4 changes: 4 additions & 0 deletions lib/shader/utils.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,7 @@ ivec2 getStructParameterTexCoord(uint structIdx, uint paramIdx, uint structStrid
return ivec2((structIdx * structStride + paramIdx) % MAX_TEXTURE_SIZE,
(structIdx * structStride + paramIdx) / MAX_TEXTURE_SIZE);
}

vec3 to_linear_rgb(vec3 srgb) {
return pow(srgb.xyz, vec3(2.2));
}

0 comments on commit 5ad44ca

Please sign in to comment.