summaryrefslogtreecommitdiffstats
path: root/Shaders/grass/motion.cginc
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2023-11-18 18:39:47 -0800
committeryum <yum.food.vr@gmail.com>2023-11-18 18:41:48 -0800
commit03368f2d8660c90b760051363be50552e4b27cc8 (patch)
tree7ab236675f6e29cdc38158780a6b69b251ec7824 /Shaders/grass/motion.cginc
parentad7e60eb3f63c492c1ddcbcf7be8279257015c27 (diff)
Add grass shader
See prefab. Given a 10m square of grass, this repeats it in a 4x4 grid and applies a sway animation to it using a height map.
Diffstat (limited to 'Shaders/grass/motion.cginc')
-rw-r--r--Shaders/grass/motion.cginc90
1 files changed, 90 insertions, 0 deletions
diff --git a/Shaders/grass/motion.cginc b/Shaders/grass/motion.cginc
new file mode 100644
index 0000000..d6458e9
--- /dev/null
+++ b/Shaders/grass/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_
+