Effects - apply rotation on vector without scale and translation (in HLSL)?

Might be a silly question but couldn’t find an answer…
Given a matrix and a vector, whats the proper way to apply rotation only on the vector? ie no scale and translation.

Thanks!

Edit: guess I wasn’t clear enough, I meant in HLSL not in c#…)

``````// Apply rotation on Vector
__Rotation.X   = 95;
__Rotatatuin.Y = 45;

// Compute Rotation matrix from a Vector
//
XF.Matrix.CreateFromYawPitchRoll(
XF.MathHelper.ToRadians( __Rotation.Z ), out __MatRotation );

// NOTE: From here you already have matrix rotation from a vector

// No Scale
__MatScale    = Matrix.Identity;

// No Transalation
__MatTranslation = Matrix.Identity;

// Compute world
__EntityWorld = ( __MatScale * __MatRotation ) * __MatTranslation;
``````

I do it like the code above, If I understand your inquiry correctly ^_^y

This may be a (ugly?) solution :

``````Vector3 ApplyRotationOnly(Vector3 v, Matrix matrix)
{
Vector3 scale, translation;
Quaternion rotationQuat;
matrix.Decompose(out scale, out rotationQuat, out translation);
return Vector3.Transform(v, rotationQuat);
}``````

In HLSL you pretty much have to grab the 3x3 out the larger matrix. After you’ve rotated + scaled the normal using that matrix then normalize the normal.

Otherwise you have to normalize your vectors before you build the new 3x3, which is kind of stupid. 3 vs. 1 normalize.

HLSL does have matrix swizzle operators for extracting vectors. They are arcane as hell though. `myMatrix._01_02_03` etc.

1 Like

Thanks but wouldn’t this apply translation too? Also if I normalize the vector instead of the matrix it wouldnt give right results if scale is not uniformed (for example 5,1,1)

I guess it’s not the most efficient solution but what should work is that you save the length of the vector before multiplying with the float3x3 casted matrix. After the multiplication you would then normalize the vector and multiply it again with the saved length…

Oh just overlooked the last answer which suggests the same.

I think with non uniform scale this will not work though…