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 int cHalfMaxValue = int(cMaxValue >> 1);
50 const float cScale = 2.0f * cOneOverSqrt2 / float(cMaxValue);
51
52 // Restore two components
53 float3 v3 = float3(float(int(inValue & cMask) - cHalfMaxValue), float(int((inValue >> cNumBits) & cMask) - cHalfMaxValue), float(int(inValue >> (2 * cNumBits)) & cMask) - cHalfMaxValue) * cScale;
54
55 // Restore the highest component
56 float4 v = float4(v3, sqrt(max(1.0f - dot(v3, v3), 0.0f)));
57
58 // Extract sign
59 if ((inValue & 0x80000000u) != 0)
60 v = -v;
61
62 // Swizzle the components in place
63 uint max_element = (inValue >> 29) & 3;
64 v = max_element == 0? v.wxyz : (max_element == 1? v.xwyz : (max_element == 2? v.xywz : v));
65
66 return v;
67}
68
69inline JPH_Quat JPH_QuatFromMat33(float3 inCol0, float3 inCol1, float3 inCol2)
70{
71 float tr = inCol0.x + inCol1.y + inCol2.z;
72 if (tr >= 0.0f)
73 {
74 float s = sqrt(tr + 1.0f);
75 float is = 0.5f / s;
76 return JPH_Quat(
77 (inCol1.z - inCol2.y) * is,
78 (inCol2.x - inCol0.z) * is,
79 (inCol0.y - inCol1.x) * is,
80 0.5f * s);
81 }
82 else
83 {
84 if (inCol0.x > inCol1.y && inCol0.x > inCol2.z)
85 {
86 float s = sqrt(inCol0.x - (inCol1.y + inCol2.z) + 1);
87 float is = 0.5f / s;
88 return JPH_Quat(
89 0.5f * s,
90 (inCol1.x + inCol0.y) * is,
91 (inCol0.z + inCol2.x) * is,
92 (inCol1.z - inCol2.y) * is);
93 }
94 else if (inCol1.y > inCol2.z)
95 {
96 float s = sqrt(inCol1.y - (inCol2.z + inCol0.x) + 1);
97 float is = 0.5f / s;
98 return JPH_Quat(
99 (inCol1.x + inCol0.y) * is,
100 0.5f * s,
101 (inCol2.y + inCol1.z) * is,
102 (inCol2.x - inCol0.z) * is);
103 }
104 else
105 {
106 float s = sqrt(inCol2.z - (inCol0.x + inCol1.y) + 1);
107 float is = 0.5f / s;
108 return JPH_Quat(
109 (inCol0.z + inCol2.x) * is,
110 (inCol2.y + inCol1.z) * is,
111 0.5f * s,
112 (inCol0.y - inCol1.x) * is);
113 }
114 }
115}
unsigned int uint
Definition Core.h:510
#define yzx
Definition HLSLToCPP.h:516
float4 JPH_Quat
Definition ShaderCore.h:50
float3 JPH_QuatRotateAxisZ(JPH_Quat inRotation)
Definition ShaderQuat.h:33
JPH_Quat JPH_QuatFromMat33(float3 inCol0, float3 inCol1, float3 inCol2)
Definition ShaderQuat.h:69
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