-
Notifications
You must be signed in to change notification settings - Fork 1
/
SceneGraph.js
90 lines (73 loc) · 2.29 KB
/
SceneGraph.js
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
import * as THREE from 'three';
import Model from './Model';
import Store from '../Store';
export default class SceneGraph {
constructor() {
const { scene, tour } = Store.getState();
this.modelLookup = {}; // This will store models by their id
this.buildSceneGraph(tour.tour_data.sceneGraph);
}
buildSceneGraph(nodes) {
const { scene } = Store.getState();
nodes.forEach(node => {
if (node.type === 'model') {
const model = new Model(node, this.light);
this.modelLookup[node.id] = model; // Store the model by its id
} else if (node.type === 'group') {
const group = new THREE.Group();
group.position.set(...(node.position || [0, 0, 0]));
node.children.forEach(childNode => this.buildSceneGraph([childNode])); // Pass child node as array
scene.add(group);
this.modelLookup[node.id] = group; // Store the group by its id
}
});
}
addHelpers() {
const { scene } = Store.getState();
const size = 10; // Size of the grid (you can adjust this based on your scene scale)
const divisions = 10; // How many divisions in the grid
const gridHelper = new THREE.GridHelper(size, divisions);
scene.add(gridHelper);
const length = 5; // Length of the axes (you can adjust this based on your scene scale)
const axesHelper = new THREE.AxesHelper(length);
scene.add(axesHelper);
}
getModelById(id) {
const model = this.modelLookup[id];
return model;
}
showModelById(id) {
const model = this.modelLookup[id];
if (model) {
model.show();
} else {
console.warn(`Model with id ${id} not found.`);
}
}
hideModelById(id) {
const model = this.modelLookup[id];
if (model) {
model.hide();
} else {
console.warn(`Model with id ${id} not found.`);
}
}
// Hide all models
hideAllModels() {
Object.keys(this.modelLookup).forEach(id => {
const model = this.modelLookup[id];
if (model && typeof model.hide === 'function') {
model.hide();
}
});
}
update() {
// Update all models in the scene graph
Object.keys(this.modelLookup).forEach(id => {
const model = this.modelLookup[id];
if (model && typeof model.update === 'function') {
model.update();
}
});
}
}