# Texturize 3D model (ray tracing)

**URL:** <https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340>\
**Category:** Development\
**Tags:** visualization\
**Created:** [June 27, 2019, 12:44pm UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340 "2019-06-27T12:44:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Ef](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Michael\_Ef](https://discourse.slicer.org/u/Michael_Ef)\
**Post date:** [June 27, 2019, 12:44pm UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340/1 "2019-06-27T12:44:54Z")

</div>

Hi there,

I have created a 3D model from a stack of histological images. Now i would like to texturize/colorize this model by using the original rgb-images. Is there a way to project this “color” information on the model for example by ray casting (like it is done in volume rendering module)? Or any other way how I can achieve this? It should also work if i cut through the model.

I would appreciate any ideas or solutions. 🙂

Thanks in advance  
Michael

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [June 30, 2019, 5:25pm UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340/2 "2019-06-30T17:25:20Z")

</div>

You can visualize a 3D color volume using ray casting as described in this post:

> [@Merge colored images and show them as 1 volume](https://discourse.slicer.org/t/merge-colored-images-and-show-them-as-1-volume/6472/6):
>
> If you have a full-color image then you can show a direct volume rendering of it by turning off “independent component” option in the volume renderer (it makes image components interpreted as RGBA). Type this in the Python console after enabling volume rendering: getNode('VolumeProperty').GetVolumeProperty().SetIndependentComponents(0) Note that you need an RGBA image (not just RGB). The fourth (alpha) component controls opacity. If you only have an RGB image then you can create the “alpha” v…

---

<div class="post-metadata">

**Author:** ![Michael\_Ef](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Michael\_Ef](https://discourse.slicer.org/u/Michael_Ef)\
**Post date:** [July 27, 2019, 1:33pm UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340/3 "2019-07-27T13:33:16Z")

</div>

Thanks a lot Andras. 🙂 That worked so far, except for the fact

 ![Screenshot](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/c/acebe60f7364df4877c6cd2416028291c51c4b52.png) but that didnt help.

Do you know how to solve this problem?

Thanks and best regards,  
Michael

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 27, 2019, 1:58pm UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340/4 "2019-07-27T13:58:21Z")

</div>

As described in the linked post, you need an RGBA volume - the alpha channel containing transparency of voxels. If you want to make the background transparent then set alpha values of the background to 0.

---

<div class="post-metadata">

**Author:** ![Michael\_Ef](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Michael\_Ef](https://discourse.slicer.org/u/Michael_Ef)\
**Post date:** [July 28, 2019, 12:25pm UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340/5 "2019-07-28T12:25:52Z")

</div>

Sorry to bother you again, but where do I have to set the alpha values? In the scalar volume or can I set this in the volume properties? Hope this question isn’t too stupid, but I’m still new to Slicer. Thanks!

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 29, 2019, 3:58am UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340/6 "2019-07-29T03:58:35Z")

</div>

There is no GUI for manipulating voxel values, but you can retrieve voxel values in the Python console as a numpy array (using `slicer.util.arrayFromVolume`), adjust the values of the alpha channel using standard numpy array manipulations, then update the volume with the changes (`slicer.util.arrayFromVolumeModified`). See example [here](https://www.slicer.org/wiki/Documentation/Nightly/Developers/Python_scripting#Accessing_Volume_data_as_numpy_array).

---

<div class="post-metadata">

**Author:** ![Michael\_Ef](https://avatars.discourse-cdn.com/v4/letter/m/c0e974/32.png) [@Michael\_Ef](https://discourse.slicer.org/u/Michael_Ef)\
**Post date:** [August 9, 2019, 9:20am UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340/7 "2019-08-09T09:20:40Z")

</div>

Hello Andras,  
that worked well, thanks 🙂 Is there a way to keep these settings when I save the whole scene as a medical record bundle? Currently, when I reload the scene from the bundle my modifications are not displayed anymore. Display node is observing the volume properties and the volume node.  
How can I ensure to reload the scene in exactly the same state as I saved it?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 10, 2019, 6:32pm UTC](https://discourse.slicer.org/t/texturize-3d-model-ray-tracing/7340/8 "2019-08-10T18:32:54Z")

</div>

Probably `IndependentComponents` property is not saved in the file. Probably you would just need to add a couple of lines to [vtkMRMLVolumePropertyNode](https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/VolumeRendering/MRML/vtkMRMLVolumePropertyNode.cxx) to read/write the property to XML.
