patriciogonzalezvivo / lygia

LYGIA, it's a granular and multi-language (GLSL, HLSL, WGSL, MSL and CUDA) shader library designed for performance and flexibility
https://lygia.xyz
Other
2.61k stars 169 forks source link

Fixed and optimised rotate functions. #191

Closed shadielhajj closed 3 months ago

shadielhajj commented 3 months ago

GLSL rotate functions were incorrectly transposed. I took the opportunity to optimise both the GLSL and HLSL versions. For reference the rotation matrices are (Source: Wikipedia): I think I got it all right, but @patriciogonzalezvivo please feel free to double-check.

2D

$$R(\theta) = \begin{bmatrix} \cos\theta & -\sin\theta \ \sin\theta & \cos\theta \end{bmatrix}$$

3D

$$\begin{bmatrix} 1 & 0 & 0 \ 0 & \cos\theta & -\sin\theta \ 0 & \sin\theta & \cos\theta \end{bmatrix}_x$$

$$\begin{bmatrix} \cos\theta & 0 & \sin\theta \ 0 & 1 & 0 \ -\sin\theta & 0 & \cos\theta \end{bmatrix}_y$$

$$\begin{bmatrix} \cos\theta & -\sin\theta & 0 \ \sin\theta & \cos\theta & 0 \ 0 & 0 & 1 \end{bmatrix}_z$$

Axis-angle

(sorry, too lazy to type this in Latex) image

GLSL Constructors

GLSL matrices constructors are column-major.

mat3(
  vec3 col1,    // first column
  vec3 col2,    // second column
  vec3 col3);   // third column

HLSL Constructors

HLSL matrices constructors are row-major.

float3x3(
  float3 row1,    // first row
  float3 row2,    // second row
  float3 row3);   // third row
patriciogonzalezvivo commented 3 months ago

Thank you!