I’m trying to render the volume with RGB voxel information, but it is not working properly.
What should I do in order to render the correct RGB value in the scene? The below code in the scene only gives a red colored cube, not the color that I set.
const dims = new Vector3(400,400,400 * 3);
// array that holds r,g,b values for each voxel
const voxelArray = new Uint8Array(dims.x * dims.y * dims.z * 3);
for (var x = 0; x < dims.x; x++) {
for (var y = 0; y < dims.y; y++) {
for (var z = 0; z < dims.z; z++) {
var index = (x + y * dims.x + z * dims.x * dims.y) * 3;
voxelArray[index] = 30; // Red component
voxelArray[index + 1] = 120; // Green component
voxelArray[index + 2] = 170; // Blue component
}
}
}
const texture = new THREE.Data3DTexture(
voxelArray,
dims.x,
dims.y,
dims.z,
);
texture.format = THREE.RGBAFormat;
texture.type = THREE.UnsignedByteType;
texture.minFilter = THREE.LinearFilter;
texture.magFilter = THREE.LinearFilter;
const uniforms = {
volume: { type: 't', value: texture },
resolution: {
type: 'v3',
value: new THREE.Vector3(resolution.x, resolution.y, resolution.z),
},
};
const material = new THREE.ShaderMaterial({
uniforms: uniforms,
});
const geometry = new THREE.BoxGeometry(resolution.x, resolution.y, resolution.z);
const mesh = new THREE.Mesh(geometry, material);