Skip to content

Commit

Permalink
Volume shader tricubic filtering bugfix for WebGL
Browse files Browse the repository at this point in the history
  • Loading branch information
OKaluza committed Jul 12, 2018
1 parent 13679c0 commit 6b033a6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
32 changes: 15 additions & 17 deletions lavavu/html/viewer.html
Original file line number Diff line number Diff line change
Expand Up @@ -536,19 +536,26 @@

#ifdef WEBGL

vec2 islices;
float maxslice;
vec2 cmin;
vec2 cmax;
vec2 islices = vec2(1.0 / slices.x, 1.0 / slices.y);
float maxslice = slices.x * slices.y - 1.0;
vec2 cmin = vec2(0.5/(slices.x*slices.y), 0.5/(slices.x*slices.y));
vec2 cmax = vec2(1.0, 1.0) - cmin;


float sample(vec3 pos)
{
//Get z slice index and position between two slices
float Z = pos.z * maxslice;
float slice = floor(Z); //Index of first slice
Z = fract(Z);
//Edge case at z min (possible with tricubic filtering)
if (int(slice) < 0)
{
slice = 0.0;
Z = 0.0;
}
//Edge case at z max
if (int(slice) > int(maxslice)-1)
else if (int(slice) > int(maxslice)-1)
{
slice = maxslice-1.0;
Z = 1.0;
Expand Down Expand Up @@ -602,10 +609,8 @@
}

#else
float sample(vec3 pos)
{
return texture3D(uVolume, pos).x;
}

#define sample(pos) texture3D(uVolume, pos).x

float tex3D(vec3 pos)
{
Expand All @@ -615,7 +620,7 @@
density = interpolate_tricubic_fast(pos);
else
#endif
density = sample(pos);
density = texture3D(uVolume, pos).x;

//Normalise the density over provided range
//(used for float textures only, all other formats are already [0,1])
Expand Down Expand Up @@ -684,13 +689,6 @@
bbMin = clamp(uBBMin, vec3(0.0), vec3(1.0));
bbMax = clamp(uBBMax, vec3(0.0), vec3(1.0));

#ifdef WEBGL
islices = vec2(1.0 / slices.x, 1.0 / slices.y);
maxslice = slices.x * slices.y - 1.0;
cmin = vec2(0.5/(slices.x*slices.y), 0.5/(slices.x*slices.y));
cmax = vec2(1.0, 1.0) - cmin;
#endif

//Compute eye space coord from window space to get the ray direction
mat4 invMVMatrix = transpose(uMVMatrix);
//ObjectSpace *[MV] = EyeSpace *[P] = Clip /w = Normalised device coords ->VP-> Window
Expand Down
32 changes: 15 additions & 17 deletions lavavu/shaders/volumeShader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,26 @@ float interpolate_tricubic_fast(vec3 coord);

#ifdef WEBGL

vec2 islices;
float maxslice;
vec2 cmin;
vec2 cmax;
vec2 islices = vec2(1.0 / slices.x, 1.0 / slices.y);
float maxslice = slices.x * slices.y - 1.0;
vec2 cmin = vec2(0.5/(slices.x*slices.y), 0.5/(slices.x*slices.y));
vec2 cmax = vec2(1.0, 1.0) - cmin;


float sample(vec3 pos)
{
//Get z slice index and position between two slices
float Z = pos.z * maxslice;
float slice = floor(Z); //Index of first slice
Z = fract(Z);
//Edge case at z min (possible with tricubic filtering)
if (int(slice) < 0)
{
slice = 0.0;
Z = 0.0;
}
//Edge case at z max
if (int(slice) > int(maxslice)-1)
else if (int(slice) > int(maxslice)-1)
{
slice = maxslice-1.0;
Z = 1.0;
Expand Down Expand Up @@ -127,10 +134,8 @@ mat4 transpose(in mat4 m)
}

#else
float sample(vec3 pos)
{
return texture3D(uVolume, pos).x;
}

#define sample(pos) texture3D(uVolume, pos).x

float tex3D(vec3 pos)
{
Expand All @@ -140,7 +145,7 @@ float tex3D(vec3 pos)
density = interpolate_tricubic_fast(pos);
else
#endif
density = sample(pos);
density = texture3D(uVolume, pos).x;

//Normalise the density over provided range
//(used for float textures only, all other formats are already [0,1])
Expand Down Expand Up @@ -209,13 +214,6 @@ void main()
bbMin = clamp(uBBMin, vec3(0.0), vec3(1.0));
bbMax = clamp(uBBMax, vec3(0.0), vec3(1.0));

#ifdef WEBGL
islices = vec2(1.0 / slices.x, 1.0 / slices.y);
maxslice = slices.x * slices.y - 1.0;
cmin = vec2(0.5/(slices.x*slices.y), 0.5/(slices.x*slices.y));
cmax = vec2(1.0, 1.0) - cmin;
#endif

//Compute eye space coord from window space to get the ray direction
mat4 invMVMatrix = transpose(uMVMatrix);
//ObjectSpace *[MV] = EyeSpace *[P] = Clip /w = Normalised device coords ->VP-> Window
Expand Down

0 comments on commit 6b033a6

Please sign in to comment.