# Get Actor x, y, z rotation angle in shader

**URL:** <https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362>\
**Category:** Rendering\
**Tags:** unreal-engine\
**Created:** [December 21, 2020, 2:08am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362 "2020-12-21T02:08:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![somawheels](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@somawheels](https://forums.unrealengine.com/u/somawheels)\
**Post date:** [December 21, 2020, 2:08am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/1 "2020-12-21T02:08:45Z")

</div>

I need to get rotations angles in a shader. There is a node called “getFoliageZRotation” which gets the z rotation angle. But I would like to get all rotation angles, is this possible?

P.S Using Blueprints for this is not an option for my use case.

---

<div class="post-metadata">

**Author:** ![MostHost\_LA](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mosthost_la/32/907035_2.png) [@MostHost\_LA](https://forums.unrealengine.com/u/MostHost_LA)\
**Post date:** [December 21, 2020, 2:17am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/2 "2020-12-21T02:17:34Z")

</div>

Actor rotation is a node in shaders.  
it may be labeled Object rotation.  
either way its an integral part of doing crazy stuff, so yes, it exists.

Alternatively, though you don’t need to. You can always use Material Parameter Collections and update the content with an unrelated BP.

Whats that mean??  
You have an actor bp in the level with nothing at all that is simply in charge of writing the value of ItemA inside the MPC.

For things like actor location this is useful when you are dealing with shaders managing effects like foot prints or trails or any sort of thing that has to pan an image in world space around a material - where the actor location has nothing to do at all with the location you need.

Edit: the link you could have Googled…  
[https://docs.unrealengine.com/en-US/…tes/index.html](https://docs.unrealengine.com/en-US/RenderingAndGraphics/Materials/ExpressionReference/Coordinates/index.html)

ObjectOrientation

---

<div class="post-metadata">

**Author:** ![somawheels](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@somawheels](https://forums.unrealengine.com/u/somawheels)\
**Post date:** [December 21, 2020, 3:16am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/3 "2020-12-21T03:16:16Z")

</div>

Thanks for your reply. There is a node called ObjectOrientation. Its basically an up direction vector i think. What i need though, is either a quaternion rotation, or the x, y, z rotation in angles.

Blueprints and material parameter collections aren’t suitable for me, as i would be using this shader on thousands of objects.

---

<div class="post-metadata">

**Author:** ![Arkiras](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arkiras/32/1008648_2.png) [@Arkiras](https://forums.unrealengine.com/u/Arkiras)\
**Post date:** [December 21, 2020, 4:22am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/4 "2020-12-21T04:22:23Z")

</div>

> [@somawheels](#):
>
> Blueprints and material parameter collections aren’t suitable for me, as i would be using this shader on thousands of objects.

Depending on how you’re placing them you might be able to use [custom primitive data](https://forums.unrealengine.com/community/community-content-tools-and-tutorials/1677563-custom-primitive-data-in-ue4-materials).

---

<div class="post-metadata">

**Author:** ![MostHost\_LA](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mosthost_la/32/907035_2.png) [@MostHost\_LA](https://forums.unrealengine.com/u/MostHost_LA)\
**Post date:** [December 21, 2020, 4:49am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/5 "2020-12-21T04:49:08Z")

</div>

What?  
you have the up vector. Just dot between that and the world position…

---

<div class="post-metadata">

**Author:** ![neofuturelabs](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neofuturelabs/32/1194562_2.png) [@neofuturelabs](https://forums.unrealengine.com/u/neofuturelabs)\
**Post date:** [October 6, 2022, 3:08pm UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/6 "2022-10-06T15:08:28Z")

</div>

I found the solution on this Japanese blog post:

> **[【UE4】ObjectのRotationをMaterialから取得する方法 【★★☆】 | キンアジのブログ](https://kinnaji.com/2019/08/19/getanglefrommaterial/)**

Add a custom node with these contents to retrieve a Float3 containing the object’s rotation in radians (recommend making this a material function):

```auto
float3x3 WtoL = LWCToFloat(GetPrimitiveData(Parameters.PrimitiveId).WorldToLocal);
float3 R = float3(
atan2(WtoL[2].y,WtoL[2].z) * -1.0,
asin(WtoL[2].x),
atan2(WtoL[1].x,WtoL[0].x)
);
return R < 0 ? 2 * pi - abs(R) : R;

```

You will need to add pi as an argument to the custom node.  
To convert to degrees, multiply the result by 57.2958.

Note that if you want to use this in the “RotateAboutAxis” material node, Component Mask out the degrees value of, for example, B (yaw), divide by 360 (RotateAboutAxis uses a 0-1 scale for rotation), and input it into RotationAngle. Add the result of the RotationAngle node to the initial vector that you’re rotating in order to acquire the final rotated vector.

---

<div class="post-metadata">

**Author:** ![DotBow](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@DotBow](https://forums.unrealengine.com/u/DotBow)\
**Post date:** [April 5, 2023, 5:21pm UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/7 "2023-04-05T17:21:02Z")

</div>

[mpxc](https://forums.unrealengine.com/u/mpxc) this piece of code helped me a lot, thanks!  
However, the Y value was wrong so I modified code to work properly (UE5.1.1)

```auto
float3x3 WtoL = LWCToFloat(GetPrimitiveData(Parameters.PrimitiveId).WorldToLocal);
float3 R = float3(
atan2(WtoL[2].y,WtoL[2].z) * -1.0,
atan2(WtoL[2].x,WtoL[2].z),
atan2(WtoL[1].x,WtoL[0].x)
);
return R < 0 ? 2 * pi - abs(R) : R;

```

---

<div class="post-metadata">

**Author:** ![neofuturelabs](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neofuturelabs/32/1194562_2.png) [@neofuturelabs](https://forums.unrealengine.com/u/neofuturelabs)\
**Post date:** [April 6, 2023, 5:24am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/8 "2023-04-06T05:24:56Z")

</div>

Oh wow, thanks!!

I compared the two versions and you’re right that the Y value was totally broken in the original code.

Your version is giving much closer results, but it seems like the Y value still isn’t actually perfectly matching the Y value for the object’s rotation, although X and Z is correct, particularly when multiple axes are in a rotated state. I checked this by connecting the material function to a DebugFloat3Values node and comparing the displayed values to the actual values in the Actor Details while rotating the 3 axes of a sphere with the material set to it.

---

<div class="post-metadata">

**Author:** ![powergannon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/powergannon/32/900728_2.png) [@powergannon](https://forums.unrealengine.com/u/powergannon)\
**Post date:** [February 21, 2024, 3:52am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/9 "2024-02-21T03:52:32Z")

</div>

Hello. I know this is an older thread, but was wondering if you happened to find a fix for the y value still not coming out quite right. I’ve been trying to get the roll of an object specifically and have been scratching my head trying to get it right.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/c/b/acbf6e99d448810a04f9e00a867c572fc8c5c1e6.jpeg)  
This is what I’ve tried so far but it’s having issues. It’s not returning the proper values.

---

<div class="post-metadata">

**Author:** ![neofuturelabs](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neofuturelabs/32/1194562_2.png) [@neofuturelabs](https://forums.unrealengine.com/u/neofuturelabs)\
**Post date:** [February 21, 2024, 4:56am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/10 "2024-02-21T04:56:50Z")

</div>

Here is an updated function which accounts for gimbal lock. Can you let me know if this works correctly?

```auto
float3x3 WtoL = LWCToFloat(GetPrimitiveData(Parameters.PrimitiveId).WorldToLocal);
float sy = sqrt(WtoL[0][0] * WtoL[0][0] + WtoL[1][0] * WtoL[1][0]);

bool singular = sy < 1e-6; // If true, gimbal lock is likely

float x, y, z;
if (!singular) {
    x = atan2(WtoL[2][1], WtoL[2][2]);
    y = atan2(-WtoL[2][0], sy);
    z = atan2(WtoL[1][0], WtoL[0][0]);
} else {
    x = atan2(-WtoL[1][2], WtoL[1][1]);
    y = atan2(-WtoL[2][0], sy);
    z = 0;
}

float3 R = float3(x, y, z);
return R < 0 ? 2 * pi - abs(R) : R;

```

---

<div class="post-metadata">

**Author:** ![powergannon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/powergannon/32/900728_2.png) [@powergannon](https://forums.unrealengine.com/u/powergannon)\
**Post date:** [February 22, 2024, 12:05am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/11 "2024-02-22T00:05:52Z")

</div>

Hi, thanks for the reply.  
It’s definitely a lot closer but looks like there’s still something off.

A rotation of 0 = 0.0,  
A rotation of 90 = 0.75  
A rotation of 270 = 0.25  
So I would expect a rotation of 180 to equal 0.5, but it’s actually outputting 0.0 as well.

---

<div class="post-metadata">

**Author:** ![neofuturelabs](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/neofuturelabs/32/1194562_2.png) [@neofuturelabs](https://forums.unrealengine.com/u/neofuturelabs)\
**Post date:** [February 22, 2024, 1:06am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/12 "2024-02-22T01:06:10Z")

</div>

Hmm the results are somewhat different from the values displayed in the editor viewport, but I thought they were at least mathematically equivalent? Anyway, this is pretty much my limit for this kind of stuff, sorry. Maybe someone else can figure out a more comprehensive solution.

---

<div class="post-metadata">

**Author:** ![powergannon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/powergannon/32/900728_2.png) [@powergannon](https://forums.unrealengine.com/u/powergannon)\
**Post date:** [February 22, 2024, 1:50am UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/13 "2024-02-22T01:50:01Z")

</div>

No problem. You helped a lot, thanks!

---

<div class="post-metadata">

**Author:** ![Poncho\_a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Poncho\_a](https://forums.unrealengine.com/u/Poncho_a)\
**Post date:** [October 5, 2024, 9:39pm UTC](https://forums.unrealengine.com/t/get-actor-x-y-z-rotation-angle-in-shader/155362/14 "2024-10-05T21:39:51Z")

</div>

Hi !  
I know it’s already an older post, but since Google took me here, here’s my solution to fix the weird values.  
It’s not perfect, but there is only a couple of specific angles where the Y value is wrong, and it is only a few angles off.

```auto
float3x3 WtoL = LWCToFloat(GetPrimitiveData(Parameters.PrimitiveId).WorldToLocal);
float sy = sqrt(WtoL[0][0] * WtoL[0][0] + WtoL[1][0] * WtoL[1][0]);

bool singular = sy < 1e-6; // If true, gimbal lock is likely

float x, y, z;
if (!singular) {
    x = -atan2(WtoL[2][1], WtoL[2][2]);
    y = -atan2(-WtoL[2][0], sy);
    z = atan2(WtoL[1][0], WtoL[0][0]);
} else {
    x = -atan2(-WtoL[1][2], WtoL[1][1]);
    y = -atan2(-WtoL[2][0], sy);
    z = 0;
}

float3 R = float3(x, y, z);
return R < 0 ? (2 * pi - abs(R)) : R;

```

There is probably a better way to fix this, as I am only barely cognisant when it comes to angular mathematics…
