When I try to access foliage transform data in the material editor, I do not get the desired result
Introduction
To begin with, it will help to have some information on Instancing.
Instancing is more efficient way of rendering staticmeshes. If you are unfamiliar with the term, there is lots of information available on the web. This great video covers the subject if you would like to learn more.
Static-mesh instances can be created in UE4 via blueprints or code, and the default foliage tool paints instances.
The Problem
You may be wanting to access specific transform data per instance in Unreal to create some kind of shader effect.
Here are some examples of the kinds of effects that are only possible with instances in Unreal using the following method:
Incorrect Method
Lets say that we are trying draw fog balls and we naively try to access the position as follows.
Then we will just end up with a single fog sphere at the center of our clumps of instances or no visible sphere whatsoever.
Reason
This approach failed because by default, unless you plug pins into world displacement, or world position offset, you are doing work in the Pixel shader.
In UE4, operations happen either in the vertex shader or the pixel shader. The vertex shader handles information about the geometry of the mesh, and the pixel Shader is usually used to handle information about the colour.
Since we are querying the geometrical position of each instance…
we need to pass our calculations through the vertex shader
Tutorial
Method 1
There are two ways to work in the vertex shader, the first, is to setup customized Uv’s:
However, customized UV’s can only pass through2D vectors, so if we try to access a 3D vector like position, we will be missing the Blue channel, or the Z-axis information. This can result in errors:
This can be resolved by using more than one customized UV, and appending the vectors. This will give us the result we are looking for.
Lets say that we want to drive the radius of the fog by using the scale of the sphere:
Method 2
Some might argue that this approach is a little un-intuitive. Luckily, there is another way of doing work on the vertex shader that Epic have included.
There, that’s a bit neater, and the solution will be exactly the same.
Object Orientation Peculiarities
One final thing to note is that the Object Orientation node does not return per-instance data, unless you plug the output pin of the VertexInterpolater into the input of a Transform node which converts from Local to World space. I’m not sure why this is the only vector which requires the transform.
Conclusion
This is a really useful little trick that I hope will enable you to get a little bit more out of Unreal and Instances.
Possibly, I did not need to include the Customized Uv’s method, but I thought that it is quite useful to know both techniques, who knows when one or the other might come in handy.
I hope you enjoy playing with instances a bit more now and are able to avoid some of the frustrations I have faced!
Additional Notes
Thanks to Deathrey, Ryan brucks and all the other guru’s and sages that have written the documentation and stay active on the forums and on answerhub.
Thank you! Great article