- Home /
Shadergraph: How do I get an object's rotation value inside of shadergraph? (HDRP)
Hello, I'm trying to add a rotational value to the "Rotate About Axis" node to offset my vertices positions relative to the object. I need to use the object's rotation.euler.y
value to dynamically offset those vertices, but I can't seem to find a way to reference the object's rotation value inside of shadergraph? Right now I just have a float "Counter Rotation" in place of the rotation for testing purposes, but I need the shader to be able to dynamically plug in the object's y rotation value to compute the vertex positions.
Please help, thank you!
Answer by andrew-lukasik · May 23, 2021 at 10:41 AM
Try multiplying {1,0,0}
vector by InverseModel
matrix and then plug resulting x
and z
components into atan2
omg that worked perfectly! thank you so much! I was stuck on this part for so long and you just made my entire week!
Question: what exactly is the inverse model matrix, and would you $$anonymous$$d explaining what the math behind this formula is? I couldn't find too much documentation on what exactly the transformation matrix does but I'd love to learn more about it.
Thanks in advance!
what exactly is the inverse model matrix
Afaik Model
matrix is more or less a transform.localToWorldMatrix
and Inverse Model
is just transform.localToWorldMatrix.inverse
. So, role of this matrix is tell shader how every vertex of your mesh (or source file "model", as it was called 20 years ago) should be transformed to display it as rotated, scaled and positioned correctly in space. I proposed inverse
matrix here only because you wanted to negate (or "inverse") the rotation.
math behind this formula
There is not that much to it, really. Atan2
is there to measure that angle in XZ plane. And because of how atan2 function works (angle 0 is {1,0} as input vector) I made sure to feed it the correctly oriented vector by multiplying {1,0,0} with this matrix (multiplication will rotate it and this rotation we want to measure here).
Awesome! That makes a lot of sense. Thanks for the explanation, I learned a lot!
Im trying to solve similar problem, but this method gives me rotation relative to 0,0,0. How do you get local rotation in shader graph?
Shader has no access to scene hierarchy data by default. If you want to change that you must pass this data manually via material property block.
I solved the issue by setting position column to zero in transformation matrix
Amazing, this worked! I used this in 2D and had to change the split node B->B to use G->B instead, to make it work when rotating in Z. Also I had to change from Inverse Model to Model on the first node. Thank you!
Hi. Ask this question as a separate thread, explain the situation, link here and we will figure it out.
Thanks, i have no more questions. You might have seen my previous comment, but that was a mistake. :)