Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
ShaderQuat.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2025 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5inline float3 JPH_QuatMulVec3(JPH_Quat inLHS, float3 inRHS)
6{
7 float3 v_xyz = inLHS.xyz;
8 float3 v_yzx = inLHS.yzx;
9 float3 q_cross_p = (inRHS.yzx * v_xyz - v_yzx * inRHS).yzx;
10 float3 q_cross_q_cross_p = (q_cross_p.yzx * v_xyz - v_yzx * q_cross_p).yzx;
11 float3 v = inLHS.w * q_cross_p + q_cross_q_cross_p;
12 return inRHS + (v + v);
13}
14
16{
17 float x = inLHS.w * inRHS.x + inLHS.x * inRHS.w + inLHS.y * inRHS.z - inLHS.z * inRHS.y;
18 float y = inLHS.w * inRHS.y - inLHS.x * inRHS.z + inLHS.y * inRHS.w + inLHS.z * inRHS.x;
19 float z = inLHS.w * inRHS.z + inLHS.x * inRHS.y - inLHS.y * inRHS.x + inLHS.z * inRHS.w;
20 float w = inLHS.w * inRHS.w - inLHS.x * inRHS.x - inLHS.y * inRHS.y - inLHS.z * inRHS.z;
21 return JPH_Quat(x, y, z, w);
22}
23
24inline JPH_Quat JPH_QuatImaginaryMulQuat(float3 inLHS, JPH_Quat inRHS)
25{
26 float x = +inLHS.x * inRHS.w + inLHS.y * inRHS.z - inLHS.z * inRHS.y;
27 float y = -inLHS.x * inRHS.z + inLHS.y * inRHS.w + inLHS.z * inRHS.x;
28 float z = +inLHS.x * inRHS.y - inLHS.y * inRHS.x + inLHS.z * inRHS.w;
29 float w = -inLHS.x * inRHS.x - inLHS.y * inRHS.y - inLHS.z * inRHS.z;
30 return JPH_Quat(x, y, z, w);
31}
32
33inline float3 JPH_QuatRotateAxisZ(JPH_Quat inRotation)
34{
35 return (inRotation.z + inRotation.z) * inRotation.xyz + (inRotation.w + inRotation.w) * float3(inRotation.y, -inRotation.x, inRotation.w) - float3(0, 0, 1);
36}
37
39{
40 return JPH_Quat(-inRotation.x, -inRotation.y, -inRotation.z, inRotation.w);
41}
42
44{
45 const float cOneOverSqrt2 = 0.70710678f;
46 const uint cNumBits = 9;
47 const uint cMask = (1u << cNumBits) - 1;
48 const uint cMaxValue = cMask - 1; // Need odd number of buckets to quantize to or else we can't encode 0
49 const float cScale = 2.0f * cOneOverSqrt2 / float(cMaxValue);
50
51 // Restore two components
52 float3 v3 = float3(float(inValue & cMask), float((inValue >> cNumBits) & cMask), float((inValue >> (2 * cNumBits)) & cMask)) * cScale - float3(cOneOverSqrt2, cOneOverSqrt2, cOneOverSqrt2);
53
54 // Restore the highest component
55 float4 v = float4(v3, sqrt(max(1.0f - dot(v3, v3), 0.0f)));
56
57 // Extract sign
58 if ((inValue & 0x80000000u) != 0)
59 v = -v;
60
61 // Swizzle the components in place
62 uint max_element = (inValue >> 29) & 3;
63 v = max_element == 0? v.wxyz : (max_element == 1? v.xwyz : (max_element == 2? v.xywz : v));
64
65 return v;
66}
67
68inline JPH_Quat JPH_QuatFromMat33(float3 inCol0, float3 inCol1, float3 inCol2)
69{
70 float tr = inCol0.x + inCol1.y + inCol2.z;
71 if (tr >= 0.0f)
72 {
73 float s = sqrt(tr + 1.0f);
74 float is = 0.5f / s;
75 return JPH_Quat(
76 (inCol1.z - inCol2.y) * is,
77 (inCol2.x - inCol0.z) * is,
78 (inCol0.y - inCol1.x) * is,
79 0.5f * s);
80 }
81 else
82 {
83 if (inCol0.x > inCol1.y && inCol0.x > inCol2.z)
84 {
85 float s = sqrt(inCol0.x - (inCol1.y + inCol2.z) + 1);
86 float is = 0.5f / s;
87 return JPH_Quat(
88 0.5f * s,
89 (inCol1.x + inCol0.y) * is,
90 (inCol0.z + inCol2.x) * is,
91 (inCol1.z - inCol2.y) * is);
92 }
93 else if (inCol1.y > inCol2.z)
94 {
95 float s = sqrt(inCol1.y - (inCol2.z + inCol0.x) + 1);
96 float is = 0.5f / s;
97 return JPH_Quat(
98 (inCol1.x + inCol0.y) * is,
99 0.5f * s,
100 (inCol2.y + inCol1.z) * is,
101 (inCol2.x - inCol0.z) * is);
102 }
103 else
104 {
105 float s = sqrt(inCol2.z - (inCol0.x + inCol1.y) + 1);
106 float is = 0.5f / s;
107 return JPH_Quat(
108 (inCol0.z + inCol2.x) * is,
109 (inCol2.y + inCol1.z) * is,
110 0.5f * s,
111 (inCol0.y - inCol1.x) * is);
112 }
113 }
114}
unsigned int uint
Definition Core.h:500
#define yzx
Definition HLSLToCPP.h:516
float4 JPH_Quat
Definition ShaderCore.h:48
float3 JPH_QuatRotateAxisZ(JPH_Quat inRotation)
Definition ShaderQuat.h:33
JPH_Quat JPH_QuatFromMat33(float3 inCol0, float3 inCol1, float3 inCol2)
Definition ShaderQuat.h:68
JPH_Quat JPH_QuatMulQuat(JPH_Quat inLHS, JPH_Quat inRHS)
Definition ShaderQuat.h:15
JPH_Quat JPH_QuatDecompress(uint inValue)
Definition ShaderQuat.h:43
JPH_Quat JPH_QuatImaginaryMulQuat(float3 inLHS, JPH_Quat inRHS)
Definition ShaderQuat.h:24
JPH_Quat JPH_QuatConjugate(JPH_Quat inRotation)
Definition ShaderQuat.h:38
float3 JPH_QuatMulVec3(JPH_Quat inLHS, float3 inRHS)
Definition ShaderQuat.h:5