@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
package/LICENSE
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2011-2026 PlayCanvas Ltd.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# PlayCanvas Inspector
|
|
2
|
+
|
|
3
|
+
A debug panel for running [PlayCanvas](https://github.com/playcanvas/engine) apps. It shows the entity
|
|
4
|
+
hierarchy and every component's properties, and tabs for the cameras, assets, frame graph, render
|
|
5
|
+
targets, textures, meshes, materials, scripts, memory, shaders and physics of the app, with live
|
|
6
|
+
previews of textures and render targets, picking and flying in the view, and a debug frame mode
|
|
7
|
+
that steps through the draw calls of a render pass.
|
|
8
|
+
|
|
9
|
+
The inspector is read-only, apart from toggling entities on and off, pausing and stepping the app,
|
|
10
|
+
and view-only overrides (render modes, wireframe, a flown camera) it removes when it closes.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```sh
|
|
15
|
+
npm install @playcanvas/inspector
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
It requires PlayCanvas Engine 2.23 or newer. `playcanvas` is a peer dependency: the inspector works
|
|
19
|
+
on your app's own engine, so it must be the same `playcanvas` package your app imports.
|
|
20
|
+
|
|
21
|
+
TypeScript declarations are included, generated from the JSDoc of the sources.
|
|
22
|
+
|
|
23
|
+
## Use
|
|
24
|
+
|
|
25
|
+
```javascript
|
|
26
|
+
import { Inspector } from '@playcanvas/inspector';
|
|
27
|
+
|
|
28
|
+
const inspector = new Inspector(app, { dock: 'left', width: 480 });
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Press <kbd>`</kbd> to show or hide the panel, <kbd>F9</kbd> to pause and <kbd>F10</kbd> to step a
|
|
32
|
+
frame while paused. The keys, dock side, width and more are options of the constructor.
|
|
33
|
+
|
|
34
|
+
Or, from the Editor or a scripted scene, as a script on an entity:
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
import { EntityInspector } from '@playcanvas/inspector';
|
|
38
|
+
|
|
39
|
+
const entity = new Entity('inspector');
|
|
40
|
+
entity.addComponent('script');
|
|
41
|
+
entity.script.create(EntityInspector, { properties: { dock: 'left' } });
|
|
42
|
+
app.root.addChild(entity);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Engine versions
|
|
46
|
+
|
|
47
|
+
Each inspector release names the engine versions it supports in its `playcanvas` peer dependency.
|
|
48
|
+
The inspector reads engine internals to show what it shows, so it is tied to engine releases
|
|
49
|
+
rather than claiming a wide version range.
|
|
50
|
+
|
|
51
|
+
## The devtools hook
|
|
52
|
+
|
|
53
|
+
From engine 2.23, every PlayCanvas app announces itself to a hook defined on the global object,
|
|
54
|
+
which is how the upcoming browser extension finds apps on any page:
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
globalThis[Symbol.for('playcanvas.inspector')] = {
|
|
58
|
+
register(app, { version, revision, protocol }) {},
|
|
59
|
+
unregister(app) {}
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The hook must be defined before the engine creates the app. `register` is called once each app is
|
|
64
|
+
initialized, and `unregister` when it is destroyed. `protocol` is `1`.
|
|
65
|
+
|
|
66
|
+
## Development
|
|
67
|
+
|
|
68
|
+
Node 22.19 or newer.
|
|
69
|
+
|
|
70
|
+
```sh
|
|
71
|
+
npm install
|
|
72
|
+
npm run lint
|
|
73
|
+
npm test
|
|
74
|
+
npm run build:types # writes the TypeScript declarations to types/
|
|
75
|
+
npm run test:types # compiles a TypeScript usage of the package against them
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The tests run against the `playcanvas` version in `devDependencies`, on the engine's null graphics
|
|
79
|
+
device under jsdom.
|
|
80
|
+
|
|
81
|
+
A few engine helpers the inspector draws with are copied into `src/renderers`, `src/picker` and
|
|
82
|
+
`src/input`, so the inspector does not depend on them being in the app's engine build. See
|
|
83
|
+
[COPIED_FROM.md](COPIED_FROM.md).
|
|
84
|
+
|
|
85
|
+
## Releasing
|
|
86
|
+
|
|
87
|
+
Releases are published to npm from GitHub Actions when a version tag is pushed. Only repository
|
|
88
|
+
admins and maintainers can push tags.
|
|
89
|
+
|
|
90
|
+
1. Update your `main` to the commit to release, with CI green.
|
|
91
|
+
2. Bump the version and tag it:
|
|
92
|
+
|
|
93
|
+
```sh
|
|
94
|
+
npm version minor # or major, or patch
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
3. Push the commit and the tag:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
git push --follow-tags
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
The **Publish** workflow runs the lint and tests, publishes to npm with provenance, and creates a
|
|
104
|
+
GitHub release with generated notes.
|
|
105
|
+
|
|
106
|
+
A tag that does not match the `package.json` version fails the run without publishing.
|
|
107
|
+
|
|
108
|
+
## License
|
|
109
|
+
|
|
110
|
+
MIT
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@playcanvas/inspector",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A debug panel for PlayCanvas apps: the entity hierarchy, assets, frame graph, render targets, textures, meshes, materials, scripts, cameras, memory, shaders and physics of a running app",
|
|
5
|
+
"author": "PlayCanvas <support@playcanvas.com>",
|
|
6
|
+
"homepage": "https://github.com/playcanvas/inspector#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/playcanvas/inspector/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/playcanvas/inspector.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"playcanvas",
|
|
17
|
+
"inspector",
|
|
18
|
+
"debug",
|
|
19
|
+
"devtools",
|
|
20
|
+
"webgl",
|
|
21
|
+
"webgpu"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "src/index.js",
|
|
25
|
+
"types": "types/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./types/index.d.ts",
|
|
29
|
+
"default": "./src/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"src",
|
|
34
|
+
"types",
|
|
35
|
+
"README.md",
|
|
36
|
+
"LICENSE"
|
|
37
|
+
],
|
|
38
|
+
"sideEffects": false,
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=22.19.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"lint": "eslint src test",
|
|
44
|
+
"lint:fix": "eslint --fix src test",
|
|
45
|
+
"test": "mocha --recursive test --timeout 5000",
|
|
46
|
+
"build:types": "tsc",
|
|
47
|
+
"test:types": "tsc -p test/types"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"playcanvas": "^2.23.0-beta.24"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@playcanvas/eslint-config": "3.0.0-beta.8",
|
|
54
|
+
"chai": "6.2.2",
|
|
55
|
+
"eslint": "9.39.5",
|
|
56
|
+
"globals": "17.12.0",
|
|
57
|
+
"jsdom": "30.1.0",
|
|
58
|
+
"mocha": "12.0.2",
|
|
59
|
+
"playcanvas": "2.23.0-beta.24",
|
|
60
|
+
"typescript": "5.9.3"
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { assetUsers, containerParts } from './asset-view.js';
|
|
2
|
+
import { describeValue } from './describe.js';
|
|
3
|
+
import { collectMeshInstances } from './instance-survey.js';
|
|
4
|
+
import { materialsUsingTexture } from './material-view.js';
|
|
5
|
+
import { nodeLabel } from './memory-view.js';
|
|
6
|
+
import { pushRow } from './model.js';
|
|
7
|
+
|
|
8
|
+
/** @import { AppBase } from 'playcanvas' */
|
|
9
|
+
/** @import { Asset } from 'playcanvas' */
|
|
10
|
+
/** @import { Described } from './describe.js' */
|
|
11
|
+
/** @import { PropertySection } from './model.js' */
|
|
12
|
+
|
|
13
|
+
// most users a resource lists
|
|
14
|
+
const MAX_USERS = 100;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The anim component layers playing an animation track: an animation assigned to a layer with
|
|
18
|
+
* `assignAnimation` is given as its track, taken from the asset's resource, so the component
|
|
19
|
+
* never names the asset. Each layer keeps the tracks it plays as the clips of its evaluator.
|
|
20
|
+
*
|
|
21
|
+
* @param {AppBase} app - The app.
|
|
22
|
+
* @param {object} track - The track.
|
|
23
|
+
* @returns {Described[]} One linked entry per clip playing it.
|
|
24
|
+
*/
|
|
25
|
+
function trackUsers(app, track) {
|
|
26
|
+
const users = [];
|
|
27
|
+
app.root?.forEach((node) => {
|
|
28
|
+
const anim = /** @type {any} */ (node).c?.anim;
|
|
29
|
+
for (const layer of anim?.layers ?? []) {
|
|
30
|
+
// the controller of a layer is not exposed, but its evaluator's clips are
|
|
31
|
+
for (const clip of layer._controller?.animEvaluator?.clips ?? []) {
|
|
32
|
+
if (clip.track !== track) continue;
|
|
33
|
+
users.push({ label: 'anim', text: `${nodeLabel(node)} · layer "${layer.name}" · "${clip.name}"`, cls: 'ref', target: node });
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return users;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {AppBase} app - The app.
|
|
42
|
+
* @param {object} material - A material.
|
|
43
|
+
* @returns {Described[]} One linked entry per mesh instance drawing with it.
|
|
44
|
+
*/
|
|
45
|
+
function materialUsers(app, material) {
|
|
46
|
+
return collectMeshInstances(app)
|
|
47
|
+
.filter(({ instance }) => instance.material === material)
|
|
48
|
+
.map(({ instance }) => ({ label: 'mesh instance', text: nodeLabel(instance.node ?? null), cls: 'ref', target: instance.node ?? null }));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* What uses an asset's resource directly, handed over without the asset: the anim layers playing
|
|
53
|
+
* an animation's track, the mesh instances drawing with a material, and the materials sampling a
|
|
54
|
+
* texture. The components referencing the asset itself are found separately, by
|
|
55
|
+
* {@link assetUsers}.
|
|
56
|
+
*
|
|
57
|
+
* @param {AppBase} app - The app.
|
|
58
|
+
* @param {Asset} asset - The asset.
|
|
59
|
+
* @returns {Described[]} One entry per user.
|
|
60
|
+
*/
|
|
61
|
+
function resourceUsers(app, asset) {
|
|
62
|
+
const resource = asset.loaded ? asset.resource : null;
|
|
63
|
+
if (!resource) return [];
|
|
64
|
+
if (asset.type === 'animation') return trackUsers(app, resource);
|
|
65
|
+
if (asset.type === 'material') return materialUsers(app, resource);
|
|
66
|
+
if (asset.type === 'texture') {
|
|
67
|
+
return materialsUsingTexture(app, resource).map(material => ({ label: 'material', ...describeValue(material) }));
|
|
68
|
+
}
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Adds to the Used by section of an asset what uses its resource directly, and for a container,
|
|
74
|
+
* which of its contents are in use: a container itself is rarely referenced, while the renders,
|
|
75
|
+
* materials and animations made of its file are.
|
|
76
|
+
*
|
|
77
|
+
* @param {PropertySection} section - The asset's Used by section.
|
|
78
|
+
* @param {Asset} asset - The asset.
|
|
79
|
+
* @param {AppBase} app - The app.
|
|
80
|
+
*/
|
|
81
|
+
function pushResourceUsage(section, asset, app) {
|
|
82
|
+
const users = resourceUsers(app, asset);
|
|
83
|
+
if (users.length) {
|
|
84
|
+
pushRow(section.rows, 'resource', {
|
|
85
|
+
text: `used directly by ${users.length}`,
|
|
86
|
+
cls: 'obj',
|
|
87
|
+
items: users.slice(0, MAX_USERS)
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const kinds = containerParts(app.assets ?? null).parts.get(asset);
|
|
92
|
+
if (kinds?.length) {
|
|
93
|
+
const used = [];
|
|
94
|
+
for (const [kind, parts] of kinds) {
|
|
95
|
+
for (const part of parts) {
|
|
96
|
+
const count = assetUsers(app, part).length + resourceUsers(app, part).length;
|
|
97
|
+
if (count) used.push({ label: kind, text: `${part.name}, ${count} user${count === 1 ? '' : 's'}`, cls: 'ref', target: part });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
pushRow(section.rows, 'contents', {
|
|
101
|
+
text: used.length ? `${used.length} in use` : 'none in use',
|
|
102
|
+
cls: used.length ? 'obj' : 'null',
|
|
103
|
+
items: used
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export { pushResourceUsage, resourceUsers };
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { Asset } from 'playcanvas';
|
|
2
|
+
|
|
3
|
+
import { collectProperties, describeValue } from './describe.js';
|
|
4
|
+
import { formatBytes, makeSection, push, read, reflectRows } from './model.js';
|
|
5
|
+
|
|
6
|
+
/** @import { AppBase } from 'playcanvas' */
|
|
7
|
+
/** @import { AssetRegistry } from 'playcanvas' */
|
|
8
|
+
/** @import { Component } from 'playcanvas' */
|
|
9
|
+
/** @import { Entity } from 'playcanvas' */
|
|
10
|
+
/** @import { Described } from './describe.js' */
|
|
11
|
+
/** @import { ListRow } from './list-view.js' */
|
|
12
|
+
/** @import { PropertySection } from './model.js' */
|
|
13
|
+
|
|
14
|
+
// asset properties the model shows explicitly, or that are plumbing
|
|
15
|
+
const SKIP_ASSET = [
|
|
16
|
+
'id', 'name', 'type', 'file', 'tags', 'preload', 'loaded', 'loading', 'resource', 'resources',
|
|
17
|
+
'registry', 'urlObject', 'loadFaces'
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/** The orders the list can be sorted in, as selector options. */
|
|
21
|
+
const ASSET_SORTS = [
|
|
22
|
+
['type', 'type'], ['size', 'file size'], ['name', 'name'], ['id', 'id']
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
// the sub-asset lists of a container's resource, in the order they are shown
|
|
26
|
+
const CONTAINER_PARTS = [
|
|
27
|
+
['renders', 'renders'], ['materials', 'materials'], ['textures', 'textures'], ['animations', 'animations'],
|
|
28
|
+
['gsplats', 'gsplats']
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The sub-assets of the loaded containers in a registry. A container asset adds the render,
|
|
33
|
+
* material, animation and gsplat assets it creates to the registry and keeps them on its resource,
|
|
34
|
+
* along with the texture assets it loads, and the model asset once something has asked for it. So
|
|
35
|
+
* the link from a sub-asset to its container is read from the container, not guessed from names,
|
|
36
|
+
* which the textures do not carry. A sub-asset shared by several containers belongs to the first.
|
|
37
|
+
*
|
|
38
|
+
* @param {AssetRegistry|null} registry - The registry.
|
|
39
|
+
* @returns {{ parentOf: Map<Asset, Asset>, parts: Map<Asset, [string, Asset[]][]> }} The container of
|
|
40
|
+
* each sub-asset, and the sub-assets of each container by kind.
|
|
41
|
+
*/
|
|
42
|
+
function containerParts(registry) {
|
|
43
|
+
/** @type {Map<Asset, Asset>} */
|
|
44
|
+
const parentOf = new Map();
|
|
45
|
+
/** @type {Map<Asset, [string, Asset[]][]>} */
|
|
46
|
+
const parts = new Map();
|
|
47
|
+
for (const asset of registry?.list() ?? []) {
|
|
48
|
+
const resource = asset.type === 'container' && asset.loaded ? asset.resource : null;
|
|
49
|
+
if (!resource) continue;
|
|
50
|
+
/** @type {[string, Asset[]][]} */
|
|
51
|
+
const kinds = [];
|
|
52
|
+
const take = (label, list) => {
|
|
53
|
+
const owned = (list ?? []).filter(child => child instanceof Asset && child !== asset && !parentOf.has(child));
|
|
54
|
+
for (const child of owned) parentOf.set(child, asset);
|
|
55
|
+
if (owned.length) kinds.push([label, owned]);
|
|
56
|
+
};
|
|
57
|
+
for (const [field, label] of CONTAINER_PARTS) take(label, resource[field]);
|
|
58
|
+
// made the first time something reads the container's model
|
|
59
|
+
take('model', resource._model ? [resource._model] : []);
|
|
60
|
+
parts.set(asset, kinds);
|
|
61
|
+
}
|
|
62
|
+
return { parentOf, parts };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Cache of {@link assetPropertyNames}, keyed by the prototype the names were found on.
|
|
67
|
+
*
|
|
68
|
+
* @type {Map<object, string[]>}
|
|
69
|
+
*/
|
|
70
|
+
const assetProps = new Map();
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The asset-referencing property names of a component. Components expose these as either an id or
|
|
74
|
+
* an {@link Asset}, and some as an array of either, so the names are what the panel needs to
|
|
75
|
+
* resolve them. The result is cached per component class, since reflection is not free and finding
|
|
76
|
+
* the users of an asset walks every component of the scene. An object whose properties are its own
|
|
77
|
+
* fields rather than accessors of a class is not cached, as its shape says nothing about the next
|
|
78
|
+
* one with the same prototype.
|
|
79
|
+
*
|
|
80
|
+
* @param {Component} component - The component.
|
|
81
|
+
* @returns {string[]} Its asset-referencing property names.
|
|
82
|
+
*/
|
|
83
|
+
function assetPropertyNames(component) {
|
|
84
|
+
const scan = () => collectProperties(component, [], []).filter(name => /asset/i.test(name));
|
|
85
|
+
const proto = Object.getPrototypeOf(component);
|
|
86
|
+
if (!proto || proto === Object.prototype) return scan();
|
|
87
|
+
|
|
88
|
+
let names = assetProps.get(proto);
|
|
89
|
+
if (!names) {
|
|
90
|
+
names = scan();
|
|
91
|
+
assetProps.set(proto, names);
|
|
92
|
+
}
|
|
93
|
+
return names;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Describes the value of an asset-referencing property, resolving ids through the registry so the
|
|
98
|
+
* row links to the asset rather than showing a bare number. Arrays are expanded entry by entry.
|
|
99
|
+
*
|
|
100
|
+
* @param {*} value - An asset, an id, an array of either, or nothing.
|
|
101
|
+
* @param {AssetRegistry|null} registry - The registry to resolve ids in.
|
|
102
|
+
* @returns {Described} The described value.
|
|
103
|
+
*/
|
|
104
|
+
function describeAssetValue(value, registry) {
|
|
105
|
+
if (Array.isArray(value)) {
|
|
106
|
+
const items = value.map((entry, i) => ({ label: `[${i}]`, ...describeAssetValue(entry, registry) }));
|
|
107
|
+
return {
|
|
108
|
+
text: value.length ? `Array(${value.length}) of asset` : '[]',
|
|
109
|
+
cls: value.length ? 'obj' : 'null',
|
|
110
|
+
items: value.length ? items : undefined
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
if (typeof value === 'number') {
|
|
114
|
+
const asset = registry?.get(value) ?? null;
|
|
115
|
+
return asset ? describeValue(asset) : { text: `#${value} (not in the registry)`, cls: 'null' };
|
|
116
|
+
}
|
|
117
|
+
return describeValue(value);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Appends one row per asset-referencing property of a component, with ids resolved to the assets
|
|
122
|
+
* they name.
|
|
123
|
+
*
|
|
124
|
+
* @param {PropertySection} section - The section to append to.
|
|
125
|
+
* @param {Component} component - The component.
|
|
126
|
+
* @returns {string[]} The property names handled, to leave out of the reflected rows.
|
|
127
|
+
*/
|
|
128
|
+
function pushAssetRows(section, component) {
|
|
129
|
+
const names = assetPropertyNames(component);
|
|
130
|
+
const registry = /** @type {any} */ (component).system?.app?.assets ?? null;
|
|
131
|
+
for (const name of names) {
|
|
132
|
+
let value;
|
|
133
|
+
try {
|
|
134
|
+
value = /** @type {any} */ (component)[name];
|
|
135
|
+
} catch (e) {
|
|
136
|
+
push(section, name, { text: `<${e?.message ?? e}>`, cls: 'err' });
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
push(section, name, describeAssetValue(value, registry));
|
|
140
|
+
}
|
|
141
|
+
return names;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The size of an asset's file. Only a descriptor from a project manifest carries one, so an asset
|
|
146
|
+
* loaded from a plain url, and every sub-asset of a container, has a file of unknown size. An
|
|
147
|
+
* asset built in memory, such as a sprite of an atlas, has nothing to download at all.
|
|
148
|
+
*
|
|
149
|
+
* @param {Asset} asset - An asset.
|
|
150
|
+
* @returns {string} The size, or why there is none.
|
|
151
|
+
*/
|
|
152
|
+
function sizeText(asset) {
|
|
153
|
+
if (asset.file?.size) return formatBytes(asset.file.size);
|
|
154
|
+
return asset.file?.url ? 'size unknown' : 'no file';
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @param {Asset} asset - An asset.
|
|
159
|
+
* @returns {string} Its load state.
|
|
160
|
+
*/
|
|
161
|
+
function stateName(asset) {
|
|
162
|
+
return asset.loaded ? 'loaded' : asset.loading ? 'loading' : 'not loaded';
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Every asset in the registry, in the requested order. Assets with no file sort last by size, and
|
|
167
|
+
* ties fall back to the name so the list does not jitter between refreshes.
|
|
168
|
+
*
|
|
169
|
+
* @param {AssetRegistry|null} registry - The registry.
|
|
170
|
+
* @param {string} [sort] - One of {@link ASSET_SORTS}.
|
|
171
|
+
* @returns {Asset[]} The assets.
|
|
172
|
+
*/
|
|
173
|
+
function collectAssets(registry, sort = 'type') {
|
|
174
|
+
const assets = registry ? [...registry.list()] : [];
|
|
175
|
+
const byName = (a, b) => a.name.localeCompare(b.name) || a.id - b.id;
|
|
176
|
+
if (sort === 'name') return assets.sort(byName);
|
|
177
|
+
if (sort === 'id') return assets.sort((a, b) => a.id - b.id);
|
|
178
|
+
if (sort === 'size') return assets.sort((a, b) => (b.file?.size ?? 0) - (a.file?.size ?? 0) || byName(a, b));
|
|
179
|
+
return assets.sort((a, b) => a.type.localeCompare(b.type) || byName(a, b));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The asset each loaded resource came from, so a panel showing a resource can name its source.
|
|
184
|
+
* An asset can produce several resources, such as the faces of a cubemap.
|
|
185
|
+
*
|
|
186
|
+
* @param {AssetRegistry|null} registry - The registry.
|
|
187
|
+
* @returns {Map<object, Asset>} The asset by resource.
|
|
188
|
+
*/
|
|
189
|
+
function resourceAssets(registry) {
|
|
190
|
+
/** @type {Map<object, Asset>} */
|
|
191
|
+
const assets = new Map();
|
|
192
|
+
for (const asset of registry?.list() ?? []) {
|
|
193
|
+
for (const resource of asset.resources ?? []) {
|
|
194
|
+
if (resource && typeof resource === 'object' && !assets.has(resource)) assets.set(resource, asset);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return assets;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* The components referencing an asset, found by walking the scene: neither the asset nor the
|
|
202
|
+
* registry tracks who uses it.
|
|
203
|
+
*
|
|
204
|
+
* @param {AppBase} app - The app.
|
|
205
|
+
* @param {Asset} asset - The asset.
|
|
206
|
+
* @returns {{ entity: Entity, name: string, property: string }[]} One entry per referencing property.
|
|
207
|
+
*/
|
|
208
|
+
function assetUsers(app, asset) {
|
|
209
|
+
const users = [];
|
|
210
|
+
const uses = (value) => {
|
|
211
|
+
if (Array.isArray(value)) return value.some(uses);
|
|
212
|
+
return value === asset || value === asset.id;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/** @param {Entity} entity - The entity to test, with its descendants. */
|
|
216
|
+
const walk = (entity) => {
|
|
217
|
+
const components = /** @type {any} */ (entity).c;
|
|
218
|
+
for (const name in components) {
|
|
219
|
+
const component = components[name];
|
|
220
|
+
for (const property of assetPropertyNames(component)) {
|
|
221
|
+
try {
|
|
222
|
+
if (uses(component[property])) users.push({ entity, name, property });
|
|
223
|
+
} catch (e) {
|
|
224
|
+
// a property that throws cannot be telling us about this asset
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const child of entity.children) walk(/** @type {any} */ (child));
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
if (app.root) walk(/** @type {any} */ (app.root));
|
|
232
|
+
return users;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* One row per asset in the registry, dimmed while it is not loaded. The sub-assets of a container
|
|
237
|
+
* follow it, indented and bracketed with it, in the order the container keeps them, whatever the
|
|
238
|
+
* sort; the rest of the list is sorted as asked.
|
|
239
|
+
*
|
|
240
|
+
* @param {AssetRegistry|null} registry - The registry.
|
|
241
|
+
* @param {string} sort - The order to list them in.
|
|
242
|
+
* @returns {ListRow[]} The rows.
|
|
243
|
+
*/
|
|
244
|
+
function assetRows(registry, sort) {
|
|
245
|
+
const { parentOf, parts } = containerParts(registry);
|
|
246
|
+
|
|
247
|
+
const row = (asset, parent) => {
|
|
248
|
+
const name = asset.name || '(unnamed)';
|
|
249
|
+
const size = parent ? 'embedded' : sizeText(asset);
|
|
250
|
+
const cells = [{ text: name, cls: 'pci-cell-name' }];
|
|
251
|
+
cells.push({ text: asset.type, cls: 'pci-cell-tag pci-cell-tag-info' });
|
|
252
|
+
if (!asset.loaded) cells.push({ text: stateName(asset), cls: 'pci-cell-tag' });
|
|
253
|
+
cells.push({ text: size, cls: 'pci-cell-info' });
|
|
254
|
+
cells.push({ text: `#${asset.id}`, cls: 'pci-cell-info pci-cell-right' });
|
|
255
|
+
const title = `${name}\n${asset.type}, ${stateName(asset)}, ${size}` +
|
|
256
|
+
`${parent ? `\nin container "${parent.name}"` : asset.file?.url ? `\n${asset.file.url}` : ''}`;
|
|
257
|
+
return { key: `asset${asset.id}`, item: asset, name, dim: !asset.loaded, title, cells };
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/** @type {ListRow[]} */
|
|
261
|
+
const rows = [];
|
|
262
|
+
for (const asset of collectAssets(registry, sort)) {
|
|
263
|
+
if (parentOf.has(asset)) continue;
|
|
264
|
+
const containerRow = row(asset, null);
|
|
265
|
+
rows.push(containerRow);
|
|
266
|
+
|
|
267
|
+
const children = (parts.get(asset) ?? []).flatMap(([, list]) => list);
|
|
268
|
+
if (!children.length) continue;
|
|
269
|
+
// a filter naming the container keeps what it holds, and one naming a part keeps the container
|
|
270
|
+
const names = children.map(child => child.name.toLowerCase());
|
|
271
|
+
const own = containerRow.name.toLowerCase();
|
|
272
|
+
containerRow.matches = filter => own.includes(filter) || names.some(name => name.includes(filter));
|
|
273
|
+
containerRow.guides = { lanes: 1, segments: ['start'], tick: 0 };
|
|
274
|
+
children.forEach((child, i) => {
|
|
275
|
+
const childRow = row(child, asset);
|
|
276
|
+
childRow.indent = 1;
|
|
277
|
+
childRow.matches = filter => names[i].includes(filter) || own.includes(filter);
|
|
278
|
+
childRow.guides = { lanes: 1, segments: [i === children.length - 1 ? 'end' : 'mid'], tick: -1 };
|
|
279
|
+
rows.push(childRow);
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// every row takes the same gutter, so the names line up whether or not a row is bracketed
|
|
284
|
+
if (rows.some(r => r.guides)) {
|
|
285
|
+
for (const r of rows) r.guides ??= { lanes: 1, segments: [null], tick: -1 };
|
|
286
|
+
}
|
|
287
|
+
return rows;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Everything the property view shows for an asset: where it came from, the resources it loaded
|
|
292
|
+
* into (linked), the components using it (linked), and every other public property.
|
|
293
|
+
*
|
|
294
|
+
* @param {Asset} asset - The asset.
|
|
295
|
+
* @param {{ app: AppBase }} ctx - The app, to find the asset's users.
|
|
296
|
+
* @returns {PropertySection[]} The sections.
|
|
297
|
+
*/
|
|
298
|
+
function buildAssetModel(asset, ctx) {
|
|
299
|
+
const sections = [];
|
|
300
|
+
const file = asset.file;
|
|
301
|
+
|
|
302
|
+
const general = makeSection('asset', 'Asset');
|
|
303
|
+
push(general, 'name', describeValue(asset.name));
|
|
304
|
+
push(general, 'id', describeValue(asset.id));
|
|
305
|
+
push(general, 'type', { text: asset.type, cls: 'obj' });
|
|
306
|
+
const { parentOf, parts } = containerParts(ctx.app?.assets ?? null);
|
|
307
|
+
const container = parentOf.get(asset) ?? null;
|
|
308
|
+
if (container) push(general, 'container', describeValue(container));
|
|
309
|
+
push(general, 'state', { text: stateName(asset), cls: asset.loaded ? 'bool' : 'null' });
|
|
310
|
+
push(general, 'preload', read(asset, 'preload'));
|
|
311
|
+
if (container) {
|
|
312
|
+
push(general, 'file', { text: `embedded in "${container.name}"`, cls: 'obj' });
|
|
313
|
+
} else {
|
|
314
|
+
push(general, 'url', file?.url ? describeValue(file.url) : { text: 'no file', cls: 'null' });
|
|
315
|
+
push(general, 'file size', { text: sizeText(asset), cls: file?.size ? 'num' : 'null' });
|
|
316
|
+
}
|
|
317
|
+
if (asset.type === 'cubemap') push(general, 'load faces', read(asset, 'loadFaces'));
|
|
318
|
+
if (file?.hash) push(general, 'hash', describeValue(file.hash));
|
|
319
|
+
push(general, 'tags', describeValue(asset.tags));
|
|
320
|
+
sections.push(general);
|
|
321
|
+
|
|
322
|
+
const resources = makeSection('resources', 'Resources');
|
|
323
|
+
if (!asset.loaded) {
|
|
324
|
+
push(resources, 'resource', { text: `${stateName(asset)}, nothing to show yet`, cls: 'null' });
|
|
325
|
+
} else if ((asset.resources?.length ?? 0) > 1) {
|
|
326
|
+
push(resources, 'resources', describeValue(asset.resources));
|
|
327
|
+
} else {
|
|
328
|
+
push(resources, 'resource', describeValue(asset.resource));
|
|
329
|
+
}
|
|
330
|
+
sections.push(resources);
|
|
331
|
+
|
|
332
|
+
// what a container made of its file, by kind, each linking to its asset
|
|
333
|
+
const kinds = parts.get(asset);
|
|
334
|
+
if (kinds?.length) {
|
|
335
|
+
const contents = makeSection('contents', 'Contents');
|
|
336
|
+
for (const [label, list] of kinds) {
|
|
337
|
+
push(contents, label, {
|
|
338
|
+
text: `${list.length} asset${list.length === 1 ? '' : 's'}`,
|
|
339
|
+
cls: 'obj',
|
|
340
|
+
items: list.map((child, i) => ({ label: `[${i}]`, ...describeValue(child) }))
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
sections.push(contents);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const users = assetUsers(ctx.app, asset);
|
|
347
|
+
const usage = makeSection('users', 'Used by');
|
|
348
|
+
push(usage, 'components', {
|
|
349
|
+
text: users.length ? `${users.length} component${users.length === 1 ? '' : 's'}` : 'no component in the scene',
|
|
350
|
+
cls: users.length ? 'obj' : 'null',
|
|
351
|
+
items: users.map(user => ({
|
|
352
|
+
label: `${user.name}.${user.property}`,
|
|
353
|
+
text: `Entity "${user.entity.name}"`,
|
|
354
|
+
cls: 'ref',
|
|
355
|
+
target: user.entity
|
|
356
|
+
}))
|
|
357
|
+
});
|
|
358
|
+
sections.push(usage);
|
|
359
|
+
|
|
360
|
+
const rest = makeSection('props', 'Properties');
|
|
361
|
+
reflectRows(rest, asset, [], SKIP_ASSET);
|
|
362
|
+
if (rest.rows.length) sections.push(rest);
|
|
363
|
+
|
|
364
|
+
return sections;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
export {
|
|
368
|
+
ASSET_SORTS, assetRows, assetUsers, buildAssetModel, collectAssets, containerParts, describeAssetValue, pushAssetRows,
|
|
369
|
+
resourceAssets, stateName
|
|
370
|
+
};
|