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 pathEntityCache.js
More file actions
332 lines (289 loc) · 8.98 KB
/
Copy pathEntityCache.js
File metadata and controls
332 lines (289 loc) · 8.98 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
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
export default (() => {
const PATCHED = '__SERIALIZATION_PATCHED__';
return class EntityCache extends EventTarget {
constructor() {
super();
this.scenes = new Set();
this.renderers = [];
this.entityMap = new Map();
// Map of uuid to version, if any, of a large
// value stored in resources, like images and buffers.
this.resourcesSent = new Map();
this.resources = {
images: {},
// The following values are custom to devtools
// resource serializations.
attributes: {},
// The resources `meta` object is passed to
// toJSON methods, and the only way to reliably
// propagate configuration throughout serialization.
devtoolsConfig: {},
};
}
getEntity(id) {
return this.entityMap.get(id);
}
/**
* Adds a renderer or scene to be registered and observed
* to iterate over in the future when looking for resources.
*
* @param {THREE.Scene | THREE.WebGLRenderer} entity
* @return {String} returns the ID of the entity if and only if it was added.
*/
add(entity) {
if (!entity || utils.isHiddenFromTools(entity)) {
//console.error('ThreeDevTools#observe must have event detail.');
return;
}
const id = this._getID(entity);
if (!id) {
return;
}
if (entity.isScene) {
this.scenes.add(entity);
this._registerEntity(entity);
} else if (typeof entity.render === 'function') {
this.entityMap.set(id, entity);
} else {
throw new Error('May only observe scenes and renderers currently.');
}
return id;
}
getSceneGraph(uuid) {
const graph = {}
const scene = this.getEntity(uuid);
const objects = [scene];
while (objects.length) {
const object = objects.shift();
this._registerEntity(object);
graph[object.uuid] = {
uuid: object.uuid,
name: object.name,
baseType: utils.getBaseType(object),
children: [],
};
if (object.parent) {
graph[object.parent.uuid].children.push(object.uuid);
}
if (object.children) {
objects.push(...object.children);
}
}
return graph;
}
/**
* Returns the associated resources that have
* already been discovered of corresponding type.
*
* @param {String} type can be "scenes", "geometries", "materials", "textures"
*/
getOverview(type) {
const entities = [];
const entitiesAdded = new Set();
for (let scene of this.scenes) {
if (type === 'scenes') {
addEntity(scene);
} else {
utils.forEachDependency(scene, entity => {
this._registerEntity(entity);
const valid = type === 'geometries' ? (entity.isGeometry || entity.isBufferGeometry) :
type === 'materials' ? entity.isMaterial :
type === 'textures' ? entity.isTexture : false;
if (valid && !entitiesAdded.has(entity.uuid)) {
addEntity(entity);
}
}, {
recursive: true,
});
}
}
function addEntity(entity) {
entities.push({
name: entity.name,
uuid: entity.uuid,
baseType: utils.getBaseType(entity),
});
entitiesAdded.add(entity.uuid);
}
return entities;
}
getRenderingInfo(id) {
const entity = this.getEntity(id);
if (!entity || !/renderer/.test(id)) {
return;
}
return {
type: 'renderer',
uuid: id,
info: {
render: entity.info.render,
memory: entity.info.memory,
programs: entity.info.programs.length,
}
};
}
getSerializedEntity(id) {
const entity = this.getEntity(id);
if (!entity) {
return;
}
if (/renderer/.test(id)) {
const data = InstrumentedToJSON.call(entity);
data.type = 'renderer';
data.uuid = id;
return data;
}
// The observe call for our own internal scene
// fires as soon as the devtools scene superclass
// is created, before any signifiers can be added
// by the inheriting class. Luckily(?) there is a tick
// after observing an object and when it refreshes.
// Check here to see if the internal scene should
// be removed
if (utils.isHiddenFromTools(entity)) {
this.entityMap.delete(id);
return;
}
// Cache attribute and image generation if possible
const meta = {
geometries: [],
materials: [],
textures: [],
shapes: [],
images: this.resources.images,
// @TODO Right now, the InstrumentedToJSON doesn't
// stop the overhead of slicing a large array, like
// textures do with images that have already been serialized.
attributes: this.resources.attributes,
devtoolsConfig: {
serializeChildren: !entity.isObject3D,
},
}
// Register all dependencies, as they may not have
// been patched with a custom toJSON yet.
utils.forEachDependency(entity, dep => {
this._registerEntity(dep);
});
let entitiesAdded = new Set();
let serializedEntity = this._serializeEntity(entity, meta);
let entities = [serializedEntity];
entitiesAdded.add(serializedEntity.uuid);
this._postSerialization(meta);
// Accumulate serialized dependencies.
for (let resourceType of ['geometries', 'materials', 'textures', 'shapes']) {
for (let resource of Object.values(meta[resourceType])) {
if (!entitiesAdded.has(resource.uuid)) {
entities.push(resource);
entitiesAdded.add(resource.uuid);
}
}
}
// Only add the stored resources of images and attributes
// if they haven't been sent over the wire yet
for (let resourceType of ['images', 'attributes']) {
const resources = this.resources[resourceType];
for (let uuid of Object.keys(resources)) {
if (!this.resourcesSent.has(uuid)) {
const entity = resources[uuid];
this.resourcesSent.set(uuid, entity.version);
entities.push(entity);
}
}
}
return entities;
}
/**
* Patches the `toJSON` method on objects that either:
* 1) do not have the method
* 2) returns insufficient information
* 3) throws an error on serialization
*
* /!\ This may destructively modify an entity's toJSON method. /!\
*/
_patchToJSON(entity) {
// Patch BufferGeometry's InterleavedBufferAttributes
// since it does not have its own toJSON method.
// Attributes can be added and removed as well,
// so check everytime.
// Note that attribute's `toJSON` does *not*
// receive the meta resource object so it's not
// possible to customize with config.
// https://github.com/mrdoob/three.js/pull/17328
if (entity.isBufferGeometry) {
for (let key of Object.keys(entity.attributes)) {
const attr = entity.attributes[key];
if (attr.isInterleavedBufferAttribute) {
this._patchToJSON(attr);
}
}
}
if (!entity[PATCHED]) {
// via `src/content/toJSON.js`
entity.toJSON = InstrumentedToJSON;
entity[PATCHED] = true;
}
}
/**
* This turns a Three entity into something serializable.
* Mostly the built-in 'toJSON()' method with cached metadata.
*
* @param {*} entity
* @param {Object} options
* @param {Boolean} options.serializeChildren [true]
*/
_serializeEntity(entity, meta={}) {
let json;
try {
//console.time('toJSON-'+entity.uuid);
json = entity.toJSON(meta);
//console.timeEnd('toJSON-'+entity.uuid);
} catch (e) {
// If this throws, it could be because of some object not being serializable.
// @TODO handle this, throw it for now.
console.error(`${entity.uuid} does not appear to be serializable.`, e);
}
return json && json.object ? json.object : json;
}
/**
* Tag an entity to be recorded as a resource, and patch
* any necessary methods. Only executed once per entity.
*/
_registerEntity(entity) {
const { uuid } = entity;
if (uuid && !this.entityMap.has(uuid)) {
this._patchToJSON(entity);
this.entityMap.set(uuid, entity);
}
}
/**
* Run after collecting resources. Ensure that this
* is executed after merging a newly serialized object
* back into the resources.
*/
_postSerialization(data) {
// Moves all Geometry attributes to their own category,
// like textures do with images, so that they don't
// clog up the data transfer.
for (let geo of Object.values(data.geometries)) {
if (geo.data) {
const id = `attrs-${geo.uuid}`;
data.attributes[id] = geo.data;
delete geo.data;
}
}
}
_getID(entity) {
// Store any observed renderer so IDs can be synthesized.
if (typeof entity.render === 'function') {
let rendererIndex = this.renderers.indexOf(entity);
if (rendererIndex === -1) {
rendererIndex = this.renderers.length;
this.renderers.push(entity);
}
return `renderer-${rendererIndex}`;
} else if (entity.uuid) {
return entity.uuid;
}
}
};
});