# Cycles: Generate 3d coordinates before modifier stack is resolved

Im trying to create a volumetric material that uses a 3d texture. I need this volumetric to be animated along a curve. Right now, I have the material using generated coordinates. But the 3d texture does not bend with the object. Is there a way I can have a 3d coordinate space that is calculated before the bend of the object? Or perhaps a way to bend my coordinates to match the geometry?

Repro Steps:

-Create a cylinder with multiple divisions down its length.

-Rotate the cylinder 90* on the Y axis

-Scale the cylinder to 1,1,4

-Create a curve and add a curve modifier to the cylinder using the created curve

-In cycles create a musgrave texture with Generated texture coordinates as the vector input

-Create a colour ramp to clamp the texture values

-Create an emmisive shader and plug the colour ramp into the color of the shader

-plug the shader into the volumetric output

-notice how the texture does not curve with the geometry

• Have you tried using the object coordinates? – cegaton Feb 26 '18 at 14:47
• yes, it appears that object coordinates, like the generated coordinates are defined by the bounding box of the object after all modifiers have been applied. thanks for the suggestion, unfortunate it doesn't work. – Eavin Feb 26 '18 at 15:00
• I cant share my file as its for a project under nda. I can however give detailed repro steps. – Eavin Feb 26 '18 at 15:21
• – kheetor Feb 26 '18 at 23:26

It can be done!

# Approaching it on a Surface

Before we try to do this with a 3-dimensional texture, let's attempt it in 2-dimensions. This is actually fairly straightforward. We can create a proxy mesh by duplicating our object (Shift + D), removing the modifiers from our duplicated object, then setting the Texture Mesh setting for our original object to the mesh of the second object:

Result:

Hey, that works perfectly! Unfortunately, it doesn't work for 3D textures. When we attempt this with a volumetric texture it does absolutely nothing.

# Approaching it in a Volume

The best way to do this in a volume (believe me, I've tested others) is to create a proxy mesh, then take the coordinates from the proxy mesh.

### What should our proxy mesh look like?

Unfortunately, Blender does not provide an easy way to transfer volumetric data on and off of meshes. Our saving grace is the Point Density node in Cycles. The Point Density node will create a volumetric texture based off of the vertices of any mesh, and interpolate between them. It follows, then, that our coordinate proxy mesh should be a 3D grid of points.

### Creating the proxy mesh

We'll start by adding a plane, then in Edit Mode, moving and scaling it so it has the same size and location of the bottom of the cylinder's bounding box (in the image, I enabled the bounding box display):

Next, we need to have more points in our coordinate grid. Use the Loop Cut tool (Ctrl + R) to add lots of loop cuts in both directions, trying to make the faces roughly square (I did $63$ and $15$, to get a $64\times16$ grid:

Next, in Object Mode add an Array modifier. Use Constant Offset in the $Z$ direction to create a "stack" of planes (I used an offset of $0.125$). Add enough planes to fill the bounding box of the cylinder:

Apply the Array modifier and add a Curve modifier. Set up the Curve modifier the same way that it is set up on the cylinder. Check to make sure everything deforms properly (proxy mesh set to Wireframe display):

Now the hard part:

### Creating a coordinate system

The Point Density node can only take color from Vertex Colors, Weights, and Normals. By far the most customizable of these options is Vertex Colors. We need to find a way to put Texture Coordinates into Vertex Colors.

Before we start adding nodes, we need to figure out how to emulate Generated texture coordinates. According to the Blender Manual:

Generated
Automatically-generated texture coordinates from the vertex positions of the mesh without deformation, keeping them sticking to the surface under animation. Range from 0.0 to 1. 0 over the bounding box of the undeformed mesh.

This means we need to normalize the vectors representing the positions of our vertices and put them in the positive-positive-positive octant. Here is the formula we will use:

$$\mathbf p'=\frac{\mathbf p - \mathbf c}{\mathbf d} + \mathbf{0.5}$$

where $\mathbf p$ is a point in 3D space, $\mathbf c$ is the center of the bounding box, and $\mathbf d$ is the vector of the longest diagonal of the bounding box (i.e. the dimensions).

Let's get our inputs first. Add the following nodes (press Shift + A to bring up the list of nodes or Ctrl + A to search):

The object in the Object Input node is our coordinate proxy object. The text in the Object Attribute node is dimensions. Note that "Use Modifiers" is unchecked. This ensures that the coordinates we get don't include the Curve modifier. Next, we'll use Vector Math nodes to implement the above formula:

The Convert node will be added automatically. We have one more issue with the texture coordinates: texture coordinates are encoded in a linear color space, while Vertex Colors are encoded in sRGB. From the linked Wikipedia page, we get the formula for encoding colors in sRGB:

$$C_\text{srgb}=\begin{cases} 12.92C_\text{linear}, & C_\text{linear} \leq 0.0031308\\ 1.055C_\text{linear}^{1/2.4}-0.055, & C_\text{linear} > 0.0031308 \end{cases}$$

To utilize this, create a loop subprogram by adding a Loop Input node. Create this loop:

The numbers are truncated in the screenshot. Here they are, row by row:

0.003131
12.92
0.416667  1.055  0.055


Now, add 3 Invoke Subprogram nodes with Separate and Combine Vector nodes to encode the $R$, $G$, and $B$ channels.

Now, we need to get our vectors into a Vertex Color. @Omar Ahmed has already made fabulous explanation of how to do this. You can find his answer here. I will copy the relevant parts below, but I recommend reading his answer for a detailed (and illustrated!) explanation of how Vertex Colors work in Blender.

First, create this loop:

Then, create this script (make sure to name things properly):

Here is the script:

#Create a vertex color map named "Normals" if it is not already created.
if "Coords" in Object.data.vertex_colors:
map = Object.data.vertex_colors["Coords"]
else:
map = Object.data.vertex_colors.new("Coords")

#Loop over Colors and assign them to the vertex color map.
for i, color in enumerate(Colors):
map.data[i].color = color


Finally, link up the last nodes (for an explanation of how they work, see the answer linked above). Here's the final tree:

### Getting the coordinate system into Cycles

We will be using the Point Density node as I mentioned above. On the Cylinder object, create a new material. Add a Point Density node. Set it to Object Vertices, with the Coordinate Proxy as the object and the Color Source as Vertex Color. Choose the Coords Vertex Color. Next, disable visibility for the proxy object, and link the Color output of the Point Density node to an Emission node plugged into the Volume socket of the material output (in the picture I'm using the Node Wrangler add-on to get an Emission node):

Activate Rendered View in the viewport (Shift + Z). Position the 3D View so that you are looking at one of the "caps" of the cylinder. Adjust the Radius parameter of the Point Density node to be just big enough that there are no "gaps" ($0.1$ worked for me):

Now we have our coordinates! Use the Color output just like you would normal texture coordinates, and volumetric texture-ize away!

Just remember that anything you do to animate the cylinder also needs to be done to the proxy.

(Requires Animation Nodes)