summaryrefslogtreecommitdiffstats
path: root/Shaders/Motion.cginc
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2023-08-09 18:54:17 -0700
committeryum <yum.food.vr@gmail.com>2023-08-09 18:54:17 -0700
commit3bf013dc3b5479f4fbb458d44801403afe0bb1d2 (patch)
treed91ef918797b2036e1005afa1e9b221d293b696f /Shaders/Motion.cginc
parent1285caf31578d758c2b52b915eedb17cc12a1826 (diff)
Add ray-marched custom chatbox
* Refactor shader code to make development easier. Templates are now as small as possible. * Update scaling code. Use Unity scaling instead of a blendshape. * Check in a fuckton of shader FOSS. Mostly unused. * Update TaSTT.fbx. Now has 6 faces instead of 2.
Diffstat (limited to 'Shaders/Motion.cginc')
-rw-r--r--Shaders/Motion.cginc90
1 files changed, 90 insertions, 0 deletions
diff --git a/Shaders/Motion.cginc b/Shaders/Motion.cginc
new file mode 100644
index 0000000..d6458e9
--- /dev/null
+++ b/Shaders/Motion.cginc
@@ -0,0 +1,90 @@
+#ifndef MOTION_
+#define MOTION_
+
+// xyz represent quaternion vector, w represents theta.
+typedef float4 Quaternion;
+
+// Math from here:
+// https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
+float3 qrot(in float3 v, in Quaternion q)
+{
+ float a = q.w;
+ float b = q.x;
+ float c = q.y;
+ float d = q.z;
+
+ float a2 = a*a;
+ float b2 = b*b;
+ float c2 = c*c;
+ float d2 = d*d;
+
+ float3x3 rot = float3x3(
+ (a2 + b2 - c2) - d2, 2*b*c - 2*a*d, 2*b*d + 2*a*c,
+ 2*b*c + 2*a*d, (a2 - b2) + (c2 - d2), 2*c*d - 2*a*b,
+ 2*b*d - 2*a*c, 2*c*d + 2*a*b, ((a2 - b2) - c2) + d2
+ );
+
+ return mul(rot, v);
+}
+
+Quaternion qinv(in Quaternion q)
+{
+ return Quaternion(q.xyz, -q.w);
+}
+
+// Multiply two quaternions.
+// Math from here: https://www.haroldserrano.com/blog/quaternions-in-computer-graphics
+Quaternion qmul(in Quaternion a, in Quaternion b)
+{
+ return Quaternion(a.w * b.xyz + b.w * a.xyz + cross(a.xyz, b.xyz), a.w * b.w - dot(a.xyz, b.xyz));
+}
+
+float4 affine3(in float3 m)
+{
+ return float4(m, 1.0);
+}
+
+float4x4 affine3x3(in float3x3 m)
+{
+ return float4x4(
+ m[0][0], m[0][1], m[0][2], 0,
+ m[1][0], m[1][1], m[1][2], 0,
+ m[2][0], m[2][1], m[2][2], 0,
+ 0, 0, 0, 1
+ );
+}
+
+float4x4 eye()
+{
+ return float4x4(
+ 1, 0, 0, 0,
+ 0, 1, 0, 0,
+ 0, 0, 1, 0,
+ 0, 0, 0, 1
+ );
+}
+
+// Return affine translation matrix.
+float4x4 translate(in float dx, in float dy, in float dz)
+{
+ return float4x4(
+ 1, 0, 0, dx,
+ 0, 1, 0, dy,
+ 0, 0, 1, dz,
+ 0, 0, 0, 1
+ );
+}
+
+// Return affine scaling matrix.
+float4x4 scale(in float sx, in float sy, in float sz)
+{
+ return float4x4(
+ sx, 0, 0, 0,
+ 0, sy, 0, 0,
+ 0, 0, sz, 0,
+ 0, 0, 0, 1
+ );
+}
+
+#endif // MOTION_
+