@playcanvas/inspector 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +110 -0
  3. package/package.json +62 -0
  4. package/src/asset-usage.js +108 -0
  5. package/src/asset-view.js +370 -0
  6. package/src/camera-view.js +128 -0
  7. package/src/debug.js +31 -0
  8. package/src/describe.js +302 -0
  9. package/src/entity-inspector-script.js +130 -0
  10. package/src/frame-graph-view.js +767 -0
  11. package/src/hierarchy-view.js +475 -0
  12. package/src/index.js +4 -0
  13. package/src/input/fly-controller.js +116 -0
  14. package/src/input/input.js +299 -0
  15. package/src/input/math.js +8 -0
  16. package/src/input/orbit-controller.js +158 -0
  17. package/src/input/pose.js +207 -0
  18. package/src/inspector.js +3293 -0
  19. package/src/instance-survey.js +50 -0
  20. package/src/list-view.js +410 -0
  21. package/src/material-view.js +197 -0
  22. package/src/memory-view.js +344 -0
  23. package/src/mesh-view.js +235 -0
  24. package/src/model.js +170 -0
  25. package/src/node-model.js +404 -0
  26. package/src/physics-debug.js +527 -0
  27. package/src/physics-view.js +314 -0
  28. package/src/picker/picker.js +545 -0
  29. package/src/picker/render-pass-picker.js +206 -0
  30. package/src/property-view.js +628 -0
  31. package/src/render-target-view.js +288 -0
  32. package/src/renderers/texture-renderer-shaders.js +139 -0
  33. package/src/renderers/texture-renderer.js +430 -0
  34. package/src/renderers/wire-renderer.js +954 -0
  35. package/src/script-view.js +288 -0
  36. package/src/shader-view.js +287 -0
  37. package/src/styles.js +918 -0
  38. package/src/texture-view.js +186 -0
  39. package/src/tooltip.js +72 -0
  40. package/src/viewport-tools.js +695 -0
  41. package/types/asset-usage.d.ts +23 -0
  42. package/types/asset-view.d.ts +94 -0
  43. package/types/camera-view.d.ts +45 -0
  44. package/types/debug.d.ts +14 -0
  45. package/types/describe.d.ts +43 -0
  46. package/types/entity-inspector-script.d.ts +87 -0
  47. package/types/frame-graph-view.d.ts +155 -0
  48. package/types/hierarchy-view.d.ts +172 -0
  49. package/types/index.d.ts +3 -0
  50. package/types/input/fly-controller.d.ts +42 -0
  51. package/types/input/input.d.ts +192 -0
  52. package/types/input/math.d.ts +1 -0
  53. package/types/input/orbit-controller.d.ts +63 -0
  54. package/types/input/pose.d.ts +126 -0
  55. package/types/inspector.d.ts +1304 -0
  56. package/types/instance-survey.d.ts +35 -0
  57. package/types/list-view.d.ts +151 -0
  58. package/types/material-view.d.ts +63 -0
  59. package/types/memory-view.d.ts +95 -0
  60. package/types/mesh-view.d.ts +89 -0
  61. package/types/model.d.ts +104 -0
  62. package/types/node-model.d.ts +54 -0
  63. package/types/physics-debug.d.ts +254 -0
  64. package/types/physics-view.d.ts +81 -0
  65. package/types/picker/picker.d.ts +255 -0
  66. package/types/picker/render-pass-picker.d.ts +47 -0
  67. package/types/property-view.d.ts +189 -0
  68. package/types/render-target-view.d.ts +98 -0
  69. package/types/renderers/texture-renderer-shaders.d.ts +9 -0
  70. package/types/renderers/texture-renderer.d.ts +231 -0
  71. package/types/renderers/wire-renderer.d.ts +347 -0
  72. package/types/script-view.d.ts +56 -0
  73. package/types/shader-view.d.ts +46 -0
  74. package/types/styles.d.ts +22 -0
  75. package/types/texture-view.d.ts +30 -0
  76. package/types/tooltip.d.ts +17 -0
  77. package/types/viewport-tools.d.ts +237 -0
@@ -0,0 +1,50 @@
1
+ import { MeshInstance } from 'playcanvas';
2
+
3
+ /** @import { AppBase } from 'playcanvas' */
4
+
5
+ /**
6
+ * A mesh instance found, and where it was found.
7
+ *
8
+ * @typedef {object} FoundInstance
9
+ * @property {MeshInstance} instance - The mesh instance.
10
+ * @property {string} role - Where it was found, such as 'render component' or 'drawn on "World"'.
11
+ * @ignore
12
+ */
13
+
14
+ /**
15
+ * Finds the mesh instances of the app. Nothing keeps a list of them, so they are gathered from
16
+ * what holds them: the render and model components of the hierarchy, including those of disabled
17
+ * entities, which the layers drop, then every layer of the composition, which adds sprites, text,
18
+ * particles, batches and the sky. An instance is held by its component and listed by every layer
19
+ * it renders in, so it is reported once, under the first place it is found. The batches of debug
20
+ * lines are not included: they are pushed straight into the visible lists each frame, so callers
21
+ * find them through the immediate renderer.
22
+ *
23
+ * @param {AppBase} app - The app.
24
+ * @returns {FoundInstance[]} The mesh instances, in the order found.
25
+ */
26
+ function collectMeshInstances(app) {
27
+ /** @type {FoundInstance[]} */
28
+ const found = [];
29
+ const seen = new Set();
30
+ const add = (instance, role) => {
31
+ if (!(instance instanceof MeshInstance) || seen.has(instance)) return;
32
+ seen.add(instance);
33
+ found.push({ instance, role });
34
+ };
35
+
36
+ app.root?.forEach((node) => {
37
+ const components = /** @type {any} */ (node).c;
38
+ if (!components) return;
39
+ for (const instance of components.render?.meshInstances ?? []) add(instance, 'render component');
40
+ for (const instance of components.model?.meshInstances ?? []) add(instance, 'model component');
41
+ });
42
+
43
+ for (const layer of app.scene?.layers?.layerList ?? []) {
44
+ for (const instance of layer.meshInstances ?? []) add(instance, `drawn on "${layer.name}"`);
45
+ }
46
+
47
+ return found;
48
+ }
49
+
50
+ export { collectMeshInstances };
@@ -0,0 +1,410 @@
1
+ import { BRACKET_COLORS } from './styles.js';
2
+ import { setTip } from './tooltip.js';
3
+
4
+ // the width of one bracket lane in the gutter left of a row, and the space after the last lane
5
+ const LANE_WIDTH = 10;
6
+ const GUTTER_GAP = 2;
7
+
8
+ /**
9
+ * @typedef {object} ListCell
10
+ * @ignore
11
+ * @property {string} text - The text.
12
+ * @property {string} [cls] - Extra classes, e.g. 'pci-cell-name'.
13
+ * @property {string} [title] - A tooltip.
14
+ * @property {*} [target] - A link target. Clicking the cell calls {@link ListView#onLink} with it
15
+ * instead of selecting the row.
16
+ * @property {boolean} [toggle] - Render the cell as a checkbox instead of text. Changing it calls
17
+ * {@link ListView#onToggle} with the row's item and the new state, without selecting the row.
18
+ * @property {boolean} [checked] - The state of a checkbox cell.
19
+ * @property {boolean} [disabled] - Whether a checkbox cell is disabled.
20
+ */
21
+
22
+ /**
23
+ * @typedef {object} ListRow
24
+ * @ignore
25
+ * @property {string} key - A key unique within the list, stable across refreshes.
26
+ * @property {*} item - The subject selected when the row is clicked.
27
+ * @property {string} name - The text the filter matches against.
28
+ * @property {(filter: string) => boolean} [matches] - Decides whether the row matches the lower-cased
29
+ * filter, for rows that match on more than their name. Called only while a filter is set.
30
+ * @property {ListCell[]} cells - The cells, left to right.
31
+ * @property {number} [indent] - The indentation level.
32
+ * @property {ListGuides} [guides] - The brackets drawn in a gutter left of the row, which group
33
+ * runs of rows without indenting them.
34
+ * @property {*} [preview] - What hovering the row reports through {@link ListView#onHover}, such
35
+ * as the render target of a pass. A link cell inside the row reports its own target instead while
36
+ * it is hovered.
37
+ * @property {boolean} [dim] - Whether the row is shown dimmed.
38
+ * @property {string} [title] - A tooltip for the whole row.
39
+ */
40
+
41
+ /**
42
+ * The brackets a row shows in the gutter on its left. A bracket spans a run of rows in one lane,
43
+ * each lane a nesting level, and marks the row it belongs to with a tick pointing at the row.
44
+ *
45
+ * @typedef {object} ListGuides
46
+ * @ignore
47
+ * @property {number} lanes - The lanes the gutter holds. The same on every row of a list, so the
48
+ * rows stay aligned.
49
+ * @property {Array<'start'|'mid'|'end'|null>} segments - What each lane shows on this row: the top
50
+ * end of a bracket, its middle, its bottom end, or nothing.
51
+ * @property {number} tick - The lane of the bracket this row owns, or -1 when it owns none.
52
+ */
53
+
54
+ /**
55
+ * @typedef {object} ListEntry
56
+ * @ignore
57
+ * @property {HTMLElement} el - The row element.
58
+ * @property {HTMLElement[]} cells - The cell elements.
59
+ * @property {ListRow} row - The row last rendered.
60
+ * @property {number} indent - The indentation last rendered.
61
+ * @property {string} guides - The brackets last rendered, as a signature.
62
+ */
63
+
64
+ /**
65
+ * The background layers drawing a row's brackets: a one pixel line per lane, the full height of the
66
+ * row in the middle of a bracket and half of it at either end, so brackets meeting in one lane stay
67
+ * apart, and a tick from the owned bracket's line across the rest of the gutter.
68
+ *
69
+ * @param {ListGuides} guides - The brackets.
70
+ * @returns {{ image: string, position: string, size: string }|null} The layers, or null for none.
71
+ */
72
+ function guideStyle(guides) {
73
+ const images = [];
74
+ const positions = [];
75
+ const sizes = [];
76
+ const layer = (lane, position, size) => {
77
+ const color = BRACKET_COLORS[lane % BRACKET_COLORS.length];
78
+ images.push(`linear-gradient(${color}, ${color})`);
79
+ positions.push(position);
80
+ sizes.push(size);
81
+ };
82
+ guides.segments.forEach((segment, lane) => {
83
+ if (!segment) return;
84
+ const x = lane * LANE_WIDTH + LANE_WIDTH / 2;
85
+ if (segment === 'mid') layer(lane, `${x}px 0`, '1px 100%');
86
+ else if (segment === 'start') layer(lane, `${x}px 100%`, '1px 50%');
87
+ else layer(lane, `${x}px 0`, '1px 50%');
88
+ });
89
+ if (guides.tick >= 0) {
90
+ const x = guides.tick * LANE_WIDTH + LANE_WIDTH / 2;
91
+ layer(guides.tick, `${x}px 50%`, `${guides.lanes * LANE_WIDTH - x}px 1px`);
92
+ }
93
+ return images.length ? { image: images.join(', '), position: positions.join(', '), size: sizes.join(', ') } : null;
94
+ }
95
+
96
+ /**
97
+ * A flat, selectable list of rows with cells. Rows are keyed and reconciled in place, so a list
98
+ * rebuilt from scratch a couple of times a second keeps its selection and scroll position and only
99
+ * touches the text that changed.
100
+ *
101
+ * @ignore
102
+ */
103
+ class ListView {
104
+ /**
105
+ * The element the list is rendered into.
106
+ *
107
+ * @type {HTMLElement}
108
+ */
109
+ container;
110
+
111
+ /**
112
+ * Called with the selected item and its row key, or null when the selection is cleared.
113
+ *
114
+ * @type {(item: *, key: string|null) => void}
115
+ */
116
+ onSelect;
117
+
118
+ /**
119
+ * Called with the target of a link cell when it is clicked.
120
+ *
121
+ * @type {((target: *) => void)|undefined}
122
+ */
123
+ onLink;
124
+
125
+ /**
126
+ * Called when the pointer enters a link, with the link element and a function reading the
127
+ * link's current target, and with nulls when it leaves. The target is read through the
128
+ * function because a refresh can point the same element at a new target while it is hovered.
129
+ *
130
+ * @type {((el: HTMLElement|null, target: (() => *)|null) => void)|undefined}
131
+ */
132
+ onHover;
133
+
134
+ /**
135
+ * Called with a row's item and the new state when one of its checkbox cells changes.
136
+ *
137
+ * @type {((item: *, checked: boolean) => void)|undefined}
138
+ */
139
+ onToggle;
140
+
141
+ /**
142
+ * Case-insensitive filter on the rows' names.
143
+ *
144
+ * @type {string}
145
+ */
146
+ filter = '';
147
+
148
+ /**
149
+ * The key of the selected row, or null.
150
+ *
151
+ * @type {string|null}
152
+ */
153
+ selectedKey = null;
154
+
155
+ /**
156
+ * The number of rows shown after filtering, as of the last render.
157
+ *
158
+ * @type {number}
159
+ */
160
+ visibleCount = 0;
161
+
162
+ /**
163
+ * @type {ListRow[]}
164
+ * @private
165
+ */
166
+ _rows = [];
167
+
168
+ /**
169
+ * @type {Map<string, ListEntry>}
170
+ * @private
171
+ */
172
+ _entries = new Map();
173
+
174
+ /**
175
+ * @param {HTMLElement} container - The element to render into.
176
+ * @param {(item: *, key: string|null) => void} onSelect - The selection callback.
177
+ * @param {(target: *) => void} [onLink] - The link callback.
178
+ */
179
+ constructor(container, onSelect, onLink) {
180
+ this.container = container;
181
+ this.onSelect = onSelect;
182
+ this.onLink = onLink;
183
+ }
184
+
185
+ /**
186
+ * The number of rows before filtering.
187
+ *
188
+ * @type {number}
189
+ */
190
+ get rowCount() {
191
+ return this._rows.length;
192
+ }
193
+
194
+ /**
195
+ * The item of the selected row, or null when nothing is selected or the row is gone.
196
+ *
197
+ * @type {*}
198
+ */
199
+ get selected() {
200
+ return this._rows.find(row => row.key === this.selectedKey)?.item ?? null;
201
+ }
202
+
203
+ /**
204
+ * Replaces the rows and re-renders.
205
+ *
206
+ * @param {ListRow[]} rows - The rows, in display order.
207
+ */
208
+ setRows(rows) {
209
+ this._rows = rows;
210
+ this.render();
211
+ }
212
+
213
+ /**
214
+ * Selects a row by key.
215
+ *
216
+ * @param {string|null} key - The row key, or null to clear the selection.
217
+ */
218
+ select(key) {
219
+ this.selectedKey = key;
220
+ this.render();
221
+ const row = this._rows.find(r => r.key === key);
222
+ this._entries.get(key)?.el.scrollIntoView?.({ block: 'nearest' });
223
+ this.onSelect?.(row?.item ?? null, row ? key : null);
224
+ }
225
+
226
+ /**
227
+ * Selects the first row whose item is the given object.
228
+ *
229
+ * @param {*} item - The item.
230
+ * @returns {boolean} Whether a row was found.
231
+ */
232
+ selectItem(item) {
233
+ const row = this._rows.find(r => r.item === item);
234
+ if (!row) return false;
235
+ this.select(row.key);
236
+ return true;
237
+ }
238
+
239
+ /**
240
+ * Synchronizes the DOM with the rows.
241
+ */
242
+ render() {
243
+ const filter = this.filter.trim().toLowerCase();
244
+ const seen = new Set();
245
+ let cursor = this.container.firstChild;
246
+ let visible = 0;
247
+
248
+ for (const row of this._rows) {
249
+ const entry = this._entries.get(row.key) ?? this._createEntry(row);
250
+ entry.row = row;
251
+ seen.add(row.key);
252
+
253
+ this._syncCells(entry, row.cells);
254
+
255
+ const shown = !filter || (row.matches ? row.matches(filter) : row.name.toLowerCase().includes(filter));
256
+ const display = shown ? '' : 'none';
257
+ if (entry.el.style.display !== display) entry.el.style.display = display;
258
+ if (shown) visible++;
259
+
260
+ const indent = row.indent ?? 0;
261
+ const guides = row.guides ? `${row.guides.lanes}|${row.guides.segments.join(',')}|${row.guides.tick}` : '';
262
+ if (entry.indent !== indent || entry.guides !== guides) {
263
+ entry.indent = indent;
264
+ entry.guides = guides;
265
+ const gutter = row.guides ? row.guides.lanes * LANE_WIDTH + GUTTER_GAP : 0;
266
+ entry.el.style.paddingLeft = `${gutter + indent * 14 + 6}px`;
267
+ const style = row.guides ? guideStyle(row.guides) : null;
268
+ entry.el.style.backgroundImage = style?.image ?? '';
269
+ entry.el.style.backgroundPosition = style?.position ?? '';
270
+ entry.el.style.backgroundSize = style?.size ?? '';
271
+ entry.el.style.backgroundRepeat = style ? 'no-repeat' : '';
272
+ }
273
+ const title = row.title ?? '';
274
+ if ((entry.el.dataset.tip ?? '') !== title) setTip(entry.el, title);
275
+
276
+ entry.el.classList.toggle('pci-selected', row.key === this.selectedKey);
277
+ entry.el.classList.toggle('pci-dim', !!row.dim);
278
+
279
+ if (entry.el !== cursor) {
280
+ this.container.insertBefore(entry.el, cursor);
281
+ } else {
282
+ cursor = cursor.nextSibling;
283
+ }
284
+ }
285
+
286
+ for (const [key, entry] of this._entries) {
287
+ if (!seen.has(key)) {
288
+ entry.el.remove();
289
+ this._entries.delete(key);
290
+ }
291
+ }
292
+
293
+ this.visibleCount = visible;
294
+ }
295
+
296
+ /**
297
+ * @param {ListEntry} entry - The row entry.
298
+ * @param {ListCell[]} cells - The cells to show.
299
+ * @private
300
+ */
301
+ _syncCells(entry, cells) {
302
+ while (entry.cells.length > cells.length) {
303
+ entry.cells.pop().remove();
304
+ }
305
+
306
+ cells.forEach((cell, i) => {
307
+ let cellEl = entry.cells[i];
308
+ const wantInput = !!cell.toggle;
309
+ if (cellEl && (cellEl.tagName === 'INPUT') !== wantInput) {
310
+ cellEl.remove();
311
+ cellEl = undefined;
312
+ }
313
+ if (!cellEl) {
314
+ cellEl = wantInput ? this._createToggleCell(entry, i) : this._createTextCell(entry, i);
315
+ const next = entry.cells[i + 1] ?? null;
316
+ entry.el.insertBefore(cellEl, next);
317
+ entry.cells[i] = cellEl;
318
+ }
319
+
320
+ if (wantInput) {
321
+ const input = /** @type {HTMLInputElement} */ (cellEl);
322
+ const checked = !!cell.checked;
323
+ if (input.checked !== checked) input.checked = checked;
324
+ const disabled = !!cell.disabled;
325
+ if (input.disabled !== disabled) input.disabled = disabled;
326
+ } else {
327
+ const cls = `pci-cell ${cell.cls ?? ''}${cell.target !== undefined ? ' pci-link' : ''}`;
328
+ if (cellEl.className !== cls) cellEl.className = cls;
329
+ if (cellEl.textContent !== cell.text) cellEl.textContent = cell.text;
330
+ }
331
+ const title = cell.title ?? '';
332
+ if ((cellEl.dataset.tip ?? '') !== title) setTip(cellEl, title);
333
+ });
334
+ }
335
+
336
+ /**
337
+ * @param {ListEntry} entry - The row entry.
338
+ * @param {number} index - The cell index.
339
+ * @returns {HTMLElement} A text cell, clicking a link cell calls {@link onLink}.
340
+ * @private
341
+ */
342
+ _createTextCell(entry, index) {
343
+ const cellEl = document.createElement('span');
344
+ const target = () => entry.row.cells[index]?.target;
345
+ cellEl.addEventListener('pointerenter', () => {
346
+ if (target() !== undefined) this.onHover?.(cellEl, target);
347
+ });
348
+ // still inside the row, which may preview something of its own
349
+ cellEl.addEventListener('pointerleave', () => this._hoverRow(entry));
350
+ cellEl.addEventListener('click', (e) => {
351
+ const current = entry.row.cells[index];
352
+ if (current && current.target !== undefined) {
353
+ e.stopPropagation();
354
+ this.onLink?.(current.target);
355
+ }
356
+ });
357
+ return cellEl;
358
+ }
359
+
360
+ /**
361
+ * @param {ListEntry} entry - The row entry.
362
+ * @param {number} index - The cell index.
363
+ * @returns {HTMLInputElement} A checkbox cell that reports through {@link onToggle}.
364
+ * @private
365
+ */
366
+ _createToggleCell(entry, index) {
367
+ const input = document.createElement('input');
368
+ input.type = 'checkbox';
369
+ input.className = 'pci-toggle pci-cell-toggle';
370
+ input.addEventListener('click', e => e.stopPropagation());
371
+ input.addEventListener('dblclick', e => e.stopPropagation());
372
+ input.addEventListener('change', () => {
373
+ if (entry.row.cells[index]?.toggle) this.onToggle?.(entry.row.item, input.checked);
374
+ });
375
+ return input;
376
+ }
377
+
378
+ /**
379
+ * @param {ListRow} row - The row.
380
+ * @returns {ListEntry} The new entry.
381
+ * @private
382
+ */
383
+ _createEntry(row) {
384
+ const rowEl = document.createElement('div');
385
+ rowEl.className = 'pci-lrow';
386
+ rowEl.addEventListener('click', () => this.select(row.key));
387
+
388
+ const entry = { el: rowEl, cells: [], row, indent: -1, guides: '' };
389
+ rowEl.addEventListener('pointerenter', () => this._hoverRow(entry));
390
+ rowEl.addEventListener('pointerleave', () => this.onHover?.(null, null));
391
+ this._entries.set(row.key, entry);
392
+ return entry;
393
+ }
394
+
395
+ /**
396
+ * Reports the row the pointer is over, when it previews something, or that nothing is hovered.
397
+ *
398
+ * @param {ListEntry} entry - The row entry.
399
+ * @private
400
+ */
401
+ _hoverRow(entry) {
402
+ if (entry.row.preview !== undefined && entry.row.preview !== null) {
403
+ this.onHover?.(entry.el, () => entry.row.preview);
404
+ } else {
405
+ this.onHover?.(null, null);
406
+ }
407
+ }
408
+ }
409
+
410
+ export { ListView };
@@ -0,0 +1,197 @@
1
+ import { Material } from 'playcanvas';
2
+
3
+ import { describeValue } from './describe.js';
4
+ import { collectMeshInstances } from './instance-survey.js';
5
+ import { nodeLabel } from './memory-view.js';
6
+ import { makeSection, push } from './model.js';
7
+ import { materialRows, materialTextures, meshInstanceRows } from './node-model.js';
8
+
9
+ /** @import { AppBase } from 'playcanvas' */
10
+ /** @import { Asset } from 'playcanvas' */
11
+ /** @import { Shader } from 'playcanvas' */
12
+ /** @import { Texture } from 'playcanvas' */
13
+ /** @import { ListRow } from './list-view.js' */
14
+ /** @import { PropertySection } from './model.js' */
15
+
16
+ /**
17
+ * Where a material was found besides its mesh instances, which the material lists itself.
18
+ *
19
+ * @typedef {object} MaterialSources
20
+ * @property {Asset[]} assets - The material assets holding it.
21
+ * @property {string[]} lines - The layers whose debug lines draw with it.
22
+ * @ignore
23
+ */
24
+
25
+ // most mesh instances a material lists in the property view; a shared material can have thousands
26
+ const MAX_INSTANCES = 100;
27
+
28
+ /**
29
+ * Finds the materials of the app. Nothing keeps a list of them, so they are found through what
30
+ * uses them: the mesh instances of the components and layers, see {@link collectMeshInstances},
31
+ * which add the sky, sprite, text and particle materials and the default material; the batches of
32
+ * debug lines, one per material; and the loaded material assets of the registry, which include
33
+ * those a glb container creates and hold materials nothing may be drawing with. A material only the
34
+ * app's own code holds, never given to a mesh instance, cannot be found.
35
+ *
36
+ * @param {AppBase} app - The app.
37
+ * @returns {Map<Material, MaterialSources>} The materials, in the order found, with the assets and
38
+ * debug lines using each.
39
+ */
40
+ function surveyMaterials(app) {
41
+ /** @type {Map<Material, MaterialSources>} */
42
+ const materials = new Map();
43
+ const sourcesOf = (material) => {
44
+ if (!(material instanceof Material)) return null;
45
+ let sources = materials.get(material);
46
+ if (!sources) {
47
+ sources = { assets: [], lines: [] };
48
+ materials.set(material, sources);
49
+ }
50
+ return sources;
51
+ };
52
+
53
+ for (const { instance } of collectMeshInstances(app)) sourcesOf(instance.material);
54
+
55
+ for (const [layer, batches] of app.scene?.immediate?.batchesMap ?? []) {
56
+ for (const batch of batches.map?.values() ?? []) sourcesOf(batch.material)?.lines.push(layer.name);
57
+ }
58
+
59
+ for (const asset of app.assets?.list() ?? []) {
60
+ if (asset.type === 'material' && asset.loaded) sourcesOf(asset.resource)?.assets.push(asset);
61
+ }
62
+
63
+ return materials;
64
+ }
65
+
66
+ /**
67
+ * @param {Material} material - A material.
68
+ * @returns {string} Its class name, such as 'StandardMaterial'.
69
+ */
70
+ function typeOf(material) {
71
+ return material.constructor?.name || 'Material';
72
+ }
73
+
74
+ /**
75
+ * @param {Material} material - A material.
76
+ * @returns {string} Its name, or a generated one when it has none.
77
+ */
78
+ function materialName(material) {
79
+ return material.name || `Material #${material.id}`;
80
+ }
81
+
82
+ /**
83
+ * One row per material found, the most used first, dimmed when no mesh instance uses it.
84
+ *
85
+ * @param {Map<Material, MaterialSources>} survey - The materials found.
86
+ * @returns {ListRow[]} The rows.
87
+ */
88
+ function materialListRows(survey) {
89
+ const materials = [...survey.keys()].sort((a, b) => b.meshInstances.size - a.meshInstances.size ||
90
+ materialName(a).localeCompare(materialName(b)) || a.id - b.id);
91
+ return materials.map((material) => {
92
+ const name = materialName(material);
93
+ const instances = material.meshInstances.size;
94
+ const textures = materialTextures(material).size;
95
+ const variants = material.variants.size;
96
+ const assets = survey.get(material).assets;
97
+ const cells = [{ text: name, cls: 'pci-cell-name' }];
98
+ cells.push({ text: typeOf(material).replace(/Material$/, '') || 'Material', cls: 'pci-cell-tag pci-cell-tag-info' });
99
+ if (material.transparent) cells.push({ text: 'transparent', cls: 'pci-cell-tag pci-cell-tag-info' });
100
+ const counts = `${instances} instance${instances === 1 ? '' : 's'} · ${textures} texture${textures === 1 ? '' : 's'} · ` +
101
+ `${variants} variant${variants === 1 ? '' : 's'}`;
102
+ cells.push({ text: counts, cls: 'pci-cell-info' });
103
+ cells.push({ text: `#${material.id}`, cls: 'pci-cell-info pci-cell-right' });
104
+ const title = `${typeOf(material)} "${name}" #${material.id}\n` +
105
+ `${instances} mesh instance${instances === 1 ? '' : 's'}, ${textures} texture${textures === 1 ? '' : 's'}, ` +
106
+ `${variants} compiled variant${variants === 1 ? '' : 's'}` +
107
+ `${assets.length ? `\nfrom asset "${assets[0].name}"` : ''}`;
108
+ return { key: `material${material.id}`, item: material, name, dim: instances === 0, title, cells };
109
+ });
110
+ }
111
+
112
+ /**
113
+ * Everything the property view shows for a material: its textures, its compiled variants and its
114
+ * properties, and what uses it, with each mesh instance opening in place.
115
+ *
116
+ * @param {Material} material - The material.
117
+ * @param {{ app: AppBase }} ctx - The app, to find the assets and debug lines using the material.
118
+ * @returns {PropertySection[]} The sections.
119
+ */
120
+ function buildMaterialModel(material, ctx) {
121
+ const sources = surveyMaterials(ctx.app).get(material) ?? { assets: [], lines: [] };
122
+
123
+ const general = makeSection('material', `${typeOf(material)} "${materialName(material)}"`);
124
+ push(general, 'id', describeValue(material.id));
125
+ const textures = [...materialTextures(material)];
126
+ push(general, 'textures', {
127
+ text: textures.length ? `${textures.length} texture${textures.length === 1 ? '' : 's'}` : 'none',
128
+ cls: textures.length ? 'obj' : 'null',
129
+ items: textures.map((texture, index) => ({ label: `[${index}]`, ...describeValue(texture) }))
130
+ });
131
+ general.rows.push(...materialRows(material));
132
+
133
+ const usage = makeSection('users', 'Used by');
134
+ const instances = [...material.meshInstances];
135
+ push(usage, 'mesh instances', {
136
+ text: instances.length ? `${instances.length} mesh instance${instances.length === 1 ? '' : 's'}` : 'none',
137
+ cls: instances.length ? 'obj' : 'null',
138
+ items: instances.slice(0, MAX_INSTANCES).map((instance, index) => ({
139
+ label: `[${index}]`,
140
+ ...describeValue(instance),
141
+ text: nodeLabel(instance.node ?? null),
142
+ expand: () => meshInstanceRows(instance)
143
+ }))
144
+ });
145
+ if (instances.length > MAX_INSTANCES) {
146
+ usage.rows.push({ key: 'more', label: '', value: { text: `… ${instances.length - MAX_INSTANCES} more`, cls: 'null' }, depth: 1 });
147
+ }
148
+ if (sources.assets.length) {
149
+ push(usage, 'assets', {
150
+ text: `${sources.assets.length} asset${sources.assets.length === 1 ? '' : 's'}`,
151
+ cls: 'obj',
152
+ items: sources.assets.map((asset, index) => ({ label: `[${index}]`, ...describeValue(asset) }))
153
+ });
154
+ }
155
+ if (sources.lines.length) {
156
+ push(usage, 'debug lines', { text: sources.lines.map(name => `"${name}"`).join(', '), cls: 'obj' });
157
+ }
158
+
159
+ return [general, usage];
160
+ }
161
+
162
+ /**
163
+ * @param {AppBase} app - The app.
164
+ * @param {Texture} texture - A texture.
165
+ * @returns {Material[]} The materials found that sample it.
166
+ */
167
+ function materialsUsingTexture(app, texture) {
168
+ return [...surveyMaterials(app).keys()].filter(material => materialTextures(material).has(texture));
169
+ }
170
+
171
+ /**
172
+ * @param {AppBase} app - The app.
173
+ * @param {Shader} shader - A shader.
174
+ * @returns {Material[]} The materials found that compiled it as one of their variants.
175
+ */
176
+ function materialsUsingShader(app, shader) {
177
+ return [...surveyMaterials(app).keys()].filter(material => [...material.variants.values()].includes(shader));
178
+ }
179
+
180
+ /**
181
+ * @param {Material[]} materials - The materials using something.
182
+ * @returns {PropertySection} A section listing them, each linking to the Materials tab.
183
+ */
184
+ function usedByMaterialsSection(materials) {
185
+ const section = makeSection('materials', 'Used by materials');
186
+ push(section, 'materials', {
187
+ text: materials.length ? `${materials.length} material${materials.length === 1 ? '' : 's'}` : 'none found',
188
+ cls: materials.length ? 'obj' : 'null',
189
+ items: materials.map((material, index) => ({ label: `[${index}]`, ...describeValue(material) }))
190
+ });
191
+ return section;
192
+ }
193
+
194
+ export {
195
+ buildMaterialModel, materialListRows, materialsUsingShader, materialsUsingTexture, surveyMaterials,
196
+ usedByMaterialsSection
197
+ };