question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Single-channel float data texture with WebGL2 context

See original GitHub issue

Is it possible to create float data textures with PIXI v5? If so, how would I have to configure the BaseTexture?

I’ve tried the following but it doesn’t work:

const dataArray = new Float32Array([
  1.0, 0.1, 0.1,
  0.1, 0.75, 0.1,
  0.1, 0.1, 0.5,
]);

const createDataTexture = (data, width, height) => {
  const resource = new PIXI.resources.BufferResource(dataArray, { width, height });
  return new PIXI.BaseTexture(resource, {
    format: 0x822e, // gl.R32F 
    type: PIXI.FLOAT
  })
}

const dataTexture = createDataTexture(dataArray, 3, 3);

E.g., in this demo nothing is rendered, i.e., I assume the texture didn’t get loaded: https://jsfiddle.net/flek/pct2qugr/136/

However, if I blow-up the data and create a 4-channel float texture it works:

const array = new Float32Array([
  1.0, 0.1, 0.1, 1.0,  0.1, 0.1, 0.1, 1.0,  0.1, 0.1, 0.1, 1.0,
  0.1, 0.1, 0.1, 1.0,  0.75, 0.1, 0.1, 1.0,  0.1, 0.1, 0.1, 1.0,
  0.1, 0.1, 0.1, 1.0,  0.1, 0.1, 0.1, 1.0,  0.5, 0.1, 0.1, 1.0,
]);

const texture = PIXI.Texture.fromBuffer(array, 3, 3);

Notice how the first channel is identical.

Here’s the updated demo, which works as expected: https://jsfiddle.net/flek/pct2qugr/137/

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
blakjak44commented, Mar 23, 2020

Hi all,

I’m very new at this but I’m trying to use this same strategy to render 16 bit unsigned integer data texture. However, it doesn’t look like Pixi supports the “usampler2d” uniform. Is there a way to support this uniform type so that I don’t need to convert to 32 bit float? If not, are there plans to add support?

1reaction
flekschascommented, Feb 26, 2020

Thanks a lot for the quick reply and detailed response!

I ended up extending BufferResource and adjusted the format and internalFormat in upload(), which did the trick!

This is what I ended up doing:

class CustomBufferResource extends PIXI.resources.Resource {
  constructor(source, options) {
    const { width, height, internalFormat, format, type } = options || {};

    if (!width || !height || !internalFormat || !format || !type) {
      throw new Error(
        'CustomBufferResource width, height, internalFormat, format, or type invalid'
      );
    }

    super(width, height);

    this.data = source;
    this.internalFormat = internalFormat;
    this.format = format;
    this.type = type;
  }
    
  upload(renderer, baseTexture, glTexture) {
    const gl = renderer.gl;

    gl.pixelStorei(
      gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL,
      baseTexture.alphaMode === 1 // PIXI.ALPHA_MODES.UNPACK but `PIXI.ALPHA_MODES` are not exported
    );

    glTexture.width = baseTexture.width;
    glTexture.height = baseTexture.height;

    gl.texImage2D(
      baseTexture.target,
      0,
      gl[this.internalFormat],
      baseTexture.width,
      baseTexture.height,
      0,
      gl[this.format],
      gl[this.type],
      this.data
    );

    return true;
  }
}

const resource = new CustomBufferResource(dataArray, {
  width: 3,
  height: 3,
  internalFormat: 'R32F',
  format: 'RED',
  type: 'FLOAT'
});

Here’s the updated demo: https://jsfiddle.net/flek/pct2qugr/175/

I wonder if you could expose internalFormat as that seems to be the only limitation of BufferResource.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Single-channel float data texture with WebGL2 context #6436
Is it possible to create float data textures with PIXI v5? If so, how would I have to configure the BaseTexture? I've tried...
Read more >
Can I store signed 16bit value in single channel in webgl2?
1 Answer 1 ... @ffsa The sampler type must be isampler2D . See Sampler (GLSL). In this case texture(u_image, v_texCoord) returns a ivec4...
Read more >
WebGL2 3D - Data Textures
Sized Format Base Format R bits G bits Color renderable Texture filterable R8 RED 8 ○ ○ R8_SNORM RED s8 ○ RG8 RG 8 8 ○...
Read more >
Emulating palette based graphics in WebGL
The palette texture is 256x1 RGBA pixels. Your image texture is any size you want but just a single channel ALPHA texture. You...
Read more >
single channel float textures as render target - Google Groups
Hi,. I would like to use floating point textures for GPGPU (OES_float_extension). ... The best option for me is LUMINANCE (single channel texture,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found