Material Local Space and Instanced Static Meshes
Hi people. I'm working with materials on Instanced Static Meshes and I'm noticing that when transforming positions to Local Space in the Material Editor (I'm specifically using the ObjectPivotPoint node), I always get the pivot point for the Instanced Static Mesh component (or the actor that owns it, not sure), but not each instance's pivot point. I'm doing cylindrical billboarding around the local X axis using World Position Offset, which works wonders on separate meshes. But on lots of instanced mesh sheets, which as well are segments of a lightning bolt, I'm always getting the same location for the pivot point. And since I need a reference of the camera location respect of the object space, this leads to incorrect behaviour. And the local X axis i'm getting is wrong also.
Help is appreciated.
I've taken a look at MaterialTemplate.USF, and it appears that TransformLocalVectorToWorld should return you per instance transformed position, but only if used in vertex frequency shader. and only if used with instanced meshes or particles. So to get what you need, you would have to use either (0,0,0) hooked up to TransformPosition(Local to Absolute World) or ObjectPosition node. They would have to be connected to two custom UVs slots. Then you can retrive the data in pixel shader by TextureCoordinate node. There might be a better solution, but this is what comes as obvious answer.
I ran into the same problem.I am using instanced meshes and I needed to do local to world transform in the material pixelshader(not vertex shader) but the transform node is not supported in the PS when using instancing. However I found out you can fairly easily modify the USF files to include that functionality without having to change anything in the engine's c++ code (you will also be able to access instance position this way in the PS). I simply added the needed matrix stuff to LocalVertexFactory.usf, MaterialTemplate.usf and LocalVertexFactory.usf and made it possible to access this stuff from the pixel/fragment shader in the material editor.
The following steps work for UE 4.14: In these files (LocalVertexFactoryCommon.usf, MaterialTemplate.usf and LocalVertexFactory.usf) you just need to look for the "USE_INSTANCING" defines. I did the following changes (please backup the files before you make any changes to them!):
1.Change following in "struct FVertexFactoryInterpolantsVSToPS" in LocalVertexFactoryCommon.usf :
2.Now in LocalVertexFactors.usf go to function "FVertexFactoryInterpolantsVSToPS VertexFactoryGetInterpolantsVSToPS(FVertexFactoryInput Input," Look for #if USE_INSTANCING. Here we copy the Instance Transform data to the Interpolants(so that they are copied to the pixelshader):
3.After that go to "struct FMaterialPixelParameters" in MaterialTemplate.usf and also look for #if USE_INSTANCING and change it too:
4.go to LocalVertexFactory.usf to function "FMaterialPixelParameters GetMaterialPixelParameters(FVertexFactoryInterpolantsVSToPS Interpolants," Again look for #if USE_INSTANCING and change it to the following code:
5.Now we must add an entire new function "GetInstanceTransform". So in LocalVertexFactory.usf look for the other GetInstanceTransform functions and just paste the following function somewhere there:
6.Finally in MaterialTemplate.usf we need to change the function "MaterialFloat3 TransformLocalVectorToWorld(FMaterialPixelParameters" which will allow us to use this transform node in the pixelfragment part of the material editor:
7.Now if you want to get instance object position in the PS too you can do that by modifying "float3 GetObjectWorldPosition(FMaterialPixelParameters Parameters)" in MaterialTemplate.usf
That's all that is needed. If you need access to other transform functions in the PS you might have to change one of the functions in MaterialTemplate.usf too but change only those that have (FMaterialPixelParameters Parameters).
answered Jun 21 '17 at 09:14 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here