-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyContents.js
More file actions
141 lines (114 loc) · 3.61 KB
/
MyContents.js
File metadata and controls
141 lines (114 loc) · 3.61 KB
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
import { MyAxis } from './MyAxis.js';
import { MyFileReader } from './parser/MyFileReader.js';
import { MyYASFParser } from './parser/MyYASFParser.js';
/**
* This class contains the contents of out application
*/
class MyContents {
/**
constructs the object
@param {MyApp} app The application object
*/
constructor(app) {
this.app = app
this.axis = null
this.parser = new MyYASFParser(this.app.scene);
this.reader = new MyFileReader(this.onSceneLoaded.bind(this));
this.reader.open("scenes/scene.json");
this.objects = [];
this.lights = [];
}
/**
* initializes the contents
*/
init() {
// create once
if (this.axis === null) {
// create and attach the axis to the scene
this.axis = new MyAxis(this)
this.app.scene.add(this.axis)
}
}
/**
* Called when the scene JSON file load is completed
* @param {Object} data with the entire scene object
*/
async onSceneLoaded(data) {
console.info("YASF loaded.")
await this.parser.parse(data);
this.addGlobals();
this.addCameras();
this.addLights();
this.addObjects();
// this.onAfterSceneLoadedAndBeforeRender(data)
}
printYASF(data, indent = '') {
for (let key in data) {
if (typeof data[key] === 'object' && data[key] !== null) {
console.log(`${indent}${key}:`);
this.printYASF(data[key], indent + '\t');
} else {
console.log(`${indent}${key}: ${data[key]}`);
}
}
}
onAfterSceneLoadedAndBeforeRender(data) {
this.printYASF(data)
}
addGlobals() {
if (this.parser.globals.background) {
this.app.scene.background = this.parser.globals.background;
}
if (this.parser.globals.ambient) {
this.app.scene.add(this.parser.globals.ambient);
}
if (this.parser.globals.fog) {
this.app.scene.fog = this.parser.globals.fog;
}
if (this.parser.globals.skybox) {
this.app.scene.add(this.parser.globals.skybox);
}
}
addCameras() {
this.app.cameras = this.parser.cameras;
this.app.setActiveCamera(this.parser.initialCameraName || Object.keys(this.app.cameras)[0]);
this.app.gui.updateCameraOptions();
}
addLights() {
if (this.parser.lights) {
this.parser.lights.forEach(light => {
this.app.scene.add(light);
this.lights.push(light);
});
this.app.gui.updateLights();
}
}
addObjects() {
if (this.parser.objects) {
const root = this.parser.objects[this.parser.rootid];
if (root) {
this.app.scene.add(root);
this.objects.push(root);
this.app.gui.updateObjects();
}
}
}
toggleWireframe(enableWireframe) {
this.app.scene.traverse(node => {
if (node.isMesh && node.material) {
if (Array.isArray(node.material)) {
node.material.forEach(material => {
material.wireframe = enableWireframe;
material.needsUpdate = true;
});
} else {
node.material.wireframe = enableWireframe;
node.material.needsUpdate = true;
}
}
});
}
update() {
}
}
export { MyContents };