Display Translucent Volume with Advanced Light Scattering
This example shows how to display translucent volumes using realistic light scattering.
volshow function provides several rendering styles for displaying volumes, including standard volume rendering, cinematic rendering, and light scattering. The light scattering style most accurately models light propagation through an object, but is computationally expensive. Consider using the light scattering style when displaying translucent volumes with soft opacity gradients. For opaque volumes, consider the cinematic rendering style, which is less computationally expensive and generates photorealistic displays. For an example, see Display Volume Using Cinematic Rendering.
In this example, you use light scattering to accurately model light shining through a cloud.
Download Image Volume Data
This example uses a version of the Walt Disney Animation Studios Cloud Data Set  saved as a MAT file. Run this code to download the
lightScatteringCloud.mat file from the MathWorks® website. The size of the data file is approximately 6 MB.
file = matlab.internal.examples.downloadSupportFile("image","data/lightScatteringCloud.mat");
Load the file into the workspace. The
vol variable contains the cloud image volume.
load(file) whos vol
Name Size Bytes Class Attributes vol 307x250x189 58023000 single
Display Using Volume Rendering
Create a viewer window in which to display the volume.
viewer = viewer3d;
By default, the viewer contains ambient light and one light source located above and to the right of the camera.
ans = "right"
Specify the colormap and alphamap for displaying the volume.
alpha = [0.0 repmat(0.1,[1 255])]; color = "white";
Display the volume using the default rendering style,
VolumeRendering. Specify the
SpecularReflectance value as
0 so the cloud does not appear shiny.
obj = volshow(vol, ... Parent=viewer, ... RenderingStyle="VolumeRendering", ... SpecularReflectance=0, ... Alphamap=alpha, ... Colormap=color);
Adjust the camera position to view the front of the cloud.
viewer.CameraPosition = [-313.2140 154 95]; viewer.CameraZoom = 1.8;
Change the rendering style to light scattering. The display appears more realistic.
obj.RenderingStyle = "LightScattering";
Move the light behind the cloud to simulate light shining through the cloud.
viewer.LightPositionMode = "target-behind";
Manage Light Scattering Performance
You can balance rendering quality and performance by adjusting the
LightScatteringQuality property of the
Volume object. By default, the
LightScatteringQuality value is
auto, and the software automatically decreases quality during interactions, such as pan, zoom, and rotate, and maximizes quality between interactions. This animation shows the temporary change in quality during an interaction.
If your application requires that the light scattering quality remain constant, specify the
LightScatteringQuality value as a numeric scalar from 0 to 1, where a greater value corresponds to higher quality. For example, this code specifies a value that balances quality and performance.
obj.LightScatteringQuality = 0.5;
 Walt Disney Animation Studios. “Walt Disney Animation Studios - Clouds.” Accessed June 30, 2023. https://disneyanimation.com/resources/clouds/. The Clouds data set is licensed under the CC-BY-SA 3.0 license. See the license for details.