three.js icon indicating copy to clipboard operation
three.js copied to clipboard

Texture: Support update ranges with other formats than RGBA.

Open rotu opened this issue 1 month ago • 3 comments

Description

Creating a non-RGBA texture and using the partial texture update via addUpdateRange doesn't update the intended part of the texture.

Reproduction steps

See fiddle https://jsfiddle.net/vx3u16zj/2/

Code

  const width = 8;
  const height = 8;
  const data = new Float32Array(width * height);
  for (let i = 0; i < data.length; ++i) {
    data[i] = i / data.length;
  }
  const dataTexture = new THREE.DataTexture(data, width, height);
  dataTexture.format = THREE.RedFormat;
  dataTexture.type = THREE.FloatType;
  dataTexture.internalFormat = 'RED32F';
  dataTexture.needsUpdate = true;


  setTimeout(() => {
    data.fill(0);
    // this should update the 12 texels starting at #32
    // instead it updates the 3 texels starting at #8
    dataTexture.addUpdateRange(32, 12);
    dataTexture.needsUpdate = true;
  }, 1000);

Live example

https://jsfiddle.net/vx3u16zj/2/

Screenshots

Image

Version

0.181.2

Device

No response

Browser

No response

OS

No response

rotu avatar Dec 04 '25 03:12 rotu

This is a known limitation. It's because of this line:

https://github.com/mrdoob/three.js/blob/56fc856c1597c14842816f1f43242cecdc36acaa/src/renderers/webgl/WebGLTextures.js#L759

We should update the documentation of Texture.addUpdateRange() to make that clear.

If I change the componentStride to 1 to support RedFormat, I get this warning:

GL_INVALID_OPERATION : glTexSubImage2D: invalid unpack params combination

Mugen87 avatar Dec 04 '25 09:12 Mugen87

The logic of texture.updateRange has a problem anyway that I'm trying to fix here (only WebGLTextures.js), but the fix needs to be improved.

agargaro avatar Dec 04 '25 10:12 agargaro

This is a known limitation. It's because of this line … We should update the documentation of Texture.addUpdateRange() to make that clear

I thought that might be the culprit. The docs should mention what the unit of the offset and length are (e.g. bytes/texels/typed array elements).

If I change the componentStride to 1 to support RedFormat, I get this warning

I think that warning is telling us that the given line is not the only place that embeds this assumption. Maybe it’s missing a pixelStorei(… UNPACK_ALIGNMENT) or somesuch?

rotu avatar Dec 04 '25 15:12 rotu