-
Notifications
You must be signed in to change notification settings - Fork 0
/
useSceneModifier.ts
365 lines (298 loc) · 9.76 KB
/
useSceneModifier.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import { Registry, Vec3, Vec4 } from '@behave-graph/core';
import { ObjectMap } from '@react-three/fiber';
import { Dispatch, SetStateAction, useCallback, useEffect, useState } from 'react';
import { Event, Material, MeshBasicMaterial, Object3D, Quaternion, Vector3, Vector4 } from 'three';
import { GLTF } from 'three-stdlib';
import { ISceneWithQueries, Properties, ResourceTypes } from '../abstractions';
import { registerSharedSceneProfiles, registerSpecificSceneProfiles } from '../hooks/profiles';
import { GLTFJson } from './GLTFJson';
function toVec3(value: Vector3): Vec3 {
return new Vec3(value.x, value.y, value.z);
}
function toVec4(value: Vector4 | Quaternion): Vec4 {
return new Vec4(value.x, value.y, value.z, value.w);
}
const shortPathRegEx = /^\/?(?<resource>[^/]+)\/(?<index>\d+)$/;
const jsonPathRegEx = /^\/?(?<resource>[^/]+)\/(?<index>\d+)\/(?<property>[^/]+)$/;
export type Optional<T> = {
[K in keyof T]: T[K] | undefined;
};
export type Path = {
resource: ResourceTypes;
index: number;
property: string;
};
export function toJsonPathString({ index, property, resource: resourceType }: Optional<Path>, short: boolean) {
if (short) {
if (!resourceType || typeof index === undefined) return;
return `${resourceType}/${index}`;
} else {
if (!resourceType || typeof index === undefined || !property) return;
return `${resourceType}/${index}/${property}`;
}
}
export function parseJsonPath(jsonPath: string, short = false): Path {
// hack = for now we see if there are 2 segments to know if its short
const regex = short ? shortPathRegEx : jsonPathRegEx;
const matches = regex.exec(jsonPath);
if (matches === null) throw new Error(`can not parse jsonPath: ${jsonPath}`);
if (matches.groups === undefined) throw new Error(`can not parse jsonPath (no groups): ${jsonPath}`);
return {
resource: matches.groups.resource as ResourceTypes,
index: +matches.groups.index,
property: matches.groups.property,
};
}
function applyPropertyToModel(
{ resource, index, property }: Path,
gltf: GLTF & ObjectMap,
value: any,
properties: Properties,
setActiveAnimations: (animation: string, active: boolean) => void
) {
const nodeName = getResourceName({ resource, index }, properties);
if (!nodeName) throw new Error(`could not get node at index ${index}`);
if (resource === 'nodes') {
const node = gltf.nodes[nodeName] as unknown as Object3D | undefined;
if (!node) {
console.error(`no node at path ${nodeName}`);
return;
}
applyNodeModifier(property, node, value);
return;
}
if (resource === 'materials') {
const node = gltf.materials[nodeName] as unknown as Material | undefined;
if (!node) {
console.error(`no node at path ${nodeName}`);
return;
}
applyMaterialModifier(property, node, value);
return;
}
if (resource === 'animations') {
setActiveAnimations(nodeName, value as boolean);
return;
}
console.error(`unknown resource type ${resource}`);
}
const getResourceName = ({ resource, index }: Pick<Path, 'resource' | 'index'>, properties: Properties) => {
return properties[resource]?.options[index].name;
};
const getPropertyFromModel = ({ resource, index, property }: Path, gltf: GLTF & ObjectMap, properties: Properties) => {
if (resource === 'nodes') {
const nodeName = getResourceName({ resource, index }, properties);
if (!nodeName) throw new Error(`could not get node at index ${index}`);
const node = gltf.nodes[nodeName] as unknown as Object3D | undefined;
if (!node) {
console.error(`no node at path ${nodeName}`);
return;
}
getPropertyValue(property, node);
return;
}
};
function applyNodeModifier(property: string, objectRef: Object3D, value: any) {
switch (property) {
case 'visible': {
objectRef.visible = value as boolean;
break;
}
case 'translation': {
const v = value as Vec3;
objectRef.position.set(v.x, v.y, v.z);
break;
}
case 'scale': {
const v = value as Vec3;
objectRef.scale.set(v.x, v.y, v.z);
break;
}
case 'rotation': {
const v = value as Vec4;
objectRef.quaternion.set(v.x, v.y, v.z, v.w);
break;
}
}
}
function applyMaterialModifier(property: string, materialRef: Material, value: any) {
switch (property) {
case 'color': {
const basic = materialRef as MeshBasicMaterial;
if (basic.color) {
const v = value as Vec3;
basic.color.setRGB(v.x, v.y, v.z);
basic.needsUpdate = true;
}
break;
}
}
}
function getPropertyValue(property: string, objectRef: Object3D<Event>) {
switch (property) {
case 'visible': {
return objectRef.visible;
}
case 'translation': {
return toVec3(objectRef.position);
}
case 'scale': {
return toVec3(objectRef.scale);
}
case 'rotation': {
return toVec4(objectRef.quaternion);
}
default:
throw new Error(`unrecognized property: ${property}`);
}
}
const extractProperties = (gltf: GLTF): Properties => {
const nodeProperties = ['visible', 'translation', 'scale', 'rotation', 'color'];
const animationProperties = ['playing'];
const materialProperties = ['color'];
const gltfJson = gltf.parser.json as GLTFJson;
const nodeOptions = gltfJson.nodes?.map(({ name }, index) => ({
name: name || index.toString(),
index,
}));
const materialOptions = gltfJson.materials?.map(({ name }, index) => ({
name: name || index.toString(),
index,
}));
const animationOptions = gltf.animations?.map(({ name }, index) => ({
name: name || index.toString(),
index,
}));
const properties: Properties = {};
if (nodeOptions) {
properties.nodes = { options: nodeOptions, properties: nodeProperties };
}
if (materialOptions) {
properties.materials = {
options: materialOptions,
properties: materialProperties,
};
}
if (animationOptions) {
properties.animations = {
options: animationOptions,
properties: animationProperties,
};
}
return properties;
};
export type OnClickCallback = (jsonPath: string) => void;
export type OnClickListener = {
path: Path;
elementName: string;
callbacks: OnClickCallback[];
};
export type OnClickListeners = {
[jsonPath: string]: OnClickListener;
};
const buildSceneModifier = (
gltf: GLTF & ObjectMap,
setOnClickListeners: Dispatch<SetStateAction<OnClickListeners>>,
setActiveAnimations: (animation: string, active: boolean) => void
) => {
const properties = extractProperties(gltf);
const addOnClickedListener = (jsonPath: string, callback: (jsonPath: string) => void) => {
const path = parseJsonPath(jsonPath, true);
setOnClickListeners((existing) => {
const listenersForPath = existing[jsonPath] || {
path,
elementName: getResourceName({ resource: path.resource, index: path.index }, properties),
callbacks: [],
};
const updatedListeners: OnClickListener = {
...listenersForPath,
callbacks: [...listenersForPath.callbacks, callback],
};
const result: OnClickListeners = {
...existing,
[jsonPath]: updatedListeners,
};
return result;
});
};
const removeOnClickedListener = (jsonPath: string, callback: (jsonPath: string) => void) => {
setOnClickListeners((existing) => {
const listenersForPath = existing[jsonPath];
if (!listenersForPath) return existing;
const updatedCallbacks = listenersForPath.callbacks.filter((x) => x !== callback);
if (updatedCallbacks.length > 0) {
const updatedListeners = {
...listenersForPath,
callback: updatedCallbacks,
};
return {
...existing,
[jsonPath]: updatedListeners,
};
}
const result = {
...existing,
};
delete result[jsonPath];
return result;
});
};
const getProperty = (jsonPath: string, valueTypeName: string) => {
const path = parseJsonPath(jsonPath);
return getPropertyFromModel(path, gltf, properties);
};
const setProperty = (jsonPath: string, valueTypeName: string, value: any) => {
const path = parseJsonPath(jsonPath);
applyPropertyToModel(path, gltf, value, properties, setActiveAnimations);
};
const getProperties = () => properties;
const scene: ISceneWithQueries = {
getProperty,
setProperty,
getProperties,
addOnClickedListener,
removeOnClickedListener,
};
return scene;
};
export type AnimationsState = { [key: string]: boolean };
const useSceneModifier = (gltf: (GLTF & ObjectMap) | undefined) => {
const [scene, setScene] = useState<ISceneWithQueries>();
const [activeAnimations, setActiveAnimations] = useState<AnimationsState>({});
const [sceneOnClickListeners, setSceneOnClickListeners] = useState<OnClickListeners>({});
useEffect(() => {
// reset state on new active animations
setActiveAnimations({});
}, [gltf]);
const setAnimationActive = useCallback((animation: string, active: boolean) => {
setActiveAnimations((existing) => {
if (!!existing[animation] === active) return existing;
return {
...existing,
[animation]: active,
};
});
}, []);
useEffect(() => {
if (!gltf) {
setScene(undefined);
} else {
setScene(buildSceneModifier(gltf, setSceneOnClickListeners, setAnimationActive));
}
}, [gltf, setSceneOnClickListeners, setAnimationActive]);
const registerProfile = useCallback(
(registry: Registry) => {
if (!scene) return;
registerSharedSceneProfiles(registry, scene);
registerSpecificSceneProfiles(registry, scene);
},
[scene]
);
return {
scene,
animations: activeAnimations,
sceneOnClickListeners,
registerSceneProfile: registerProfile,
};
};
export default useSceneModifier;