Rotating an Instance to its normal in GLSL

Hello,

I want to write a GLSLMat that rotates the instances on their normal.
I’d like to do the same as when, in the Geometry COMP, you pass the normals to the Rotate to Vector parameter, but in GLSL
I pass the position and normals through CHOPs to array uniforms.
I have no problem with the positions but when I use TDRotateToVector matrix it works as long as the N.y has positive values. If it’s negative, it’s like the matrix does the reverse rotation.

Here’s attached a sample showing the problem comparing the instancing on the Geo and on the GLSL material.

What am I missing?

Thanks a lot

Greetings

V.RotateToVector_GLSL.1.toe (13.9 KB)

Hi there,

You’re multiplying the matrix on the right side (newP *= matrix;) This is equal to a transposed matrix multiplication. Try switching the order. So something like:

mat3 m = TDRotateToVector(vec3(rot.x,rot.y,rot.z),vec3(0,0,-1));
newP = m * newP;
newN = m * newN;

Cheers,
tim

1 Like

Thanks man!

It worked. I guess I must review my maths lessons.

Greetings

V.

It’s actually a weird behavior of GLSL. Technically it should be illegal to do V * M in GLSL by the standard rules of matrix multiplication. Since V is a column vector, only M * V should be legal.
GLSL has this shorthand for ease of use though.