react-three-game 0.0.84 → 0.0.86
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.
- package/README.md +110 -36
- package/dist/index.d.ts +3 -7
- package/dist/index.js +1 -4
- package/dist/tools/prefabeditor/InstanceProvider.d.ts +0 -4
- package/dist/tools/prefabeditor/InstanceProvider.js +13 -44
- package/dist/tools/prefabeditor/PrefabEditor.d.ts +7 -2
- package/dist/tools/prefabeditor/PrefabEditor.js +13 -24
- package/dist/tools/prefabeditor/PrefabRoot.js +99 -48
- package/dist/tools/prefabeditor/{runtime.d.ts → assetRuntime.d.ts} +0 -25
- package/dist/tools/prefabeditor/assetRuntime.js +37 -0
- package/dist/tools/prefabeditor/components/BufferGeometryComponent.d.ts +3 -0
- package/dist/tools/prefabeditor/components/BufferGeometryComponent.js +98 -0
- package/dist/tools/prefabeditor/components/CameraComponent.js +1 -1
- package/dist/tools/prefabeditor/components/ComponentRegistry.d.ts +0 -3
- package/dist/tools/prefabeditor/components/DataComponent.d.ts +3 -0
- package/dist/tools/prefabeditor/components/DataComponent.js +87 -0
- package/dist/tools/prefabeditor/components/DirectionalLightComponent.js +1 -1
- package/dist/tools/prefabeditor/components/EnvironmentComponent.js +1 -1
- package/dist/tools/prefabeditor/components/GeometryComponent.js +4 -2
- package/dist/tools/prefabeditor/components/Input.d.ts +2 -13
- package/dist/tools/prefabeditor/components/Input.js +0 -55
- package/dist/tools/prefabeditor/components/MaterialComponent.d.ts +3 -0
- package/dist/tools/prefabeditor/components/MaterialComponent.js +49 -18
- package/dist/tools/prefabeditor/components/ModelComponent.js +3 -3
- package/dist/tools/prefabeditor/components/PhysicsComponent.d.ts +4 -0
- package/dist/tools/prefabeditor/components/PhysicsComponent.js +69 -132
- package/dist/tools/prefabeditor/components/PointLightComponent.js +1 -1
- package/dist/tools/prefabeditor/components/SoundComponent.js +17 -17
- package/dist/tools/prefabeditor/components/SpotLightComponent.js +1 -1
- package/dist/tools/prefabeditor/components/index.js +10 -8
- package/dist/tools/prefabeditor/types.d.ts +1 -0
- package/dist/tools/prefabeditor/types.js +18 -0
- package/dist/tools/prefabeditor/usePointerEvents.d.ts +27 -0
- package/dist/tools/prefabeditor/usePointerEvents.js +52 -0
- package/package.json +1 -1
- package/dist/tools/prefabeditor/GameEvents.d.ts +0 -128
- package/dist/tools/prefabeditor/GameEvents.js +0 -118
- package/dist/tools/prefabeditor/components/ClickComponent.d.ts +0 -3
- package/dist/tools/prefabeditor/components/ClickComponent.js +0 -52
- package/dist/tools/prefabeditor/runtime.js +0 -184
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
import { createContext, useContext, useMemo } from "react";
|
|
3
|
-
import { getComponentDef } from "./components/ComponentRegistry";
|
|
4
|
-
import { createScene } from "./scene";
|
|
5
|
-
export const AssetRuntimeContext = createContext(null);
|
|
6
|
-
const EntityRuntimeContext = createContext(null);
|
|
7
|
-
export function useAssetRuntime() {
|
|
8
|
-
const ctx = useContext(AssetRuntimeContext);
|
|
9
|
-
if (!ctx)
|
|
10
|
-
throw new Error("useAssetRuntime must be used inside <PrefabRoot>");
|
|
11
|
-
return ctx;
|
|
12
|
-
}
|
|
13
|
-
export function useEntityRuntime() {
|
|
14
|
-
const ctx = useContext(EntityRuntimeContext);
|
|
15
|
-
if (!ctx)
|
|
16
|
-
throw new Error("useEntityRuntime must be used inside a component View rendered by <PrefabRoot>");
|
|
17
|
-
return ctx;
|
|
18
|
-
}
|
|
19
|
-
export function useEntityObjectRef() {
|
|
20
|
-
const { getObject } = useEntityRuntime();
|
|
21
|
-
return useMemo(() => ({ get current() { return getObject(); } }), [getObject]);
|
|
22
|
-
}
|
|
23
|
-
export function useEntityRigidBodyRef() {
|
|
24
|
-
const { getRigidBody } = useEntityRuntime();
|
|
25
|
-
return useMemo(() => ({ get current() { return getRigidBody(); } }), [getRigidBody]);
|
|
26
|
-
}
|
|
27
|
-
export function EntityRuntimeScope({ nodeId, editMode, isSelected, children, }) {
|
|
28
|
-
const asset = useContext(AssetRuntimeContext);
|
|
29
|
-
if (!asset)
|
|
30
|
-
throw new Error("EntityRuntimeScope must be used inside <PrefabRoot>");
|
|
31
|
-
const value = useMemo(() => ({
|
|
32
|
-
nodeId,
|
|
33
|
-
editMode,
|
|
34
|
-
isSelected,
|
|
35
|
-
getObject: () => asset.getObject(nodeId),
|
|
36
|
-
getRigidBody: () => asset.getRigidBody(nodeId),
|
|
37
|
-
}), [asset, editMode, isSelected, nodeId]);
|
|
38
|
-
return _jsx(EntityRuntimeContext.Provider, { value: value, children: children });
|
|
39
|
-
}
|
|
40
|
-
export function createRuntimeEngine({ store, getObject, getRigidBody, }) {
|
|
41
|
-
const scene = createScene({
|
|
42
|
-
getRootId: () => store.getState().rootId,
|
|
43
|
-
getNode: (id) => { var _a; return (_a = store.getState().nodesById[id]) !== null && _a !== void 0 ? _a : null; },
|
|
44
|
-
getChildIds: (id) => { var _a; return (_a = store.getState().childIdsById[id]) !== null && _a !== void 0 ? _a : []; },
|
|
45
|
-
getParentId: (id) => { var _a; return (_a = store.getState().parentIdById[id]) !== null && _a !== void 0 ? _a : null; },
|
|
46
|
-
updateNode: (id, update) => store.getState().updateNode(id, update),
|
|
47
|
-
updateNodes: (updates) => {
|
|
48
|
-
store.getState().updateNodes(Object.entries(updates).map(([id, update]) => ({ id, update })));
|
|
49
|
-
},
|
|
50
|
-
addNode: (node, options) => {
|
|
51
|
-
var _a;
|
|
52
|
-
const parentId = (_a = options === null || options === void 0 ? void 0 : options.parentId) !== null && _a !== void 0 ? _a : store.getState().rootId;
|
|
53
|
-
store.getState().addChild(parentId, node);
|
|
54
|
-
return node.id;
|
|
55
|
-
},
|
|
56
|
-
removeNode: (id) => store.getState().deleteNode(id),
|
|
57
|
-
getObject,
|
|
58
|
-
getRigidBody,
|
|
59
|
-
});
|
|
60
|
-
// key = `${nodeId}:${componentKey}:${componentType}`
|
|
61
|
-
const instances = new Map();
|
|
62
|
-
let active = false;
|
|
63
|
-
let dirty = true;
|
|
64
|
-
let lastRevision = store.getState().revision;
|
|
65
|
-
const unsubscribe = store.subscribe((state) => {
|
|
66
|
-
if (state.revision === lastRevision)
|
|
67
|
-
return;
|
|
68
|
-
lastRevision = state.revision;
|
|
69
|
-
dirty = true;
|
|
70
|
-
});
|
|
71
|
-
function destroy(key) {
|
|
72
|
-
var _a, _b;
|
|
73
|
-
const record = instances.get(key);
|
|
74
|
-
if (!record)
|
|
75
|
-
return;
|
|
76
|
-
try {
|
|
77
|
-
(_b = (_a = record.instance).destroy) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
78
|
-
}
|
|
79
|
-
catch (error) {
|
|
80
|
-
console.error(`[runtime] destroy ${key}`, error);
|
|
81
|
-
}
|
|
82
|
-
instances.delete(key);
|
|
83
|
-
}
|
|
84
|
-
function sync() {
|
|
85
|
-
const state = store.getState();
|
|
86
|
-
const live = new Set();
|
|
87
|
-
let hasPendingMount = false;
|
|
88
|
-
const visit = (nodeId) => {
|
|
89
|
-
var _a, _b, _c;
|
|
90
|
-
const node = state.nodesById[nodeId];
|
|
91
|
-
if (!node)
|
|
92
|
-
return;
|
|
93
|
-
if (!node.disabled && node.components) {
|
|
94
|
-
for (const componentKey in node.components) {
|
|
95
|
-
const data = node.components[componentKey];
|
|
96
|
-
if (!(data === null || data === void 0 ? void 0 : data.type))
|
|
97
|
-
continue;
|
|
98
|
-
const def = getComponentDef(data.type);
|
|
99
|
-
if (!(def === null || def === void 0 ? void 0 : def.create))
|
|
100
|
-
continue;
|
|
101
|
-
const key = `${nodeId}:${componentKey}:${data.type}`;
|
|
102
|
-
live.add(key);
|
|
103
|
-
const object = getObject(nodeId);
|
|
104
|
-
if (!object) {
|
|
105
|
-
hasPendingMount = true;
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
const rigidBody = getRigidBody(nodeId);
|
|
109
|
-
const existing = instances.get(key);
|
|
110
|
-
if (existing && existing.object === object && existing.rigidBody === rigidBody) {
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
if (existing) {
|
|
114
|
-
destroy(key);
|
|
115
|
-
}
|
|
116
|
-
const entity = scene.find(nodeId);
|
|
117
|
-
const component = entity === null || entity === void 0 ? void 0 : entity.getComponent(componentKey);
|
|
118
|
-
if (!entity || !component)
|
|
119
|
-
continue;
|
|
120
|
-
let instance;
|
|
121
|
-
try {
|
|
122
|
-
instance = (_a = def.create({ scene, component, object, rigidBody })) !== null && _a !== void 0 ? _a : {};
|
|
123
|
-
}
|
|
124
|
-
catch (error) {
|
|
125
|
-
console.error(`[runtime] create ${key}`, error);
|
|
126
|
-
continue;
|
|
127
|
-
}
|
|
128
|
-
instances.set(key, { instance, object, rigidBody });
|
|
129
|
-
try {
|
|
130
|
-
(_b = instance.start) === null || _b === void 0 ? void 0 : _b.call(instance);
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
console.error(`[runtime] start ${key}`, error);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
for (const childId of (_c = state.childIdsById[nodeId]) !== null && _c !== void 0 ? _c : [])
|
|
138
|
-
visit(childId);
|
|
139
|
-
};
|
|
140
|
-
visit(state.rootId);
|
|
141
|
-
for (const key of Array.from(instances.keys())) {
|
|
142
|
-
if (!live.has(key))
|
|
143
|
-
destroy(key);
|
|
144
|
-
}
|
|
145
|
-
dirty = hasPendingMount;
|
|
146
|
-
}
|
|
147
|
-
return {
|
|
148
|
-
tick(dt) {
|
|
149
|
-
if (!active)
|
|
150
|
-
return;
|
|
151
|
-
if (dirty)
|
|
152
|
-
sync();
|
|
153
|
-
for (const [key, record] of instances) {
|
|
154
|
-
if (!record.instance.update)
|
|
155
|
-
continue;
|
|
156
|
-
try {
|
|
157
|
-
record.instance.update(dt);
|
|
158
|
-
}
|
|
159
|
-
catch (error) {
|
|
160
|
-
console.error(`[runtime] update ${key}`, error);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
},
|
|
164
|
-
setActive(nextActive) {
|
|
165
|
-
if (active === nextActive)
|
|
166
|
-
return;
|
|
167
|
-
active = nextActive;
|
|
168
|
-
dirty = true;
|
|
169
|
-
if (!active) {
|
|
170
|
-
for (const key of Array.from(instances.keys()))
|
|
171
|
-
destroy(key);
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
invalidate() {
|
|
175
|
-
dirty = true;
|
|
176
|
-
},
|
|
177
|
-
dispose() {
|
|
178
|
-
active = false;
|
|
179
|
-
for (const key of Array.from(instances.keys()))
|
|
180
|
-
destroy(key);
|
|
181
|
-
unsubscribe();
|
|
182
|
-
},
|
|
183
|
-
};
|
|
184
|
-
}
|