0
votes

two question. first off, how could I set a particular value in a 3d texture to 1, lets say the y coordinate of the element at index 1,1,1 in the following Int16Array so I could later read it. I think it'd go something like this:

var data = new Int16Array(size * size * size);
data.fill(0);

// ??? (somehow I'd set values of the data array at index 1,1,1 but I'm unsure how)
data ??? = 1;

gl.texImage3D(
  gl.TEXTURE_3D,
  0,
  gl.R16I,
  size,
  size,
  size,
  0,
  gl.RED_INTEGER,
  gl.SHORT,
  data);

secondly, later in my fragment shader, how could I grab that value using the GLSL texture function. I think it'd go something like this:

uniform isampler3d t_sampler;

...

ivec4 value = texture( t_sampler , vec3( 1.0 , 1.0 , 1.0 ) );
if( value.y == 1 ){
  // do some special stuff
}

any help would be appreciated. again I'm just trying to create my texture using a data array I create and then read that value in the frag shader.

fyi this code is running but failing to get to the "do some special stuff" part.

thanks

1

1 Answers

1
votes

// ??? (somehow I'd set values of the data array at index 1,1,1 but I'm unsure how) data ??? = 1;

const width = ??
const height = ??
const depth = ??
const numChannels = 1; // 1 for RED, 2 for RG, 3 for RGB, 4 for RGBA
const sliceSize = width * height * numChannels;
const rowSize = width * numChannels;
const x = 1;
const y = 1;
const z = 1;

const offset = z * sliceSize + y * rowSize + x;
data[offset] = redValue;

If there are more channels, for example RGBA then

data[offset + 0] = redValue;
data[offset + 1] = greenValue;
data[offset + 2] = blueValue;
data[offset + 3] = alphaValue;

how could I grab that value using the GLSL texture function

To get a specific value from a texture you can use texelFetch with pixel/texel coordinates.

uniform isampler3d t_sampler;

...
int x = 1;
int y = 1;
int z = 1;
int mipLevel = 0;
ivec4 value = texelFetch(t_sampler, ivec3(x, y, z), mipLevel);
if( value.y == 1 ){
  // do some special stuff
}

Be sure to check the JavaScript console for errors. In your case you probably need to set filtering to NEAREST since you're not providing mips and since integer textures can not be filtered.