Skip to content

Commit

Permalink
perf(rendering): optimize volume shadow calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
sankhesh committed Jan 31, 2023
1 parent 7d7e616 commit e0a3045
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions Sources/Rendering/OpenGL/glsl/vtkVolumeFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -672,24 +672,25 @@ float volume_shadow(vec3 posIS, vec3 lightDirNormIS)
}
vec4 scalar = vec4(0.0);
float maxdist = hit.tmax;
if(maxdist < EPSILON) {
return 1.0;
}

// interpolate shadow ray length between: 1 unit of sample distance in IS to SQRT3, based on globalIlluminationReach
float maxgi = mix(sampleDistanceISVS_jitter,SQRT3,giReach);
maxdist = min(maxdist,maxgi);
if(maxdist < EPSILON) {
return 1.0;
}

// support gradient opacity
#ifdef vtkGradientOpacityOn
vec4 normal;
#endif

vec3 current_step = sampleDistanceISVS_jitter * lightDirNormIS;
float maxSteps = ceil(maxdist/sampleDistanceISVS_jitter);
float opacityDelta = 0.0;
float current_dist = 0.0;
float current_step = length(sampleDistanceISVS_jitter * lightDirNormIS);
float clamped_step = 0.0;
float sampled_dist = 0.0;

for (float i = 0.0; i < maxSteps; i++)
while(current_dist < maxdist)
{
scalar = getTextureValue(posIS);
opacity = texture2D(otexture, vec2(scalar.r * oscale0 + oshift0, 0.5)).r;
Expand All @@ -704,18 +705,13 @@ float volume_shadow(vec3 posIS, vec3 lightDirNormIS)
return 0.0;
}

// optimization: increase/decrease sample distance based on changed in opacity value
opacityDelta = opacityPrev - opacity;
opacityPrev = opacity;
if (opacityDelta > 0.0){
current_step *= 0.9;
} else if (opacityDelta < 0.0){
current_step *= 1.1;
}
posIS += current_step;
clamped_step = min(maxdist - current_dist, current_step);
sampled_dist = current_dist + clamped_step;
posIS += sampled_dist;
current_dist += current_step;
}

return shadow;
return shadow;
}

vec3 applyShadowRay(vec3 tColor, vec3 posIS, vec3 viewDirectionVC)
Expand Down

0 comments on commit e0a3045

Please sign in to comment.