x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

alt text

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.

alt text

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[10];) 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.

Product Version: UE 4.13
Tags:
manual.png (497.3 kB)
more ▼

asked Oct 27 '16 at 08:00 PM in Blueprint Scripting

avatar image

pemcode
50 10 14 15

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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.

more ▼

answered Oct 28 '16 at 12:49 PM

avatar image

Deathrey
7.1k 126 27 286

avatar image pemcode Oct 29 '16 at 03:00 AM

I don't think customized UV is necessary plus TexCord is a Float2 (not a Float3). I can just use VertexNormalWS as the input, then Normal as the output. However, the terms are not well-defined so the math is not obvious.

avatar image Deathrey Oct 29 '16 at 02:37 PM

Whatever you are doing with your normals on those screenshots, you are doing it per pixel. By using two customized UVs you can offload the math onto the vertex shader.

avatar image pemcode Oct 29 '16 at 10:46 PM

Then maybe I need two use two custom UVs (one for XY and one for Z and unused) since custom UV is Float2 while normal is Float3. In any case, doing the calculations per-vertex vs. per-pixel is a performance optimization.

My problem is that the functionality I implemented doesn't work as expected. As seen in my first screen shot, I use RotateAboutWorldAxis_cheap on position and on normal, and I expected this to give me correct normals for lighting calculations. But the lighting looks broken.

To test how broken the lighting is, I hardcoded Base Color to white and I used the material editor's preview with a sphere primitive. Since the light is fixed and both the position and normal are rotated the same, I expect the sphere's color to look static. But it doesn't. It looks like it's rotating (or like the light is rotating about it).

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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 alt text

with the results alt text alt text

Problem is, if I start rotating the mesh, this happens alt text

Do you have any suggestion how to account for object rotation?

lever_shader_v1.png (373.6 kB)
lever02.png (622.2 kB)
lever01.png (534.1 kB)
lever02.png (622.2 kB)
lever03.png (236.4 kB)
more ▼

answered Apr 29 '17 at 06:41 PM

avatar image

AF01
1 1

avatar image Ohsento Aug 03 '17 at 09:07 AM

You could try to transform your "ForwardAxis" parameter from local space to world space like thisalt text

This setup is different from yours, but it should work.

avatar image Petterson Jun 29 '18 at 08:27 PM

This is Exactly what I was looking for! Thank you very Much!!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question