Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Volume shader light falloff #744

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/main/kotlin/graphics/scenery/volumes/VolumeManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,13 @@ class VolumeManager(
/** Amount of randomisation for ray start and ray steps. 0.0 turns this off, 1.0 is the default,
* values larger than 1.0 might lead to noisy renderings. */
var shuffleDegree = 1.0f
set(value) {
field = value
shaderProperties["shuffleDegree"] = value
}

var maxOcclusionDistance = 4.0f
set(value) {
field = value
shaderProperties["maxOcclusionDistance"] = value
}

var kernelSize = 8.0f
var kernelSize = 5.0f
set(value) {
field = value
shaderProperties["kernelSize"] = value
Expand All @@ -149,6 +144,20 @@ class VolumeManager(
shaderProperties["occlusionSteps"] = value
}

// how strongly to blend the light falloff pass over the volume. Range 0-1
var falloffBlend = 1f
set(value) {
field = value
shaderProperties["falloffBlend"] = value
}

// the alpha value at which to register the ray hit to calculate the light falloff gradient
var falloffHitThreshold = 0.02f
set(value) {
field = value
shaderProperties["falloffHitThreshold"] = value
}

var aoDebug = 0
set(value) {
field = value
Expand Down Expand Up @@ -213,6 +222,13 @@ class VolumeManager(
shaderProperties["viewportSize"] = Vector2f()
shaderProperties["dsp"] = Vector2f()
shaderProperties["shuffleDegree"] = shuffleDegree
shaderProperties["maxOcclusionDistance"] = maxOcclusionDistance
shaderProperties["aoDebug"] = aoDebug
shaderProperties["kernelSize"] = kernelSize
shaderProperties["occlusionSteps"] = occlusionSteps
shaderProperties["falloffBlend"] = falloffBlend
shaderProperties["falloffHitThreshold"] = falloffHitThreshold

val oldKeys = material().textures.filter { it.key !in customTextures }.keys
val texturesToKeep = material().textures.filter { it.key in customTextures }
oldKeys.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ uniform int sceneGraphVisibility;
vis = vis && bool(sceneGraphVisibility);
if (vis && step > localNear && step < localFar)
{
vec4 x = sampleVolume(wpos);
vec4 x = sampleVolume(wpos, vec3(0.0));

float newAlpha = x.a;
vec3 newColor = x.rgb;
Expand All @@ -15,6 +15,32 @@ if (vis && step > localNear && step < localFar)
v.rgb = v.rgb + (1.0f - v.a) * newColor * adjusted_alpha;
v.a = v.a + (1.0f - v.a) * adjusted_alpha;


if(x.a > falloffHitThreshold && occlusionSteps > 0 && !isHit) {

float d = x.a;
float d0 = sampleVolume(wpos, vec3(kernelSize, 0.0, 0.0)).a;
float d1 = sampleVolume(wpos, vec3(0.0, -kernelSize, 0.0)).a;
float d2 = sampleVolume(wpos, vec3(0.0, 0.0, kernelSize)).a;
vec3 gradient = vec3(d - d0, d - d1, d - d2);
vec3 N = normalize(gradient);

vec3 viewNormal = normalize(ViewMatrices[0] * vec4(N, 0.0)).xyz;
vec3 sampleDir = vec3(0, 0, 1);
float NdotV = max(dot(viewNormal, sampleDir), 0.0);

[[unroll]] for(int s = 0; s < occlusionSteps; s++) {
vec3 lpos = wpos.rgb + vec3(poisson16[s], (poisson16[s].x + poisson16[s].y) / 2.0) * kernelSize;
float dist = distance(wpos.rgb, lpos);
float a = smoothstep(maxOcclusionDistance, maxOcclusionDistance * 2.0, dist);
shadowDist += a * NdotV/occlusionSteps;
}

shadowing = clamp(shadowDist, 0.0, 1.0);
isHit = true;
}


if(v.a >= 1.0f) {
break;
}
Expand Down
51 changes: 47 additions & 4 deletions src/main/resources/graphics/scenery/volumes/BDVVolume.frag
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#extension GL_EXT_control_flow_attributes : enable
#extension GL_EXT_debug_printf : enable
#extension SPV_KHR_non_semantic_info : enable
#extension GL_EXT_control_flow_attributes: enable

out vec4 FragColor;
uniform vec2 viewportSize;
uniform float shuffleDegree;
uniform float maxOcclusionDistance;
uniform float kernelSize;
uniform int occlusionSteps;
uniform float falloffBlend;
uniform float falloffHitThreshold;
uniform int aoDebug;
uniform vec2 dsp;
uniform float fwnw;
Expand Down Expand Up @@ -102,6 +106,37 @@ vec3 randomSpherePoint(vec3 rand) {
// $insert{SampleVolume}
// ---------------------

const vec2 poisson16[] = vec2[](
vec2( -0.94201624, -0.39906216 ),
vec2( 0.94558609, -0.76890725 ),
vec2( -0.094184101, -0.92938870 ),
vec2( 0.34495938, 0.29387760 ),
vec2( -0.91588581, 0.45771432 ),
vec2( -0.81544232, -0.87912464 ),
vec2( -0.38277543, 0.27676845 ),
vec2( 0.97484398, 0.75648379 ),
vec2( 0.44323325, -0.97511554 ),
vec2( 0.53742981, -0.47373420 ),
vec2( -0.26496911, -0.41893023 ),
vec2( 0.79197514, 0.19090188 ),
vec2( -0.24188840, 0.99706507 ),
vec2( -0.81409955, 0.91437590 ),
vec2( 0.19984126, 0.78641367 ),
vec2( 0.14383161, -0.14100790 )
);

// code from https://github.com/jamieowen/glsl-blend/blob/master/overlay.glsl
float blendOverlay(float base, float blend) {
return base<0.5?(2.0*base*blend):(1.0-2.0*(1.0-base)*(1.0-blend));
}
vec3 blendOverlay(vec3 base, vec3 blend) {
return vec3(blendOverlay(base.r,blend.r),blendOverlay(base.g,blend.g),blendOverlay(base.b,blend.b));
}
vec3 blendOverlay(vec3 base, vec3 blend, float opacity) {
return (blendOverlay(base, blend) * opacity + base * (1.0 - opacity));
}


void main()
{
mat4 ipv = Vertex.inverseView * Vertex.inverseProjection;
Expand All @@ -119,7 +154,7 @@ void main()

vec2 depthUV = (vrParameters.stereoEnabled ^ 1) * Vertex.textureCoord + vrParameters.stereoEnabled * vec2((Vertex.textureCoord.x/2.0 + currentEye.eye * 0.5), Vertex.textureCoord.y);
depthUV = depthUV * 2.0 - vec2(1.0);

// NDC of frag on near and far plane
vec4 front = vec4( uv, -1, 1 );
vec4 back = vec4( uv, 1, 1 );
Expand All @@ -130,6 +165,9 @@ void main()
vec4 wback = ipv * back;
wback *= 1 / wback.w;

vec4 direc = Vertex.inverseView * normalize(wback-wfront);
direc.w = 0.0f;

// -- bounding box intersection for all volumes ----------
float tnear = 1, tfar = 0, tmax = getMaxDepth( depthUV );
float n, f;
Expand Down Expand Up @@ -186,7 +224,11 @@ void main()
float step_prev = step - stepWidth;
vec4 wprev = mix(wfront, wback, step_prev);
vec4 v = vec4( 0 );
float shadowDist = 0.0f;
float shadowing = 0.0f;
vec4 previous = vec4(0.0f);
bool isHit = false;
vec3 foo = vec3(0.9, 0.3, 0.2);
for ( int i = 0; i < numSteps; ++i)
{
vec4 wpos = mix( wfront, wback, step );
Expand All @@ -203,15 +245,16 @@ void main()
*/
wprev = wpos;


if(fixedStepSize) {
step += stepWidth * (1.0f+shuffleDegree*shuffle.x/2.0f);
} else {
step += nw + step * fwnw * (1.0f+shuffleDegree*shuffle.x/2.0f);
}
}
FragColor = v;


// use shadow squared as the opacity of the final value
// to get rid of black artifacts from the shadows
FragColor = vec4(blendOverlay(v.xyz, vec3(shadowing), falloffBlend), shadowing * shadowing);
if(v.w < 0.001f) {
discard;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ uniform sampler3D volume;
uniform sampler2D transferFunction;
uniform sampler2D colorMap;

vec4 sampleVolume( vec4 wpos )
vec4 sampleVolume( vec4 wpos, vec3 kernelOffset )
{
bool cropping = slicingMode == 1 || slicingMode == 3;
bool slicing = slicingMode == 2 || slicingMode == 3;
Expand All @@ -28,7 +28,7 @@ vec4 sampleVolume( vec4 wpos )
float dv = slicingPlane.x * wpos.x + slicingPlane.y * wpos.y + slicingPlane.z * wpos.z;

// compare position to slicing plane
// negative w inverts the comparision
// negative w inverts the comparison
isCropped = isCropped || (slicingPlane.w >= 0 && dv > slicingPlane.w) || (slicingPlane.w < 0 && dv < abs(slicingPlane.w));

float dist = abs(dv - abs(slicingPlane.w)) / length(slicingPlane.xyz);
Expand All @@ -41,10 +41,9 @@ vec4 sampleVolume( vec4 wpos )
return vec4(0);
}


vec3 pos = (im * wpos).xyz + 0.5;

float rawsample = convert(texture( volume, pos / textureSize( volume, 0 ) ).r);
float rawsample = convert(texture( volume, pos / textureSize( volume, 0 ) + kernelOffset / textureSize(volume, 0) ).r);
float tf = texture(transferFunction, vec2(rawsample + 0.001f, 0.5f)).r;
vec3 cmapplied = texture(colorMap, vec2(rawsample + 0.001f, 0.5f)).rgb;

Expand Down
Loading