How can I use ray cast volume rendering customizing my own shader?

Hi,
I’m working on ray cast volume rendering and would like to customize the rendering process by injecting my own shader code using Shader Replacement at specific stages.
So, I tryed to use vtkOpenGLGPUVolumeRayCastMapper to get access to shader control and use AddShaderReplacement(..)

However, when I try to use vtkOpenGLGPUVolumeRayCastMapper, Python reports that the class is not available or hasn’t been imported.
How can I access or enable this class in my environment?

Thanks in advance for your help.

In python you may have to explicitly import like this:

from vtkmodules.vtkRenderingVolumeOpenGL2 import vtkOpenGLGPUVolumeRayCastMapper

Also I think AddShaderReplacement(..) has to be called on the volumeprops and not the mapper itself.

Yes, I’m doing exactly what you described. Here’s a snippet of my code:

Build the volume data from a set of images

vtk_image = numpy_to_vtk_image_data(np_array, spacing, origin)

mapper = vtkOpenGLGPUVolumeRayCastMapper()
mapper.SetInputData(vtk_image)
mapper.SetBlendModeToComposite()
volumeProperty = vtk.vtkVolumeProperty()

define the specific volume properties: SetScalarOpacity & color

#…
volume = vtk.vtkVolume()
volume.SetMapper(mapper) # vtkOpenGLGPUVolumeRayCastMapper
volume.SetProperty(volumeProperty)

shaderProp = volume.GetShaderProperty()

just for debugging, all the fragments are set to red color

shaderProp.AddShaderReplacement(
vtk.vtkShader.Fragment,
“//VTK::Base::Color::Impl”,
True,
“g_fragColor = vec4(1.0, 0.0, 0.0, 1.0);”,
False
)

renderer = vtk.vtkRenderer()
renderer.AddVolume(volume)

continue specifying a RenderWindow etc.

Since the render shows the expected volume output instead of the red image, it seems the shader replacement isn’t taking effect.

Thanks in advance for your help,

Try //VTK::Shading::Impl. For a list of fragment shader replacements for this mapper, see Rendering/VolumeOpenGL2/shaders/raycasterfs.glsl.