@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.
- package/LICENSE +19 -0
- package/README.md +110 -0
- package/package.json +62 -0
- package/src/asset-usage.js +108 -0
- package/src/asset-view.js +370 -0
- package/src/camera-view.js +128 -0
- package/src/debug.js +31 -0
- package/src/describe.js +302 -0
- package/src/entity-inspector-script.js +130 -0
- package/src/frame-graph-view.js +767 -0
- package/src/hierarchy-view.js +475 -0
- package/src/index.js +4 -0
- package/src/input/fly-controller.js +116 -0
- package/src/input/input.js +299 -0
- package/src/input/math.js +8 -0
- package/src/input/orbit-controller.js +158 -0
- package/src/input/pose.js +207 -0
- package/src/inspector.js +3293 -0
- package/src/instance-survey.js +50 -0
- package/src/list-view.js +410 -0
- package/src/material-view.js +197 -0
- package/src/memory-view.js +344 -0
- package/src/mesh-view.js +235 -0
- package/src/model.js +170 -0
- package/src/node-model.js +404 -0
- package/src/physics-debug.js +527 -0
- package/src/physics-view.js +314 -0
- package/src/picker/picker.js +545 -0
- package/src/picker/render-pass-picker.js +206 -0
- package/src/property-view.js +628 -0
- package/src/render-target-view.js +288 -0
- package/src/renderers/texture-renderer-shaders.js +139 -0
- package/src/renderers/texture-renderer.js +430 -0
- package/src/renderers/wire-renderer.js +954 -0
- package/src/script-view.js +288 -0
- package/src/shader-view.js +287 -0
- package/src/styles.js +918 -0
- package/src/texture-view.js +186 -0
- package/src/tooltip.js +72 -0
- package/src/viewport-tools.js +695 -0
- package/types/asset-usage.d.ts +23 -0
- package/types/asset-view.d.ts +94 -0
- package/types/camera-view.d.ts +45 -0
- package/types/debug.d.ts +14 -0
- package/types/describe.d.ts +43 -0
- package/types/entity-inspector-script.d.ts +87 -0
- package/types/frame-graph-view.d.ts +155 -0
- package/types/hierarchy-view.d.ts +172 -0
- package/types/index.d.ts +3 -0
- package/types/input/fly-controller.d.ts +42 -0
- package/types/input/input.d.ts +192 -0
- package/types/input/math.d.ts +1 -0
- package/types/input/orbit-controller.d.ts +63 -0
- package/types/input/pose.d.ts +126 -0
- package/types/inspector.d.ts +1304 -0
- package/types/instance-survey.d.ts +35 -0
- package/types/list-view.d.ts +151 -0
- package/types/material-view.d.ts +63 -0
- package/types/memory-view.d.ts +95 -0
- package/types/mesh-view.d.ts +89 -0
- package/types/model.d.ts +104 -0
- package/types/node-model.d.ts +54 -0
- package/types/physics-debug.d.ts +254 -0
- package/types/physics-view.d.ts +81 -0
- package/types/picker/picker.d.ts +255 -0
- package/types/picker/render-pass-picker.d.ts +47 -0
- package/types/property-view.d.ts +189 -0
- package/types/render-target-view.d.ts +98 -0
- package/types/renderers/texture-renderer-shaders.d.ts +9 -0
- package/types/renderers/texture-renderer.d.ts +231 -0
- package/types/renderers/wire-renderer.d.ts +347 -0
- package/types/script-view.d.ts +56 -0
- package/types/shader-view.d.ts +46 -0
- package/types/styles.d.ts +22 -0
- package/types/texture-view.d.ts +30 -0
- package/types/tooltip.d.ts +17 -0
- package/types/viewport-tools.d.ts +237 -0
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { PROJECTION_ORTHOGRAPHIC } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
import { describeValue } from './describe.js';
|
|
4
|
+
import { nodeLabel } from './memory-view.js';
|
|
5
|
+
import { makeSection, push, read } from './model.js';
|
|
6
|
+
import { layersValue } from './node-model.js';
|
|
7
|
+
import { RENDER_MODES, isScreenCamera } from './viewport-tools.js';
|
|
8
|
+
|
|
9
|
+
/** @import { AppBase } from 'playcanvas' */
|
|
10
|
+
/** @import { CameraComponent } from 'playcanvas' */
|
|
11
|
+
/** @import { ListRow } from './list-view.js' */
|
|
12
|
+
/** @import { PropertySection } from './model.js' */
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* What the Cameras tab offers on a camera, supplied by the inspector.
|
|
16
|
+
*
|
|
17
|
+
* @typedef {object} CameraActions
|
|
18
|
+
* @property {(camera: CameraComponent) => void} fly - Flies the camera.
|
|
19
|
+
* @property {CameraComponent|null} flying - The camera being flown, if any.
|
|
20
|
+
* @ignore
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const MODE_NAMES = new Map(RENDER_MODES);
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Every camera component of the hierarchy, disabled ones included, in the order they render: by
|
|
27
|
+
* priority, then in hierarchy order.
|
|
28
|
+
*
|
|
29
|
+
* @param {AppBase} app - The app.
|
|
30
|
+
* @returns {CameraComponent[]} The cameras.
|
|
31
|
+
*/
|
|
32
|
+
function collectCameras(app) {
|
|
33
|
+
const cameras = [];
|
|
34
|
+
app.root?.forEach((node) => {
|
|
35
|
+
const camera = /** @type {any} */ (node).c?.camera;
|
|
36
|
+
if (camera) cameras.push(camera);
|
|
37
|
+
});
|
|
38
|
+
return cameras.map((camera, order) => ({ camera, order }))
|
|
39
|
+
.sort((a, b) => a.camera.priority - b.camera.priority || a.order - b.order)
|
|
40
|
+
.map(({ camera }) => camera);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @param {CameraComponent} camera - A camera.
|
|
45
|
+
* @returns {string} The name of the shader pass it renders with.
|
|
46
|
+
*/
|
|
47
|
+
function renderModeOf(camera) {
|
|
48
|
+
const name = camera.camera.shaderPassInfo?.name ?? 'forward';
|
|
49
|
+
return MODE_NAMES.get(name) ?? name;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {CameraComponent} camera - A camera.
|
|
54
|
+
* @returns {string} Where it draws: its viewport of the screen, or the texture it renders into.
|
|
55
|
+
*/
|
|
56
|
+
function targetText(camera) {
|
|
57
|
+
if (camera.renderTarget) return `texture ${camera.renderTarget.name || '(unnamed)'}`;
|
|
58
|
+
const { x, y, z, w } = camera.rect;
|
|
59
|
+
return x === 0 && y === 0 && z === 1 && w === 1 ? 'screen' : `screen rect ${x}, ${y}, ${z}, ${w}`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* One row per camera, in render order, dimmed when it does not render.
|
|
64
|
+
*
|
|
65
|
+
* @param {AppBase} app - The app.
|
|
66
|
+
* @param {CameraComponent|null} flying - The camera being flown, if any.
|
|
67
|
+
* @returns {ListRow[]} The rows.
|
|
68
|
+
*/
|
|
69
|
+
function cameraRows(app, flying) {
|
|
70
|
+
return collectCameras(app).map((camera) => {
|
|
71
|
+
const entity = camera.entity;
|
|
72
|
+
const name = nodeLabel(entity);
|
|
73
|
+
const cells = [{ text: name, cls: 'pci-cell-name' }];
|
|
74
|
+
cells.push({ text: camera.renderTarget ? 'texture' : 'screen', cls: 'pci-cell-tag pci-cell-tag-info' });
|
|
75
|
+
if (camera.projection === PROJECTION_ORTHOGRAPHIC) cells.push({ text: 'ortho', cls: 'pci-cell-tag pci-cell-tag-info' });
|
|
76
|
+
const mode = renderModeOf(camera);
|
|
77
|
+
if (mode !== 'standard') cells.push({ text: mode, cls: 'pci-cell-tag' });
|
|
78
|
+
if (camera === flying) cells.push({ text: 'flying', cls: 'pci-cell-tag' });
|
|
79
|
+
cells.push({ text: `priority ${camera.priority} · ${camera.layers.length} layers · ${targetText(camera)}`, cls: 'pci-cell-info' });
|
|
80
|
+
const running = camera.enabled && entity.enabled;
|
|
81
|
+
return { key: `camera${entity.getGuid?.() ?? name}`, item: camera, name, dim: !running, title: `${name}\n${targetText(camera)}`, cells };
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Everything the property view shows for a camera: where and how it draws, the layers it renders,
|
|
87
|
+
* its render mode, and a button to fly it.
|
|
88
|
+
*
|
|
89
|
+
* @param {CameraComponent} camera - The camera.
|
|
90
|
+
* @param {{ app: AppBase, cameraActions: CameraActions }} ctx - The app, and what the tab offers.
|
|
91
|
+
* @returns {PropertySection[]} The sections.
|
|
92
|
+
*/
|
|
93
|
+
function buildCameraModel(camera, ctx) {
|
|
94
|
+
const { cameraActions } = ctx;
|
|
95
|
+
const entity = camera.entity;
|
|
96
|
+
const general = makeSection('camera', `Camera "${entity.name}"`);
|
|
97
|
+
push(general, 'entity', describeValue(entity));
|
|
98
|
+
push(general, 'enabled', { text: String(camera.enabled && entity.enabled), cls: 'bool' });
|
|
99
|
+
push(general, 'fly', {
|
|
100
|
+
text: camera === cameraActions.flying ? 'flying now, Esc to stop' : 'fly it with the mouse and WASD',
|
|
101
|
+
cls: 'obj',
|
|
102
|
+
actions: [{
|
|
103
|
+
text: camera === cameraActions.flying ? 'Flying' : 'Fly',
|
|
104
|
+
title: 'Fly this camera: WASD and QE move, drag to look, the wheel sets the speed, Shift is faster, Esc stops',
|
|
105
|
+
disabled: camera === cameraActions.flying,
|
|
106
|
+
run: () => cameraActions.fly(camera)
|
|
107
|
+
}]
|
|
108
|
+
});
|
|
109
|
+
push(general, 'render mode', { text: renderModeOf(camera), cls: 'obj' });
|
|
110
|
+
push(general, 'priority', read(camera, 'priority'));
|
|
111
|
+
push(general, 'target', camera.renderTarget ? describeValue(camera.renderTarget) : { text: targetText(camera), cls: 'obj' });
|
|
112
|
+
push(general, 'rect', read(camera, 'rect'));
|
|
113
|
+
push(general, 'projection', { text: camera.projection === PROJECTION_ORTHOGRAPHIC ? 'orthographic' : 'perspective', cls: 'obj' });
|
|
114
|
+
push(general, camera.projection === PROJECTION_ORTHOGRAPHIC ? 'ortho height' : 'fov',
|
|
115
|
+
read(camera, camera.projection === PROJECTION_ORTHOGRAPHIC ? 'orthoHeight' : 'fov'));
|
|
116
|
+
push(general, 'near clip', read(camera, 'nearClip'));
|
|
117
|
+
push(general, 'far clip', read(camera, 'farClip'));
|
|
118
|
+
push(general, 'clear', {
|
|
119
|
+
text: [camera.clearColorBuffer && 'color', camera.clearDepthBuffer && 'depth', camera.clearStencilBuffer && 'stencil'].filter(Boolean).join(', ') || 'nothing',
|
|
120
|
+
cls: 'obj'
|
|
121
|
+
});
|
|
122
|
+
push(general, 'layers', layersValue(camera.layers, ctx.app.scene?.layers ?? null));
|
|
123
|
+
const passes = camera.framePasses ?? [];
|
|
124
|
+
if (passes.length) push(general, 'frame passes', describeValue(passes));
|
|
125
|
+
return [general];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { buildCameraModel, cameraRows, collectCameras, isScreenCamera };
|
package/src/debug.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stand-ins for the engine's `Debug` and `DebugGraphics`, which it does not export, used by the
|
|
3
|
+
* helpers copied from the engine (see COPIED_FROM.md) so those stay close to their originals.
|
|
4
|
+
* Assertions and GPU markers are dropped, as a release engine build drops them; warnings print
|
|
5
|
+
* once each.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** @type {Set<string>} */
|
|
9
|
+
const warned = new Set();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string} message - The message.
|
|
13
|
+
*/
|
|
14
|
+
const warnOnce = (message) => {
|
|
15
|
+
if (warned.has(message)) return;
|
|
16
|
+
warned.add(message);
|
|
17
|
+
console.warn(message);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const Debug = {
|
|
21
|
+
assert() {},
|
|
22
|
+
warnOnce,
|
|
23
|
+
errorOnce: warnOnce
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const DebugGraphics = {
|
|
27
|
+
pushGpuMarker() {},
|
|
28
|
+
popGpuMarker() {}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export { Debug, DebugGraphics };
|
package/src/describe.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Asset, BoundingBox, Color, Curve, CurveSet, Entity, GraphNode, IndexBuffer, Layer, Mat3, Mat4, Material, Mesh, MeshInstance, OrientedBox, pixelFormatInfo, Quat, RenderTarget, Shader, Sprite, StorageBuffer, Tags, Texture, Vec2, Vec3, Vec4, VertexBuffer
|
|
3
|
+
} from 'playcanvas';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A value rendered by the property view.
|
|
7
|
+
*
|
|
8
|
+
* @typedef {object} Described
|
|
9
|
+
* @ignore
|
|
10
|
+
* @property {string} text - The display text.
|
|
11
|
+
* @property {string} [cls] - A class suffix selecting the color: 'num', 'bool', 'str', 'null',
|
|
12
|
+
* 'obj', 'ref' or 'err'.
|
|
13
|
+
* @property {*} [target] - An object the value refers to. Rendered as a link that selects it: a
|
|
14
|
+
* node in the hierarchy, a texture, shader, asset or buffer on its tab.
|
|
15
|
+
* @property {string} [swatch] - A CSS color, rendered as a chip in front of the text.
|
|
16
|
+
* @property {string} [code] - A block of text, such as shader source, shown under the row when it
|
|
17
|
+
* is expanded. The text is its summary, and the row gets a copy button.
|
|
18
|
+
* @property {{ text: string, title?: string, disabled?: boolean, run: () => void }[]} [actions] -
|
|
19
|
+
* Buttons shown after the value, such as the pages of a long list. The view refreshes after one runs.
|
|
20
|
+
* @property {() => void} [select] - Makes the row a choice rather than a link or a toggle: clicking
|
|
21
|
+
* it calls this, and a row that also expands then opens from its caret alone.
|
|
22
|
+
* @property {boolean} [active] - Whether the row is the current choice, drawn highlighted.
|
|
23
|
+
* @property {() => import('./model.js').PropertyRow[]} [expand] - Builds the rows shown under this
|
|
24
|
+
* one while it is expanded, letting a value be opened in place. Called on every refresh, so it
|
|
25
|
+
* reads the live object rather than a snapshot.
|
|
26
|
+
* @property {Described[]} [items] - Expanded entries of a collection, each carrying a `label`.
|
|
27
|
+
* @property {string} [label] - The label of an expanded collection entry.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/** The number of collection entries expanded into their own rows. */
|
|
31
|
+
const MAX_ITEMS = 12;
|
|
32
|
+
|
|
33
|
+
/** The number of characters of a string shown before it is cut. */
|
|
34
|
+
const MAX_STRING = 96;
|
|
35
|
+
|
|
36
|
+
/** Collections of primitives up to this length are printed inline. */
|
|
37
|
+
const INLINE_ARRAY = 8;
|
|
38
|
+
|
|
39
|
+
const _euler = new Vec3();
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Formats a number compactly: integers as they are, everything else to four decimals with the
|
|
43
|
+
* trailing zeros removed (so float noise like 1.8e-15 reads as 0), and huge magnitudes in
|
|
44
|
+
* exponent form.
|
|
45
|
+
*
|
|
46
|
+
* @param {number} n - The number.
|
|
47
|
+
* @returns {string} The formatted number.
|
|
48
|
+
*/
|
|
49
|
+
function formatNumber(n) {
|
|
50
|
+
if (Number.isInteger(n) || !Number.isFinite(n)) {
|
|
51
|
+
return String(n);
|
|
52
|
+
}
|
|
53
|
+
if (Math.abs(n) >= 1e7) {
|
|
54
|
+
return n.toExponential(3);
|
|
55
|
+
}
|
|
56
|
+
return String(parseFloat(n.toFixed(4)));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @param {ArrayLike<number>} components - The vector components.
|
|
61
|
+
* @returns {string} The components in parentheses.
|
|
62
|
+
*/
|
|
63
|
+
function tuple(components) {
|
|
64
|
+
return `(${Array.from(components, formatNumber).join(', ')})`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* @param {*} value - Any object.
|
|
69
|
+
* @returns {string} The name of the object's class.
|
|
70
|
+
*/
|
|
71
|
+
function typeName(value) {
|
|
72
|
+
return value?.constructor?.name || 'Object';
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @param {string} text - The text.
|
|
77
|
+
* @returns {string} The text quoted, cut to {@link MAX_STRING} characters.
|
|
78
|
+
*/
|
|
79
|
+
function quote(text) {
|
|
80
|
+
return JSON.stringify(text.length > MAX_STRING ? `${text.slice(0, MAX_STRING)}…` : text);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Expands the first {@link MAX_ITEMS} entries of an iterable into labelled items.
|
|
85
|
+
*
|
|
86
|
+
* @param {Iterable<[string, *]>} entries - Label and value pairs.
|
|
87
|
+
* @param {number} count - The total number of entries.
|
|
88
|
+
* @returns {Described[]} The items.
|
|
89
|
+
*/
|
|
90
|
+
function expandItems(entries, count) {
|
|
91
|
+
const items = [];
|
|
92
|
+
for (const [label, value] of entries) {
|
|
93
|
+
if (items.length >= MAX_ITEMS) {
|
|
94
|
+
items.push({ label: '', text: `… ${count - MAX_ITEMS} more`, cls: 'null' });
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
items.push({ label, ...describeValue(value, 1) });
|
|
98
|
+
}
|
|
99
|
+
return items;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Summarizes a value for display. Engine types get a purpose-built one-line summary; nodes come
|
|
104
|
+
* back with a `target` so the UI can link to them; arrays, sets and maps expand their first
|
|
105
|
+
* entries into `items` at the top level only.
|
|
106
|
+
*
|
|
107
|
+
* @param {*} value - The value to describe.
|
|
108
|
+
* @param {number} [depth] - The nesting depth. Nested values are kept to a short type name.
|
|
109
|
+
* @returns {Described} The description.
|
|
110
|
+
*/
|
|
111
|
+
function describeValue(value, depth = 0) {
|
|
112
|
+
if (value === null) return { text: 'null', cls: 'null' };
|
|
113
|
+
if (value === undefined) return { text: 'undefined', cls: 'null' };
|
|
114
|
+
|
|
115
|
+
switch (typeof value) {
|
|
116
|
+
case 'boolean': return { text: String(value), cls: 'bool' };
|
|
117
|
+
case 'number': return { text: formatNumber(value), cls: 'num' };
|
|
118
|
+
case 'bigint': return { text: `${value}n`, cls: 'num' };
|
|
119
|
+
case 'string': return { text: quote(value), cls: 'str' };
|
|
120
|
+
case 'symbol': return { text: value.toString(), cls: 'str' };
|
|
121
|
+
case 'function': return { text: `ƒ ${value.name || 'anonymous'}`, cls: 'null' };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// math
|
|
125
|
+
if (value instanceof Vec2) return { text: tuple([value.x, value.y]), cls: 'num' };
|
|
126
|
+
if (value instanceof Vec3) return { text: tuple([value.x, value.y, value.z]), cls: 'num' };
|
|
127
|
+
if (value instanceof Vec4) return { text: tuple([value.x, value.y, value.z, value.w]), cls: 'num' };
|
|
128
|
+
if (value instanceof Quat) {
|
|
129
|
+
value.getEulerAngles(_euler);
|
|
130
|
+
return {
|
|
131
|
+
text: `${tuple([value.x, value.y, value.z, value.w])} euler ${tuple([_euler.x, _euler.y, _euler.z])}`,
|
|
132
|
+
cls: 'num'
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
if (value instanceof Color) {
|
|
136
|
+
const c = v => Math.round(Math.min(1, Math.max(0, v)) * 255);
|
|
137
|
+
return {
|
|
138
|
+
text: tuple([value.r, value.g, value.b, value.a]),
|
|
139
|
+
cls: 'num',
|
|
140
|
+
swatch: `rgba(${c(value.r)}, ${c(value.g)}, ${c(value.b)}, ${Math.min(1, Math.max(0, value.a))})`
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
if (value instanceof Mat4 || value instanceof Mat3) {
|
|
144
|
+
return { text: `${typeName(value)} [${Array.from(value.data, formatNumber).join(', ')}]`, cls: 'num' };
|
|
145
|
+
}
|
|
146
|
+
if (value instanceof OrientedBox) {
|
|
147
|
+
return { text: `OrientedBox halfExtents ${tuple([value.halfExtents.x, value.halfExtents.y, value.halfExtents.z])}`, cls: 'num' };
|
|
148
|
+
}
|
|
149
|
+
if (value instanceof BoundingBox) {
|
|
150
|
+
const { center: c, halfExtents: h } = value;
|
|
151
|
+
return { text: `center ${tuple([c.x, c.y, c.z])} halfExtents ${tuple([h.x, h.y, h.z])}`, cls: 'num' };
|
|
152
|
+
}
|
|
153
|
+
if (value instanceof Curve) return { text: `Curve (${value.keys.length} keys)`, cls: 'obj' };
|
|
154
|
+
if (value instanceof CurveSet) return { text: `CurveSet (${value.curves.length} curves)`, cls: 'obj' };
|
|
155
|
+
|
|
156
|
+
// scene graph
|
|
157
|
+
if (value instanceof Entity) return { text: `Entity "${value.name}"`, cls: 'ref', target: value };
|
|
158
|
+
if (value instanceof GraphNode) return { text: `GraphNode "${value.name}"`, cls: 'ref', target: value };
|
|
159
|
+
if (value instanceof Tags) return describeValue(value.list(), depth);
|
|
160
|
+
|
|
161
|
+
// resources
|
|
162
|
+
if (value instanceof Asset) {
|
|
163
|
+
return {
|
|
164
|
+
text: `Asset #${value.id} "${value.name}" (${value.type}${value.loaded ? ', loaded' : ''})`,
|
|
165
|
+
cls: 'obj',
|
|
166
|
+
target: value
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
if (value instanceof Texture) {
|
|
170
|
+
const shape = value.cubemap ? ' cubemap' : (value.volume ? ` ×${value.depth}` : (value.array ? ` [${value.arrayLength}]` : ''));
|
|
171
|
+
return {
|
|
172
|
+
text: `Texture "${value.name}" ${value.width}×${value.height}${shape} ${pixelFormatInfo.get(value.format)?.name ?? value.format}${value.mipmaps ? ' mips' : ''}`,
|
|
173
|
+
cls: 'obj',
|
|
174
|
+
target: value
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
if (value instanceof VertexBuffer) return { text: `VertexBuffer ${value.numVertices} vertices`, cls: 'obj', target: value };
|
|
178
|
+
if (value instanceof IndexBuffer) return { text: `IndexBuffer ${value.numIndices} indices`, cls: 'obj', target: value };
|
|
179
|
+
if (isUniformBuffer(value)) return { text: `UniformBuffer ${value.format?.byteSize ?? 0} B`, cls: 'obj', target: value };
|
|
180
|
+
if (value instanceof StorageBuffer) return { text: `StorageBuffer ${value.byteSize} B`, cls: 'obj', target: value };
|
|
181
|
+
if (value instanceof RenderTarget) {
|
|
182
|
+
return { text: `RenderTarget "${value.name}" ${value.width}×${value.height}`, cls: 'obj' };
|
|
183
|
+
}
|
|
184
|
+
if (value instanceof Material) return { text: `${typeName(value)} "${value.name}"`, cls: 'obj', target: value };
|
|
185
|
+
if (value instanceof Shader) {
|
|
186
|
+
return { text: `Shader #${value.id} "${value.name}"${value.failed ? ' (failed)' : value.ready ? '' : ' (compiling)'}`, cls: 'obj', target: value };
|
|
187
|
+
}
|
|
188
|
+
if (value instanceof Mesh) {
|
|
189
|
+
const prim = value.primitive?.[0];
|
|
190
|
+
return {
|
|
191
|
+
text: `Mesh ${value.vertexBuffer?.numVertices ?? 0} verts${prim ? `, ${prim.count} ${prim.indexed ? 'indices' : 'vertices drawn'}` : ''}`,
|
|
192
|
+
cls: 'obj',
|
|
193
|
+
target: value
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
if (value instanceof MeshInstance) {
|
|
197
|
+
return {
|
|
198
|
+
text: `MeshInstance material "${value.material?.name ?? ''}" ${value.mesh?.vertexBuffer?.numVertices ?? 0} verts${value.visible ? '' : ' (hidden)'}`,
|
|
199
|
+
cls: 'obj',
|
|
200
|
+
target: value.node
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (value instanceof Layer) return { text: `Layer "${value.name}" (id ${value.id})`, cls: 'obj' };
|
|
204
|
+
if (value instanceof Sprite) return { text: `Sprite ${value.frameKeys?.length ?? 0} frames`, cls: 'obj' };
|
|
205
|
+
|
|
206
|
+
// DOM
|
|
207
|
+
if (globalThis.Element && value instanceof globalThis.Element) {
|
|
208
|
+
const cls = value.className ? `.${String(value.className).trim().split(/\s+/).join('.')}` : '';
|
|
209
|
+
return { text: `<${value.tagName.toLowerCase()}${value.id ? `#${value.id}` : ''}${cls}>`, cls: 'obj' };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// collections
|
|
213
|
+
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
|
|
214
|
+
const inline = value.length <= INLINE_ARRAY ? ` ${tuple(value)}` : '';
|
|
215
|
+
return { text: `${typeName(value)}[${value.length}]${inline}`, cls: 'num' };
|
|
216
|
+
}
|
|
217
|
+
if (Array.isArray(value)) {
|
|
218
|
+
const n = value.length;
|
|
219
|
+
if (n === 0) return { text: '[]', cls: 'null' };
|
|
220
|
+
const primitives = value.every(v => v === null || typeof v !== 'object');
|
|
221
|
+
if (primitives && n <= INLINE_ARRAY) {
|
|
222
|
+
return { text: `[${value.map(v => describeValue(v, depth + 1).text).join(', ')}]`, cls: 'obj' };
|
|
223
|
+
}
|
|
224
|
+
const result = { text: `Array(${n}) of ${primitives ? typeof value[0] : typeName(value[0])}`, cls: 'obj' };
|
|
225
|
+
if (depth === 0) {
|
|
226
|
+
result.items = expandItems(value.map((v, i) => [`[${i}]`, v]), n);
|
|
227
|
+
}
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
if (value instanceof Set) {
|
|
231
|
+
const result = { text: `Set(${value.size})`, cls: 'obj' };
|
|
232
|
+
if (depth === 0 && value.size) {
|
|
233
|
+
result.items = expandItems(Array.from(value, (v, i) => [`[${i}]`, v]), value.size);
|
|
234
|
+
}
|
|
235
|
+
return result;
|
|
236
|
+
}
|
|
237
|
+
if (value instanceof Map) {
|
|
238
|
+
const result = { text: `Map(${value.size})`, cls: 'obj' };
|
|
239
|
+
if (depth === 0 && value.size) {
|
|
240
|
+
result.items = expandItems(Array.from(value, ([k, v]) => [describeValue(k, 1).text, v]), value.size);
|
|
241
|
+
}
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// anything else: class name plus a few public fields at the top level
|
|
246
|
+
const name = typeName(value);
|
|
247
|
+
if (depth > 0) return { text: name, cls: 'obj' };
|
|
248
|
+
const keys = Object.keys(value).filter(k => !k.startsWith('_') && typeof value[k] !== 'function').slice(0, 6);
|
|
249
|
+
if (keys.length === 0) return { text: name, cls: 'obj' };
|
|
250
|
+
return {
|
|
251
|
+
text: `${name} { ${keys.map(k => `${k}: ${describeValue(value[k], depth + 1).text}`).join(', ')} }`,
|
|
252
|
+
cls: 'obj'
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Lists the public properties of an object: accessors with a getter found on its prototype chain
|
|
258
|
+
* (most derived class first, in declaration order), followed by its own enumerable data fields.
|
|
259
|
+
* Names starting with an underscore, methods and anything in `skip` are left out.
|
|
260
|
+
*
|
|
261
|
+
* @param {object} obj - The object to reflect on.
|
|
262
|
+
* @param {object[]} stopPrototypes - Prototypes at which to stop walking the chain. These and
|
|
263
|
+
* anything above them are not searched.
|
|
264
|
+
* @param {string[]} [skip] - Property names to leave out.
|
|
265
|
+
* @returns {string[]} The property names.
|
|
266
|
+
*/
|
|
267
|
+
function collectProperties(obj, stopPrototypes, skip = []) {
|
|
268
|
+
const names = [];
|
|
269
|
+
const seen = new Set(skip);
|
|
270
|
+
|
|
271
|
+
for (let proto = Object.getPrototypeOf(obj); proto && proto !== Object.prototype; proto = Object.getPrototypeOf(proto)) {
|
|
272
|
+
if (stopPrototypes.includes(proto)) break;
|
|
273
|
+
for (const name of Object.getOwnPropertyNames(proto)) {
|
|
274
|
+
if (seen.has(name) || name.startsWith('_') || name === 'constructor') continue;
|
|
275
|
+
const descriptor = Object.getOwnPropertyDescriptor(proto, name);
|
|
276
|
+
if (!descriptor?.get) continue;
|
|
277
|
+
seen.add(name);
|
|
278
|
+
names.push(name);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
for (const name of Object.keys(obj)) {
|
|
283
|
+
if (seen.has(name) || name.startsWith('_') || typeof obj[name] === 'function') continue;
|
|
284
|
+
seen.add(name);
|
|
285
|
+
names.push(name);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return names;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* The engine does not export `UniformBuffer`, so a uniform buffer is recognized by its shape: a
|
|
293
|
+
* uniform buffer format, and the persistent flag only uniform buffers carry.
|
|
294
|
+
*
|
|
295
|
+
* @param {*} value - A value.
|
|
296
|
+
* @returns {boolean} Whether it is a uniform buffer.
|
|
297
|
+
*/
|
|
298
|
+
function isUniformBuffer(value) {
|
|
299
|
+
return !!value && typeof value === 'object' && Array.isArray(value.format?.uniforms) && typeof value.persistent === 'boolean';
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export { collectProperties, describeValue, formatNumber, isUniformBuffer };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { Script } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
import { Inspector } from './inspector.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hosts an {@link Inspector} on an entity, so the debug panel can be added to a project from the
|
|
7
|
+
* Editor like any other script. Attach it to a dedicated entity at the root; the panel appears
|
|
8
|
+
* when the script initializes and goes away with it.
|
|
9
|
+
*
|
|
10
|
+
* ```javascript
|
|
11
|
+
* import { EntityInspector } from '@playcanvas/inspector';
|
|
12
|
+
*
|
|
13
|
+
* const entity = new Entity('inspector');
|
|
14
|
+
* entity.addComponent('script');
|
|
15
|
+
* entity.script.create(EntityInspector, {
|
|
16
|
+
* properties: { dock: 'left', visible: false }
|
|
17
|
+
* });
|
|
18
|
+
* app.root.addChild(entity);
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* The hosting entity and its ancestors get no enabled checkbox in the hierarchy, so the panel
|
|
22
|
+
* cannot switch itself off. Everything else is the {@link Inspector} API, reachable through
|
|
23
|
+
* {@link EntityInspector#inspector}.
|
|
24
|
+
*/
|
|
25
|
+
class EntityInspector extends Script {
|
|
26
|
+
static scriptName = 'entityInspector';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Whether the panel is shown. Toggled by {@link toggleKey}.
|
|
30
|
+
*
|
|
31
|
+
* @attribute
|
|
32
|
+
* @type {boolean}
|
|
33
|
+
*/
|
|
34
|
+
visible = true;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The side of the viewport the panel docks to.
|
|
38
|
+
*
|
|
39
|
+
* @attribute
|
|
40
|
+
* @type {'left'|'right'}
|
|
41
|
+
*/
|
|
42
|
+
dock = 'right';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The width of the docked panel in CSS pixels.
|
|
46
|
+
*
|
|
47
|
+
* @attribute
|
|
48
|
+
* @type {number}
|
|
49
|
+
* @range [240, 1600]
|
|
50
|
+
*/
|
|
51
|
+
width = 420;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* A gap left above the panel in CSS pixels, to keep it clear of other overlays.
|
|
55
|
+
*
|
|
56
|
+
* @attribute
|
|
57
|
+
* @type {number}
|
|
58
|
+
* @range [0, 400]
|
|
59
|
+
*/
|
|
60
|
+
top = 0;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The `KeyboardEvent.code` or `key` that shows and hides the panel. Empty disables the key.
|
|
64
|
+
*
|
|
65
|
+
* @attribute
|
|
66
|
+
* @type {string}
|
|
67
|
+
*/
|
|
68
|
+
toggleKey = 'Backquote';
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The key that pauses and resumes the app. Empty disables the key.
|
|
72
|
+
*
|
|
73
|
+
* @attribute
|
|
74
|
+
* @type {string}
|
|
75
|
+
*/
|
|
76
|
+
pauseKey = 'F9';
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The key that advances one frame while paused. Empty disables the key.
|
|
80
|
+
*
|
|
81
|
+
* @attribute
|
|
82
|
+
* @type {string}
|
|
83
|
+
*/
|
|
84
|
+
stepKey = 'F10';
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @type {Inspector|null}
|
|
88
|
+
* @private
|
|
89
|
+
*/
|
|
90
|
+
_inspector = null;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* The inspector this script hosts, or null before the script initializes.
|
|
94
|
+
*
|
|
95
|
+
* @type {Inspector|null}
|
|
96
|
+
*/
|
|
97
|
+
get inspector() {
|
|
98
|
+
return this._inspector;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
initialize() {
|
|
102
|
+
this._inspector = new Inspector(this.app, {
|
|
103
|
+
visible: this.visible,
|
|
104
|
+
dock: this.dock,
|
|
105
|
+
width: this.width,
|
|
106
|
+
top: this.top,
|
|
107
|
+
toggleKey: this.toggleKey,
|
|
108
|
+
pauseKey: this.pauseKey,
|
|
109
|
+
stepKey: this.stepKey,
|
|
110
|
+
lockedNode: this.entity
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// disabling the script hides the panel; enabling it restores whatever the user left
|
|
114
|
+
this.on('disable', () => {
|
|
115
|
+
if (this._inspector) {
|
|
116
|
+
this.visible = this._inspector.visible;
|
|
117
|
+
this._inspector.visible = false;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
this.on('enable', () => {
|
|
121
|
+
if (this._inspector) this._inspector.visible = this.visible;
|
|
122
|
+
});
|
|
123
|
+
this.on('destroy', () => {
|
|
124
|
+
this._inspector?.destroy();
|
|
125
|
+
this._inspector = null;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export { EntityInspector };
|