-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.hlsl
324 lines (277 loc) · 7.84 KB
/
point.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
struct Point
{
float3 position;
float w;
float4 rotation;
};
#define QUATERNION_IDENTITY float4(0, 0, 0, 1)
#ifndef PI
#define PI 3.14159265359f
#endif
#ifndef mod
#define mod(x, y) ((x) - (y) * floor((x) / (y)))
#endif
float4 qmul(float4 q1, float4 q2)
{
return float4(
q2.xyz * q1.w + q1.xyz * q2.w + cross(q1.xyz, q2.xyz),
q1.w * q2.w - dot(q1.xyz, q2.xyz)
);
}
// Vector rotation with a quaternion
// http://mathworld.wolfram.com/Quaternion.html
float3 rotate_vector(float3 v, float4 quat)
{
float4 r_c = quat * float4(-1, -1, -1, 1);
return qmul(quat, qmul(float4(v, 0), r_c)).xyz;
}
// https://blog.molecular-matters.com/2013/05/24/a-faster-quaternion-vector-multiplication/
float3 rotate_vector2(float3 v, float4 q)
{
float3 t = 2 * cross(q.xyz, v);
return v + q.w * t + cross(q.xyz, t);
}
// float3 rotate_vector( float4 quat, float3 vec )
// {
// return vec + 2.0 * cross( cross( vec, quat.xyz ) + quat.w * vec, quat.xyz );
// }
float4 q_conj(float4 q)
{
return float4(-q.x, -q.y, -q.z, q.w);
}
// https://jp.mathworks.com/help/aeroblks/quaternioninverse.html
float4 q_inverse(float4 q)
{
float4 conj = q_conj(q);
return conj / (q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w);
}
// A given angle of rotation about a given axis
float4 rotate_angle_axis(float angle, float3 axis)
{
float sn = sin(angle * 0.5);
float cs = cos(angle * 0.5);
return float4(axis * sn, cs);
}
// https://stackoverflow.com/questions/1171849/finding-quaternion-representing-the-rotation-from-one-vector-to-another
float4 from_to_rotation(float3 v1, float3 v2)
{
float4 q;
float d = dot(v1, v2);
if (d < -0.999999)
{
float3 right = float3(1, 0, 0);
float3 up = float3(0, 1, 0);
float3 tmp = cross(right, v1);
if (length(tmp) < 0.000001)
{
tmp = cross(up, v1);
}
tmp = normalize(tmp);
q = rotate_angle_axis(PI, tmp);
} else if (d > 0.999999) {
q = QUATERNION_IDENTITY;
} else {
q.xyz = cross(v1, v2);
q.w = 1 + d;
q = normalize(q);
}
return q;
}
float4 q_look_at(float3 forward, float3 up)
{
float3 right = normalize(cross(forward, up));
up = normalize(cross(forward, right));
float m00 = right.x;
float m01 = right.y;
float m02 = right.z;
float m10 = up.x;
float m11 = up.y;
float m12 = up.z;
float m20 = forward.x;
float m21 = forward.y;
float m22 = forward.z;
float num8 = (m00 + m11) + m22;
float4 q = QUATERNION_IDENTITY;
if (num8 > 0.0)
{
float num = sqrt(num8 + 1.0);
q.w = num * 0.5;
num = 0.5 / num;
q.x = (m12 - m21) * num;
q.y = (m20 - m02) * num;
q.z = (m01 - m10) * num;
return q;
}
if ((m00 >= m11) && (m00 >= m22))
{
float num7 = sqrt(((1.0 + m00) - m11) - m22);
float num4 = 0.5 / num7;
q.x = 0.5 * num7;
q.y = (m01 + m10) * num4;
q.z = (m02 + m20) * num4;
q.w = (m12 - m21) * num4;
return q;
}
if (m11 > m22)
{
float num6 = sqrt(((1.0 + m11) - m00) - m22);
float num3 = 0.5 / num6;
q.x = (m10 + m01) * num3;
q.y = 0.5 * num6;
q.z = (m21 + m12) * num3;
q.w = (m20 - m02) * num3;
return q;
}
float num5 = sqrt(((1.0 + m22) - m00) - m11);
float num2 = 0.5 / num5;
q.x = (m20 + m02) * num2;
q.y = (m21 + m12) * num2;
q.z = 0.5 * num5;
q.w = (m01 - m10) * num2;
return q;
}
float4 q_slerp(in float4 a, in float4 b, float t)
{
// if either input is zero, return the other.
if (length(a) == 0.0)
{
if (length(b) == 0.0)
{
return QUATERNION_IDENTITY;
}
return b;
}
else if (length(b) == 0.0)
{
return a;
}
float cosHalfAngle = a.w * b.w + dot(a.xyz, b.xyz);
if (cosHalfAngle >= 1.0 || cosHalfAngle <= -1.0)
{
return a;
}
else if (cosHalfAngle < 0.0)
{
b.xyz = -b.xyz;
b.w = -b.w;
cosHalfAngle = -cosHalfAngle;
}
float blendA;
float blendB;
if (cosHalfAngle < 0.99)
{
// do proper slerp for big angles
float halfAngle = acos(cosHalfAngle);
float sinHalfAngle = sin(halfAngle);
float oneOverSinHalfAngle = 1.0 / sinHalfAngle;
blendA = sin(halfAngle * (1.0 - t)) * oneOverSinHalfAngle;
blendB = sin(halfAngle * t) * oneOverSinHalfAngle;
}
else
{
// do lerp if angle is really small.
blendA = 1.0 - t;
blendB = t;
}
float4 result = float4(blendA * a.xyz + blendB * b.xyz, blendA * a.w + blendB * b.w);
if (length(result) > 0.0)
{
return normalize(result);
}
return QUATERNION_IDENTITY;
}
float4 euler_to_quaternion(float yaw, float pitch, float roll)
{
return float4(
sin(roll/2) * cos(pitch/2) * cos(yaw/2) - cos(roll/2) * sin(pitch/2) * sin(yaw/2),
cos(roll/2) * sin(pitch/2) * cos(yaw/2) + sin(roll/2) * cos(pitch/2) * sin(yaw/2),
cos(roll/2) * cos(pitch/2) * sin(yaw/2) - sin(roll/2) * sin(pitch/2) * cos(yaw/2),
cos(roll/2) * cos(pitch/2) * cos(yaw/2) + sin(roll/2) * sin(pitch/2) * sin(yaw/2));
}
float4x4 quaternion_to_matrix(float4 quat)
{
float4x4 m = 0; //float4x4(float4(0, 0, 0, 0), float4(0, 0, 0, 0), float4(0, 0, 0, 0), float4(0, 0, 0, 0));
float x = quat.x, y = quat.y, z = quat.z, w = quat.w;
float x2 = x + x, y2 = y + y, z2 = z + z;
float xx = x * x2, xy = x * y2, xz = x * z2;
float yy = y * y2, yz = y * z2, zz = z * z2;
float wx = w * x2, wy = w * y2, wz = w * z2;
m[0][0] = 1.0 - (yy + zz);
m[0][1] = xy - wz;
m[0][2] = xz + wy;
m[1][0] = xy + wz;
m[1][1] = 1.0 - (xx + zz);
m[1][2] = yz - wx;
m[2][0] = xz - wy;
m[2][1] = yz + wx;
m[2][2] = 1.0 - (xx + yy);
m[3][3] = 1.0;
return m;
}
// Transposed matrix
float4x4 quaternion_to_tmatrix(float4 quat)
{
float4x4 m = 0;
float x = quat.x, y = quat.y, z = quat.z, w = quat.w;
float x2 = x + x, y2 = y + y, z2 = z + z;
float xx = x * x2, xy = x * y2, xz = x * z2;
float yy = y * y2, yz = y * z2, zz = z * z2;
float wx = w * x2, wy = w * y2, wz = w * z2;
m[0][0] = 1.0 - (yy + zz);
m[1][0] = xy - wz;
m[2][0] = xz + wy;
m[0][1] = xy + wz;
m[1][1] = 1.0 - (xx + zz);
m[2][1] = yz - wx;
m[0][2] = xz - wy;
m[1][2] = yz + wx;
m[2][2] = 1.0 - (xx + yy);
m[3][3] = 1.0;
return m;
}
float4 q_from_matrix (float3x3 m)
{
float w = sqrt( 1.0 + m._m00 + m._m11 + m._m22) / 2.0;
float w4 = (4.0 * w);
float x = (m._m21 - m._m12) / w4 ;
float y = (m._m02 - m._m20) / w4 ;
float z = (m._m10 - m._m01) / w4 ;
return float4(x,y,z,w);
}
float4 quaternion_from_matrix_precise (float3x3 m)
{
float tr = m._m00 + m._m11 + m._m22;
if (tr > 0) {
float S = sqrt(tr+1.0) * 2; // S=4*qw
return float4(
(m._m21 - m._m12) / S,
(m._m02 - m._m20) / S,
(m._m10 - m._m01) / S,
0.25 * S
);
} else if ((m._m00 > m._m11)&(m._m00 > m._m22)) {
float S = sqrt(1.0 + m._m00 - m._m11 - m._m22) * 2; // S=4*qx
return float4(
0.25 * S,
(m._m01 + m._m10) / S ,
(m._m02 + m._m20) / S ,
(m._m21 - m._m12) / S
);
} else if (m._m11 > m._m22) {
float S = sqrt(1.0 + m._m11 - m._m00 - m._m22) * 2; // S=4*qy
return float4(
(m._m01 + m._m10) / S,
0.25 * S,
(m._m12 + m._m21) / S,
(m._m02 - m._m20) / S
);
} else {
float S = sqrt(1.0 + m._m22 - m._m00 - m._m11) * 2; // S=4*qz
return float4(
(m._m02 + m._m20) / S,
(m._m12 + m._m21) / S,
0.25 * S,
(m._m10 - m._m01) / S
);
}
}