How to create a texture?

:sunrise:
As from the view data amplitudes:number[] = []; create a texture for uniform sampler2D soundTex; ?

I want to send the texture to the shader

mat.setProperty("soundTex", soundTex);```

You mean create texture from pixel data like [[255,255,255,255],[128,123,123,123],…]?

this is how I get the data

audioSource.getPCMData(0).then(dataView => {
 if (!dataView)  return;
 for (let i = 0; i < dataView.length; ++i) {
   console.log('data: ' + dataView.getData(i));
 }
}); 

in the console I get this data

-0.000030517578125
0
0.000030518509447574615
-0.000030517578125
0
0.000030518509447574615
-0.000030517578125

I put the data in an array
this.amplitudes.push(dataView.getData(i));
this is what is in the array

[
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0, -0.000030517578125,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0,
  0, 0, 0,                  0
]

Sorry, I am not familiar with this problem.

how is a texture created based on pixel data?

Like this, this create a red picture.

    start() {
        let spriteFrame = new SpriteFrame();
        spriteFrame.texture = this.createTexture();
        let sp = this.getComponent(Sprite);
        sp.spriteFrame = spriteFrame;
    }

    createTexture(){
        let texture = new Texture2D();
        texture.reset({
            width: 2,
            height: 2,
            format: Texture2D.PixelFormat.RGBA8888
        });
        let data = new Int8Array([
            255,0,0,255,
            255,0,0,255,
            255,0,0,255,
            255,0,0,255,
        ]);
        texture.uploadData(data, 0);

        return texture;
    }

image

2 Likes

great! mark!