Skip to content

Commit 6a2f2ce

Browse files
FrostKiwigreggman
authored andcommitted
correct LUT miscalculation
1 parent 15c05eb commit 6a2f2ce

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

webgl/lessons/webgl-qna-how-to-simulate-a-3d-texture-in-webgl.md

+21-19
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,25 @@ Thanks in advance!
6161
You can simulate a 3d texture by storing each plane of the 3d texture in a 2d texture
6262

6363
Then a function like this will let you use it as a 3d texture
64-
65-
vec4 sampleAs3DTexture(sampler2D tex, vec3 texCoord, float size) {
66-
float sliceSize = 1.0 / size; // space of 1 slice
67-
float slicePixelSize = sliceSize / size; // space of 1 pixel
68-
float sliceInnerSize = slicePixelSize * (size - 1.0); // space of size pixels
69-
float zSlice0 = min(floor(texCoord.z * size), size - 1.0);
70-
float zSlice1 = min(zSlice0 + 1.0, size - 1.0);
71-
float xOffset = slicePixelSize * 0.5 + texCoord.x * sliceInnerSize;
72-
float s0 = xOffset + (zSlice0 * sliceSize);
73-
float s1 = xOffset + (zSlice1 * sliceSize);
74-
vec4 slice0Color = texture2D(tex, vec2(s0, texCoord.y));
75-
vec4 slice1Color = texture2D(tex, vec2(s1, texCoord.y));
76-
float zOffset = mod(texCoord.z * size, 1.0);
77-
return mix(slice0Color, slice1Color, zOffset);
78-
}
79-
64+
65+
vec4 sampleAs3DTexture(sampler2D tex, vec3 texCoord, float size)
66+
{
67+
float sliceSize = 1.0 / size; // space of 1 slice
68+
float slicePixelSize = sliceSize / size; // space of 1 pixel
69+
float width = size - 1.0;
70+
float sliceInnerSize = slicePixelSize * width; // space of size pixels
71+
float zSlice0 = floor(texCoord.z * width);
72+
float zSlice1 = min(zSlice0 + 1.0, width);
73+
float xOffset = slicePixelSize * 0.5 + texCoord.x * sliceInnerSize;
74+
float yRange = (texCoord.y * width + 0.5) / size;
75+
float s0 = xOffset + (zSlice0 * sliceSize);
76+
float s1 = xOffset + (zSlice1 * sliceSize);
77+
vec4 slice0Color = texture2D(tex, vec2(s0, yRange));
78+
vec4 slice1Color = texture2D(tex, vec2(s1, yRange));
79+
float zOffset = mod(texCoord.z * width, 1.0);
80+
return mix(slice0Color, slice1Color, zOffset);
81+
}
82+
8083
If your 3d texture was 8x8x8 then you'd make a 2d texture that is 64x8 and put each plane of the 3d texture in your 2d texture. Then, knowing that was originally 8x8x8 you'd pass in `8.0` for the size to `sampleAs3DTexture`
8184

8285
precision mediump float;
@@ -91,10 +94,9 @@ If your 3d texture was 8x8x8 then you'd make a 2d texture that is 64x8 and put e
9194
gl_FragColor = sampleAs3DTexture(u_my3DTexture, v_texCoord, CUBE_SIZE);
9295
}
9396

94-
Note: the function above assumes you want bilinear filtering between the planes. If you don't you can simplify the function.
95-
96-
There's [a video explanation of this code here][1] which is from [this sample][2].
97+
Note: the function above assumes you want bilinear filtering between the planes. If you don't you can simplify the function, by returning `return texture2D(tex, vec2( s0, yRange));` immediately after calculating s0.
9798

99+
There's [a video explanation of this code here][1] which is from [this sample][2]. The video explanation and the final code in the sample differ slightly. This is due to the code miscalculating LUT size and shifting all colors blue, which was corrected by the author in 2019.
98100

99101
[1]: http://www.youtube.com/watch?v=rfQ8rKGTVlg#t=26m00s
100102
[2]: http://webglsamples.googlecode.com/hg/color-adjust/color-adjust.html

0 commit comments

Comments
 (0)