Skip to content

Instantly share code, notes, and snippets.

@lucasvanmol
Created November 13, 2020 16:40
Show Gist options
  • Select an option

  • Save lucasvanmol/ccf79ef37c125d61abc288dbd216dfe2 to your computer and use it in GitHub Desktop.

Select an option

Save lucasvanmol/ccf79ef37c125d61abc288dbd216dfe2 to your computer and use it in GitHub Desktop.
bend3D shader but using quaternions instead of matrices
shader_type spatial;
uniform vec3 strength = vec3(0.0);
uniform vec3 center = vec3(0.0, 0.0, 1.0);
vec4 quat_mult(vec4 q1, vec4 q2) {
vec4 qr;
qr.x = (q1.w * q2.x) + (q1.x * q2.w) + (q1.y * q2.z) - (q1.z * q2.y);
qr.y = (q1.w * q2.y) - (q1.x * q2.z) + (q1.y * q2.w) + (q1.z * q2.x);
qr.z = (q1.w * q2.z) + (q1.x * q2.y) - (q1.y * q2.x) + (q1.z * q2.w);
qr.w = (q1.w * q2.w) - (q1.x * q2.x) - (q1.y * q2.y) - (q1.z * q2.z);
return qr;
}
vec3 rotate_vertex(vec3 v, vec4 q) {
return v + 2.0 * cross(q.xyz, cross(q.xyz, v) + q.w * v);
}
void vertex() {
vec3 v = VERTEX - center;
float delta = v.y * 0.1;
vec3 angles = strength * delta;
vec4 qx = vec4(sin(angles.x), 0.0, 0.0, cos(angles.x));
vec4 qy = vec4(0.0, sin(angles.y), 0.0, cos(angles.y));
vec4 qz = vec4(0.0, 0.0, sin(angles.z), cos(angles.z));
vec4 qt = quat_mult(qz, qx);
qt = quat_mult(qt, qy);
VERTEX = rotate_vertex(v, qt) + center;
NORMAL = rotate_vertex(NORMAL, qt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment