@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,314 @@
1
+ import { BODYTYPE_DYNAMIC, BODYTYPE_KINEMATIC, BODYTYPE_STATIC, Mat4, Quat, Vec3 } from 'playcanvas';
2
+
3
+ import { formatNumber } from './describe.js';
4
+
5
+ /** @import { AppBase } from 'playcanvas' */
6
+ /** @import { CollisionComponent } from 'playcanvas' */
7
+ /** @import { RigidBodyComponent } from 'playcanvas' */
8
+ /** @import { Entity } from 'playcanvas' */
9
+ /** @import { WireRenderer } from './renderers/wire-renderer.js' */
10
+ /** @import { ListRow } from './list-view.js' */
11
+
12
+ /**
13
+ * @typedef {object} PhysicsStats
14
+ * @property {boolean} system - Whether a rigid body component system is registered.
15
+ * @property {boolean} world - Whether the physics world exists, i.e. the backend is loaded.
16
+ * @property {number} bodies - The number of rigid body components.
17
+ * @property {number} active - The number of awake dynamic bodies.
18
+ * @property {number} contacts - The number of contact points in the last step.
19
+ * @property {number} joints - The number of joint components.
20
+ * @property {number} time - The CPU time of the last physics step, in milliseconds.
21
+ */
22
+
23
+ const _pos = new Vec3();
24
+ const _rot = new Quat();
25
+ const _mat = new Mat4();
26
+ const _a = new Vec3();
27
+ const _b = new Vec3();
28
+ const _dir = new Vec3();
29
+
30
+ /**
31
+ * @param {AppBase} app - The app.
32
+ * @returns {Entity[]} Every entity carrying a rigid body component.
33
+ */
34
+ function bodyEntities(app) {
35
+ const store = app.systems.rigidbody?.store;
36
+ return store ? Object.values(store).map(record => record.entity) : [];
37
+ }
38
+
39
+ /**
40
+ * @param {CollisionComponent|undefined} collision - A collision component.
41
+ * @returns {string} The shape and its dimensions.
42
+ */
43
+ function shapeText(collision) {
44
+ if (!collision) return 'no collision';
45
+ switch (collision.type) {
46
+ case 'box': {
47
+ const h = collision.halfExtents;
48
+ return `box ${formatNumber(h.x * 2)}×${formatNumber(h.y * 2)}×${formatNumber(h.z * 2)}`;
49
+ }
50
+ case 'sphere':
51
+ return `sphere r ${formatNumber(collision.radius)}`;
52
+ case 'capsule':
53
+ case 'cylinder':
54
+ case 'cone':
55
+ return `${collision.type} r ${formatNumber(collision.radius)} h ${formatNumber(collision.height)}`;
56
+ case 'mesh':
57
+ return collision.convexHull ? 'convex hull' : 'mesh';
58
+ default:
59
+ return collision.type;
60
+ }
61
+ }
62
+
63
+ /**
64
+ * @param {RigidBodyComponent} rigidbody - A rigid body component.
65
+ * @returns {string} Its simulation state.
66
+ */
67
+ function bodyState(rigidbody) {
68
+ if (!rigidbody.enabled || !rigidbody.entity.enabled) return 'disabled';
69
+ switch (rigidbody.type) {
70
+ case BODYTYPE_STATIC: return 'static';
71
+ case BODYTYPE_KINEMATIC: return 'kinematic';
72
+ default:
73
+ if (!rigidbody.body) return 'no body';
74
+ return rigidbody.isActive() ? 'active' : 'sleeping';
75
+ }
76
+ }
77
+
78
+ /**
79
+ * One row per rigid body: a checkbox that excludes the body from the debug drawing, name linking
80
+ * to the entity, type, shape, mass, and state with the speed of awake bodies. Sleeping and
81
+ * disabled bodies are dimmed.
82
+ *
83
+ * @param {AppBase} app - The app.
84
+ * @param {{ has: (entity: Entity) => boolean }} hidden - The bodies excluded from the drawing.
85
+ * @param {boolean} drawing - Whether the world is drawn at all; the checkboxes are disabled otherwise.
86
+ * @returns {ListRow[]} The rows.
87
+ */
88
+ function bodyRows(app, hidden, drawing) {
89
+ return bodyEntities(app).map((entity) => {
90
+ const rigidbody = entity.rigidbody;
91
+ const state = bodyState(rigidbody);
92
+ const dynamic = rigidbody.type === BODYTYPE_DYNAMIC;
93
+ const speed = dynamic && state === 'active' ? ` · ${formatNumber(rigidbody.linearVelocity.length())} m/s` : '';
94
+
95
+ return {
96
+ key: entity.guid,
97
+ item: entity,
98
+ name: entity.name,
99
+ dim: state === 'sleeping' || state === 'disabled',
100
+ cells: [
101
+ { toggle: true, checked: !hidden.has(entity), disabled: !drawing, title: 'Draw this body' },
102
+ { text: entity.name, cls: 'pci-cell-name', target: entity },
103
+ { text: rigidbody.type, cls: 'pci-cell-tag pci-cell-tag-info' },
104
+ { text: shapeText(entity.collision), cls: 'pci-cell-info' },
105
+ { text: dynamic ? `${formatNumber(rigidbody.mass)} kg` : '', cls: 'pci-cell-info' },
106
+ { text: `${state}${speed}`, cls: 'pci-cell-info pci-cell-right' }
107
+ ]
108
+ };
109
+ });
110
+ }
111
+
112
+ /**
113
+ * @param {AppBase} app - The app.
114
+ * @returns {Entity[]} Every entity carrying a joint component.
115
+ */
116
+ function jointEntities(app) {
117
+ const store = app.systems.joint?.store;
118
+ return store ? Object.values(store).map(record => record.entity) : [];
119
+ }
120
+
121
+ /**
122
+ * One row per joint: name linking to the entity, the joint type, the two bodies it connects as
123
+ * links, and whether it has broken. Joints without a second body are attached to the world.
124
+ *
125
+ * @param {AppBase} app - The app.
126
+ * @returns {ListRow[]} The rows.
127
+ */
128
+ function jointRows(app) {
129
+ return jointEntities(app).map((entity) => {
130
+ const joint = entity.joint;
131
+ const a = joint.entityA;
132
+ const b = joint.entityB;
133
+ const broken = !!joint.isBroken;
134
+ const flags = [
135
+ joint.enableLimits ? 'limits' : '',
136
+ joint.motorSpeed ? 'motor' : '',
137
+ joint.breakImpulse > 0 && joint.breakImpulse < Number.MAX_VALUE ? 'breakable' : '',
138
+ joint.enableCollision ? '' : 'no collision'
139
+ ].filter(Boolean).join(' · ');
140
+
141
+ return {
142
+ key: `joint:${entity.guid}`,
143
+ item: entity,
144
+ name: entity.name,
145
+ dim: !joint.enabled || !entity.enabled || broken,
146
+ cells: [
147
+ { text: '', cls: 'pci-cell-spacer' },
148
+ { text: entity.name, cls: 'pci-cell-name', target: entity },
149
+ { text: `${joint.type} joint`, cls: 'pci-cell-tag pci-cell-tag-info' },
150
+ { text: a ? a.name : 'world', cls: 'pci-cell-info', target: a ?? undefined },
151
+ { text: '↔', cls: 'pci-cell-info' },
152
+ { text: b ? b.name : 'world', cls: 'pci-cell-info', target: b ?? undefined },
153
+ { text: flags, cls: 'pci-cell-info' },
154
+ { text: broken ? 'broken' : (joint.enabled ? 'active' : 'disabled'), cls: 'pci-cell-info pci-cell-right' }
155
+ ]
156
+ };
157
+ });
158
+ }
159
+
160
+ /**
161
+ * Outlines a joint. The joint entity's own frame is drawn as axes, and for each connected body the
162
+ * joint frame the constraint was created with is drawn where that body currently carries it: axes
163
+ * at the anchor (red is the primary joint axis, green the secondary), a line from the body's center
164
+ * to the anchor, and a line from the joint entity to the anchor. Where the two anchor frames have
165
+ * drifted apart, the constraint is being violated by that much.
166
+ *
167
+ * @param {WireRenderer} wire - The renderer to draw with. Its color and depth test are used as set.
168
+ * @param {Entity} entity - The entity carrying the joint component.
169
+ * @param {number} size - The axis length.
170
+ * @returns {boolean} False when the entity has no joint component.
171
+ */
172
+ function drawJoint(wire, entity, size) {
173
+ const joint = /** @type {any} */ (entity.joint);
174
+ if (!joint) return false;
175
+
176
+ const position = entity.getPosition();
177
+ wire.axes(entity.getWorldTransform(), size);
178
+
179
+ const frames = [[joint.entityA, joint._frameA], [joint.entityB, joint._frameB]];
180
+ for (const [body, frame] of frames) {
181
+ if (!frame) {
182
+ // no constraint yet: at least show what the joint connects
183
+ if (body) wire.line(position, body.getPosition());
184
+ continue;
185
+ }
186
+
187
+ // bodies ignore entity scale, so the anchor follows the body's unscaled transform
188
+ if (body) {
189
+ _mat.setTRS(body.getPosition(), body.getRotation(), Vec3.ONE).mul(frame);
190
+ } else {
191
+ _mat.copy(frame);
192
+ }
193
+ _mat.getTranslation(_a);
194
+
195
+ wire.axes(_mat, size * 0.75);
196
+ wire.line(position, _a);
197
+ if (body) wire.line(body.getPosition(), _a);
198
+ }
199
+ return true;
200
+ }
201
+
202
+ /**
203
+ * @param {AppBase} app - The app.
204
+ * @returns {PhysicsStats} Counts for the status bar.
205
+ */
206
+ function physicsStats(app) {
207
+ const system = app.systems.rigidbody;
208
+ const entities = bodyEntities(app);
209
+
210
+ let active = 0;
211
+ for (const entity of entities) {
212
+ const rigidbody = entity.rigidbody;
213
+ if (rigidbody.type === BODYTYPE_DYNAMIC && rigidbody.isActive()) active++;
214
+ }
215
+
216
+ let contacts = 0;
217
+ const dispatcher = system?.dispatcher;
218
+ if (dispatcher) {
219
+ const manifolds = dispatcher.getNumManifolds();
220
+ for (let i = 0; i < manifolds; i++) {
221
+ contacts += dispatcher.getManifoldByIndexInternal(i).getNumContacts();
222
+ }
223
+ }
224
+
225
+ return {
226
+ system: !!system,
227
+ world: !!system?.dynamicsWorld,
228
+ bodies: entities.length,
229
+ active,
230
+ contacts,
231
+ joints: jointEntities(app).length,
232
+ time: app.stats?.frame?.physicsTime ?? 0
233
+ };
234
+ }
235
+
236
+ /**
237
+ * @param {number} axis - A collision component axis: 0 for x, 1 for y, 2 for z.
238
+ * @param {Vec3} out - Receives the unit vector.
239
+ * @returns {Vec3} The unit vector.
240
+ */
241
+ function axisVector(axis, out) {
242
+ return out.set(axis === 0 ? 1 : 0, axis === 1 ? 1 : 0, axis === 2 ? 1 : 0);
243
+ }
244
+
245
+ /**
246
+ * Outlines an entity's primitive collision shape where the physics engine has it: at the entity's
247
+ * position and rotation with the component's offsets applied, ignoring the entity scale as
248
+ * primitive shapes do.
249
+ *
250
+ * @param {WireRenderer} wire - The renderer to draw with. Its color and depth test are used as set.
251
+ * @param {Entity} entity - The entity.
252
+ * @returns {boolean} False when the entity has no primitive collision shape to outline.
253
+ */
254
+ function drawCollisionShape(wire, entity) {
255
+ const collision = entity.collision;
256
+ if (!collision?.enabled) return false;
257
+
258
+ const type = collision.type;
259
+ if (!['box', 'sphere', 'capsule', 'cylinder', 'cone'].includes(type)) return false;
260
+
261
+ const rotation = entity.getRotation();
262
+ _rot.copy(rotation);
263
+ if (collision.angularOffset) _rot.mul(collision.angularOffset);
264
+
265
+ _pos.copy(entity.getPosition());
266
+ if (collision.linearOffset) {
267
+ rotation.transformVector(collision.linearOffset, _a);
268
+ _pos.add(_a);
269
+ }
270
+
271
+ _mat.setTRS(_pos, _rot, Vec3.ONE);
272
+ wire.transform = _mat;
273
+
274
+ const radius = collision.radius;
275
+ const height = collision.height;
276
+ axisVector(collision.axis, _dir);
277
+
278
+ switch (type) {
279
+ case 'box': {
280
+ const h = collision.halfExtents;
281
+ _a.set(-h.x, -h.y, -h.z);
282
+ _b.set(h.x, h.y, h.z);
283
+ wire.boxMinMax(_a, _b);
284
+ break;
285
+ }
286
+ case 'sphere':
287
+ wire.sphere(Vec3.ZERO, radius);
288
+ break;
289
+ case 'capsule': {
290
+ const half = Math.max(0, height / 2 - radius);
291
+ _a.copy(_dir).mulScalar(-half);
292
+ _b.copy(_dir).mulScalar(half);
293
+ wire.capsule(_a, _b, radius);
294
+ break;
295
+ }
296
+ case 'cylinder':
297
+ _a.copy(_dir).mulScalar(-height / 2);
298
+ _b.copy(_dir).mulScalar(height / 2);
299
+ wire.cylinder(_a, _b, radius);
300
+ break;
301
+ case 'cone': {
302
+ _a.copy(_dir).mulScalar(height / 2);
303
+ _b.copy(_dir).mulScalar(-1);
304
+ const angle = Math.atan2(radius, height) * 180 / Math.PI;
305
+ wire.cone(_a, _b, angle, height);
306
+ break;
307
+ }
308
+ }
309
+
310
+ wire.transform = null;
311
+ return true;
312
+ }
313
+
314
+ export { bodyRows, drawCollisionShape, drawJoint, jointRows, physicsStats };