I am using the Volume Shader module to display my Nrrd file in three js using the following:
new NRRDLoader().load( './vioptix.nrrd', function ( volume ) {
const texture = new THREE.Data3DTexture( volume.data, volume.xLength, volume.yLength, volume.zLength );
texture.format = THREE.RedFormat;
texture.type = THREE.FloatType;
texture.minFilter = texture.magFilter = THREE.LinearFilter;
texture.unpackAlignment = 1;
texture.needsUpdate = true;
shader = VolumeRenderShader1;
uniforms = THREE.UniformsUtils.clone( shader.uniforms );
uniforms[ 'u_data' ].value = texture;
uniforms[ 'u_size' ].value.set( volume.xLength, volume.yLength, volume.zLength );
uniforms[ 'u_clim' ].value.set( volconfig.clim1, volconfig.clim2 );
uniforms[ 'u_renderstyle' ].value = volconfig.renderstyle == 'mip' ? 0 : 1; // 0: MIP, 1: ISO
uniforms[ 'u_renderthreshold' ].value = volconfig.Upper_threshold; // For ISO renderstyle
uniforms[ 'u_cmdata' ].value = cmtextures[ volconfig.colormap ];
material = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: shader.vertexShader,
fragmentShader: shader.fragmentShader,
side: THREE.BackSide, // The volume shader uses the backface as its "reference point"
} );
geometry = new THREE.BoxGeometry( volume.xLength, volume.yLength, volume.zLength );
geometry.translate( volume.xLength / 2 - 0.5, volume.yLength / 2 - 0.5, volume.zLength / 2 - 0.5 );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh);
render();
} );
The volume.data is a float32array passed onto the u_data uniform to color the vertices. However, any 0 values in the array map to blue when using MIP as shown in the picture:
I was trying to figure out how I can make the vertex shader only display the vertices associated with values greater than 0 in the volume.data array. I thought about passing the volume.data as an attribute to the vertex shader but I am unclear as to how to associate a value with a particular vertex position and then not displaying the blue at all.
// Vertex Shader
void main() {
mat4 viewtransformf = modelViewMatrix;
mat4 viewtransformi = inverse(modelViewMatrix);
// Project local vertex coordinate to camera position. Then do a step
// backward (in cam coords) to the near clipping plane, and project back. Do
// the same for the far clipping plane. This gives us all the information we
// need to calculate the ray and truncate it to the viewing cone.
vec4 position4 = vec4(position, 1.0);
vec4 pos_in_cam = viewtransformf * position4;
// Intersection of ray and near clipping plane (z = -1 in clip coords)
pos_in_cam.z = -pos_in_cam.w;
v_nearpos = viewtransformi * pos_in_cam; // This is a vector from
// Intersection of ray and far clipping plane (z = +1 in clip coords)
pos_in_cam.z = pos_in_cam.w;
v_farpos = viewtransformi * pos_in_cam;
v_position = position4.xyz; // This is position of the particular vertex in 3D
gl_Position = projectionMatrix * viewMatrix * modelMatrix * position4;
}`,