material rotate position and normal
I've implemented a shader that causes vertices to rotate by outputing to World Position Offset. However, this breaks lighting.
Thinking back to basic CG concepts, I remembered that normals in a shader need to be transformed by an inverse transpose matrix. So I tried doing this in the material. But the problem is that I don't know how to populate InverseTransformMatrix. RotateAboutWorldAxis_cheap does not output a matrix - it outputs a transformed vertex. I think I want to get the matrix used by RotateAboutWorldAxis_cheap, then feed that as input into InverseTransformMatrix.
Update #1: I read that inverse transpose is not necessary if all I do is rotations and translation. So I tried modifying normal the same as world position offset but it does not work. I even tried creating the matrices myself and it works for World Position Offset but apparently not for Normal because the lighting still looks broken.
Update #2: Let me explain what I really am trying to do with a simplified example. I have two sets of vertices vtx 0-7 and vtx 8-15 in the same static mesh. I want to give each of these independent rotation and translation. I could make them two separate actors then it would be easy, but my understanding of how UE4 works is that this would do two separate draw calls, which seems wasteful. So I'm trying to do the rotation and translation for vtx 0-7 and vtx 8-15 in the material. My simplified example says two but in reality I may have 24 or more (maybe even 100+) of these.
In OpenGL the GLSL from this skeletal animation example would do it ( https://www.opengl.org/wiki/Skeletal_Animation ). Notice the shader has (uniform mat4 Bone;) which lets you specify mat4 transformations for 10 bones (aka group of verts). If there's a better way to do this in UE4 please let me know.
Update #3: The manual "creating the matrices myself" is wrong because it rotates around the world's x-axis without considering the object's pivot point. RotateAboutWorldAxis_cheap is better in that it's PivotPoint by default uses object pivit point. However, it breaks lighting because normals aren't rotated. In theory I should be able to just rotate the normals the same way... Except I'm not sure how. "World Position Offset" seems to be different than "Normal" somehow. I think "World Position Offset" is a delta (an offset) while Normal is not. So just doing VertexNormalWS -> RotateAboutWorldAxis_cheap -> Normal, does not work.
Firstly, You should calculate new normals per-vertex, in a vertex shader. To achieve that, use Custom UVs Secondly, download content examples project. Load PivotPainter level,and check example 1.6 and 1.7. You can reverse engineer the material to see how normals are reconstructed.
answered Oct 28 '16 at 12:49 PM
Hey @pemcode, first of all thank you for posting your shader work, even if it is not working as you wanted it to. It helped me a lot, and I got it working almost as I need it. Strangely enough (or it's just me missing something) I don't seem to have the normals problem you had. So I slightly adapted it to
with the results
Problem is, if I start rotating the mesh, this happens
Do you have any suggestion how to account for object rotation?
answered Apr 29 '17 at 06:41 PM
It's a pretty old question, but if someone still meets it on google, here is setup which works fine.
answered Aug 13 '19 at 05:23 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here