yum-archive/SoggyShaders

Old Unity shaders.

git clone https://git.yummers.dev/yum-archive/SoggyShaders

yumAdd avatar cloning shader273a3ab

master
1.7 KiB90 linesraw
1#ifndef MOTION_
2#define MOTION_
3
4// xyz represent quaternion vector, w represents theta.
5typedef float4 Quaternion;
6
7// Math from here:
8//   https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
9float3 qrot(in float3 v, in Quaternion q)
10{
11  float a = q.w;
12  float b = q.x;
13  float c = q.y;
14  float d = q.z;
15
16  float a2 = a*a;
17  float b2 = b*b;
18  float c2 = c*c;
19  float d2 = d*d;
20
21  float3x3 rot = float3x3(
22    (a2 + b2 - c2) - d2, 2*b*c - 2*a*d, 2*b*d + 2*a*c,
23    2*b*c + 2*a*d, (a2 - b2) + (c2 - d2), 2*c*d - 2*a*b,
24    2*b*d - 2*a*c, 2*c*d + 2*a*b, ((a2 - b2) - c2) + d2
25  );
26
27  return mul(rot, v);
28}
29
30Quaternion qinv(in Quaternion q)
31{
32  return Quaternion(q.xyz, -q.w);
33}
34
35// Multiply two quaternions.
36// Math from here: https://www.haroldserrano.com/blog/quaternions-in-computer-graphics
37Quaternion qmul(in Quaternion a, in Quaternion b)
38{
39	return Quaternion(a.w * b.xyz + b.w * a.xyz + cross(a.xyz, b.xyz), a.w * b.w - dot(a.xyz, b.xyz));
40}
41
42float4 affine3(in float3 m)
43{
44  return float4(m, 1.0);
45}
46
47float4x4 affine3x3(in float3x3 m)
48{
49  return float4x4(
50    m[0][0], m[0][1], m[0][2], 0,
51    m[1][0], m[1][1], m[1][2], 0,
52    m[2][0], m[2][1], m[2][2], 0,
53    0,       0,       0,       1
54  );
55}
56
57float4x4 eye()
58{
59  return float4x4(
60    1, 0, 0, 0,
61    0, 1, 0, 0,
62    0, 0, 1, 0,
63    0, 0, 0, 1
64  );
65}
66
67// Return affine translation matrix.
68float4x4 translate(in float dx, in float dy, in float dz)
69{
70  return float4x4(
71    1, 0, 0, dx,
72    0, 1, 0, dy,
73    0, 0, 1, dz,
74    0, 0, 0, 1
75  );
76}
77
78// Return affine scaling matrix.
79float4x4 scale(in float sx, in float sy, in float sz)
80{
81  return float4x4(
82    sx, 0,  0,  0,
83    0,  sy, 0,  0,
84    0,  0,  sz, 0,
85    0,  0,  0,  1
86  );
87}
88
89#endif // MOTION_
90