This repository was archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathThreeDevTools.js
More file actions
275 lines (240 loc) · 7.73 KB
/
Copy pathThreeDevTools.js
File metadata and controls
275 lines (240 loc) · 7.73 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
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
export default (() => {
/**
* Supported events:
*
* `scene`
* `renderer`
*/
return class ThreeDevTools {
constructor(target) {
this.USE_RENDER_OVERLAY = false;
this.target = target;
this.scenes = new Set();
this.renderers = new Set();
this.entityCache = new EntityCache();
this.entitiesRecentlyObserved = new Set();
this.devtoolsScene = null;
this.selected = window.$t = null;
// These events are dispatched by the extension, or
// by the three.js library itself. Content could
// also listen to these events and respond accordingly.
this.target.addEventListener('observe', e => this.observe(e.detail));
this.target.addEventListener('register', e => this.register(e.detail && e.detail.revision));
this.target.addEventListener('select', e => this.select(e.detail && e.detail.uuid));
// @TODO "update" is too general -- maybe something like "set-property"?
this.target.addEventListener('entity-update', e => this.update(e.detail));
window.ctor = this.target.constructor;
// Underscored events are intended to be private.
this.target.addEventListener('_request-rendering-info', e => this.requestRenderingInfo(e.detail && e.detail.uuid));
this.target.addEventListener('_request-entity', e => this.requestEntity(e.detail && e.detail.uuid));
this.target.addEventListener('_request-overview', e => this.requestOverview(e.detail && e.detail.type));
this.target.addEventListener('_request-scene-graph', e => this.requestSceneGraph(e.detail && e.detail.uuid));
this.target.addEventListener('_transform-controls-update', e => {
if (this.devtoolsScene) {
const { space, mode } = e.detail;
// Space isn't a string, just a truthy value will trigger a toggle
if (space) {
this.devtoolsScene.toggleTransformSpace(space);
}
if (mode) {
this.devtoolsScene.setTransformMode(mode);
}
}
});
// The 'visualization-change' event is fired by DevToolsScene, indicating
// something has changed and if not rendering on a RAF loop, a render
// is necessary to render the devtools content.
// Noted here as documentation.
// this.target.addEventListener('visualization-change', () => {});
document.addEventListener('keydown', e => {
if (!this.devtoolsScene) {
return;
}
switch (e.key) {
case 'q': this.devtoolsScene.toggleTransformSpace(); break;
case 'w': this.devtoolsScene.setTransformMode('translate'); break;
case 'e': this.devtoolsScene.setTransformMode('rotate'); break;
case 'r': this.devtoolsScene.setTransformMode('scale'); break;
}
}, { passive: true })
}
/**
* API for extension, should not be called by content
*/
/**
* This is the active object in the devtools viewer.
*/
select(uuid) {
this.log('select', uuid);
const selected = this.entityCache.getEntity(uuid);
if (selected) {
if (this.devtoolsScene) {
this.devtoolsScene.selectObject(selected);
}
this.selected = window.$t = selected;
}
}
update({ uuid, property, value, dataType }) {
this.log('update', uuid, property, value, dataType);
const entity = this.entityCache.getEntity(uuid);
if (!entity) {
return;
}
const { target, key } = utils.getTargetAndKey(entity, property);
if (dataType === 'color') {
if (target[key] && target[key].isColor) {
target[key].setHex(value);
} else {
// Use our own loaded version of THREE; using just
// as a color data type, it shouldn't have any conflicts.
target[key] = new Color((value >> 16 & 255) / 255,
(value >> 8 & 255) / 255,
(value & 255) / 255);
}
}
else if (dataType === 'enum') {
target[key] = value === -1 ? null : value;
}
else {
target[key] = value;
}
}
register(revision) {
this.log('register', arguments[0]);
this.send('register', {
revision,
});
}
requestSceneGraph(uuid) {
this.log('requestSceneGraph', uuid);
try {
const data = this.entityCache.getSceneGraph(uuid);
this.send('scene-graph', {
uuid,
graph: data,
});
} catch (e) {
// Why must this be wrapped in a try/catch
// to report errors? Where's the async??
console.error(e);
}
}
requestOverview(type) {
this.log('requestOverview', type);
try {
const data = this.entityCache.getOverview(type);
this.send('overview', {
type,
entities: data,
});
} catch (e) {
// Why must this be wrapped in a try/catch
// to report errors? Where's the async??
console.error(e);
}
}
requestEntity(uuid) {
this.log('requestEntity', uuid);
try {
let data = this.entityCache.getSerializedEntity(uuid);
if (data) {
this.send('entity', data);
}
} catch (e) {
// Why must this be wrapped in a try/catch
// to report errors? Where's the async??
console.error(e);
}
}
requestRenderingInfo(uuid) {
this.log('requestRenderingInfo', uuid);
let data = this.entityCache.getRenderingInfo(uuid);
if (data) {
this.send('rendering-info', data);
}
}
observe(entity) {
this.log('observe', entity);
const uuid = this.entityCache.add(entity);
if (!uuid) {
this.warn(`${uuid} is unobservable`);
return;
}
// Fire on next tick; otherwise this is called when the scene is created,
// which won't have any objects. Will have to explore more in #18.
// Batch up multiple scenes added in the same tick.
if (this.entitiesRecentlyObserved.size === 0) {
requestAnimationFrame(() => {
this.send('observe', {
uuids: [...this.entitiesRecentlyObserved],
});
this.entitiesRecentlyObserved.clear();
});
}
this.entitiesRecentlyObserved.add(uuid);
}
send(type, data) {
this.log('emitting', type, data);
try {
window.postMessage({
id: 'three-devtools',
type: type,
data,
}, '*');
} catch (e) {
if (!data) {
throw e;
}
// If this throws, it could be because of user data not being
// able to be cloned. This will be much slower, but it will work.
console.error('Data could not be cloned; ensure "userData" is serializable.', e);
window.postMessage({
id: 'three-devtools',
type,
data: JSON.parse(JSON.stringify(data))
});
}
}
log(...message) {
if (DEBUG) {
console.log('%c ThreeDevTools:', 'color:red', ...message);
}
}
warn(...message) {
if (DEBUG) {
console.warn('%c ThreeDevTools:', 'color:red', ...message);
}
}
createDevToolsScene(renderer, camera) {
if (this.devtoolsScene) {
return this.devtoolsScene;
}
this.devtoolsScene = new DevToolsScene(this.target, renderer.domElement, camera);
return this.devtoolsScene;
}
setActiveRenderer(renderer) {
// Hide the overlay rendering unless
// enabled while some buggy cases are ironed out
if (!this.USE_RENDER_OVERLAY) {
return;
}
const render = renderer.render;
const devtools = this;
let devtoolsScene;
renderer.render = function (scene, camera) {
const target = renderer.getRenderTarget();
render.call(this, scene, camera);
if (!target) {
if (!devtoolsScene) {
devtoolsScene = devtools.createDevToolsScene(renderer, camera);
}
devtoolsScene.setCamera(camera);
const autoClear = renderer.autoClear;
renderer.autoClear = false;
render.call(renderer, devtoolsScene, camera);
renderer.autoClear = autoClear;
}
};
}
};
})();