react-three-game 0.0.108 → 0.0.109

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 (36) hide show
  1. package/README.md +5 -5
  2. package/dist/editor.d.ts +22 -0
  3. package/dist/editor.js +15 -0
  4. package/dist/plugins/crashcat/CrashcatPhysicsComponent.js +1 -1
  5. package/dist/plugins/crashcat/CrashcatRuntime.js +1 -1
  6. package/dist/tools/prefabeditor/EditorContext.d.ts +36 -0
  7. package/dist/tools/prefabeditor/EditorContext.js +17 -0
  8. package/dist/tools/prefabeditor/EditorTree.js +1 -1
  9. package/dist/tools/prefabeditor/EditorTreeMenus.js +1 -1
  10. package/dist/tools/prefabeditor/EditorUI.js +1 -1
  11. package/dist/tools/prefabeditor/PrefabEditor.d.ts +4 -37
  12. package/dist/tools/prefabeditor/PrefabEditor.js +15 -30
  13. package/dist/tools/prefabeditor/PrefabRoot.d.ts +3 -27
  14. package/dist/tools/prefabeditor/PrefabRoot.js +79 -60
  15. package/dist/tools/prefabeditor/SceneContext.d.ts +28 -0
  16. package/dist/tools/prefabeditor/SceneContext.js +14 -0
  17. package/dist/tools/prefabeditor/assetRuntime.d.ts +4 -0
  18. package/dist/tools/prefabeditor/components/ComponentRegistry.d.ts +1 -1
  19. package/dist/tools/prefabeditor/components/ModelComponent.js +1 -1
  20. package/dist/tools/prefabeditor/components/PrefabRefComponent.js +4 -4
  21. package/dist/tools/prefabeditor/components/TransformComponent.js +1 -1
  22. package/dist/tools/prefabeditor/components/index.d.ts +1 -0
  23. package/dist/tools/prefabeditor/components/index.js +8 -0
  24. package/dist/tools/prefabeditor/components/runtime.d.ts +4 -0
  25. package/dist/tools/prefabeditor/components/runtime.js +372 -0
  26. package/dist/tools/prefabeditor/prefabStore.d.ts +1 -1
  27. package/dist/tools/prefabeditor/prefabStore.js +5 -1
  28. package/dist/tools/prefabeditor/runtimeUtils.d.ts +10 -0
  29. package/dist/tools/prefabeditor/runtimeUtils.js +30 -0
  30. package/dist/tools/prefabeditor/utils.d.ts +1 -9
  31. package/dist/tools/prefabeditor/utils.js +3 -28
  32. package/dist/viewer.d.ts +22 -0
  33. package/dist/viewer.js +14 -0
  34. package/package.json +10 -8
  35. package/dist/index.d.ts +0 -40
  36. package/dist/index.js +0 -32
@@ -0,0 +1,28 @@
1
+ import type { Object3D, Texture } from "three";
2
+ import type { GameObject, Prefab } from "./types";
3
+ export declare enum PrefabEditorMode {
4
+ Edit = "edit",
5
+ Play = "play"
6
+ }
7
+ export type PrefabNode = Omit<GameObject, "children">;
8
+ export interface Scene {
9
+ root: Object3D | null;
10
+ mode: PrefabEditorMode;
11
+ basePath: string;
12
+ get(id: string): GameObject | null;
13
+ getObject(id: string): Object3D | null;
14
+ getHandle<T = unknown>(id: string, kind: string): T | null;
15
+ getModel(path: string): Object3D | null;
16
+ add(node: GameObject, parentId?: string): GameObject;
17
+ update(id: string, fn: (node: PrefabNode) => PrefabNode): void;
18
+ replaceNode(id: string, node: GameObject): void;
19
+ remove(id: string): void;
20
+ duplicate(id: string): string | null;
21
+ move(draggedId: string, targetId: string, position: "before" | "inside"): void;
22
+ replace(prefab: Prefab): void;
23
+ addModel(path: string, model: Object3D): void;
24
+ addTexture(path: string, texture: Texture): void;
25
+ addSound(path: string, sound: AudioBuffer): void;
26
+ }
27
+ export declare const SceneContext: import("react").Context<Scene | null>;
28
+ export declare function useScene(): Scene;
@@ -0,0 +1,14 @@
1
+ import { createContext, useContext } from "react";
2
+ export var PrefabEditorMode;
3
+ (function (PrefabEditorMode) {
4
+ PrefabEditorMode["Edit"] = "edit";
5
+ PrefabEditorMode["Play"] = "play";
6
+ })(PrefabEditorMode || (PrefabEditorMode = {}));
7
+ export const SceneContext = createContext(null);
8
+ export function useScene() {
9
+ const scene = useContext(SceneContext);
10
+ if (!scene) {
11
+ throw new Error("useScene must be used within a PrefabRoot or PrefabEditor scene provider");
12
+ }
13
+ return scene;
14
+ }
@@ -1,7 +1,11 @@
1
1
  import { type ReactNode } from "react";
2
2
  import type { Object3D, Texture } from "three";
3
3
  export interface AssetRuntime {
4
+ registerObject: (id: string, object: Object3D | null) => void;
4
5
  registerHandle: (id: string, kind: string, handle: unknown) => void;
6
+ registerModel: (path: string, model: Object3D) => void;
7
+ registerTexture: (path: string, texture: Texture) => void;
8
+ registerSound: (path: string, sound: AudioBuffer) => void;
5
9
  getHandle: <T = unknown>(id: string, kind: string) => T | null;
6
10
  getModel: (path: string) => Object3D | null;
7
11
  getTexture: (path: string) => Texture | null;
@@ -38,7 +38,7 @@ export interface Component {
38
38
  name: string;
39
39
  /** Set when this component occupies a single slot on a node. Use a string to share a slot across component types. */
40
40
  disableSiblingComposition?: boolean | string;
41
- Editor: FC<{
41
+ Editor?: FC<{
42
42
  node?: GameObject;
43
43
  component: ComponentData;
44
44
  onUpdate: (newComp: Record<string, unknown>) => void;
@@ -5,7 +5,7 @@ import { Mesh } from 'three';
5
5
  import { assetRef, assetRefs } from './ComponentRegistry';
6
6
  import { BooleanField, FieldGroup, Label, ListEditor, NumberInput, SelectInput, StringField } from './Input';
7
7
  import { useAssetRuntime } from '../assetRuntime';
8
- import { useEditorContext, useEditorRef } from '../PrefabEditor';
8
+ import { useEditorContext, useEditorRef } from '../EditorContext';
9
9
  import { getRepeatAxesFromModelProperties, normalizeRepeatAxes } from '../InstanceProvider';
10
10
  import { base, colors, ui } from '../styles';
11
11
  import { decomposeModelToPrefabNodes } from '../modelPrefab';
@@ -8,12 +8,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
11
- import { useEffect, useState } from 'react';
12
- import PrefabRoot from '../PrefabRoot';
13
- import { useEditorRef } from '../PrefabEditor';
11
+ import { lazy, Suspense, useEffect, useState } from 'react';
12
+ import { useEditorRef } from '../EditorContext';
14
13
  import { withBasePath } from '../utils';
15
14
  import { base, colors } from '../styles';
16
15
  import { FieldGroup, Label } from './Input';
16
+ const PrefabRoot = lazy(() => import('../PrefabRoot'));
17
17
  function PrefabRefView({ properties, children, basePath = '' }) {
18
18
  var _a;
19
19
  const [loadedPrefab, setLoadedPrefab] = useState(null);
@@ -30,7 +30,7 @@ function PrefabRefView({ properties, children, basePath = '' }) {
30
30
  .catch(err => console.warn('[PrefabRef] Failed to load:', resolvedUrl, err));
31
31
  return () => { cancelled = true; };
32
32
  }, [resolvedUrl]);
33
- return (_jsxs(_Fragment, { children: [loadedPrefab && _jsx(PrefabRoot, { data: loadedPrefab, editMode: false, basePath: basePath }), children] }));
33
+ return (_jsxs(_Fragment, { children: [loadedPrefab && (_jsx(Suspense, { fallback: null, children: _jsx(PrefabRoot, { data: loadedPrefab, editMode: false, basePath: basePath }) })), children] }));
34
34
  }
35
35
  function PrefabRefEditor({ node, component, onUpdate, basePath = '', }) {
36
36
  var _a, _b;
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
2
  import { Label, Vector3Input } from "./Input";
3
- import { useEditorContext } from "../PrefabEditor";
3
+ import { useEditorContext } from "../EditorContext";
4
4
  import { colors } from "../styles";
5
5
  const buttonStyle = {
6
6
  padding: '2px 6px',
@@ -1 +1,2 @@
1
1
  export declare const builtinComponents: import("./ComponentRegistry").Component[];
2
+ export declare function registerBuiltinComponents(): void;
@@ -15,6 +15,7 @@ import EnvironmentComponent from "./EnvironmentComponent";
15
15
  import CameraComponent from "./CameraComponent";
16
16
  import SoundComponent from "./SoundComponent";
17
17
  import DataComponent from "./DataComponent";
18
+ import { registerComponent } from "./ComponentRegistry";
18
19
  // this controls the order of components in the editor, and also which components are available to add
19
20
  export const builtinComponents = [
20
21
  TransformComponent,
@@ -38,3 +39,10 @@ export const builtinComponents = [
38
39
  DataComponent,
39
40
  PrefabRefComponent,
40
41
  ];
42
+ let didRegisterBuiltinComponents = false;
43
+ export function registerBuiltinComponents() {
44
+ if (didRegisterBuiltinComponents)
45
+ return;
46
+ builtinComponents.forEach(registerComponent);
47
+ didRegisterBuiltinComponents = true;
48
+ }
@@ -0,0 +1,4 @@
1
+ import { type Component } from "./ComponentRegistry";
2
+ declare const runtimeComponents: Component[];
3
+ export declare function registerRuntimeComponents(): void;
4
+ export { runtimeComponents };
@@ -0,0 +1,372 @@
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { Environment } from "@react-three/drei";
3
+ import { useFrame, useThree } from "@react-three/fiber";
4
+ import { lazy, Suspense, useEffect, useMemo, useRef, useState } from "react";
5
+ import { AudioListener, ClampToEdgeWrapping, DoubleSide, LinearFilter, LinearMipmapLinearFilter, Mesh, NoColorSpace, RepeatWrapping, SRGBColorSpace } from "three";
6
+ import { Text } from "three-text/three/react";
7
+ import { assetRef, assetRefs, getComponentDef, registerComponent } from "./ComponentRegistry";
8
+ import { useAssetRuntime, useNode } from "../assetRuntime";
9
+ import { gameEvents } from "../GameEvents";
10
+ import { withBasePath } from "../runtimeUtils";
11
+ const PrefabRoot = lazy(() => import("../PrefabRoot"));
12
+ const RuntimeText = Text;
13
+ function GeometryView({ properties }) {
14
+ const { geometryType } = properties;
15
+ const args = Array.isArray(properties.args) ? properties.args : [];
16
+ const geometryKey = `${geometryType !== null && geometryType !== void 0 ? geometryType : "box"}:${JSON.stringify(args)}`;
17
+ switch (geometryType) {
18
+ case "sphere":
19
+ return _jsx("sphereGeometry", { args: args }, geometryKey);
20
+ case "plane":
21
+ return _jsx("planeGeometry", { args: args }, geometryKey);
22
+ case "cylinder":
23
+ return _jsx("cylinderGeometry", { args: args }, geometryKey);
24
+ case "box":
25
+ default:
26
+ return _jsx("boxGeometry", { args: (args.length ? args : [1, 1, 1]) }, geometryKey);
27
+ }
28
+ }
29
+ const DEFAULT_TRIANGLE_POSITIONS = [0, 0, 0, 1, 0, 0, 0, 1, 0];
30
+ const DEFAULT_TRIANGLE_INDICES = [0, 1, 2];
31
+ const DEFAULT_TRIANGLE_UVS = [0, 0, 1, 0, 0, 1];
32
+ function numberArray(value, fallback) {
33
+ return Array.isArray(value) && value.every(entry => typeof entry === "number" && Number.isFinite(entry))
34
+ ? value
35
+ : fallback;
36
+ }
37
+ function indexArray(indices) {
38
+ if (indices.length === 0)
39
+ return null;
40
+ return Math.max(...indices) > 65535 ? new Uint32Array(indices) : new Uint16Array(indices);
41
+ }
42
+ function BufferGeometryView({ properties }) {
43
+ const positions = numberArray(properties.positions, DEFAULT_TRIANGLE_POSITIONS);
44
+ const indices = numberArray(properties.indices, DEFAULT_TRIANGLE_INDICES);
45
+ const normals = numberArray(properties.normals, []);
46
+ const uvs = numberArray(properties.uvs, DEFAULT_TRIANGLE_UVS);
47
+ const hasNormals = normals.length === positions.length;
48
+ const hasUvs = uvs.length >= (positions.length / 3) * 2;
49
+ const indicesArray = indexArray(indices);
50
+ return (_jsxs("bufferGeometry", { onUpdate: (geometry) => {
51
+ if (properties.computeVertexNormals !== false && !hasNormals)
52
+ geometry.computeVertexNormals();
53
+ geometry.computeBoundingBox();
54
+ geometry.computeBoundingSphere();
55
+ }, children: [_jsx("bufferAttribute", { attach: "attributes-position", args: [new Float32Array(positions), 3] }), indicesArray ? _jsx("bufferAttribute", { attach: "index", args: [indicesArray, 1] }) : null, hasNormals ? _jsx("bufferAttribute", { attach: "attributes-normal", args: [new Float32Array(normals), 3] }) : null, hasUvs ? _jsx("bufferAttribute", { attach: "attributes-uv", args: [new Float32Array(uvs), 2] }) : null] }));
56
+ }
57
+ function ModelView({ properties, children }) {
58
+ const { getModel } = useAssetRuntime();
59
+ const sourceModel = properties.filename ? getModel(properties.filename) : null;
60
+ const clonedModel = useMemo(() => {
61
+ if (!sourceModel || !properties.filename || properties.instanced)
62
+ return null;
63
+ const clone = sourceModel.clone();
64
+ clone.traverse((object) => {
65
+ if (object instanceof Mesh) {
66
+ object.castShadow = true;
67
+ object.receiveShadow = true;
68
+ }
69
+ });
70
+ return clone;
71
+ }, [properties.filename, properties.instanced, sourceModel]);
72
+ return clonedModel ? _jsx("primitive", { object: clonedModel, children: children }) : _jsx(_Fragment, { children: children });
73
+ }
74
+ function configureTexture(texture, properties, colorSpace) {
75
+ var _a, _b, _c, _d;
76
+ if (!texture)
77
+ return;
78
+ const repeat = Boolean(properties.repeat);
79
+ const repeatCount = Array.isArray(properties.repeatCount) ? properties.repeatCount : [1, 1];
80
+ const offset = Array.isArray(properties.offset) ? properties.offset : [0, 0];
81
+ texture.wrapS = texture.wrapT = repeat ? RepeatWrapping : ClampToEdgeWrapping;
82
+ texture.repeat.set(Number((_a = repeatCount[0]) !== null && _a !== void 0 ? _a : 1), Number((_b = repeatCount[1]) !== null && _b !== void 0 ? _b : 1));
83
+ texture.offset.set(Number((_c = offset[0]) !== null && _c !== void 0 ? _c : 0), Number((_d = offset[1]) !== null && _d !== void 0 ? _d : 0));
84
+ texture.colorSpace = colorSpace;
85
+ texture.generateMipmaps = properties.generateMipmaps !== false;
86
+ texture.minFilter = (properties.minFilter === "LinearFilter" ? LinearFilter : LinearMipmapLinearFilter);
87
+ texture.magFilter = LinearFilter;
88
+ texture.needsUpdate = true;
89
+ }
90
+ function useConfiguredTexture(path, properties, colorSpace) {
91
+ const { getTexture } = useAssetRuntime();
92
+ const sourceTexture = typeof path === "string" ? getTexture(path) : null;
93
+ const texture = useMemo(() => sourceTexture === null || sourceTexture === void 0 ? void 0 : sourceTexture.clone(), [sourceTexture]);
94
+ useEffect(() => {
95
+ configureTexture(texture, properties, colorSpace);
96
+ }, [properties, texture, colorSpace]);
97
+ return texture !== null && texture !== void 0 ? texture : null;
98
+ }
99
+ function MaterialView({ properties }) {
100
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
101
+ const materialType = (_a = properties.materialType) !== null && _a !== void 0 ? _a : "standard";
102
+ const colorMap = useConfiguredTexture(properties.texture, properties, SRGBColorSpace);
103
+ const normalMap = useConfiguredTexture(properties.normalMapTexture, properties, NoColorSpace);
104
+ const offsetSpeed = Array.isArray(properties.offsetSpeed) ? properties.offsetSpeed : [0, 0];
105
+ const animateOffset = Boolean(properties.animateOffset);
106
+ useFrame((_state, delta) => {
107
+ var _a, _b;
108
+ if (!animateOffset)
109
+ return;
110
+ for (const texture of [colorMap, normalMap]) {
111
+ if (!texture)
112
+ continue;
113
+ texture.offset.x += Number((_a = offsetSpeed[0]) !== null && _a !== void 0 ? _a : 0) * delta;
114
+ texture.offset.y += Number((_b = offsetSpeed[1]) !== null && _b !== void 0 ? _b : 0) * delta;
115
+ }
116
+ });
117
+ const sharedProps = {
118
+ attach: (_b = properties.attach) !== null && _b !== void 0 ? _b : "material",
119
+ color: (_c = properties.color) !== null && _c !== void 0 ? _c : "#ffffff",
120
+ opacity: (_d = properties.opacity) !== null && _d !== void 0 ? _d : 1,
121
+ transparent: Boolean(properties.transparent),
122
+ wireframe: Boolean(properties.wireframe),
123
+ toneMapped: properties.toneMapped !== false,
124
+ side: DoubleSide,
125
+ map: colorMap,
126
+ };
127
+ if (materialType === "basic")
128
+ return _jsx("meshBasicMaterial", Object.assign({}, sharedProps));
129
+ if (materialType === "sprite") {
130
+ return (_jsx("spriteMaterial", Object.assign({}, sharedProps, { rotation: (_e = properties.rotation) !== null && _e !== void 0 ? _e : 0, sizeAttenuation: properties.sizeAttenuation !== false, depthTest: properties.depthTest !== false, depthWrite: Boolean(properties.depthWrite) })));
131
+ }
132
+ const normalScale = Array.isArray(properties.normalScale) ? properties.normalScale : [1, 1];
133
+ return (_jsx("meshStandardMaterial", Object.assign({}, sharedProps, { metalness: (_f = properties.metalness) !== null && _f !== void 0 ? _f : 0, roughness: (_g = properties.roughness) !== null && _g !== void 0 ? _g : 1, normalMap: normalMap, normalScale: normalMap ? [Number((_h = normalScale[0]) !== null && _h !== void 0 ? _h : 1), Number((_j = normalScale[1]) !== null && _j !== void 0 ? _j : 1)] : undefined })));
134
+ }
135
+ function TextView({ properties, children, basePath = "" }) {
136
+ const { text = "", font, size, depth, width, align, color } = properties;
137
+ const textContent = String(text || "");
138
+ const resolvedFont = typeof font === "string" ? withBasePath(basePath, font) : undefined;
139
+ const [offset, setOffset] = useState([0, 0, 0]);
140
+ useEffect(() => {
141
+ Text.setHarfBuzzPath(withBasePath(basePath, "/fonts/hb.wasm"));
142
+ }, [basePath]);
143
+ if (!textContent)
144
+ return null;
145
+ const textProps = {
146
+ size: size,
147
+ depth: depth,
148
+ layout: { align: align, width: width },
149
+ color: color,
150
+ onLoad: (_geometry, info) => {
151
+ const bounds = info === null || info === void 0 ? void 0 : info.planeBounds;
152
+ if (!bounds)
153
+ return;
154
+ const x = align === "center" ? -(bounds.min.x + bounds.max.x) / 2 : align === "right" ? -bounds.max.x : -bounds.min.x;
155
+ setOffset([x, -(bounds.min.y + bounds.max.y) / 2, 0]);
156
+ },
157
+ };
158
+ if (resolvedFont)
159
+ textProps.font = resolvedFont;
160
+ return (_jsxs("group", { position: offset, children: [_jsx(RuntimeText, Object.assign({}, textProps, { children: textContent })), children] }));
161
+ }
162
+ function merge(defaults, properties) {
163
+ return Object.assign(Object.assign({}, defaults), properties);
164
+ }
165
+ function AmbientLightView({ properties, children }) {
166
+ const props = merge({ color: "#ffffff", intensity: 1 }, properties);
167
+ return _jsxs(_Fragment, { children: [_jsx("ambientLight", { color: props.color, intensity: props.intensity }), children] });
168
+ }
169
+ function PointLightView({ properties, children }) {
170
+ const props = merge({ color: "#ffffff", intensity: 1, distance: 0, decay: 2 }, properties);
171
+ return (_jsx("pointLight", { color: props.color, intensity: props.intensity, distance: props.distance, decay: props.decay, children: children }));
172
+ }
173
+ function DirectionalLightView({ properties, children }) {
174
+ const props = merge({ color: "#ffffff", intensity: 1, castShadow: false }, properties);
175
+ return _jsx("directionalLight", { color: props.color, intensity: props.intensity, castShadow: Boolean(props.castShadow), children: children });
176
+ }
177
+ function SpotLightView({ properties, children }) {
178
+ const props = merge({ color: "#ffffff", intensity: 1, angle: Math.PI / 3, penumbra: 0, distance: 0, decay: 2, castShadow: false }, properties);
179
+ return (_jsx("spotLight", { color: props.color, intensity: props.intensity, angle: props.angle, penumbra: props.penumbra, distance: props.distance, decay: props.decay, castShadow: Boolean(props.castShadow), children: children }));
180
+ }
181
+ function CameraView({ properties, children }) {
182
+ var _a, _b, _c;
183
+ const projection = (_a = properties.projection) !== null && _a !== void 0 ? _a : "perspective";
184
+ if (projection === "orthographic") {
185
+ return _jsx("orthographicCamera", { zoom: (_b = properties.zoom) !== null && _b !== void 0 ? _b : 1, children: children });
186
+ }
187
+ return _jsx("perspectiveCamera", { fov: (_c = properties.fov) !== null && _c !== void 0 ? _c : 50, children: children });
188
+ }
189
+ function EnvironmentView({ properties, children }) {
190
+ var _a, _b;
191
+ const { getAssetRevision } = useAssetRuntime();
192
+ const intensity = (_a = properties.intensity) !== null && _a !== void 0 ? _a : 1;
193
+ const resolution = (_b = properties.resolution) !== null && _b !== void 0 ? _b : 256;
194
+ return (_jsx(Environment, { background: true, environmentIntensity: intensity, resolution: resolution, frames: 1, children: children }, `${getAssetRevision()}::${intensity}::${resolution}`));
195
+ }
196
+ function PrefabRefView({ properties, children, basePath = "" }) {
197
+ const [loadedPrefab, setLoadedPrefab] = useState(null);
198
+ const resolvedUrl = properties.url ? withBasePath(basePath, properties.url) : "";
199
+ useEffect(() => {
200
+ if (!resolvedUrl)
201
+ return undefined;
202
+ let cancelled = false;
203
+ void fetch(resolvedUrl)
204
+ .then(response => response.json())
205
+ .then(data => {
206
+ if (!cancelled)
207
+ setLoadedPrefab(data);
208
+ })
209
+ .catch(error => console.warn("[PrefabRef] Failed to load:", resolvedUrl, error));
210
+ return () => {
211
+ cancelled = true;
212
+ };
213
+ }, [resolvedUrl]);
214
+ return (_jsxs(_Fragment, { children: [loadedPrefab ? (_jsx(Suspense, { fallback: null, children: _jsx(PrefabRoot, { data: loadedPrefab, editMode: false, basePath: basePath }) })) : null, children] }));
215
+ }
216
+ let sharedAudioListener = null;
217
+ function getSharedAudioListener() {
218
+ if (!sharedAudioListener)
219
+ sharedAudioListener = new AudioListener();
220
+ return sharedAudioListener;
221
+ }
222
+ function clampRange(min, max, fallbackMin, fallbackMax) {
223
+ const safeMin = Number.isFinite(min) ? Number(min) : fallbackMin;
224
+ const safeMax = Number.isFinite(max) ? Number(max) : fallbackMax;
225
+ return safeMin <= safeMax ? [safeMin, safeMax] : [safeMax, safeMin];
226
+ }
227
+ function sampleRange(min, max) {
228
+ return min + Math.random() * (max - min);
229
+ }
230
+ function getPitchValue(properties) {
231
+ if (properties.randomizePitch) {
232
+ const [pitchFloor, pitchCeiling] = clampRange(properties.minPitch, properties.maxPitch, 0.96, 1.04);
233
+ return sampleRange(pitchFloor, pitchCeiling);
234
+ }
235
+ return Number.isFinite(properties.pitch) ? Number(properties.pitch) : 1;
236
+ }
237
+ function getVolumeValue(properties) {
238
+ if (properties.randomizeVolume) {
239
+ const [volumeFloor, volumeCeiling] = clampRange(properties.minVolume, properties.maxVolume, 0.9, 1);
240
+ return sampleRange(volumeFloor, volumeCeiling);
241
+ }
242
+ return Number.isFinite(properties.volume) ? Number(properties.volume) : 1;
243
+ }
244
+ function resolveClipPaths({ clips, clipMode }) {
245
+ const paths = (clips !== null && clips !== void 0 ? clips : []).map(clip => clip.trim()).filter(Boolean);
246
+ return { paths, mode: paths.length > 0 ? clipMode !== null && clipMode !== void 0 ? clipMode : "random" : "single" };
247
+ }
248
+ function pickClip(paths, mode, sequenceIndexRef) {
249
+ if (paths.length <= 1 || mode === "single")
250
+ return paths[0];
251
+ if (mode === "sequence") {
252
+ const clip = paths[sequenceIndexRef.current % paths.length];
253
+ sequenceIndexRef.current += 1;
254
+ return clip;
255
+ }
256
+ return paths[Math.floor(Math.random() * paths.length)];
257
+ }
258
+ function payloadMatchesNode(nodeId, payload) {
259
+ if (!nodeId || !payload || typeof payload !== "object")
260
+ return true;
261
+ const eventPayload = payload;
262
+ const relatedNodeIds = [
263
+ eventPayload.nodeId,
264
+ eventPayload.sourceEntityId,
265
+ eventPayload.sourceNodeId,
266
+ eventPayload.targetEntityId,
267
+ eventPayload.targetNodeId,
268
+ eventPayload.instanceEntityId,
269
+ ].filter((value) => typeof value === "string");
270
+ return relatedNodeIds.length > 0 ? relatedNodeIds.includes(nodeId) : true;
271
+ }
272
+ function playBufferedAudio(audio, buffer, properties) {
273
+ void audio.listener.context.resume();
274
+ if (audio.isPlaying)
275
+ audio.stop();
276
+ audio.setBuffer(buffer);
277
+ audio.setLoop(Boolean(properties.loop));
278
+ audio.setPlaybackRate(getPitchValue(properties));
279
+ audio.setVolume(getVolumeValue(properties));
280
+ audio.play();
281
+ }
282
+ function SoundView({ properties, children }) {
283
+ const { getSound } = useAssetRuntime();
284
+ const { editMode, nodeId } = useNode();
285
+ const { camera } = useThree();
286
+ const { eventName, autoplay = false, positional = false, refDistance = 1, maxDistance = 24, rolloffFactor = 1, distanceModel = "inverse" } = properties;
287
+ const sequenceIndexRef = useRef(0);
288
+ const listenerRef = useRef(null);
289
+ const positionalAudioRef = useRef(null);
290
+ const { paths, mode } = resolveClipPaths(properties);
291
+ if (!listenerRef.current)
292
+ listenerRef.current = getSharedAudioListener();
293
+ useEffect(() => {
294
+ var _a;
295
+ const listener = listenerRef.current;
296
+ if (!listener)
297
+ return;
298
+ if (listener.parent !== camera) {
299
+ (_a = listener.parent) === null || _a === void 0 ? void 0 : _a.remove(listener);
300
+ camera.add(listener);
301
+ }
302
+ return () => {
303
+ if (listener.parent === camera)
304
+ camera.remove(listener);
305
+ };
306
+ }, [camera]);
307
+ useEffect(() => {
308
+ const audio = positionalAudioRef.current;
309
+ if (!audio)
310
+ return;
311
+ audio.setRefDistance(positional ? refDistance : Math.max(refDistance, 1));
312
+ audio.setMaxDistance(positional ? maxDistance : 1000000);
313
+ audio.setRolloffFactor(positional ? rolloffFactor : 0);
314
+ audio.setDistanceModel(positional ? distanceModel : "inverse");
315
+ }, [distanceModel, maxDistance, positional, refDistance, rolloffFactor]);
316
+ useEffect(() => {
317
+ if (editMode || paths.length === 0 || !eventName)
318
+ return;
319
+ return gameEvents.on(eventName, (payload) => {
320
+ if (!payloadMatchesNode(nodeId, payload))
321
+ return;
322
+ const clip = pickClip(paths, mode, sequenceIndexRef);
323
+ const audio = positionalAudioRef.current;
324
+ const buffer = clip ? getSound(clip) : null;
325
+ if (audio && buffer)
326
+ playBufferedAudio(audio, buffer, properties);
327
+ });
328
+ }, [editMode, eventName, getSound, mode, nodeId, paths, properties]);
329
+ useEffect(() => {
330
+ if (editMode || !autoplay || paths.length === 0)
331
+ return;
332
+ const clip = pickClip(paths, mode, sequenceIndexRef);
333
+ const audio = positionalAudioRef.current;
334
+ const buffer = clip ? getSound(clip) : null;
335
+ if (audio && buffer)
336
+ playBufferedAudio(audio, buffer, properties);
337
+ return () => {
338
+ if (audio === null || audio === void 0 ? void 0 : audio.isPlaying)
339
+ audio.stop();
340
+ };
341
+ }, [autoplay, editMode, getSound, mode, paths, properties]);
342
+ return (_jsxs(_Fragment, { children: [listenerRef.current ? _jsx("positionalAudio", { ref: positionalAudioRef, args: [listenerRef.current] }) : null, children] }));
343
+ }
344
+ const runtimeComponents = [
345
+ { name: "Transform", disableSiblingComposition: true, defaultProperties: {} },
346
+ { name: "Geometry", disableSiblingComposition: "geometry", View: GeometryView, defaultProperties: { geometryType: "box", args: [1, 1, 1], emitClickEvent: false, clickEventName: "" } },
347
+ { name: "BufferGeometry", disableSiblingComposition: "geometry", View: BufferGeometryView, defaultProperties: { positions: DEFAULT_TRIANGLE_POSITIONS, indices: DEFAULT_TRIANGLE_INDICES, normals: [], uvs: DEFAULT_TRIANGLE_UVS, groups: [], computeVertexNormals: true, emitClickEvent: false, clickEventName: "" } },
348
+ { name: "Model", View: ModelView, defaultProperties: {}, getAssetRefs: properties => assetRefs(assetRef("model", properties.filename)) },
349
+ { name: "Sprite", disableSiblingComposition: true, defaultProperties: { center: [0.5, 0.5], emitClickEvent: false, clickEventName: "node:click" } },
350
+ { name: "Text", View: TextView, defaultProperties: { text: "Hello World", color: "#ffffff", size: 1, depth: 0.05, align: "center" } },
351
+ { name: "Material", View: MaterialView, defaultProperties: { attach: "material", materialType: "standard", color: "#ffffff", toneMapped: true, wireframe: false, transparent: false, opacity: 1, sizeAttenuation: true, offset: [0, 0], animateOffset: false, offsetSpeed: [0, 0], metalness: 0, roughness: 1 }, getAssetRefs: properties => assetRefs(assetRef("texture", properties.texture), assetRef("texture", properties.normalMapTexture)) },
352
+ { name: "SpotLight", View: SpotLightView, defaultProperties: {}, getAssetRefs: properties => assetRefs(assetRef("texture", properties.map)) },
353
+ { name: "PointLight", View: PointLightView, defaultProperties: {} },
354
+ { name: "DirectionalLight", View: DirectionalLightView, defaultProperties: {} },
355
+ { name: "AmbientLight", View: AmbientLightView, defaultProperties: {} },
356
+ { name: "Environment", disableSiblingComposition: true, View: EnvironmentView, defaultProperties: {} },
357
+ { name: "Camera", disableSiblingComposition: true, View: CameraView, defaultProperties: { projection: "perspective", fov: 50, near: 0.1, zoom: 1, far: 1000 } },
358
+ { name: "Sound", View: SoundView, defaultProperties: { eventName: "", autoplay: false, loop: false, clips: [], clipMode: "single", positional: false, refDistance: 1, maxDistance: 24, rolloffFactor: 1, distanceModel: "inverse", pitch: 1, randomizePitch: false, minPitch: 0.96, maxPitch: 1.04, volume: 1, randomizeVolume: false, minVolume: 0.9, maxVolume: 1 }, getAssetRefs: (properties) => Array.isArray(properties.clips) ? properties.clips.filter((clip) => typeof clip === "string").map(path => ({ type: "sound", path })) : [] },
359
+ { name: "Data", defaultProperties: { values: {} } },
360
+ { name: "PrefabRef", View: PrefabRefView, defaultProperties: { url: "" } },
361
+ ];
362
+ let didRegisterRuntimeComponents = false;
363
+ export function registerRuntimeComponents() {
364
+ if (didRegisterRuntimeComponents)
365
+ return;
366
+ runtimeComponents.forEach(component => {
367
+ if (!getComponentDef(component.name))
368
+ registerComponent(component);
369
+ });
370
+ didRegisterRuntimeComponents = true;
371
+ }
372
+ export { runtimeComponents };
@@ -15,7 +15,7 @@ export type PrefabStoreApi = StoreApi<PrefabStoreState>;
15
15
  export declare function PrefabStoreProvider({ store, children, }: {
16
16
  store: PrefabStoreApi;
17
17
  children: ReactNode;
18
- }): import("react").FunctionComponentElement<import("react").ProviderProps<PrefabStoreApi | null>>;
18
+ }): import("react").FunctionComponentElement<import("react").FragmentProps> | import("react").FunctionComponentElement<import("react").ProviderProps<PrefabStoreApi | null>>;
19
19
  export declare function usePrefabStoreApi(): PrefabStoreApi;
20
20
  export declare function usePrefabStore<T>(selector: (state: PrefabStoreState) => T): T;
21
21
  export declare function usePrefabRootId(): string;
@@ -1,4 +1,4 @@
1
- import { createContext, createElement, useContext } from "react";
1
+ import { Fragment, createContext, createElement, useContext } from "react";
2
2
  import { subscribeWithSelector } from "zustand/middleware";
3
3
  import { useStore } from "zustand";
4
4
  import { createStore } from "zustand/vanilla";
@@ -45,6 +45,10 @@ function insertSubtreeIntoGraph(node, parentId, next) {
45
45
  addAssetRefs(next.assetRefCounts, collectSubtreeAssetRefs(node));
46
46
  }
47
47
  export function PrefabStoreProvider({ store, children, }) {
48
+ const parentStore = useContext(PrefabStoreContext);
49
+ if (parentStore === store) {
50
+ return createElement(Fragment, null, children);
51
+ }
48
52
  return createElement(PrefabStoreContext.Provider, { value: store }, children);
49
53
  }
50
54
  export function usePrefabStoreApi() {
@@ -0,0 +1,10 @@
1
+ import { Matrix4 } from "three";
2
+ export declare function isExternalPath(path: string): boolean;
3
+ export declare function withBasePath(basePath: string | undefined, path: string): string;
4
+ export declare function decompose(m: Matrix4): {
5
+ position: [number, number, number];
6
+ rotation: [number, number, number];
7
+ scale: [number, number, number];
8
+ };
9
+ /** Build a local Matrix4 from position/rotation/scale arrays. */
10
+ export declare function composeTransform(position?: [number, number, number], rotation?: [number, number, number], scale?: [number, number, number]): Matrix4;
@@ -0,0 +1,30 @@
1
+ import { Euler, Matrix4, Quaternion, Vector3 } from "three";
2
+ export function isExternalPath(path) {
3
+ return (path.startsWith("data:") ||
4
+ path.startsWith("http://") ||
5
+ path.startsWith("https://"));
6
+ }
7
+ export function withBasePath(basePath, path) {
8
+ if (!path)
9
+ return (basePath !== null && basePath !== void 0 ? basePath : "").replace(/\/$/, "");
10
+ if (isExternalPath(path))
11
+ return path;
12
+ const normalizedBasePath = (basePath !== null && basePath !== void 0 ? basePath : "").replace(/\/$/, "");
13
+ return path.startsWith("/") ? `${normalizedBasePath}${path}` : `${normalizedBasePath}/${path}`;
14
+ }
15
+ export function decompose(m) {
16
+ const p = new Vector3();
17
+ const q = new Quaternion();
18
+ const s = new Vector3();
19
+ m.decompose(p, q, s);
20
+ const e = new Euler().setFromQuaternion(q);
21
+ return {
22
+ position: [p.x, p.y, p.z],
23
+ rotation: [e.x, e.y, e.z],
24
+ scale: [s.x, s.y, s.z],
25
+ };
26
+ }
27
+ /** Build a local Matrix4 from position/rotation/scale arrays. */
28
+ export function composeTransform(position = [0, 0, 0], rotation = [0, 0, 0], scale = [1, 1, 1]) {
29
+ return new Matrix4().compose(new Vector3(...position), new Quaternion().setFromEuler(new Euler(...rotation)), new Vector3(...scale));
30
+ }
@@ -1,7 +1,6 @@
1
1
  import { GameObject, Prefab } from "./types";
2
2
  import { Matrix4, Object3D, Vector3 } from "three";
3
- export declare function isExternalPath(path: string): boolean;
4
- export declare function withBasePath(basePath: string | undefined, path: string): string;
3
+ export { composeTransform, decompose, isExternalPath, withBasePath } from "./runtimeUtils";
5
4
  export interface ExportGLBOptions {
6
5
  filename?: string;
7
6
  }
@@ -23,13 +22,6 @@ export declare function exportGLBData(sceneRoot: Object3D): Promise<ArrayBuffer>
23
22
  */
24
23
  export declare function exportGLB(sceneRoot: Object3D, options?: ExportGLBOptions): Promise<ArrayBuffer>;
25
24
  export declare function focusCameraOnObject(object: Object3D, camera: Object3D, target: Vector3, update?: () => void): void;
26
- export declare function decompose(m: Matrix4): {
27
- position: [number, number, number];
28
- rotation: [number, number, number];
29
- scale: [number, number, number];
30
- };
31
- /** Build a local Matrix4 from position/rotation/scale arrays. */
32
- export declare function composeTransform(position?: [number, number, number], rotation?: [number, number, number], scale?: [number, number, number]): Matrix4;
33
25
  /** Compute the parent world matrix for a node using the normalized store data */
34
26
  export declare function computeParentWorldMatrix(state: {
35
27
  nodesById: Record<string, {