@pinkaphant/kolmo 0.5.0 → 0.5.1

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.
@@ -0,0 +1,610 @@
1
+ import { A as writeEntityKinematics, C as flipVehicleBodyUpright, D as useActiveKolmoWorld, E as pilotedVehicleQuery, S as ensureWorldSession, _ as VehicleTelemetryTrait, f as InteractableTrait, g as TransformTrait, h as PilotedTag, j as writeVehicleTelemetry, t as KolmoGravityContext, v as VehicleTrait, w as kolmoActions, x as createEmptyVehicleCmd, y as VelocityTrait } from "./physics-ZDFXaCli.js";
2
+ import React, { createContext, useCallback, useContext, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
3
+ import * as THREE from "three";
4
+ import { useKeyboardControls } from "@react-three/drei";
5
+ import { useButtonStore, useJoystickStore } from "@pinkaphant/ecctrl/input";
6
+ import { useFrame } from "@react-three/fiber";
7
+ import { CuboidCollider, RigidBody, useRapier } from "@react-three/rapier";
8
+ import { useHas } from "koota/react";
9
+ import { KhrAssembly, assignRef, cloneKhrBundle, useKhrGlb } from "@khr3/core";
10
+ import { jsx, jsxs } from "react/jsx-runtime";
11
+ import { easing } from "maath";
12
+ import { KhrVehicle } from "@khr3/core/vehicle";
13
+ // src/engine/input.ts
14
+ var KolmoKeyboardContext = createContext(null);
15
+ function KolmoKeyboardBridge({ children }) {
16
+ const [, getKeys] = useKeyboardControls();
17
+ return React.createElement(KolmoKeyboardContext.Provider, { value: getKeys }, children);
18
+ }
19
+ var KOLMO_STICK_DEADZONE = .08;
20
+ var EMPTY_KEYS = Object.freeze({
21
+ forward: false,
22
+ backward: false,
23
+ left: false,
24
+ right: false,
25
+ brake: false,
26
+ boost: false,
27
+ interact: false,
28
+ turnLeft: false,
29
+ turnRight: false,
30
+ descend: false,
31
+ cameraView: false,
32
+ reset: false,
33
+ modeToggle: false
34
+ });
35
+ var EMPTY_KOLMO_INPUT = Object.freeze({
36
+ ...EMPTY_KEYS,
37
+ stickX: 0,
38
+ stickY: 0
39
+ });
40
+ var KOLMO_KEY_MAP = [
41
+ {
42
+ name: "forward",
43
+ keys: ["KeyW", "ArrowUp"]
44
+ },
45
+ {
46
+ name: "backward",
47
+ keys: ["KeyS", "ArrowDown"]
48
+ },
49
+ {
50
+ name: "left",
51
+ keys: ["KeyA", "ArrowLeft"]
52
+ },
53
+ {
54
+ name: "right",
55
+ keys: ["KeyD", "ArrowRight"]
56
+ },
57
+ {
58
+ name: "brake",
59
+ keys: ["Space"]
60
+ },
61
+ {
62
+ name: "boost",
63
+ keys: ["ShiftLeft", "ShiftRight"]
64
+ },
65
+ {
66
+ name: "interact",
67
+ keys: ["KeyF"]
68
+ },
69
+ {
70
+ name: "turnLeft",
71
+ keys: ["KeyQ"]
72
+ },
73
+ {
74
+ name: "turnRight",
75
+ keys: ["KeyC"]
76
+ },
77
+ {
78
+ name: "descend",
79
+ keys: ["KeyX", "ControlLeft"]
80
+ },
81
+ {
82
+ name: "cameraView",
83
+ keys: ["KeyV"]
84
+ },
85
+ {
86
+ name: "reset",
87
+ keys: ["KeyR"]
88
+ },
89
+ {
90
+ name: "modeToggle",
91
+ keys: ["KeyG"]
92
+ }
93
+ ];
94
+ /**
95
+ * Input reader merging Drei `<KeyboardControls>`, `@pinkaphant/ecctrl/input`
96
+ * Zustand stores (`useJoystickStore` + `useButtonStore`), and optional `getInput()` overrides.
97
+ * Analog thumbstick values stay analog (`stickX`, `stickY`) rather than slamming digital WASD booleans.
98
+ */
99
+ function useKolmoInput(getInput) {
100
+ const getKeyboard = useContext(KolmoKeyboardContext);
101
+ const getterRef = useRef(getInput);
102
+ getterRef.current = getInput;
103
+ const outRef = useRef({ ...EMPTY_KOLMO_INPUT });
104
+ return useCallback(() => {
105
+ const custom = getterRef.current?.();
106
+ const k = getKeyboard ? getKeyboard() : EMPTY_KEYS;
107
+ const rawStick = useJoystickStore.getState().joysticks.default;
108
+ const btns = useButtonStore.getState().buttons;
109
+ const rx = rawStick?.x ?? 0;
110
+ const ry = rawStick?.y ?? 0;
111
+ const mag = Math.hypot(rx, ry);
112
+ const scaled = mag > .08 ? (Math.min(1, mag) - KOLMO_STICK_DEADZONE) / (.92 * mag) : 0;
113
+ const out = outRef.current;
114
+ out.stickX = custom?.stickX ?? rx * scaled;
115
+ out.stickY = custom?.stickY ?? ry * scaled;
116
+ out.forward = Boolean(custom?.forward ?? k.forward);
117
+ out.backward = Boolean(custom?.backward ?? k.backward);
118
+ out.left = Boolean(custom?.left ?? k.left);
119
+ out.right = Boolean(custom?.right ?? k.right);
120
+ out.brake = Boolean(custom?.brake ?? (k.brake || btns.brake || btns.jump));
121
+ out.boost = Boolean(custom?.boost ?? (k.boost || btns.boost || btns.run));
122
+ out.interact = Boolean(custom?.interact ?? (k.interact || btns.interact));
123
+ out.turnLeft = Boolean(custom?.turnLeft ?? (k.turnLeft || btns.turnLeft));
124
+ out.turnRight = Boolean(custom?.turnRight ?? (k.turnRight || btns.turnRight));
125
+ out.descend = Boolean(custom?.descend ?? (k.descend || btns.descend));
126
+ out.cameraView = Boolean(custom?.cameraView ?? (k.cameraView || btns.cameraView));
127
+ out.reset = Boolean(custom?.reset ?? (k.reset || btns.reset));
128
+ out.modeToggle = Boolean(custom?.modeToggle ?? (k.modeToggle || btns.modeToggle));
129
+ return out;
130
+ }, [getKeyboard]);
131
+ }
132
+ // src/engine/vehicle.tsx
133
+ function hasActiveCmd(c) {
134
+ return Boolean(c.forward || c.backward || c.steerLeft || c.steerRight || c.brake || c.throttleUp || c.throttleDown || c.yawLeft || c.yawRight || c.pitchForward || c.pitchBackward || c.rollLeft || c.rollRight || c.joystickL && (c.joystickL.x !== 0 || c.joystickL.y !== 0) || c.joystickR && (c.joystickR.x !== 0 || c.joystickR.y !== 0));
135
+ }
136
+ function clearCmdInPlace(cmd, stickL, stickR) {
137
+ cmd.forward = false;
138
+ cmd.backward = false;
139
+ cmd.steerLeft = false;
140
+ cmd.steerRight = false;
141
+ cmd.brake = false;
142
+ cmd.throttleUp = false;
143
+ cmd.throttleDown = false;
144
+ cmd.yawLeft = false;
145
+ cmd.yawRight = false;
146
+ cmd.pitchForward = false;
147
+ cmd.pitchBackward = false;
148
+ cmd.rollLeft = false;
149
+ cmd.rollRight = false;
150
+ stickL.x = 0;
151
+ stickL.y = 0;
152
+ stickR.x = 0;
153
+ stickR.y = 0;
154
+ cmd.joystickL = stickL;
155
+ cmd.joystickR = stickR;
156
+ }
157
+ var _up = new THREE.Vector3();
158
+ var _fwd = new THREE.Vector3();
159
+ var _camGoal = new THREE.Vector3();
160
+ var _lookGoal = new THREE.Vector3();
161
+ function KolmoVehicleMount({ ref, vehicleRef, jointsRef: outerJointsRef, id, sourceBundle, alreadyCloned = false, piloted = false, flightMode = false, vehicleMode, interactRadius = 4.2, enable = true, enableCustomGravity, carConfig, wheelConfig, propellerConfig, droneConfig, getInput, driver, cameraController, children, ...khrRest }) {
162
+ const ctxCustomGravity = useContext(KolmoGravityContext);
163
+ const activeWorld = useActiveKolmoWorld();
164
+ useLayoutEffect(() => {
165
+ ensureWorldSession(activeWorld);
166
+ }, [activeWorld]);
167
+ const readInput = useKolmoInput(getInput);
168
+ const bundle = useMemo(() => alreadyCloned ? sourceBundle : cloneKhrBundle(sourceBundle), [sourceBundle, alreadyCloned]);
169
+ const jointsRef = useRef(null);
170
+ const innerRef = useRef(null);
171
+ const canHybridToggle = vehicleMode === void 0 && bundle.wheels.length > 0 && bundle.propellers.length > 0;
172
+ const [hybridFlight, setHybridFlight] = useState(flightMode);
173
+ useLayoutEffect(() => {
174
+ setHybridFlight(flightMode);
175
+ }, [flightMode]);
176
+ const [boostThrustActive, setBoostThrustActive] = useState(false);
177
+ const boostThrustRef = useRef(false);
178
+ const isFlight = Boolean(vehicleMode === "rocket" || (canHybridToggle ? hybridFlight : flightMode) || vehicleMode !== "drive" && bundle.propellers.length > 0 && bundle.wheels.length === 0);
179
+ const isManualTrans = (carConfig?.transmissionMode ?? bundle.tune?.transmissionMode) === "manual";
180
+ const seatOffset = useMemo(() => {
181
+ const dot = bundle.sockets.IK_Seat ?? bundle.sockets.seat ?? bundle.sockets.driver_butt ?? bundle.sockets.butt;
182
+ return dot ? [
183
+ dot[0],
184
+ dot[1],
185
+ dot[2]
186
+ ] : [
187
+ 0,
188
+ .65,
189
+ 0
190
+ ];
191
+ }, [bundle.sockets]);
192
+ const exitOffset = useMemo(() => {
193
+ const dot = bundle.sockets.Socket_Exit ?? bundle.sockets.exit;
194
+ return dot ? [
195
+ dot[0],
196
+ dot[1],
197
+ dot[2]
198
+ ] : [
199
+ -1.55,
200
+ .45,
201
+ 0
202
+ ];
203
+ }, [bundle.sockets]);
204
+ const propActive = bundle.propellers.length > 0 && (isFlight || vehicleMode !== "drive" && boostThrustActive);
205
+ const mergedWheelConfig = useMemo(() => {
206
+ const wheelOverride = bundle.wheels.length > 0 && vehicleMode === "rocket" ? { enable: false } : void 0;
207
+ return !wheelConfig && !wheelOverride ? void 0 : {
208
+ ...wheelOverride,
209
+ ...wheelConfig ?? {}
210
+ };
211
+ }, [
212
+ bundle.wheels.length,
213
+ vehicleMode,
214
+ wheelConfig
215
+ ]);
216
+ const mergedPropellerConfig = useMemo(() => {
217
+ const propOverride = bundle.propellers.length > 0 && !propActive ? { enable: false } : void 0;
218
+ return !propellerConfig && !propOverride ? void 0 : {
219
+ ...propOverride,
220
+ ...propellerConfig ?? {}
221
+ };
222
+ }, [
223
+ bundle.propellers.length,
224
+ propActive,
225
+ propellerConfig
226
+ ]);
227
+ const [vehEnt, setVehEnt] = useState(null);
228
+ const vehEntRef = useRef(null);
229
+ const camTargetRef = useRef(new THREE.Vector3());
230
+ const stickLRef = useRef({
231
+ x: 0,
232
+ y: 0
233
+ });
234
+ const stickRRef = useRef({
235
+ x: 0,
236
+ y: 0
237
+ });
238
+ const cmdRef = useRef(createEmptyVehicleCmd(false));
239
+ const stRef = useRef({
240
+ camInit: false,
241
+ cockpit: false,
242
+ prevCam: false,
243
+ prevReset: false,
244
+ prevMode: false,
245
+ wasPiloted: false
246
+ });
247
+ useLayoutEffect(() => {
248
+ const ent = activeWorld.spawn(VehicleTrait({
249
+ id,
250
+ handle: innerRef.current,
251
+ jointsHandle: jointsRef.current,
252
+ seatOffset,
253
+ exitOffset,
254
+ isFlight
255
+ }), InteractableTrait({
256
+ id,
257
+ radius: interactRadius,
258
+ kind: "vehicle"
259
+ }), TransformTrait, VelocityTrait, VehicleTelemetryTrait);
260
+ vehEntRef.current = ent;
261
+ setVehEnt(ent);
262
+ if (piloted && !activeWorld.queryFirst(pilotedVehicleQuery)) kolmoActions(activeWorld).boardVehicle(id);
263
+ return () => {
264
+ if (ent.isAlive() && ent.has(PilotedTag)) kolmoActions(activeWorld).exitVehicle();
265
+ vehEntRef.current = null;
266
+ ent.destroy();
267
+ };
268
+ }, [activeWorld, id]);
269
+ useLayoutEffect(() => {
270
+ const ent = vehEntRef.current;
271
+ if (!ent?.isAlive()) return;
272
+ const v = ent.get(VehicleTrait);
273
+ if (v) ent.set(VehicleTrait, {
274
+ ...v,
275
+ seatOffset,
276
+ exitOffset,
277
+ isFlight
278
+ });
279
+ ent.set(InteractableTrait, {
280
+ id,
281
+ radius: interactRadius,
282
+ kind: "vehicle"
283
+ });
284
+ }, [
285
+ id,
286
+ seatOffset,
287
+ exitOffset,
288
+ interactRadius,
289
+ isFlight
290
+ ]);
291
+ const assignVehicleRef = useCallback((handle) => {
292
+ innerRef.current = handle;
293
+ assignRef(vehicleRef, handle);
294
+ assignRef(ref, handle);
295
+ const ent = vehEntRef.current;
296
+ if (ent?.isAlive()) {
297
+ const v = ent.get(VehicleTrait);
298
+ if (v) ent.set(VehicleTrait, {
299
+ ...v,
300
+ handle
301
+ });
302
+ }
303
+ }, [ref, vehicleRef]);
304
+ const assignJointsRef = useCallback((jh) => {
305
+ jointsRef.current = jh;
306
+ assignRef(outerJointsRef, jh);
307
+ const ent = vehEntRef.current;
308
+ if (ent?.isAlive()) {
309
+ const v = ent.get(VehicleTrait);
310
+ if (v) ent.set(VehicleTrait, {
311
+ ...v,
312
+ jointsHandle: jh
313
+ });
314
+ }
315
+ }, [outerJointsRef]);
316
+ const { world: rapierWorld } = useRapier();
317
+ const isPiloted = useHas(vehEnt, PilotedTag) || piloted && vehEnt === null;
318
+ const recordKinematicsAndTelemetry = (veh, throttle, boost) => {
319
+ if (!vehEntRef.current?.isAlive()) return;
320
+ let maxSlip = 0;
321
+ for (const w of veh.wheelsInfo.values()) if (w.current.rayHit && w.current.slipStrength > maxSlip) maxSlip = w.current.slipStrength;
322
+ writeEntityKinematics(activeWorld, vehEntRef.current, veh.currPos, veh.currQuat, veh.currLinVel, veh.currLinVel.length() * 3.6);
323
+ writeVehicleTelemetry(activeWorld, vehEntRef.current, Math.round(veh.engineRPM), veh.gearIndex + 1, throttle, boost, Math.min(1, maxSlip));
324
+ };
325
+ useFrame((state, delta) => {
326
+ const dt = rapierWorld.timestep > 0 ? rapierWorld.timestep : Math.min(Math.max(delta, 1e-4), 1 / 30);
327
+ const veh = innerRef.current;
328
+ if (!veh?.body) return;
329
+ if (driver) {
330
+ driver(veh, dt, state);
331
+ recordKinematicsAndTelemetry(veh, 0, 0);
332
+ cameraController?.(veh, dt, state);
333
+ return;
334
+ }
335
+ const keys = readInput();
336
+ const st = stRef.current;
337
+ const cmd = cmdRef.current;
338
+ const stickL = stickLRef.current;
339
+ const stickR = stickRRef.current;
340
+ const wantBoostProp = isPiloted && !isFlight && vehicleMode !== "drive" && bundle.propellers.length > 0 && keys.boost && (keys.forward || keys.brake);
341
+ if (wantBoostProp !== boostThrustRef.current) {
342
+ boostThrustRef.current = wantBoostProp;
343
+ setBoostThrustActive(wantBoostProp);
344
+ }
345
+ if (isPiloted) {
346
+ st.wasPiloted = true;
347
+ if (keys.cameraView && !st.prevCam) st.cockpit = !st.cockpit;
348
+ st.prevCam = keys.cameraView;
349
+ if (keys.reset && !st.prevReset) flipVehicleBodyUpright(veh, jointsRef.current);
350
+ st.prevReset = keys.reset;
351
+ if (keys.modeToggle && !st.prevMode && canHybridToggle) setHybridFlight((f) => !f);
352
+ st.prevMode = keys.modeToggle;
353
+ let anyGrounded = false;
354
+ for (const w of veh.wheelsInfo.values()) if (w.current.rayHit) {
355
+ anyGrounded = true;
356
+ break;
357
+ }
358
+ if (isFlight) {
359
+ cmd.pitchForward = keys.forward;
360
+ cmd.pitchBackward = keys.backward;
361
+ cmd.yawLeft = keys.left;
362
+ cmd.yawRight = keys.right;
363
+ cmd.rollLeft = keys.turnLeft;
364
+ cmd.rollRight = keys.turnRight;
365
+ cmd.throttleUp = keys.brake;
366
+ cmd.throttleDown = keys.descend;
367
+ cmd.forward = anyGrounded && keys.forward;
368
+ cmd.backward = anyGrounded && keys.backward;
369
+ cmd.steerLeft = anyGrounded && keys.left;
370
+ cmd.steerRight = anyGrounded && keys.right;
371
+ cmd.brake = false;
372
+ stickL.x = 0;
373
+ stickL.y = 0;
374
+ stickR.x = keys.stickX;
375
+ stickR.y = keys.stickY;
376
+ cmd.joystickL = stickL;
377
+ cmd.joystickR = stickR;
378
+ veh.setMovement(cmd);
379
+ } else {
380
+ const fwdVel = veh.currLinVel.dot(veh.bodyZAxis);
381
+ const effForward = keys.forward || keys.stickY > .15;
382
+ const effBackward = keys.backward || keys.stickY < -.15;
383
+ const revBraking = effBackward && !effForward && fwdVel > 1;
384
+ const fwdBraking = effForward && !effBackward && fwdVel < -1;
385
+ if (!isManualTrans && (effBackward && !revBraking || effForward && Math.abs(fwdVel) < 2.5) && veh.gearIndex !== 0) veh.setGear(0);
386
+ const boostProp = keys.boost && (bundle.propellers.length > 0 || veh.propellersInfo.size > 0) && veh.gravityMag > 1e-4;
387
+ cmd.forward = effForward && !fwdBraking;
388
+ cmd.backward = effBackward && !revBraking;
389
+ cmd.steerLeft = keys.left;
390
+ cmd.steerRight = keys.right;
391
+ cmd.brake = keys.brake || revBraking || fwdBraking;
392
+ cmd.pitchForward = boostProp && effForward;
393
+ cmd.pitchBackward = false;
394
+ cmd.throttleUp = boostProp && !effForward && keys.brake;
395
+ cmd.throttleDown = false;
396
+ cmd.yawLeft = boostProp && keys.left;
397
+ cmd.yawRight = boostProp && keys.right;
398
+ cmd.rollLeft = false;
399
+ cmd.rollRight = false;
400
+ stickL.x = keys.stickX;
401
+ stickL.y = 0;
402
+ stickR.x = 0;
403
+ stickR.y = 0;
404
+ cmd.joystickL = stickL;
405
+ cmd.joystickR = stickR;
406
+ veh.setMovement(cmd);
407
+ }
408
+ } else {
409
+ st.prevCam = st.prevReset = st.prevMode = false;
410
+ if (st.wasPiloted || hasActiveCmd(cmd)) {
411
+ st.wasPiloted = false;
412
+ clearCmdInPlace(cmd, stickL, stickR);
413
+ veh.setMovement(cmd);
414
+ }
415
+ }
416
+ const throttle = isPiloted ? +(keys.forward || keys.stickY > .15 || isFlight && keys.brake) : 0;
417
+ const boost = isPiloted && keys.boost && throttle > 0 ? 1 : 0;
418
+ recordKinematicsAndTelemetry(veh, throttle, boost);
419
+ if (!isPiloted) return;
420
+ if (cameraController) {
421
+ cameraController(veh, dt, state);
422
+ return;
423
+ }
424
+ _up.copy(veh.upAxis);
425
+ if (_up.lengthSq() < 1e-6) _up.copy(veh.bodyYAxis);
426
+ if (st.cockpit) {
427
+ _camGoal.set(seatOffset[0], seatOffset[1] + .32, seatOffset[2]).applyQuaternion(veh.currQuat).add(veh.currPos);
428
+ _lookGoal.copy(_camGoal).addScaledVector(veh.bodyZAxis, 24);
429
+ easing.damp3(state.camera.position, _camGoal, .04, dt);
430
+ easing.damp3(state.camera.up, veh.bodyYAxis, .08, dt);
431
+ state.camera.lookAt(_lookGoal);
432
+ return;
433
+ }
434
+ _fwd.copy(veh.bodyZAxis);
435
+ if (!isFlight) _fwd.projectOnPlane(_up).normalize();
436
+ if (_fwd.lengthSq() < 1e-6) _fwd.copy(veh.bodyZAxis);
437
+ _camGoal.copy(veh.currPos).addScaledVector(_fwd, isFlight ? -11.2 : -6.8).addScaledVector(_up, isFlight ? 3.1 : 2.15);
438
+ _lookGoal.copy(veh.currPos).addScaledVector(_fwd, isFlight ? 7.5 : 3.6).addScaledVector(_up, .65);
439
+ if (!st.camInit) {
440
+ state.camera.position.copy(_camGoal);
441
+ camTargetRef.current.copy(_lookGoal);
442
+ st.camInit = true;
443
+ }
444
+ easing.damp3(state.camera.position, _camGoal, .14, dt);
445
+ easing.damp3(camTargetRef.current, _lookGoal, .08, dt);
446
+ easing.damp3(state.camera.up, _up, .16, dt);
447
+ state.camera.lookAt(camTargetRef.current);
448
+ });
449
+ return /* @__PURE__ */ jsx(KhrVehicle, {
450
+ ref: assignVehicleRef,
451
+ jointsRef: assignJointsRef,
452
+ bundle,
453
+ enable,
454
+ enableCustomGravity: enableCustomGravity ?? ctxCustomGravity,
455
+ ...carConfig === void 0 ? {} : { carConfig },
456
+ ...mergedWheelConfig === void 0 ? {} : { wheelConfig: mergedWheelConfig },
457
+ ...mergedPropellerConfig === void 0 ? {} : { propellerConfig: mergedPropellerConfig },
458
+ ...droneConfig === void 0 ? {} : { droneConfig },
459
+ ...khrRest,
460
+ children
461
+ });
462
+ }
463
+ function KolmoVehicleFromUrl(props) {
464
+ const { src, ...rest } = props;
465
+ const bundle = useKhrGlb(src);
466
+ return /* @__PURE__ */ jsx(KolmoVehicleMount, {
467
+ ...rest,
468
+ sourceBundle: bundle,
469
+ alreadyCloned: true
470
+ });
471
+ }
472
+ function KolmoVehicle(props) {
473
+ const reactId = useId();
474
+ const id = props.id ?? `vehicle_${reactId.replace(/:/g, "")}`;
475
+ const { src, bundle, ...rest } = props;
476
+ if (bundle) return /* @__PURE__ */ jsx(KolmoVehicleMount, {
477
+ ...rest,
478
+ id,
479
+ sourceBundle: bundle
480
+ });
481
+ if (src) return /* @__PURE__ */ jsx(KolmoVehicleFromUrl, {
482
+ ...rest,
483
+ id,
484
+ src
485
+ });
486
+ return null;
487
+ }
488
+ // src/engine/surface.tsx
489
+ function ArenaSlab({ size, position, color = "#252b36" }) {
490
+ const res = useMemo(() => ({
491
+ geo: new THREE.BoxGeometry(size, 1, size),
492
+ mat: new THREE.MeshStandardMaterial({
493
+ color,
494
+ roughness: .9,
495
+ metalness: .05
496
+ })
497
+ }), [size, color]);
498
+ useEffect(() => () => {
499
+ res.geo.dispose();
500
+ res.mat.dispose();
501
+ }, [res]);
502
+ return /* @__PURE__ */ jsxs(RigidBody, {
503
+ type: "fixed",
504
+ colliders: false,
505
+ position: [
506
+ position[0],
507
+ position[1] - .5,
508
+ position[2]
509
+ ],
510
+ friction: .9,
511
+ children: [/* @__PURE__ */ jsx(CuboidCollider, {
512
+ args: [
513
+ size * .5,
514
+ .5,
515
+ size * .5
516
+ ],
517
+ restitution: .04
518
+ }), /* @__PURE__ */ jsx("mesh", {
519
+ geometry: res.geo,
520
+ material: res.mat,
521
+ receiveShadow: true
522
+ })]
523
+ });
524
+ }
525
+ function BundleSurface({ ref, bundle, alreadyCloned = false, position, enableCustomGravity, ...rest }) {
526
+ const ctxCustomGravity = useContext(KolmoGravityContext);
527
+ const cloned = useMemo(() => alreadyCloned ? bundle : cloneKhrBundle(bundle), [bundle, alreadyCloned]);
528
+ return /* @__PURE__ */ jsx(KhrAssembly, {
529
+ ref,
530
+ bundle: cloned,
531
+ position,
532
+ fixedRoot: true,
533
+ enableCustomGravity: enableCustomGravity ?? ctxCustomGravity,
534
+ ...rest
535
+ });
536
+ }
537
+ function UrlSurface({ src, ...rest }) {
538
+ const bundle = useKhrGlb(src);
539
+ return /* @__PURE__ */ jsx(BundleSurface, {
540
+ bundle,
541
+ alreadyCloned: true,
542
+ ...rest
543
+ });
544
+ }
545
+ function KolmoSurface({ ref, src, bundle, size = 200, color, position = [
546
+ 0,
547
+ 0,
548
+ 0
549
+ ], ...rest }) {
550
+ if (bundle) return /* @__PURE__ */ jsx(BundleSurface, {
551
+ ref,
552
+ bundle,
553
+ position,
554
+ ...rest
555
+ });
556
+ if (src) return /* @__PURE__ */ jsx(UrlSurface, {
557
+ ref,
558
+ src,
559
+ position,
560
+ ...rest
561
+ });
562
+ return /* @__PURE__ */ jsx(ArenaSlab, {
563
+ size,
564
+ position,
565
+ color
566
+ });
567
+ }
568
+ function BundleProp({ ref, bundle, alreadyCloned = false, fixed = false, enableCustomGravity, onClick: userOnClick, ...rest }) {
569
+ const ctxCustomGravity = useContext(KolmoGravityContext);
570
+ const asmRef = useRef(null);
571
+ const cloned = useMemo(() => alreadyCloned ? bundle : cloneKhrBundle(bundle), [bundle, alreadyCloned]);
572
+ const assignAsmRef = useCallback((h) => {
573
+ asmRef.current = h;
574
+ assignRef(ref, h);
575
+ }, [ref]);
576
+ const onClick = useCallback((e) => {
577
+ if (e.delta <= 5 && asmRef.current?.toggleHitObject(e.object)) e.stopPropagation();
578
+ userOnClick?.(e);
579
+ }, [userOnClick]);
580
+ return /* @__PURE__ */ jsx(KhrAssembly, {
581
+ ref: assignAsmRef,
582
+ bundle: cloned,
583
+ fixedRoot: fixed,
584
+ enableCustomGravity: enableCustomGravity ?? ctxCustomGravity,
585
+ onClick,
586
+ ...rest
587
+ });
588
+ }
589
+ function UrlProp({ src, ...rest }) {
590
+ const bundle = useKhrGlb(src);
591
+ return /* @__PURE__ */ jsx(BundleProp, {
592
+ bundle,
593
+ alreadyCloned: true,
594
+ ...rest
595
+ });
596
+ }
597
+ function KolmoProp({ src, bundle, ...rest }) {
598
+ if (bundle) return /* @__PURE__ */ jsx(BundleProp, {
599
+ bundle,
600
+ ...rest
601
+ });
602
+ if (src) return /* @__PURE__ */ jsx(UrlProp, {
603
+ src,
604
+ ...rest
605
+ });
606
+ return null;
607
+ }
608
+ export { KOLMO_KEY_MAP as a, KolmoKeyboardContext as c, EMPTY_KOLMO_INPUT as i, useKolmoInput as l, KolmoSurface as n, KOLMO_STICK_DEADZONE as o, KolmoVehicle as r, KolmoKeyboardBridge as s, KolmoProp as t };
609
+
610
+ //# sourceMappingURL=surface-DUol_5ml.js.map