Skip to content

Instantly share code, notes, and snippets.

@akella
Created November 28, 2024 21:01
Show Gist options
  • Save akella/39bc6a8e62f120e4959f27107220b97b to your computer and use it in GitHub Desktop.
Save akella/39bc6a8e62f120e4959f27107220b97b to your computer and use it in GitHub Desktop.
tsl rotation
const rotate2D = Fn(({pos,angle})=>{
const s = sin(angle);
const c = cos(angle);
const m = mat2(c, s, s.mul(-1), c);
return m.mul(pos);
})
const rotate3D = Fn(({pos,axis,angle})=>{
const axisNorm = normalize(axis);
const s = sin(angle);
const c = cos(angle);
const oc = float(1).sub(c);
const matrix4 = mat4(
oc.mul(axisNorm.x).mul(axisNorm.x).add(c), oc.mul(axisNorm.x).mul(axisNorm.y).sub(axisNorm.z.mul(s)), oc.mul(axisNorm.z).mul(axisNorm.x).add(axisNorm.y.mul(s)), 0.0,
oc.mul(axisNorm.x).mul(axisNorm.y).add(axisNorm.z.mul(s)), oc.mul(axisNorm.y).mul(axisNorm.y).add(c), oc.mul(axisNorm.y).mul(axisNorm.z).sub(axisNorm.x.mul(s)), 0.0,
oc.mul(axisNorm.z).mul(axisNorm.x).sub(axisNorm.y.mul(s)), oc.mul(axisNorm.y).mul(axisNorm.z).add(axisNorm.x.mul(s)), oc.mul(axisNorm.z).mul(axisNorm.z).add(c), 0.0,
0.0, 0.0, 0.0, 1.0
)
return matrix4.mul(pos).xyz;
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment