Recently diving into WebGL (and 3D graphics in general) using three.js, I'm looking to create multiple textures from a 2D canvas for rendering various meshes, each with its own unique texture.
Simply passing the canvas to a new THREE.Texture() causes all objects to have the same texture, as it updates to match the current canvas. To work around this issue, I attempted storing each canvas array and creating a new texture using new THREE.DataTexture(). However, encountered errors on Chrome and an upside-down texture display on Firefox.
userName = $nameInput.val();
ctx2d.fillText( userName, 256, 128);
var canvasData = ctx2d.getImageData(0,0,512,256);
texture = new THREE.DataTexture(canvasData.data, 512, 256, THREE.RGBAFormat);
texture.needsUpdate = true;
material = new THREE.MeshBasicMaterial({ map: texture });
geometry = new THREE.PlaneGeometry(20,10);
textMesh = new THREE.Mesh( geometry, material);
scene.add(textMesh);
Encountering error messages such as 'WebGL: INVALID_OPERATION: texImage2D: type UNSIGNED_BYTE but ArrayBufferView not Uint8Array' on Chrome and Safari, while Firefox had the texture displayed upside down.
Referencing Mozilla documentation mentioning the data being a Uint8ClampedArray, attempted resolving the error by creating a new Uint8Array() and passing the data:
var data = new Uint8Array(canvasData.data);
texture = new THREE.DataTexture(data, 512, 256, THREE.RGBAFormat);
Still facing the issue of the texture displaying upside-down. Any insights on what could be causing this?