Search code examples
javascriptthree.jstexture2d

generate texture from array in threejs


I am trying to generate a texture from an array in threeJS and it is not working as expected.

It appears that the way I generate the texture is not correct.

If I use the following texture, it works as expected. http://www.html5canvastutorials.com/demos/assets/crate.jpg

crateTex = THREE.ImageUtils.loadTexture('data/crate.jpg');

enter image description here

If I generate a dummy texture and try to display it, it is all black...

var dummyRGBA = new Uint8Array(4 * 4 * 4);
for(var i=0; i< 4 * 4; i++){
  // RGB from 0 to 255
  dummyRGBA[4*i] = dummyRGBA[4*i + 1] = dummyRGBA[4*i + 2] = 255*i/(4*4);
  // OPACITY
  dummyRGBA[4*i + 3] = 255;
}

dummyDataTex = new THREE.DataTexture( dummyRGBA, 4, 4, THREE.RGBAFormat );
dummyDataTex.needsUpdate = true;

dummyTex = new THREE.Texture(dummyDataTex);

enter image description here


Solution

  • I think your mistake is in the fact that you make a texture of a texture.

    When you do:

    dummyDataTex = new THREE.DataTexture( dummyRGBA, 4, 4, THREE.RGBAFormat );
    

    the object dummyDataTex that you create here is already of type THREE.Texture.

    So your next step:

    dummyTex = new THREE.Texture(dummyDataTex);
    

    is not necessary. You should instead immediately use dummyDataTex.