0

I have custom shader where i have to send a segmentation id associated to each pixel in (x,y) position. Now i am sending that as a texture and getting that data as:

  float segID = sampleTexture(persTexture, vPosition.xy).r;

and the segmentation id is encoded as "r" in a texture but the problem here is that since the type is float32, if in any case i have segmentation id more than 255, i can't send that with texture because i can't put this data in texture.

Is there any way i can solve this?

can i have storage buffer or any hack to this?

var uniforms = {
     // some other uniforms
     persTexture: { type: 't', value: new THREE.Texture() },
     //some other uniforms
 }

and data is put as:

var imageData = new Uint8Array(4 * persDatas[threshold].length)
for (var x = 0; x < persDatas[threshold].length; x++) {
            var segID = Math.floor((255 * persDatas[threshold][x]) / response.data.max)
            imageData[x * 4] = segID
            imageData[x * 4 + 1] = segID
            imageData[x * 4 + 2] = segID
            imageData[x * 4 + 3] = 255
}
 var texture = new THREE.DataTexture(imageData, 4104, 1856)
 texture.needsUpdate = true
 persTextures[threshold] = texture
 uniforms.persTexture.value = texture

can anyone help on this please??

Pravin Poudel
  • 1,433
  • 3
  • 16
  • 38
  • you can use different texture format like unclamped 32bit float for example `GL_LUMINANCE32F_ARB` or use more channels and combine them together – Spektre Oct 08 '22 at 06:30
  • Hi @Spektre thank you for response !! is there anything like storage buffer that i can use in my case? – Pravin Poudel Oct 08 '22 at 06:31
  • never used those as I am stuck with compatibility... btw `segID` is really float? – Spektre Oct 08 '22 at 06:32
  • one thing that is confusing for me is, how can i assign format of texture in datatexture !! I dont see anything on documentation or examples on internet !! so i have to set data texture as GL_LUMINANCE32F_ARB format ? – Pravin Poudel Oct 08 '22 at 06:41
  • right now i am creating texture as var imageData = new Uint8Array(4 * persDatas[threshold].length) and put data in the imageData typedarray and get the data in shader as float segID = sampleTexture(persTexture, vPosition.xy).r; – Pravin Poudel Oct 08 '22 at 06:42
  • i dont know how to do with float32 and this type, i am sorry i am new to three.js – Pravin Poudel Oct 08 '22 at 06:42
  • You set the texture format in CPU side code see [Does an any image file format support negative floats?](https://stackoverflow.com/a/64767404/2521214) I do not code in you renvoronment but I would expect somethng like `imageData = new GLfloat[size_in_pixels];` for 1 channel 32 bit float texture – Spektre Oct 08 '22 at 07:14
  • when i read, i read that the float data will be clamped to 0 to 1 and distributed value between r, g, b and a. Am i missing something? https://threejs.org/docs/#api/en/constants/Textures – Pravin Poudel Oct 08 '22 at 07:23
  • that is true for most float formats but few are not clamping (they where added as extention years latter so the original GL docs you using might not know them) ... there ae also commands to turn off clamping completely however they not reliable across GL implementations – Spektre Oct 08 '22 at 07:32
  • every format that does support floating point value in three.js texture seems to clamp value from [0, 1] and i dont understand what does that mean that it will divide value between R,G, B and A. This is one of the float format, all available are with similar explanation – Pravin Poudel Oct 08 '22 at 07:39
  • " LuminanceFormat reads each element as a single luminance component. This is then converted to a floating point, clamped to the range [0,1], and then assembled into an RGBA element by placing the luminance value in the red, green and blue channels, and attaching 1.0 to the alpha channel. " - Three.js documentation – Pravin Poudel Oct 08 '22 at 07:39
  • `GL_LUMINANCE32F_ARB` is not the same as `GL_LUMINANCE` – Spektre Oct 08 '22 at 08:55

0 Answers0