@pinkaphant/ecctrl 0.0.0-stage → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +21 -0
  3. package/NOTICE +10 -0
  4. package/dist/Color-V9u0UyNg.cjs +81 -0
  5. package/dist/Color-V9u0UyNg.cjs.map +1 -0
  6. package/dist/Color-rCfz0rNn.js +28 -0
  7. package/dist/Color-rCfz0rNn.js.map +1 -0
  8. package/dist/CurveLUT-BfMhx3LX.js +88 -0
  9. package/dist/CurveLUT-BfMhx3LX.js.map +1 -0
  10. package/dist/CurveLUT-Ck18ZlkK.cjs +99 -0
  11. package/dist/CurveLUT-Ck18ZlkK.cjs.map +1 -0
  12. package/dist/EcctrlAnimationStateController-41Cm1Tt1.cjs +111 -0
  13. package/dist/EcctrlAnimationStateController-41Cm1Tt1.cjs.map +1 -0
  14. package/dist/EcctrlAnimationStateController-Bb2fARWF.js +94 -0
  15. package/dist/EcctrlAnimationStateController-Bb2fARWF.js.map +1 -0
  16. package/dist/Math-BkrN2TZK.js +49 -0
  17. package/dist/Math-BkrN2TZK.js.map +1 -0
  18. package/dist/Math-ByyUpUHB.cjs +68 -0
  19. package/dist/Math-ByyUpUHB.cjs.map +1 -0
  20. package/dist/all.cjs +52 -0
  21. package/dist/all.cjs.map +1 -0
  22. package/dist/all.d.ts +16 -0
  23. package/dist/all.js +32 -0
  24. package/dist/all.js.map +1 -0
  25. package/dist/animation.cjs +23 -0
  26. package/dist/animation.cjs.map +1 -0
  27. package/dist/animation.d.ts +8 -0
  28. package/dist/animation.js +20 -0
  29. package/dist/animation.js.map +1 -0
  30. package/dist/camera-B_UgIm14.js +71 -0
  31. package/dist/camera-B_UgIm14.js.map +1 -0
  32. package/dist/camera-CzMKt9dI.cjs +79 -0
  33. package/dist/camera-CzMKt9dI.cjs.map +1 -0
  34. package/dist/camera.cjs +10 -0
  35. package/dist/camera.d.ts +9 -0
  36. package/dist/camera.js +9 -0
  37. package/dist/character/Ecctrl.d.ts +105 -0
  38. package/dist/character/animation/EcctrlAnimationStateController.d.ts +17 -0
  39. package/dist/character/animation/index.d.ts +13 -0
  40. package/dist/character/animation/resolveAnimationState.d.ts +9 -0
  41. package/dist/character/animation/types.d.ts +19 -0
  42. package/dist/character/animation/useEcctrlAnimationStore.d.ts +21 -0
  43. package/dist/character/types.d.ts +27 -0
  44. package/dist/chunk-D6vf50IK.cjs +35 -0
  45. package/dist/curves/CurveLUT.d.ts +27 -0
  46. package/dist/curves.cjs +22 -0
  47. package/dist/curves.cjs.map +1 -0
  48. package/dist/curves.d.ts +9 -0
  49. package/dist/curves.js +20 -0
  50. package/dist/curves.js.map +1 -0
  51. package/dist/gravity/EcctrlCameraControls.d.ts +28 -0
  52. package/dist/gravity/useCustomGravity.d.ts +23 -0
  53. package/dist/gravity.cjs +21 -0
  54. package/dist/gravity.cjs.map +1 -0
  55. package/dist/gravity.d.ts +9 -0
  56. package/dist/gravity.js +20 -0
  57. package/dist/gravity.js.map +1 -0
  58. package/dist/index.cjs +14 -0
  59. package/dist/index.d.ts +12 -0
  60. package/dist/index.js +10 -0
  61. package/dist/input/Joystick.d.ts +18 -0
  62. package/dist/input/VirtualButton.d.ts +17 -0
  63. package/dist/input/stores/useButtonStore.d.ts +21 -0
  64. package/dist/input/stores/useJoystickStore.d.ts +26 -0
  65. package/dist/input-CrNBMRxm.js +315 -0
  66. package/dist/input-CrNBMRxm.js.map +1 -0
  67. package/dist/input-DBhf3WZe.cjs +340 -0
  68. package/dist/input-DBhf3WZe.cjs.map +1 -0
  69. package/dist/input.cjs +13 -0
  70. package/dist/input.d.ts +15 -0
  71. package/dist/input.js +9 -0
  72. package/dist/leva--ZV58vPr.cjs +771 -0
  73. package/dist/leva--ZV58vPr.cjs.map +1 -0
  74. package/dist/leva-BjLuypTs.js +764 -0
  75. package/dist/leva-BjLuypTs.js.map +1 -0
  76. package/dist/leva.cjs +10 -0
  77. package/dist/leva.d.ts +9 -0
  78. package/dist/leva.js +9 -0
  79. package/dist/plugins/leva/CurveEditorPlugin.d.ts +72 -0
  80. package/dist/shared/Math.d.ts +11 -0
  81. package/dist/shared/constants/Color.d.ts +16 -0
  82. package/dist/shared/types.d.ts +16 -0
  83. package/dist/src-BTTVvwrl.cjs +1172 -0
  84. package/dist/src-BTTVvwrl.cjs.map +1 -0
  85. package/dist/src-XJOuoUi3.js +1164 -0
  86. package/dist/src-XJOuoUi3.js.map +1 -0
  87. package/dist/time/TimeControl.d.ts +16 -0
  88. package/dist/time-B4iUfe91.cjs +65 -0
  89. package/dist/time-B4iUfe91.cjs.map +1 -0
  90. package/dist/time-DTVzhzJ8.js +60 -0
  91. package/dist/time-DTVzhzJ8.js.map +1 -0
  92. package/dist/time.cjs +10 -0
  93. package/dist/time.d.ts +9 -0
  94. package/dist/time.js +9 -0
  95. package/dist/useCustomGravity-B35U0d4V.cjs +45 -0
  96. package/dist/useCustomGravity-B35U0d4V.cjs.map +1 -0
  97. package/dist/useCustomGravity-elzlTR6Y.js +38 -0
  98. package/dist/useCustomGravity-elzlTR6Y.js.map +1 -0
  99. package/dist/utils.cjs +23 -0
  100. package/dist/utils.cjs.map +1 -0
  101. package/dist/utils.d.ts +8 -0
  102. package/dist/utils.js +20 -0
  103. package/dist/utils.js.map +1 -0
  104. package/dist/vehicle-AEqO39Wm.js +2525 -0
  105. package/dist/vehicle-AEqO39Wm.js.map +1 -0
  106. package/dist/vehicle-DbaRBeDD.cjs +2545 -0
  107. package/dist/vehicle-DbaRBeDD.cjs.map +1 -0
  108. package/dist/vehicle.cjs +12 -0
  109. package/dist/vehicle.d.ts +14 -0
  110. package/dist/vehicle.js +9 -0
  111. package/dist/vehicles/EcctrlVehicle.d.ts +111 -0
  112. package/dist/vehicles/components/ShapeCastWheel.d.ts +114 -0
  113. package/dist/vehicles/components/ThrustPropeller.d.ts +51 -0
  114. package/dist/vehicles/types.d.ts +39 -0
  115. package/package.json +149 -4
  116. package/readme.md +739 -0
  117. package/README.md +0 -3
@@ -0,0 +1,1172 @@
1
+ /*!
2
+ * Ecctrl
3
+ * https://github.com/pmndrs/ecctrl
4
+ *
5
+ * SPDX-FileCopyrightText: 2023-2026 Erdong Chen
6
+ * SPDX-License-Identifier: MIT
7
+ */
8
+ const require_chunk = require("./chunk-D6vf50IK.cjs");
9
+ const require_useCustomGravity = require("./useCustomGravity-B35U0d4V.cjs");
10
+ const require_CurveLUT = require("./CurveLUT-Ck18ZlkK.cjs");
11
+ const require_Math = require("./Math-ByyUpUHB.cjs");
12
+ const require_Color = require("./Color-V9u0UyNg.cjs");
13
+ let three = require("three");
14
+ three = require_chunk.__toESM(three, 1);
15
+ let _react_three_fiber = require("@react-three/fiber");
16
+ let _react_three_rapier = require("@react-three/rapier");
17
+ let react = require("react");
18
+ react = require_chunk.__toESM(react, 1);
19
+ let _dimforge_rapier3d_compat = require("@dimforge/rapier3d-compat");
20
+ let three_src_math_MathUtils_js = require("three/src/math/MathUtils.js");
21
+ //#region src/character/Ecctrl.tsx
22
+ /*!
23
+ * Ecctrl
24
+ * https://github.com/pmndrs/ecctrl
25
+ *
26
+ * SPDX-FileCopyrightText: 2023-2026 Erdong Chen
27
+ * SPDX-License-Identifier: MIT
28
+ */
29
+ var DEFAULT_CURVE_DATA = { points: [
30
+ {
31
+ x: 0,
32
+ y: 0,
33
+ r_out: 0
34
+ },
35
+ {
36
+ x: .5,
37
+ y: 0,
38
+ r_in: 0,
39
+ r_out: 0
40
+ },
41
+ {
42
+ x: 1,
43
+ y: 1,
44
+ r_in: 0
45
+ }
46
+ ] };
47
+ var Ecctrl = /* @__PURE__ */ (0, react.forwardRef)(({ children, debug = false, enable = true, capsuleHalfHeight = .3, capsuleRadius = .3, lockForward = false, useCustomForward = false, useCharacterUpAxis = false, enableCustomGravity = false, gravityDirLerpSpeed = 6, maxWalkVel = 2, maxRunVel = 5, accDeltaTime = .2, decDeltaTime = .2, rejectVelFactor = 1, moveImpulsePointOffset = .5, jumpVel = 5, jumpDuration = .1, slopeJumpFactor = 0, airDragFactor = .1, slideGripFactor = .5, fallingGravityScale = 3, fallingMaxVel = 20, enableToggleRun = true, groundDetection = "shapeCast", slopeMaxAngle = Math.PI / 2.5, floatHeight = .2, rayOriginOffest = -capsuleHalfHeight, rayHitForgiveness = .28, rayLength = capsuleRadius + 1, rayRadius = capsuleRadius / 2, springK = 80, dampingC = 6, autoBalance = true, autoBalanceSpringK = .5, autoBalanceDampingC = .03, autoBalanceSpringOnY = .08, autoBalanceDampingOnY = .006, followPlatform = true, massRatioFallOffCurveData = DEFAULT_CURVE_DATA, applyCounterMass = true, applyCounterJumpImp = true, counterJumpImpFactor = 1, applyCounterMoveImp = true, counterMoveImpFactor = 1, ...props }, ref) => {
48
+ var _props$gravityScale, _props$friction;
49
+ /**
50
+ * Rapier preset
51
+ */
52
+ const { rapier, world } = (0, _react_three_rapier.useRapier)();
53
+ /**
54
+ * Threejs preset
55
+ */
56
+ const { camera } = (0, _react_three_fiber.useThree)();
57
+ /**
58
+ * User controls setup
59
+ */
60
+ const movementState = (0, react.useRef)({
61
+ forward: false,
62
+ backward: false,
63
+ leftward: false,
64
+ rightward: false,
65
+ joystick: {
66
+ x: 0,
67
+ y: 0
68
+ },
69
+ run: false,
70
+ jump: false
71
+ });
72
+ const jumpElapsedTime = (0, react.useRef)(0);
73
+ const jumpActive = (0, react.useRef)(false);
74
+ const canJumpAgain = (0, react.useRef)(true);
75
+ const runActive = (0, react.useRef)(false);
76
+ const canRunAgain = (0, react.useRef)(false);
77
+ (0, react.useMemo)(() => new three.Vector3(0, 0, 0), []);
78
+ const fixedOrigin = (0, react.useMemo)(() => new three.Vector3(0, 0, 0), []);
79
+ (0, react.useMemo)(() => new three.Vector3(1, 0, 0), []);
80
+ (0, react.useMemo)(() => new three.Vector3(0, 1, 0), []);
81
+ const fixedZAxis = (0, react.useMemo)(() => new three.Vector3(0, 0, 1), []);
82
+ const characterRef = (0, react.useRef)(null);
83
+ const characterYAxis = (0, react.useRef)(new three.Vector3(0, 1, 0));
84
+ const characterXAxis = (0, react.useRef)(new three.Vector3(1, 0, 0));
85
+ const characterZAxis = (0, react.useRef)(new three.Vector3(0, 0, 1));
86
+ const characterColliderRef = (0, react.useRef)(null);
87
+ const setMovement = (0, react.useCallback)((movement) => {
88
+ if (movement.forward !== void 0) movementState.current.forward = movement.forward;
89
+ if (movement.backward !== void 0) movementState.current.backward = movement.backward;
90
+ if (movement.leftward !== void 0) movementState.current.leftward = movement.leftward;
91
+ if (movement.rightward !== void 0) movementState.current.rightward = movement.rightward;
92
+ if (movement.joystick) {
93
+ movementState.current.joystick.x = movement.joystick.x;
94
+ movementState.current.joystick.y = movement.joystick.y;
95
+ }
96
+ if (movement.run !== void 0) movementState.current.run = movement.run;
97
+ if (movement.jump !== void 0) movementState.current.jump = movement.jump;
98
+ }, []);
99
+ const setLockForward = (0, react.useCallback)((lock) => isLockForward.current = lock, []);
100
+ const setForwardDir = (0, react.useCallback)((dir) => forwardDirection.current.copy(dir), []);
101
+ (0, react.useImperativeHandle)(ref, () => ({
102
+ get body() {
103
+ return characterRef.current;
104
+ },
105
+ get collider() {
106
+ return characterColliderRef.current;
107
+ },
108
+ get upAxis() {
109
+ return upAxis.current;
110
+ },
111
+ get gravityDir() {
112
+ return gravityDir.current;
113
+ },
114
+ get gravityMag() {
115
+ return referenceGravityMag.current;
116
+ },
117
+ get currPos() {
118
+ return currentPos.current;
119
+ },
120
+ get currQuat() {
121
+ return currentQuat.current;
122
+ },
123
+ get currLinVel() {
124
+ return currentVel.current;
125
+ },
126
+ get currAngVel() {
127
+ return currentAngVel.current;
128
+ },
129
+ get input() {
130
+ return movementState.current;
131
+ },
132
+ get inputDir() {
133
+ return inputDir.current;
134
+ },
135
+ get movingDirection() {
136
+ return movingDirection.current;
137
+ },
138
+ get relativeVel() {
139
+ return relativeVel.current;
140
+ },
141
+ get relativeVelOnPlane() {
142
+ return relativeVelOnPlane.current;
143
+ },
144
+ get relativeVelOnUp() {
145
+ return relativeVelOnUp.current;
146
+ },
147
+ get moveImpulse() {
148
+ return moveImpulse.current;
149
+ },
150
+ get floatingImpulse() {
151
+ return floatingImpulse.current;
152
+ },
153
+ get dragFrictionImpulse() {
154
+ return dragFrictionImpulse.current;
155
+ },
156
+ get bodyXAxis() {
157
+ return characterXAxis.current;
158
+ },
159
+ get bodyYAxis() {
160
+ return characterYAxis.current;
161
+ },
162
+ get bodyZAxis() {
163
+ return characterZAxis.current;
164
+ },
165
+ get standCollider() {
166
+ return rayHitBody.current;
167
+ },
168
+ get standPoint() {
169
+ return standingPoint.current;
170
+ },
171
+ get standNormal() {
172
+ return actualSlopeNormalVec.current;
173
+ },
174
+ get isOnGround() {
175
+ return isOnGround.current;
176
+ },
177
+ get isFalling() {
178
+ return isFalling.current;
179
+ },
180
+ get isOnPlatform() {
181
+ return isOnMovingObject.current;
182
+ },
183
+ get slopeAngle() {
184
+ return slopeAngleInFront.current;
185
+ },
186
+ get actualSlopeAngle() {
187
+ return actualSlopeAngle.current;
188
+ },
189
+ get standFriction() {
190
+ return standingPointFriction.current;
191
+ },
192
+ get slideFriction() {
193
+ return slideFrictionCoef.current;
194
+ },
195
+ get isMoving() {
196
+ return inputDir.current.lengthSq() > 1e-6;
197
+ },
198
+ get moveSpeed() {
199
+ return relativeVelOnPlane.current.length();
200
+ },
201
+ get verticalSpeed() {
202
+ return relativeVelOnUp.current.dot(referenceUpAxis);
203
+ },
204
+ get runActive() {
205
+ return runActive.current;
206
+ },
207
+ get jumpActive() {
208
+ return jumpActive.current;
209
+ },
210
+ get lockForward() {
211
+ return isLockForward.current;
212
+ },
213
+ get turnOnYQuat() {
214
+ return turnOnYQuat.current;
215
+ },
216
+ setMovement,
217
+ setLockForward,
218
+ setForwardDir
219
+ }), []);
220
+ /**
221
+ * Gravity controls preset
222
+ */
223
+ const gravityField = require_useCustomGravity.useCustomGravity((state) => state.gravityField);
224
+ const applyGravityField = require_useCustomGravity.useCustomGravity((state) => state.applyGravityField);
225
+ const isZeroGravity = (0, react.useRef)(false);
226
+ const upAxis = (0, react.useRef)(new three.Vector3());
227
+ const referenceUpAxis = (0, react.useMemo)(() => useCharacterUpAxis ? characterYAxis.current : upAxis.current, [useCharacterUpAxis]);
228
+ const referenceGravity = (0, react.useRef)(new three.Vector3());
229
+ const referenceGravityMag = (0, react.useRef)(0);
230
+ const referenceGravityDir = (0, react.useRef)(new three.Vector3());
231
+ const gravityDir = (0, react.useRef)(new three.Vector3());
232
+ const slerpRef = (0, react.useRef)(require_Math.createSlerpVec3());
233
+ /**
234
+ * Controls preset
235
+ */
236
+ const relativeVel = (0, react.useRef)(new three.Vector3());
237
+ const relativeVelOnPlane = (0, react.useRef)(new three.Vector3());
238
+ const relativeVelOnUp = (0, react.useRef)(new three.Vector3());
239
+ const currentPos = (0, react.useRef)(new three.Vector3());
240
+ const currentVel = (0, react.useRef)(new three.Vector3());
241
+ const currentVelOnPlane = (0, react.useRef)(new three.Vector3());
242
+ const currentVelOnUp = (0, react.useRef)(new three.Vector3());
243
+ const currentAngVel = (0, react.useRef)(new three.Vector3());
244
+ const currentAngVelOnPlane = (0, react.useRef)(new three.Vector3());
245
+ const currentAngVelOnUp = (0, react.useRef)(new three.Vector3());
246
+ const currentQuat = (0, react.useRef)(new three.Quaternion());
247
+ const balanceCrossAxis = (0, react.useRef)(new three.Vector3());
248
+ const turnCrossAxis = (0, react.useRef)(new three.Vector3());
249
+ const turnOnYAxis = (0, react.useRef)(new three.Vector3());
250
+ const turnOnYQuat = (0, react.useRef)(new three.Quaternion());
251
+ const isLockForward = (0, react.useRef)(lockForward);
252
+ const forwardDirection = (0, react.useRef)(new three.Vector3());
253
+ const camRightDirection = (0, react.useRef)(new three.Vector3());
254
+ const rightwardDirection = (0, react.useRef)(new three.Vector3());
255
+ const inputDir = (0, react.useRef)(new three.Vector3());
256
+ const lastInputDir = (0, react.useRef)(new three.Vector3());
257
+ const baseImpulse = (0, react.useRef)(new three.Vector3());
258
+ const moveImpulse = (0, react.useRef)(new three.Vector3());
259
+ const moveImpulsePoint = (0, react.useRef)(new three.Vector3());
260
+ const moveImpulseToGround = (0, react.useRef)(new three.Vector3());
261
+ const movingDirection = (0, react.useRef)(new three.Vector3());
262
+ const movingDirCrossAxis = (0, react.useRef)(new three.Vector3());
263
+ const wantToMoveVel = (0, react.useRef)(new three.Vector3());
264
+ const rejectVel = (0, react.useRef)(new three.Vector3());
265
+ const isOnGround = (0, react.useRef)(false);
266
+ const jumpDirection = (0, react.useRef)(new three.Vector3());
267
+ const jumpVelocityVec = (0, react.useRef)(new three.Vector3());
268
+ const jumpImpulseToGround = (0, react.useRef)(new three.Vector3());
269
+ const isFalling = (0, react.useRef)(false);
270
+ const initialGravityScale = (0, react.useRef)((_props$gravityScale = props.gravityScale) !== null && _props$gravityScale !== void 0 ? _props$gravityScale : 1);
271
+ const dragFrictionImpulse = (0, react.useRef)(new three.Vector3());
272
+ /**
273
+ * Floating shape ray preset
274
+ */
275
+ const springDistVec = (0, react.useRef)(new three.Vector3());
276
+ const dampingVelVec = (0, react.useRef)(new three.Vector3());
277
+ const floatingForce = (0, react.useRef)(new three.Vector3());
278
+ const floatingImpulse = (0, react.useRef)(new three.Vector3());
279
+ const rayOrigin = (0, react.useRef)(new three.Vector3());
280
+ const groundHitOrigin = (0, react.useRef)(new three.Vector3());
281
+ const rayDirection = (0, react.useRef)(new three.Vector3());
282
+ const rayShape = (0, react.useMemo)(() => new rapier.Ball(rayRadius), [rayRadius]);
283
+ const shapeRayHit = (0, react.useRef)(null);
284
+ const rayHit = (0, react.useRef)(null);
285
+ const rayCast = (0, react.useMemo)(() => new rapier.Ray(rayOrigin.current, rayDirection.current), []);
286
+ const castRayHit = (0, react.useRef)(null);
287
+ const castShapeHit = (0, react.useRef)(null);
288
+ const groundHitDistance = (0, react.useRef)(0);
289
+ const groundFloatingDistance = (0, react.useRef)(0);
290
+ const rayHitBody = (0, react.useRef)(null);
291
+ (0, react.useEffect)(() => {
292
+ if (groundDetection === "rayCast") shapeRayHit.current = null;
293
+ else rayHit.current = null;
294
+ }, [groundDetection]);
295
+ /**
296
+ * Slope detection preset
297
+ */
298
+ const slopeAngleInFront = (0, react.useRef)(0);
299
+ const actualSlopeAngle = (0, react.useRef)(0);
300
+ const actualSlopeNormalVec = (0, react.useRef)(new three.Vector3());
301
+ /**
302
+ * Standing platform preset
303
+ */
304
+ const massRatio = (0, react.useRef)(1);
305
+ const isOnMovingObject = (0, react.useRef)(false);
306
+ const slideFrictionCoef = (0, react.useRef)(0);
307
+ const standingPointFriction = (0, react.useRef)(0);
308
+ const standingPoint = (0, react.useRef)(new three.Vector3());
309
+ const characterMassImpulse = (0, react.useRef)(new three.Vector3());
310
+ const movingObjectPosition = (0, react.useRef)(new three.Vector3());
311
+ const movingObjectVelocity = (0, react.useRef)(new three.Vector3());
312
+ const movingObjectVelocityOnPlane = (0, react.useRef)(new three.Vector3());
313
+ const movingObjectVelocityOnUp = (0, react.useRef)(new three.Vector3());
314
+ const movingObjectLinearVelocity = (0, react.useRef)(new three.Vector3());
315
+ const movingObjectAngularVelocity = (0, react.useRef)(new three.Vector3());
316
+ const movingObjectAngularVelocityValue = (0, react.useRef)(0);
317
+ const movingObjectAngularVelocityAxis = (0, react.useRef)(new three.Vector3());
318
+ const distanceFromCharacterToObjectPoint = (0, react.useRef)(new three.Vector3());
319
+ const movingObjectAngvelToLinvel = (0, react.useRef)(new three.Vector3());
320
+ const massRatioFallOffCurve = (0, react.useMemo)(() => {
321
+ var _massRatioFallOffCurv;
322
+ return require_CurveLUT.bakeCurveLUT(massRatioFallOffCurveData.points, (_massRatioFallOffCurv = massRatioFallOffCurveData.samples) !== null && _massRatioFallOffCurv !== void 0 ? _massRatioFallOffCurv : 50);
323
+ }, [massRatioFallOffCurveData]);
324
+ /**
325
+ * Debug indicators preset
326
+ */
327
+ const forwardIndicatorRef = (0, react.useRef)(null);
328
+ const forwardIndicatorMatrix = (0, react.useRef)(new three.Matrix4());
329
+ const moveIndicatorRef = (0, react.useRef)(null);
330
+ const moveIndicatorMatrix = (0, react.useRef)(new three.Matrix4());
331
+ const rayStartRef = (0, react.useRef)(null);
332
+ const rayEndRef = (0, react.useRef)(null);
333
+ const rayTriggerRef = (0, react.useRef)(null);
334
+ const rayStableRef = (0, react.useRef)(null);
335
+ const standingPointRef = (0, react.useRef)(null);
336
+ const xAxisPointRef = (0, react.useRef)(null);
337
+ const yAxisPointRef = (0, react.useRef)(null);
338
+ const zAxisPointRef = (0, react.useRef)(null);
339
+ const currVelArrowRef = (0, react.useRef)(null);
340
+ const currVelDir = (0, react.useRef)(new three.Vector3());
341
+ const debugAssets = (0, react.useMemo)(() => {
342
+ if (!debug) return null;
343
+ return {
344
+ forwardRingGeo: new three.RingGeometry(capsuleRadius * 2, capsuleRadius * 2.1, 32),
345
+ forwardPointerGeo: new three.PlaneGeometry(capsuleRadius / 2, capsuleRadius / 2),
346
+ forwardIndicatorMat: new three.MeshBasicMaterial({
347
+ color: require_Color.EC_AZURE,
348
+ side: three.DoubleSide
349
+ }),
350
+ rayCastGeo: new three.CircleGeometry(groundDetection === "rayCast" ? rayRadius / 2 : rayRadius, 12),
351
+ rayCastMat: new three.MeshBasicMaterial({
352
+ color: require_Color.EC_PURPLE,
353
+ side: three.DoubleSide,
354
+ transparent: true,
355
+ opacity: .5
356
+ }),
357
+ standingGeo: new three.OctahedronGeometry(rayRadius / 2, 3),
358
+ standingMat: new three.MeshBasicMaterial({
359
+ color: require_Color.EC_MED_PURPLE,
360
+ transparent: true,
361
+ opacity: .5
362
+ }),
363
+ movePointerGeo: new three.OctahedronGeometry(capsuleRadius / 3, 0),
364
+ moveRingGeo: new three.RingGeometry(capsuleRadius * 1.5, capsuleRadius * 2, 32),
365
+ moveIndicatorMat: new three.MeshBasicMaterial({
366
+ color: require_Color.EC_BLUE,
367
+ side: three.DoubleSide,
368
+ transparent: true,
369
+ opacity: .5
370
+ }),
371
+ modelPointerGeo: new three.OctahedronGeometry(capsuleRadius / 3, 0),
372
+ modelRingGeo: new three.RingGeometry(capsuleRadius * 1, capsuleRadius * 1.5, 32),
373
+ modelCapGeo: new three.SphereGeometry(capsuleRadius, 12, 8, 0, Math.PI, 0),
374
+ modelIndicatorMat: new three.MeshBasicMaterial({
375
+ color: require_Color.EC_CORNFLOWER_BLUE,
376
+ side: three.DoubleSide,
377
+ transparent: true,
378
+ opacity: .5
379
+ }),
380
+ modelCapMat: new three.MeshBasicMaterial({
381
+ color: require_Color.EC_CORNFLOWER_BLUE,
382
+ transparent: true,
383
+ opacity: .5
384
+ }),
385
+ xAxisPointMat: new three.MeshBasicMaterial({
386
+ color: require_Color.EC_GREEN,
387
+ transparent: true,
388
+ opacity: 1
389
+ }),
390
+ yAxisPointMat: new three.MeshBasicMaterial({
391
+ color: require_Color.EC_BLUE,
392
+ transparent: true,
393
+ opacity: 1
394
+ }),
395
+ zAxisPointMat: new three.MeshBasicMaterial({
396
+ color: require_Color.EC_RED,
397
+ transparent: true,
398
+ opacity: 1
399
+ })
400
+ };
401
+ }, [
402
+ debug,
403
+ capsuleRadius,
404
+ rayRadius,
405
+ groundDetection
406
+ ]);
407
+ (0, react.useEffect)(() => {
408
+ return () => {
409
+ if (!debugAssets) return;
410
+ for (const key in debugAssets) {
411
+ const item = debugAssets[key];
412
+ if ("dispose" in item && typeof item.dispose === "function") item.dispose();
413
+ }
414
+ };
415
+ }, [debugAssets]);
416
+ /**
417
+ *
418
+ *
419
+ *
420
+ *
421
+ *
422
+ *
423
+ *
424
+ *
425
+ *
426
+ *
427
+ *
428
+ *
429
+ *
430
+ *
431
+ *
432
+ *
433
+ *
434
+ *
435
+ *
436
+ *
437
+ *
438
+ *
439
+ *
440
+ *
441
+ *
442
+ *
443
+ *
444
+ *
445
+ *
446
+ *
447
+ */
448
+ /**
449
+ * Update character collider/model info function
450
+ */
451
+ const updateCharacterInfo = (0, react.useCallback)((body) => {
452
+ currentPos.current.copy(body.translation());
453
+ currentQuat.current.copy(body.rotation());
454
+ characterYAxis.current.set(0, 1, 0).applyQuaternion(currentQuat.current);
455
+ characterXAxis.current.set(1, 0, 0).applyQuaternion(currentQuat.current);
456
+ characterZAxis.current.set(0, 0, 1).applyQuaternion(currentQuat.current);
457
+ currentVel.current.copy(body.linvel());
458
+ currentVelOnPlane.current.copy(currentVel.current).projectOnPlane(referenceUpAxis);
459
+ currentVelOnUp.current.copy(currentVel.current).projectOnVector(referenceUpAxis);
460
+ currentAngVel.current.copy(body.angvel());
461
+ currentAngVelOnPlane.current.copy(currentAngVel.current).projectOnPlane(characterYAxis.current);
462
+ currentAngVelOnUp.current.copy(currentAngVel.current).projectOnVector(characterYAxis.current);
463
+ }, [referenceUpAxis]);
464
+ /**
465
+ * Camera project and forward direction function
466
+ */
467
+ const updateForwardDirection = (0, react.useCallback)(() => {
468
+ if (!useCustomForward) {
469
+ camera.getWorldDirection(forwardDirection.current);
470
+ camRightDirection.current.crossVectors(forwardDirection.current, camera.up).normalize();
471
+ forwardDirection.current.crossVectors(referenceUpAxis, camRightDirection.current);
472
+ rightwardDirection.current.crossVectors(forwardDirection.current, referenceUpAxis).normalize();
473
+ } else {
474
+ forwardDirection.current.projectOnPlane(referenceUpAxis).normalize();
475
+ rightwardDirection.current.crossVectors(forwardDirection.current, referenceUpAxis).normalize();
476
+ }
477
+ }, [useCustomForward, referenceUpAxis]);
478
+ /**
479
+ * User input function
480
+ */
481
+ const setInputDirection = (0, react.useCallback)((dir) => {
482
+ inputDir.current.set(0, 0, 0);
483
+ if (dir.joystick && (dir.joystick.x !== 0 || dir.joystick.y !== 0)) inputDir.current.addScaledVector(forwardDirection.current, dir.joystick.y).addScaledVector(rightwardDirection.current, dir.joystick.x);
484
+ else {
485
+ if (dir.forward) inputDir.current.add(forwardDirection.current);
486
+ if (dir.backward) inputDir.current.sub(forwardDirection.current);
487
+ if (dir.leftward) inputDir.current.sub(rightwardDirection.current);
488
+ if (dir.rightward) inputDir.current.add(rightwardDirection.current);
489
+ }
490
+ inputDir.current.normalize();
491
+ }, []);
492
+ /**
493
+ * Character moving function
494
+ */
495
+ const moveCharacter = (0, react.useCallback)((body, run, fpsCorr) => {
496
+ /**
497
+ * Setup moving direction base on inputDir and slopeAngleInFront
498
+ */
499
+ movingDirCrossAxis.current.crossVectors(inputDir.current, referenceUpAxis);
500
+ movingDirection.current.copy(inputDir.current).applyAxisAngle(movingDirCrossAxis.current, slopeAngleInFront.current);
501
+ /**
502
+ * Setup rejection velocity
503
+ */
504
+ wantToMoveVel.current.copy(relativeVelOnPlane.current).projectOnVector(inputDir.current);
505
+ rejectVel.current.copy(relativeVelOnPlane.current).sub(wantToMoveVel.current).multiplyScalar(isOnGround.current ? rejectVelFactor : 0);
506
+ /**
507
+ * Calculate required moving impulse: I = m * Δv
508
+ * If it's on a moving/rotating platform, apply platform velocity to Δv accordingly
509
+ * Also, apply reject velocity when character is moving opposite of it's moving direction
510
+ */
511
+ const multiplier = body.mass() * (0, three_src_math_MathUtils_js.clamp)(accDeltaTime, 0, 1) * (isOnGround.current ? slideFrictionCoef.current : airDragFactor) * (actualSlopeAngle.current > slopeMaxAngle ? airDragFactor : 1);
512
+ baseImpulse.current.copy(movingDirection.current).multiplyScalar(run ? maxRunVel : maxWalkVel).sub(relativeVelOnPlane.current);
513
+ moveImpulse.current.copy(baseImpulse.current).sub(rejectVel.current).multiplyScalar(multiplier);
514
+ moveImpulsePoint.current.copy(currentPos.current).addScaledVector(characterYAxis.current, moveImpulsePointOffset);
515
+ body.applyImpulseAtPoint(moveImpulse.current.multiplyScalar(fpsCorr), moveImpulsePoint.current, true);
516
+ if (applyCounterMoveImp && rayHitBody.current && isOnGround.current && rayHitBody.current.bodyType() === 0) {
517
+ moveImpulseToGround.current.copy(baseImpulse.current).multiplyScalar(multiplier * massRatio.current * counterMoveImpFactor).negate();
518
+ rayHitBody.current.applyImpulseAtPoint(moveImpulseToGround.current.multiplyScalar(fpsCorr), standingPoint.current, true);
519
+ } else moveImpulseToGround.current.set(0, 0, 0);
520
+ }, [
521
+ referenceUpAxis,
522
+ rejectVelFactor,
523
+ maxRunVel,
524
+ maxWalkVel,
525
+ accDeltaTime,
526
+ airDragFactor,
527
+ slopeMaxAngle,
528
+ moveImpulsePointOffset,
529
+ applyCounterMoveImp,
530
+ counterMoveImpFactor
531
+ ]);
532
+ /**
533
+ * Update gravity/upAxis direction and value
534
+ */
535
+ const updateGravityInfo = (0, react.useCallback)((body) => {
536
+ if (enableCustomGravity) {
537
+ referenceGravity.current.copy(gravityField(currentPos.current));
538
+ if (!isOnGround.current) applyGravityField(body, world.timestep);
539
+ } else referenceGravity.current.copy(world.gravity);
540
+ referenceGravityMag.current = referenceGravity.current.length();
541
+ referenceGravityDir.current.copy(referenceGravity.current).normalize();
542
+ if (referenceGravityDir.current.lengthSq() === 0) referenceGravityDir.current.copy(characterYAxis.current).negate();
543
+ gravityDir.current.copy(slerpRef.current(gravityDir.current, referenceGravityDir.current, 1 - Math.exp(-gravityDirLerpSpeed * world.timestep), characterXAxis.current));
544
+ upAxis.current.copy(gravityDir.current).negate();
545
+ }, [
546
+ enableCustomGravity,
547
+ gravityDirLerpSpeed,
548
+ gravityField,
549
+ applyGravityField
550
+ ]);
551
+ /**
552
+ * Character auto balance function
553
+ */
554
+ const autoBalanceCharacter = (0, react.useCallback)((body, fpsCorr) => {
555
+ balanceCrossAxis.current.crossVectors(characterYAxis.current, upAxis.current);
556
+ const torque = balanceCrossAxis.current.multiplyScalar(autoBalanceSpringK).sub(currentAngVelOnPlane.current.multiplyScalar(autoBalanceDampingC));
557
+ body.applyTorqueImpulse(torque.multiplyScalar(fpsCorr), false);
558
+ }, [autoBalanceSpringK, autoBalanceDampingC]);
559
+ /**
560
+ * Character turning function
561
+ */
562
+ const turnCharacter = (0, react.useCallback)((body, direction, fpsCorr) => {
563
+ turnCrossAxis.current.crossVectors(characterZAxis.current, direction);
564
+ let dot = (0, three_src_math_MathUtils_js.clamp)(characterZAxis.current.dot(direction), -1, 1);
565
+ if (Math.abs(dot) < 1e-10) dot = 0;
566
+ const angle = Math.atan2(turnCrossAxis.current.dot(characterYAxis.current), dot);
567
+ const torque = turnOnYAxis.current.copy(characterYAxis.current).multiplyScalar(angle * autoBalanceSpringOnY).sub(currentAngVelOnUp.current.multiplyScalar(autoBalanceDampingOnY));
568
+ body.applyTorqueImpulse(torque.multiplyScalar(fpsCorr), false);
569
+ }, [autoBalanceSpringOnY, autoBalanceDampingOnY]);
570
+ /**
571
+ * Character floating function
572
+ */
573
+ const ecctrlRayFilter = (0, react.useCallback)((collider) => {
574
+ var _collider$parent, _userData$ecctrl, _userData$ecctrl2;
575
+ const userData = (_collider$parent = collider.parent) === null || _collider$parent === void 0 || (_collider$parent = _collider$parent.call(collider)) === null || _collider$parent === void 0 ? void 0 : _collider$parent.userData;
576
+ return !((userData === null || userData === void 0 || (_userData$ecctrl = userData.ecctrl) === null || _userData$ecctrl === void 0 ? void 0 : _userData$ecctrl.excludeRay) || (userData === null || userData === void 0 || (_userData$ecctrl2 = userData.ecctrl) === null || _userData$ecctrl2 === void 0 ? void 0 : _userData$ecctrl2.excludeCharacterRay));
577
+ }, []);
578
+ const findWalkableCenterRayHit = (0, react.useCallback)((body, maxDistance) => {
579
+ var _selectedRayHit$colli;
580
+ castRayHit.current = null;
581
+ rayCast.origin = rayOrigin.current;
582
+ rayCast.dir = rayDirection.current;
583
+ world.intersectionsWithRay(rayCast, maxDistance, false, (hit) => {
584
+ if (actualSlopeNormalVec.current.copy(hit.normal).angleTo(referenceUpAxis) < slopeMaxAngle && (!castRayHit.current || hit.timeOfImpact < castRayHit.current.timeOfImpact)) castRayHit.current = hit;
585
+ return true;
586
+ }, _dimforge_rapier3d_compat.QueryFilterFlags.EXCLUDE_SENSORS, void 0, void 0, body, ecctrlRayFilter);
587
+ const selectedRayHit = castRayHit.current;
588
+ if (!selectedRayHit) return false;
589
+ rayHit.current = selectedRayHit;
590
+ rayHitBody.current = selectedRayHit.collider.parent();
591
+ actualSlopeNormalVec.current.copy(selectedRayHit.normal);
592
+ actualSlopeAngle.current = actualSlopeNormalVec.current.angleTo(referenceUpAxis);
593
+ groundHitDistance.current = selectedRayHit.timeOfImpact;
594
+ groundFloatingDistance.current = rayRadius * 2 + floatHeight;
595
+ groundHitOrigin.current.copy(rayOrigin.current);
596
+ standingPointFriction.current = (_selectedRayHit$colli = selectedRayHit.collider.friction()) !== null && _selectedRayHit$colli !== void 0 ? _selectedRayHit$colli : 0;
597
+ return true;
598
+ }, [
599
+ rayCast,
600
+ referenceUpAxis,
601
+ slopeMaxAngle,
602
+ ecctrlRayFilter,
603
+ rayRadius,
604
+ floatHeight
605
+ ]);
606
+ const floatCharacter = (0, react.useCallback)((body) => {
607
+ rayOrigin.current.copy(currentPos.current).addScaledVector(characterYAxis.current, rayOriginOffest);
608
+ rayDirection.current.copy(referenceUpAxis).negate();
609
+ rayHit.current = null;
610
+ shapeRayHit.current = null;
611
+ rayHitBody.current = null;
612
+ if (groundDetection === "rayCast") {
613
+ rayCast.origin = rayOrigin.current;
614
+ rayCast.dir = rayDirection.current;
615
+ castRayHit.current = world.castRayAndGetNormal(rayCast, rayLength, false, _dimforge_rapier3d_compat.QueryFilterFlags.EXCLUDE_SENSORS, void 0, void 0, body, ecctrlRayFilter);
616
+ if (castRayHit.current) {
617
+ actualSlopeNormalVec.current.copy(castRayHit.current.normal);
618
+ actualSlopeAngle.current = actualSlopeNormalVec.current.angleTo(referenceUpAxis);
619
+ if (actualSlopeAngle.current < slopeMaxAngle) {
620
+ var _castRayHit$current$c;
621
+ rayHit.current = castRayHit.current;
622
+ rayHitBody.current = castRayHit.current.collider.parent();
623
+ groundHitOrigin.current.copy(rayOrigin.current);
624
+ groundHitDistance.current = castRayHit.current.timeOfImpact;
625
+ groundFloatingDistance.current = rayRadius * 2 + floatHeight;
626
+ standingPointFriction.current = (_castRayHit$current$c = castRayHit.current.collider.friction()) !== null && _castRayHit$current$c !== void 0 ? _castRayHit$current$c : 0;
627
+ } else findWalkableCenterRayHit(body, rayLength);
628
+ }
629
+ } else if (groundDetection === "shapeCast") {
630
+ castShapeHit.current = world.castShape(rayOrigin.current, body.rotation(), rayDirection.current, rayShape, 0, rayLength, false, _dimforge_rapier3d_compat.QueryFilterFlags.EXCLUDE_SENSORS, void 0, void 0, body, ecctrlRayFilter);
631
+ if (castShapeHit.current) {
632
+ actualSlopeNormalVec.current.copy(castShapeHit.current.normal1);
633
+ actualSlopeAngle.current = actualSlopeNormalVec.current.angleTo(referenceUpAxis);
634
+ if (actualSlopeAngle.current < slopeMaxAngle) {
635
+ var _castShapeHit$current;
636
+ shapeRayHit.current = castShapeHit.current;
637
+ groundHitOrigin.current.copy(rayOrigin.current);
638
+ rayHitBody.current = castShapeHit.current.collider.parent();
639
+ groundHitDistance.current = castShapeHit.current.time_of_impact;
640
+ groundFloatingDistance.current = rayRadius + floatHeight;
641
+ standingPointFriction.current = (_castShapeHit$current = castShapeHit.current.collider.friction()) !== null && _castShapeHit$current !== void 0 ? _castShapeHit$current : 0;
642
+ } else findWalkableCenterRayHit(body, rayLength + rayRadius);
643
+ }
644
+ }
645
+ if (rayHitBody.current) {
646
+ isOnGround.current = groundHitDistance.current < groundFloatingDistance.current + rayHitForgiveness;
647
+ if (isOnGround.current) {
648
+ if (rayHit.current) standingPoint.current.copy(groundHitOrigin.current).addScaledVector(rayDirection.current, groundHitDistance.current);
649
+ else if (shapeRayHit.current) standingPoint.current.copy(shapeRayHit.current.witness1);
650
+ } else standingPointFriction.current = 0;
651
+ } else {
652
+ rayHitBody.current = null;
653
+ isOnGround.current = false;
654
+ actualSlopeAngle.current = 0;
655
+ slopeAngleInFront.current = 0;
656
+ standingPointFriction.current = 0;
657
+ }
658
+ }, [
659
+ rayOriginOffest,
660
+ referenceUpAxis,
661
+ groundDetection,
662
+ rayCast,
663
+ rayShape,
664
+ rayLength,
665
+ rayRadius,
666
+ floatHeight,
667
+ rayHitForgiveness,
668
+ slopeMaxAngle,
669
+ ecctrlRayFilter,
670
+ findWalkableCenterRayHit
671
+ ]);
672
+ const applyFloatingForce = (0, react.useCallback)((body) => {
673
+ if (!(rayHit.current || shapeRayHit.current) || !isOnGround.current) {
674
+ floatingImpulse.current.set(0, 0, 0);
675
+ return;
676
+ }
677
+ springDistVec.current.copy(referenceUpAxis).multiplyScalar(groundFloatingDistance.current - groundHitDistance.current);
678
+ dampingVelVec.current.copy(relativeVel.current).projectOnVector(referenceUpAxis);
679
+ floatingForce.current.subVectors(springDistVec.current.multiplyScalar(springK), dampingVelVec.current.multiplyScalar(dampingC));
680
+ floatingImpulse.current.copy(floatingForce.current).multiplyScalar(world.timestep);
681
+ if (jumpActive.current && floatingImpulse.current.dot(referenceUpAxis) < 0) floatingImpulse.current.set(0, 0, 0);
682
+ if (!body.isSleeping()) body.applyImpulse(floatingImpulse.current, false);
683
+ }, [
684
+ referenceUpAxis,
685
+ springK,
686
+ dampingC
687
+ ]);
688
+ /**
689
+ * Character mass on standing collider
690
+ */
691
+ const applyMassOnStandCollider = (0, react.useCallback)((body) => {
692
+ if (!rayHitBody.current || rayHitBody.current.bodyType() !== 0 || !isOnGround.current) return;
693
+ const impulseMag = Math.max(-floatingImpulse.current.dot(upAxis.current), 0);
694
+ const weightMag = body.mass() * referenceGravityMag.current * world.timestep;
695
+ characterMassImpulse.current.copy(gravityDir.current).multiplyScalar(Math.max(impulseMag, weightMag) * massRatio.current);
696
+ if (applyCounterMass) rayHitBody.current.applyImpulseAtPoint(characterMassImpulse.current, standingPoint.current, true);
697
+ }, [applyCounterMass]);
698
+ /**
699
+ * Friction on character function
700
+ */
701
+ const applyFriction = (0, react.useCallback)((body, fpsCorr) => {
702
+ if (!rayHitBody.current || !isOnGround.current) return;
703
+ dragFrictionImpulse.current.copy(relativeVelOnPlane.current).negate().multiplyScalar(body.mass() * slideFrictionCoef.current * (0, three_src_math_MathUtils_js.clamp)(decDeltaTime, 0, 1));
704
+ body.applyImpulse(dragFrictionImpulse.current.multiplyScalar(fpsCorr), false);
705
+ }, [decDeltaTime]);
706
+ /**
707
+ * Slope detect function
708
+ */
709
+ const slopeDetect = (0, react.useCallback)(() => {
710
+ if (rayHit.current || shapeRayHit.current) {
711
+ actualSlopeAngle.current = actualSlopeNormalVec.current.angleTo(referenceUpAxis);
712
+ if (isOnGround.current) slopeAngleInFront.current = -Math.asin(actualSlopeNormalVec.current.dot(inputDir.current));
713
+ else slopeAngleInFront.current = 0;
714
+ } else {
715
+ actualSlopeAngle.current = 0;
716
+ slopeAngleInFront.current = 0;
717
+ }
718
+ }, [referenceUpAxis]);
719
+ /**
720
+ * Falling detect function
721
+ */
722
+ const fallDetect = (0, react.useCallback)(() => {
723
+ isFalling.current = currentVelOnUp.current.dot(upAxis.current) < 0 && !isOnGround.current ? true : false;
724
+ }, []);
725
+ /**
726
+ * Zero gravity detect function
727
+ */
728
+ const zeroGravityDetect = (0, react.useCallback)(() => {
729
+ isZeroGravity.current = referenceGravityMag.current === 0;
730
+ }, []);
731
+ /**
732
+ * Detect if character is on a moving object
733
+ */
734
+ const isOnMovingObjectDetect = (0, react.useCallback)((body) => {
735
+ if (followPlatform && rayHitBody.current && isOnGround.current && (rayHitBody.current.isDynamic() || rayHitBody.current.isKinematic())) {
736
+ isOnMovingObject.current = true;
737
+ if (rayHitBody.current.isDynamic()) massRatio.current = require_CurveLUT.evaluateCurveLUT((0, three_src_math_MathUtils_js.clamp)(rayHitBody.current.mass() / Math.max(body.mass(), 1e-6), 0, 1), massRatioFallOffCurve);
738
+ else massRatio.current = 1;
739
+ movingObjectPosition.current.copy(rayHitBody.current.worldCom());
740
+ distanceFromCharacterToObjectPoint.current.copy(currentPos.current).sub(movingObjectPosition.current);
741
+ movingObjectLinearVelocity.current.copy(rayHitBody.current.linvel());
742
+ movingObjectAngularVelocity.current.copy(rayHitBody.current.angvel());
743
+ movingObjectAngvelToLinvel.current.crossVectors(movingObjectAngularVelocity.current, distanceFromCharacterToObjectPoint.current);
744
+ movingObjectVelocity.current.copy(movingObjectLinearVelocity.current).addScaledVector(movingObjectAngvelToLinvel.current, massRatio.current);
745
+ movingObjectVelocityOnPlane.current.copy(movingObjectVelocity.current).projectOnPlane(referenceUpAxis);
746
+ movingObjectVelocityOnUp.current.copy(movingObjectVelocity.current).projectOnVector(referenceUpAxis);
747
+ movingObjectAngularVelocityValue.current = movingObjectAngularVelocity.current.length();
748
+ movingObjectAngularVelocityAxis.current.copy(movingObjectAngularVelocity.current).normalize();
749
+ turnOnYQuat.current.setFromAxisAngle(movingObjectAngularVelocityAxis.current, movingObjectAngularVelocityValue.current * world.timestep);
750
+ } else {
751
+ isOnMovingObject.current = false;
752
+ movingObjectVelocity.current.set(0, 0, 0);
753
+ movingObjectVelocityOnPlane.current.set(0, 0, 0);
754
+ movingObjectVelocityOnUp.current.set(0, 0, 0);
755
+ turnOnYQuat.current.identity();
756
+ massRatio.current = 1;
757
+ }
758
+ }, [
759
+ followPlatform,
760
+ referenceUpAxis,
761
+ massRatioFallOffCurve
762
+ ]);
763
+ /**
764
+ * Compute ray hit point relative velocity
765
+ */
766
+ const computeRelativeVelocity = (0, react.useCallback)(() => {
767
+ relativeVel.current.copy(currentVel.current);
768
+ relativeVelOnPlane.current.copy(currentVelOnPlane.current);
769
+ relativeVelOnUp.current.copy(currentVelOnUp.current);
770
+ if (isOnMovingObject.current && followPlatform) {
771
+ relativeVel.current.sub(movingObjectVelocity.current);
772
+ relativeVelOnPlane.current.sub(movingObjectVelocityOnPlane.current);
773
+ relativeVelOnUp.current.sub(movingObjectVelocityOnUp.current);
774
+ }
775
+ }, [followPlatform]);
776
+ /**
777
+ * Character jump up impulse function
778
+ */
779
+ const applyJumpImpulse = (0, react.useCallback)((body) => {
780
+ jumpDirection.current.copy(referenceUpAxis).addScaledVector(actualSlopeNormalVec.current, slopeJumpFactor).normalize();
781
+ jumpVelocityVec.current.copy(relativeVelOnPlane.current).add(movingObjectVelocity.current).addScaledVector(jumpDirection.current, jumpVel);
782
+ body.setLinvel(jumpVelocityVec.current, true);
783
+ if (applyCounterJumpImp && rayHitBody.current && rayHitBody.current.bodyType() === 0) {
784
+ jumpImpulseToGround.current.copy(jumpDirection.current).multiplyScalar(-body.mass() * jumpVel * massRatio.current * counterJumpImpFactor);
785
+ rayHitBody.current.applyImpulseAtPoint(jumpImpulseToGround.current, standingPoint.current, true);
786
+ }
787
+ }, [
788
+ referenceUpAxis,
789
+ slopeJumpFactor,
790
+ jumpVel,
791
+ applyCounterJumpImp,
792
+ counterJumpImpFactor
793
+ ]);
794
+ /**
795
+ * Gravity scale control function
796
+ * zero gravity on ground and extra gravity when falling
797
+ */
798
+ const applyDynamicGravity = (0, react.useCallback)((body) => {
799
+ if (isFalling.current) {
800
+ if (currentVelOnUp.current.lengthSq() > fallingMaxVel * fallingMaxVel) {
801
+ if (body.gravityScale() !== 0) body.setGravityScale(0, false);
802
+ } else if (body.gravityScale() !== fallingGravityScale) body.setGravityScale(fallingGravityScale, false);
803
+ } else if (isOnGround.current) {
804
+ if (body.gravityScale() !== 0) body.setGravityScale(0, false);
805
+ } else if (body.gravityScale() !== initialGravityScale.current) body.setGravityScale(initialGravityScale.current, false);
806
+ }, [fallingMaxVel, fallingGravityScale]);
807
+ /**
808
+ * Jump state control function
809
+ */
810
+ const getJumpState = (0, react.useCallback)((jumpPressed) => {
811
+ if (jumpActive.current) {
812
+ jumpElapsedTime.current += world.timestep;
813
+ if (jumpElapsedTime.current >= jumpDuration) jumpActive.current = false;
814
+ } else {
815
+ if (jumpPressed && canJumpAgain.current) {
816
+ jumpActive.current = true;
817
+ jumpElapsedTime.current = 0;
818
+ canJumpAgain.current = false;
819
+ }
820
+ if (!jumpPressed) canJumpAgain.current = true;
821
+ }
822
+ return jumpActive.current;
823
+ }, [jumpDuration]);
824
+ /**
825
+ * Run state control function
826
+ */
827
+ const getRunState = (0, react.useCallback)((runPressed) => {
828
+ if (enableToggleRun) {
829
+ if (runPressed && !canRunAgain.current) runActive.current = !runActive.current;
830
+ canRunAgain.current = runPressed;
831
+ } else runActive.current = runPressed;
832
+ return runActive.current;
833
+ }, [enableToggleRun]);
834
+ /**
835
+ * Update debug indicators
836
+ */
837
+ const updateDebugger = (0, react.useCallback)(() => {
838
+ if (forwardIndicatorRef.current) {
839
+ forwardIndicatorRef.current.position.copy(rayOrigin.current);
840
+ forwardIndicatorMatrix.current.lookAt(fixedOrigin, forwardDirection.current, referenceUpAxis);
841
+ forwardIndicatorRef.current.quaternion.setFromRotationMatrix(forwardIndicatorMatrix.current);
842
+ }
843
+ if (rayStartRef.current && rayEndRef.current && rayTriggerRef.current && rayStableRef.current && standingPointRef.current) {
844
+ const debugStableDistance = groundDetection === "rayCast" ? rayRadius * 2 + floatHeight : rayRadius + groundFloatingDistance.current;
845
+ rayStartRef.current.position.copy(rayOrigin.current);
846
+ rayStartRef.current.quaternion.setFromUnitVectors(fixedZAxis, referenceUpAxis);
847
+ rayEndRef.current.position.copy(rayOrigin.current).addScaledVector(referenceUpAxis, -rayLength);
848
+ rayEndRef.current.quaternion.setFromUnitVectors(fixedZAxis, referenceUpAxis);
849
+ rayTriggerRef.current.position.copy(rayOrigin.current).addScaledVector(referenceUpAxis, -debugStableDistance - rayHitForgiveness);
850
+ rayTriggerRef.current.quaternion.setFromUnitVectors(fixedZAxis, referenceUpAxis);
851
+ rayStableRef.current.position.copy(rayOrigin.current).addScaledVector(referenceUpAxis, -debugStableDistance);
852
+ rayStableRef.current.quaternion.setFromUnitVectors(fixedZAxis, referenceUpAxis);
853
+ standingPointRef.current.position.copy(standingPoint.current);
854
+ }
855
+ if (moveIndicatorRef.current) {
856
+ moveIndicatorRef.current.position.copy(rayOrigin.current);
857
+ moveIndicatorMatrix.current.lookAt(fixedOrigin, movingDirection.current, referenceUpAxis);
858
+ moveIndicatorRef.current.quaternion.setFromRotationMatrix(moveIndicatorMatrix.current);
859
+ }
860
+ if (currVelArrowRef.current) {
861
+ currVelArrowRef.current.position.copy(currentPos.current);
862
+ currVelArrowRef.current.setDirection(currVelDir.current.copy(relativeVel.current).normalize());
863
+ currVelArrowRef.current.setLength(relativeVel.current.length() / (runActive.current ? maxRunVel : maxWalkVel));
864
+ }
865
+ }, [
866
+ fixedOrigin,
867
+ fixedZAxis,
868
+ referenceUpAxis,
869
+ groundDetection,
870
+ rayLength,
871
+ rayRadius,
872
+ floatHeight,
873
+ rayHitForgiveness,
874
+ maxRunVel,
875
+ maxWalkVel
876
+ ]);
877
+ /**
878
+ *
879
+ *
880
+ *
881
+ *
882
+ *
883
+ *
884
+ *
885
+ *
886
+ *
887
+ *
888
+ *
889
+ *
890
+ *
891
+ *
892
+ *
893
+ *
894
+ *
895
+ *
896
+ *
897
+ *
898
+ *
899
+ *
900
+ *
901
+ */
902
+ (0, _react_three_fiber.useFrame)(() => {
903
+ var _joystick$x, _joystick$y;
904
+ if (!enable || !characterRef.current) return;
905
+ const characterBody = characterRef.current;
906
+ let isSleeping = characterBody.isSleeping();
907
+ const frameRateCorrection = 60 * world.timestep;
908
+ /**
909
+ * Getting all the user input states
910
+ */
911
+ const forward = movementState.current.forward;
912
+ const backward = movementState.current.backward;
913
+ const leftward = movementState.current.leftward;
914
+ const rightward = movementState.current.rightward;
915
+ const run = getRunState(movementState.current.run || false);
916
+ const jump = getJumpState(movementState.current.jump || false);
917
+ const joystick = movementState.current.joystick;
918
+ const hasControlInput = forward || backward || leftward || rightward || jump || Math.abs((_joystick$x = joystick === null || joystick === void 0 ? void 0 : joystick.x) !== null && _joystick$x !== void 0 ? _joystick$x : 0) > 1e-4 || Math.abs((_joystick$y = joystick === null || joystick === void 0 ? void 0 : joystick.y) !== null && _joystick$y !== void 0 ? _joystick$y : 0) > 1e-4;
919
+ if (isSleeping && (isOnMovingObject.current || hasControlInput)) {
920
+ characterBody.wakeUp();
921
+ isSleeping = false;
922
+ }
923
+ if (isSleeping) return;
924
+ /**
925
+ * Update character collider pos/vel/quat/axis
926
+ */
927
+ updateCharacterInfo(characterBody);
928
+ /**
929
+ * Update gravity value & direction, and then apply gravity
930
+ */
931
+ updateGravityInfo(characterBody);
932
+ updateForwardDirection();
933
+ setInputDirection({
934
+ forward,
935
+ backward,
936
+ rightward,
937
+ leftward,
938
+ joystick
939
+ });
940
+ const hasMoveInput = inputDir.current.lengthSq() > 0;
941
+ /**
942
+ * Update character auto balance
943
+ */
944
+ if (autoBalance && !isZeroGravity.current) autoBalanceCharacter(characterBody, frameRateCorrection);
945
+ /**
946
+ * Update ground contact info
947
+ */
948
+ floatCharacter(characterBody);
949
+ slideFrictionCoef.current = (0, three_src_math_MathUtils_js.clamp)((standingPointFriction.current + slideGripFactor) * .5, 0, 1);
950
+ /**
951
+ * Detect if character is on a moving object
952
+ */
953
+ isOnMovingObjectDetect(characterBody);
954
+ /**
955
+ * Compute relative velocity
956
+ */
957
+ computeRelativeVelocity();
958
+ /**
959
+ * Float character up
960
+ */
961
+ applyFloatingForce(characterBody);
962
+ /**
963
+ * Apply character mass to standing object
964
+ */
965
+ applyMassOnStandCollider(characterBody);
966
+ /**
967
+ * Detect slope angle below character
968
+ */
969
+ slopeDetect();
970
+ /**
971
+ * Detect is character under zero gravity condition
972
+ */
973
+ zeroGravityDetect();
974
+ /**
975
+ * Detect if character is falling
976
+ */
977
+ fallDetect();
978
+ /**
979
+ * Apply drag force if character is not moving
980
+ */
981
+ if (!hasMoveInput) applyFriction(characterBody, frameRateCorrection);
982
+ /**
983
+ * Apply dynamic character gravity scale base on different conditions: grounded/jumpUp/fall/exceed-fall-max-vel
984
+ */
985
+ applyDynamicGravity(characterBody);
986
+ /**
987
+ * Apply jump impulse to character
988
+ */
989
+ if (jump && isOnGround.current) applyJumpImpulse(characterBody);
990
+ /**
991
+ * Move character model to correct direction and speed
992
+ */
993
+ if (isLockForward.current) {
994
+ if (!isZeroGravity.current) turnCharacter(characterBody, forwardDirection.current, frameRateCorrection);
995
+ if (hasMoveInput) moveCharacter(characterBody, run, frameRateCorrection);
996
+ lastInputDir.current.copy(forwardDirection.current);
997
+ } else if (hasMoveInput) {
998
+ if (!isZeroGravity.current) turnCharacter(characterBody, inputDir.current, frameRateCorrection);
999
+ moveCharacter(characterBody, run, frameRateCorrection);
1000
+ lastInputDir.current.copy(inputDir.current);
1001
+ } else {
1002
+ if (lastInputDir.current.lengthSq() === 0) lastInputDir.current.copy(characterZAxis.current);
1003
+ if (!isZeroGravity.current) turnCharacter(characterBody, isOnMovingObject.current && followPlatform ? lastInputDir.current.applyQuaternion(turnOnYQuat.current) : lastInputDir.current, frameRateCorrection);
1004
+ movingDirection.current.copy(lastInputDir.current);
1005
+ }
1006
+ /**
1007
+ * Update debug indicators
1008
+ */
1009
+ if (debug) updateDebugger();
1010
+ });
1011
+ return /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement(_react_three_rapier.RigidBody, {
1012
+ colliders: false,
1013
+ ref: characterRef,
1014
+ position: props.position || [
1015
+ 0,
1016
+ 1,
1017
+ 0
1018
+ ],
1019
+ friction: (_props$friction = props.friction) !== null && _props$friction !== void 0 ? _props$friction : -.5,
1020
+ ...props
1021
+ }, /* @__PURE__ */ react.default.createElement(_react_three_rapier.CapsuleCollider, {
1022
+ name: "character-capsule-collider",
1023
+ args: [capsuleHalfHeight, capsuleRadius],
1024
+ ref: characterColliderRef
1025
+ }), children, debug && debugAssets && /* @__PURE__ */ react.default.createElement(react.default.Fragment, null, /* @__PURE__ */ react.default.createElement("group", null, /* @__PURE__ */ react.default.createElement("mesh", {
1026
+ scale: [
1027
+ .5,
1028
+ .5,
1029
+ 2
1030
+ ],
1031
+ position: [
1032
+ 0,
1033
+ rayOriginOffest,
1034
+ capsuleRadius * 2
1035
+ ],
1036
+ geometry: debugAssets.modelPointerGeo,
1037
+ material: debugAssets.modelIndicatorMat
1038
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1039
+ rotation: [
1040
+ -Math.PI / 2,
1041
+ 0,
1042
+ 0
1043
+ ],
1044
+ position: [
1045
+ 0,
1046
+ rayOriginOffest,
1047
+ 0
1048
+ ],
1049
+ geometry: debugAssets.modelRingGeo,
1050
+ material: debugAssets.modelIndicatorMat
1051
+ })), /* @__PURE__ */ react.default.createElement("group", null, /* @__PURE__ */ react.default.createElement("mesh", {
1052
+ ref: xAxisPointRef,
1053
+ position: [
1054
+ 1,
1055
+ 0,
1056
+ 0
1057
+ ],
1058
+ geometry: debugAssets.standingGeo,
1059
+ material: debugAssets.xAxisPointMat
1060
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1061
+ ref: yAxisPointRef,
1062
+ position: [
1063
+ 0,
1064
+ 1,
1065
+ 0
1066
+ ],
1067
+ geometry: debugAssets.standingGeo,
1068
+ material: debugAssets.yAxisPointMat
1069
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1070
+ ref: zAxisPointRef,
1071
+ position: [
1072
+ 0,
1073
+ 0,
1074
+ 1
1075
+ ],
1076
+ geometry: debugAssets.standingGeo,
1077
+ material: debugAssets.zAxisPointMat
1078
+ })), /* @__PURE__ */ react.default.createElement("mesh", { position: [
1079
+ 0,
1080
+ moveImpulsePointOffset,
1081
+ 0
1082
+ ] }, /* @__PURE__ */ react.default.createElement("octahedronGeometry", { args: [capsuleRadius / 5, 0] })))), debug && debugAssets && /* @__PURE__ */ react.default.createElement("group", null, /* @__PURE__ */ react.default.createElement("group", { ref: forwardIndicatorRef }, /* @__PURE__ */ react.default.createElement("mesh", {
1083
+ rotation: [
1084
+ -Math.PI / 2,
1085
+ 0,
1086
+ 0
1087
+ ],
1088
+ geometry: debugAssets.forwardRingGeo,
1089
+ material: debugAssets.forwardIndicatorMat
1090
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1091
+ rotation: [
1092
+ -Math.PI / 2,
1093
+ 0,
1094
+ Math.PI / 4
1095
+ ],
1096
+ position: [
1097
+ 0,
1098
+ 0,
1099
+ -capsuleRadius * 2
1100
+ ],
1101
+ geometry: debugAssets.forwardPointerGeo,
1102
+ material: debugAssets.forwardIndicatorMat
1103
+ })), /* @__PURE__ */ react.default.createElement("group", null, /* @__PURE__ */ react.default.createElement("mesh", {
1104
+ ref: rayStartRef,
1105
+ geometry: debugAssets.rayCastGeo,
1106
+ material: debugAssets.rayCastMat
1107
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1108
+ ref: rayTriggerRef,
1109
+ geometry: debugAssets.rayCastGeo,
1110
+ material: debugAssets.standingMat
1111
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1112
+ ref: rayStableRef,
1113
+ geometry: debugAssets.rayCastGeo,
1114
+ material: debugAssets.standingMat
1115
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1116
+ ref: rayEndRef,
1117
+ geometry: debugAssets.rayCastGeo,
1118
+ material: debugAssets.rayCastMat
1119
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1120
+ ref: standingPointRef,
1121
+ geometry: debugAssets.standingGeo,
1122
+ material: debugAssets.rayCastMat
1123
+ })), /* @__PURE__ */ react.default.createElement("group", { ref: moveIndicatorRef }, /* @__PURE__ */ react.default.createElement("mesh", {
1124
+ scale: [
1125
+ .5,
1126
+ .5,
1127
+ 2
1128
+ ],
1129
+ position: [
1130
+ 0,
1131
+ 0,
1132
+ -capsuleRadius * 2
1133
+ ],
1134
+ geometry: debugAssets.movePointerGeo,
1135
+ material: debugAssets.moveIndicatorMat
1136
+ }), /* @__PURE__ */ react.default.createElement("mesh", {
1137
+ rotation: [
1138
+ -Math.PI / 2,
1139
+ 0,
1140
+ 0
1141
+ ],
1142
+ geometry: debugAssets.moveRingGeo,
1143
+ material: debugAssets.moveIndicatorMat
1144
+ })), /* @__PURE__ */ react.default.createElement("arrowHelper", {
1145
+ ref: currVelArrowRef,
1146
+ args: [
1147
+ void 0,
1148
+ void 0,
1149
+ void 0,
1150
+ "#FA8787"
1151
+ ]
1152
+ })));
1153
+ });
1154
+ var Ecctrl_default = react.default.memo(Ecctrl);
1155
+ //#endregion
1156
+ //#region src/index.ts
1157
+ /*!
1158
+ * Ecctrl
1159
+ * https://github.com/pmndrs/ecctrl
1160
+ *
1161
+ * SPDX-FileCopyrightText: 2023-2026 Erdong Chen
1162
+ * SPDX-License-Identifier: MIT
1163
+ */
1164
+ //#endregion
1165
+ Object.defineProperty(exports, "Ecctrl_default", {
1166
+ enumerable: true,
1167
+ get: function() {
1168
+ return Ecctrl_default;
1169
+ }
1170
+ });
1171
+
1172
+ //# sourceMappingURL=src-BTTVvwrl.cjs.map