@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,545 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Color, Layer, Mat4, math, PIXELFORMAT_RGBA8, PROJECTION_ORTHOGRAPHIC, RenderTarget, Texture, Vec3, Vec4
|
|
3
|
+
} from 'playcanvas';
|
|
4
|
+
|
|
5
|
+
import { Debug } from '../debug.js';
|
|
6
|
+
|
|
7
|
+
import { RenderPassPicker } from './render-pass-picker.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @import { AppBase } from 'playcanvas'
|
|
11
|
+
* @import { CameraComponent } from 'playcanvas'
|
|
12
|
+
* @import { GSplatComponent } from 'playcanvas'
|
|
13
|
+
* @import { MeshInstance } from 'playcanvas'
|
|
14
|
+
* @import { Scene } from 'playcanvas'
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const tempSet = new Set();
|
|
18
|
+
const _rect = new Vec4();
|
|
19
|
+
const _floatView = new Float32Array(1);
|
|
20
|
+
const _int32View = new Int32Array(_floatView.buffer);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Picker object used to select mesh instances from screen coordinates. It can also optionally
|
|
24
|
+
* capture depth information to determine world positions of picked points.
|
|
25
|
+
*
|
|
26
|
+
* The picker works by rendering mesh instances to an offscreen render target with unique IDs
|
|
27
|
+
* encoded as colors. When queried, it reads back the pixel data to identify which mesh instance
|
|
28
|
+
* was at the specified screen coordinates. If depth picking is enabled, it also captures depth
|
|
29
|
+
* values to compute world positions.
|
|
30
|
+
*
|
|
31
|
+
* **Main API methods:**
|
|
32
|
+
* - {@link prepare} - Renders the pick buffer (call once per frame before picking)
|
|
33
|
+
* - {@link getSelectionAsync} - Get mesh instances in a screen area
|
|
34
|
+
* - {@link getWorldPointAsync} - Get world position at screen coordinates (requires depth)
|
|
35
|
+
*
|
|
36
|
+
* **Performance considerations:**
|
|
37
|
+
* The picker resolution can be set lower than the screen resolution for better performance,
|
|
38
|
+
* though this reduces picking precision and may miss small objects.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* // Create a picker with depth picking enabled at quarter resolution
|
|
42
|
+
* const picker = new Picker(app, canvas.width * 0.25, canvas.height * 0.25, true);
|
|
43
|
+
*
|
|
44
|
+
* // In your update loop, prepare the picker
|
|
45
|
+
* picker.resize(canvas.width * 0.25, canvas.height * 0.25);
|
|
46
|
+
* picker.prepare(camera, scene);
|
|
47
|
+
*
|
|
48
|
+
* // Pick mesh instances in an area
|
|
49
|
+
* picker.getSelectionAsync(x, y, width, height).then((meshInstances) => {
|
|
50
|
+
* meshInstances.forEach((meshInstance) => {
|
|
51
|
+
* console.log('Picked:', meshInstance.node.name);
|
|
52
|
+
* });
|
|
53
|
+
* });
|
|
54
|
+
*
|
|
55
|
+
* // Pick world position (requires depth enabled)
|
|
56
|
+
* picker.getWorldPointAsync(x, y).then((worldPoint) => {
|
|
57
|
+
* if (worldPoint) {
|
|
58
|
+
* console.log(worldPoint);
|
|
59
|
+
* }
|
|
60
|
+
* });
|
|
61
|
+
*
|
|
62
|
+
* @see {@link http://playcanvas.github.io/#/graphics/area-picker|Area Picker Example}
|
|
63
|
+
* @see {@link https://playcanvas.github.io/#gaussian-splatting/picking|Gaussian Splatting Picking Example}
|
|
64
|
+
*
|
|
65
|
+
* @category Graphics
|
|
66
|
+
*/
|
|
67
|
+
class Picker {
|
|
68
|
+
/**
|
|
69
|
+
* @type {import('playcanvas').GraphicsDevice}
|
|
70
|
+
* @private
|
|
71
|
+
*/
|
|
72
|
+
device;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* @type {RenderPassPicker}
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
78
|
+
renderPass;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @type {boolean}
|
|
82
|
+
* @private
|
|
83
|
+
*/
|
|
84
|
+
depth;
|
|
85
|
+
|
|
86
|
+
/** @type {number} */
|
|
87
|
+
width;
|
|
88
|
+
|
|
89
|
+
/** @type {number} */
|
|
90
|
+
height;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Internal render target.
|
|
94
|
+
*
|
|
95
|
+
* @type {RenderTarget|null}
|
|
96
|
+
* @private
|
|
97
|
+
*/
|
|
98
|
+
renderTarget = null;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Color buffer texture for pick IDs.
|
|
102
|
+
*
|
|
103
|
+
* @type {Texture|null}
|
|
104
|
+
* @private
|
|
105
|
+
*/
|
|
106
|
+
colorBuffer = null;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Optional depth buffer texture for depth picking.
|
|
110
|
+
*
|
|
111
|
+
* @type {Texture|null}
|
|
112
|
+
* @private
|
|
113
|
+
*/
|
|
114
|
+
depthBuffer = null;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Internal render target for reading the depth buffer.
|
|
118
|
+
*
|
|
119
|
+
* @type {RenderTarget|null}
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
renderTargetDepth = null;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Mapping table from ids to MeshInstances or GSplatComponents.
|
|
126
|
+
*
|
|
127
|
+
* @type {Map<number, MeshInstance | GSplatComponent>}
|
|
128
|
+
* @private
|
|
129
|
+
*/
|
|
130
|
+
mapping = new Map();
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* When the device is destroyed, this allows us to ignore async results.
|
|
134
|
+
*
|
|
135
|
+
* @private
|
|
136
|
+
*/
|
|
137
|
+
deviceValid = true;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Create a new Picker instance.
|
|
141
|
+
*
|
|
142
|
+
* @param {AppBase} app - The application managing this picker instance.
|
|
143
|
+
* @param {number} width - The width of the pick buffer in pixels.
|
|
144
|
+
* @param {number} height - The height of the pick buffer in pixels.
|
|
145
|
+
* @param {boolean} [depth] - Whether to enable depth picking. When enabled, depth
|
|
146
|
+
* information is captured alongside mesh IDs using MRT. Defaults to false.
|
|
147
|
+
*/
|
|
148
|
+
constructor(app, width, height, depth = false) {
|
|
149
|
+
// Note: The only reason this class needs the app is to access the renderer. Ideally we remove this dependency and move
|
|
150
|
+
// the Picker from framework to the scene level, or even the extras.
|
|
151
|
+
Debug.assert(app);
|
|
152
|
+
this.device = app.graphicsDevice;
|
|
153
|
+
|
|
154
|
+
this.renderPass = new RenderPassPicker(this.device, app.renderer);
|
|
155
|
+
|
|
156
|
+
this.depth = depth;
|
|
157
|
+
this.width = 0;
|
|
158
|
+
this.height = 0;
|
|
159
|
+
this.resize(width, height);
|
|
160
|
+
this.allocateRenderTarget();
|
|
161
|
+
|
|
162
|
+
// handle the device getting destroyed
|
|
163
|
+
this.device.on('destroy', () => {
|
|
164
|
+
this.deviceValid = false;
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Frees resources associated with this picker.
|
|
170
|
+
*/
|
|
171
|
+
destroy() {
|
|
172
|
+
this.releaseRenderTarget();
|
|
173
|
+
this.renderPass?.destroy();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Return the list of mesh instances selected by the specified rectangle in the previously
|
|
178
|
+
* prepared pick buffer. The rectangle using top-left coordinate system.
|
|
179
|
+
*
|
|
180
|
+
* Note: This function is not supported on WebGPU. Use {@link getSelectionAsync} instead.
|
|
181
|
+
* Note: This function is blocks the main thread while reading pixels from GPU memory. It's
|
|
182
|
+
* recommended to use {@link getSelectionAsync} instead.
|
|
183
|
+
*
|
|
184
|
+
* @param {number} x - The left edge of the rectangle.
|
|
185
|
+
* @param {number} y - The top edge of the rectangle.
|
|
186
|
+
* @param {number} [width] - The width of the rectangle. Defaults to 1.
|
|
187
|
+
* @param {number} [height] - The height of the rectangle. Defaults to 1.
|
|
188
|
+
* @returns {(MeshInstance | GSplatComponent)[]} An array of mesh instances or gsplat components
|
|
189
|
+
* that are in the selection.
|
|
190
|
+
* @example
|
|
191
|
+
* // Get the selection at the point (10,20)
|
|
192
|
+
* const selection = picker.getSelection(10, 20);
|
|
193
|
+
* @example
|
|
194
|
+
* // Get all models in rectangle with corners at (10,20) and (20,40)
|
|
195
|
+
* const selection = picker.getSelection(10, 20, 10, 20);
|
|
196
|
+
*/
|
|
197
|
+
getSelection(x, y, width = 1, height = 1) {
|
|
198
|
+
const device = this.device;
|
|
199
|
+
if (device.isWebGPU) {
|
|
200
|
+
Debug.errorOnce('Picker#getSelection is not supported on WebGPU, use Picker#getSelectionAsync instead.');
|
|
201
|
+
return [];
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
Debug.assert(typeof x !== 'object', 'Picker.getSelection:param \'rect\' is deprecated, use \'x, y, width, height\' instead.');
|
|
205
|
+
|
|
206
|
+
const rect = this._sanitizeFlippedRect(x, y, width, height, this.renderTarget.height);
|
|
207
|
+
|
|
208
|
+
// read pixels from the render target
|
|
209
|
+
device.setRenderTarget(this.renderTarget);
|
|
210
|
+
device.updateBegin();
|
|
211
|
+
|
|
212
|
+
const pixels = new Uint8Array(4 * rect.z * rect.w);
|
|
213
|
+
device.readPixels(rect.x, rect.y, rect.z, rect.w, pixels);
|
|
214
|
+
|
|
215
|
+
device.updateEnd();
|
|
216
|
+
|
|
217
|
+
return this.decodePixels(pixels, this.mapping);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Return the list of mesh instances selected by the specified rectangle in the previously
|
|
222
|
+
* prepared pick buffer. The rectangle uses top-left coordinate system.
|
|
223
|
+
*
|
|
224
|
+
* This method is asynchronous and does not block the execution.
|
|
225
|
+
*
|
|
226
|
+
* @param {number} x - The left edge of the rectangle.
|
|
227
|
+
* @param {number} y - The top edge of the rectangle.
|
|
228
|
+
* @param {number} [width] - The width of the rectangle. Defaults to 1.
|
|
229
|
+
* @param {number} [height] - The height of the rectangle. Defaults to 1.
|
|
230
|
+
* @returns {Promise<(MeshInstance | GSplatComponent)[]>} - Promise that resolves with an
|
|
231
|
+
* array of mesh instances or gsplat components that are in the selection.
|
|
232
|
+
* @example
|
|
233
|
+
* // Get the mesh instances at the rectangle with start at (10,20) and size of (5,5)
|
|
234
|
+
* picker.getSelectionAsync(10, 20, 5, 5).then((meshInstances) => {
|
|
235
|
+
* console.log(meshInstances);
|
|
236
|
+
* });
|
|
237
|
+
*/
|
|
238
|
+
getSelectionAsync(x, y, width = 1, height = 1) {
|
|
239
|
+
if (!this.renderTarget || !this.renderTarget.colorBuffer) {
|
|
240
|
+
return Promise.resolve([]);
|
|
241
|
+
}
|
|
242
|
+
return this._readTexture(this.renderTarget.colorBuffer, x, y, width, height, this.renderTarget).then((pixels) => {
|
|
243
|
+
return this.decodePixels(pixels, this.mapping);
|
|
244
|
+
}).catch((error) => {
|
|
245
|
+
// a read which could not complete because the device has gone reports an empty selection,
|
|
246
|
+
// including cancellation before the device loss notification arrives. Every other failure is a real one
|
|
247
|
+
// and is passed on, rather than being disguised as nothing having been selected.
|
|
248
|
+
if (this.deviceValid && !this.device.isContextLost() && error.name !== 'AbortError') {
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
return [];
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Helper method to read pixels from a texture asynchronously.
|
|
257
|
+
*
|
|
258
|
+
* @param {Texture} texture - The texture to read from.
|
|
259
|
+
* @param {number} x - The x coordinate.
|
|
260
|
+
* @param {number} y - The y coordinate.
|
|
261
|
+
* @param {number} width - The width of the rectangle.
|
|
262
|
+
* @param {number} height - The height of the rectangle.
|
|
263
|
+
* @param {RenderTarget} renderTarget - The render target to use for reading.
|
|
264
|
+
* @returns {Promise<Uint8Array>} Promise resolving to the pixel data.
|
|
265
|
+
* @private
|
|
266
|
+
*/
|
|
267
|
+
_readTexture(texture, x, y, width, height, renderTarget) {
|
|
268
|
+
// WebGL2 reads back with a bottom-left origin, WebGPU with a top-left one
|
|
269
|
+
const rect = this.device?.isWebGL2 ?
|
|
270
|
+
this._sanitizeFlippedRect(x, y, width, height, renderTarget.height) :
|
|
271
|
+
this.sanitizeRect(x, y, width, height);
|
|
272
|
+
|
|
273
|
+
// @ts-ignore
|
|
274
|
+
return texture.read(rect.x, rect.y, rect.z, rect.w, {
|
|
275
|
+
immediate: true,
|
|
276
|
+
renderTarget: renderTarget
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Return the world position of the mesh instance picked at the specified screen coordinates.
|
|
282
|
+
* The position is reconstructed at the center of the pixel containing the coordinates, which
|
|
283
|
+
* is where its depth was rasterized.
|
|
284
|
+
*
|
|
285
|
+
* @param {number} x - The x coordinate of the pixel to pick.
|
|
286
|
+
* @param {number} y - The y coordinate of the pixel to pick.
|
|
287
|
+
* @returns {Promise<Vec3|null>} Promise that resolves with the world position of the picked point,
|
|
288
|
+
* or null if no depth is available or nothing was picked.
|
|
289
|
+
* @example
|
|
290
|
+
* // Get the world position at screen coordinates (100, 50)
|
|
291
|
+
* picker.getWorldPointAsync(100, 50).then((worldPoint) => {
|
|
292
|
+
* if (worldPoint) {
|
|
293
|
+
* console.log('World position:', worldPoint);
|
|
294
|
+
* // Use the world position
|
|
295
|
+
* } else {
|
|
296
|
+
* console.log('No object at this position');
|
|
297
|
+
* }
|
|
298
|
+
* });
|
|
299
|
+
*/
|
|
300
|
+
async getWorldPointAsync(x, y) {
|
|
301
|
+
// get the camera from the render pass
|
|
302
|
+
const camera = this.renderPass.camera;
|
|
303
|
+
if (!camera) {
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// capture camera state synchronously before awaiting
|
|
308
|
+
const viewProjMat = new Mat4().mul2(camera.camera.projectionMatrix, camera.camera.viewMatrix);
|
|
309
|
+
const invViewProj = viewProjMat.invert();
|
|
310
|
+
const near = camera.nearClip;
|
|
311
|
+
const far = camera.farClip;
|
|
312
|
+
const isOrtho = camera.projection === PROJECTION_ORTHOGRAPHIC;
|
|
313
|
+
|
|
314
|
+
const linearDepth = await this.getPointDepthAsync(x, y);
|
|
315
|
+
if (linearDepth === null) {
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// convert linear normalized depth [0,1] to NDC depth [0,1] for unprojection
|
|
320
|
+
const ndcDepth = isOrtho ? linearDepth : (far * linearDepth / (linearDepth * (far - near) + near));
|
|
321
|
+
|
|
322
|
+
// unproject to world space using the captured matrix. The depth came from the pixel
|
|
323
|
+
// containing (x, y) and was rasterized at that pixel's center, so the point lies on the
|
|
324
|
+
// ray through the center - not through the requested coordinate, which for an integer
|
|
325
|
+
// input sits on the pixel's edge. Clamping matches the read, which is clamped to the
|
|
326
|
+
// buffer by sanitizeRect.
|
|
327
|
+
const pixelX = math.clamp(Math.floor(x), 0, this.width - 1) + 0.5;
|
|
328
|
+
const pixelY = math.clamp(Math.floor(y), 0, this.height - 1) + 0.5;
|
|
329
|
+
const deviceCoord = new Vec4(
|
|
330
|
+
(pixelX / this.width) * 2 - 1,
|
|
331
|
+
(1 - pixelY / this.height) * 2 - 1,
|
|
332
|
+
ndcDepth * 2 - 1,
|
|
333
|
+
1.0
|
|
334
|
+
);
|
|
335
|
+
invViewProj.transformVec4(deviceCoord, deviceCoord);
|
|
336
|
+
deviceCoord.mulScalar(1.0 / deviceCoord.w);
|
|
337
|
+
|
|
338
|
+
return new Vec3(deviceCoord.x, deviceCoord.y, deviceCoord.z);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Return the depth value of the mesh instance picked at the specified screen coordinates.
|
|
343
|
+
*
|
|
344
|
+
* @param {number} x - The x coordinate of the pixel to pick.
|
|
345
|
+
* @param {number} y - The y coordinate of the pixel to pick.
|
|
346
|
+
* @returns {Promise<number|null>} Promise that resolves with the linear normalized depth value
|
|
347
|
+
* of the picked point (0 = near plane, 1 = far plane), or null if depth picking is not enabled
|
|
348
|
+
* or no object was picked.
|
|
349
|
+
* @ignore
|
|
350
|
+
*/
|
|
351
|
+
async getPointDepthAsync(x, y) {
|
|
352
|
+
if (!this.depthBuffer) {
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
let pixels;
|
|
357
|
+
try {
|
|
358
|
+
pixels = await this._readTexture(this.depthBuffer, x, y, 1, 1, this.renderTargetDepth);
|
|
359
|
+
} catch (error) {
|
|
360
|
+
// Mapping can be cancelled before the device loss notification arrives. Other failures
|
|
361
|
+
// on a valid device must still be reported.
|
|
362
|
+
if (this.deviceValid && !this.device.isContextLost() && error.name !== 'AbortError') {
|
|
363
|
+
throw error;
|
|
364
|
+
}
|
|
365
|
+
return null;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// reconstruct uint bits from RGBA8
|
|
369
|
+
const intBits = ((pixels[0] << 24) | (pixels[1] << 16) | (pixels[2] << 8) | pixels[3]) >>> 0;
|
|
370
|
+
|
|
371
|
+
// check for white (cleared) depth
|
|
372
|
+
if (intBits === 0xFFFFFFFF) {
|
|
373
|
+
return null;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// reinterpret bits as float
|
|
377
|
+
_int32View[0] = intBits;
|
|
378
|
+
return _floatView[0];
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Sanitize a rectangle specified in the public top-left coordinate system and convert it to
|
|
383
|
+
* the bottom-left origin used when reading pixels back.
|
|
384
|
+
*
|
|
385
|
+
* The rectangle is floored and clipped while still in top-left space, and only then flipped,
|
|
386
|
+
* which matters twice. Flipping first would floor `bufferHeight - y - height`, landing one row
|
|
387
|
+
* off the requested pixel for any fractional y - common when the pick buffer is smaller than
|
|
388
|
+
* the canvas and coordinates are scaled into it. And clipping a rectangle that overhangs the
|
|
389
|
+
* bottom edge in flipped space absorbs the overflow at the wrong end, shifting the rectangle
|
|
390
|
+
* up instead of dropping the rows that fall outside the buffer.
|
|
391
|
+
*
|
|
392
|
+
* @param {number} x - The left edge of the rectangle.
|
|
393
|
+
* @param {number} y - The top edge of the rectangle.
|
|
394
|
+
* @param {number} width - The width of the rectangle.
|
|
395
|
+
* @param {number} height - The height of the rectangle.
|
|
396
|
+
* @param {number} bufferHeight - The height of the buffer being read.
|
|
397
|
+
* @returns {Vec4} The sanitized rectangle, with y in a bottom-left coordinate system.
|
|
398
|
+
* @private
|
|
399
|
+
*/
|
|
400
|
+
_sanitizeFlippedRect(x, y, width, height, bufferHeight) {
|
|
401
|
+
const rect = this.sanitizeRect(x, y, width, height);
|
|
402
|
+
rect.y = bufferHeight - (rect.y + rect.w); // rect.w is the height
|
|
403
|
+
return rect;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
// sanitize the rectangle to make sure it's inside the texture and does not use fractions
|
|
407
|
+
sanitizeRect(x, y, width, height) {
|
|
408
|
+
const maxWidth = this.renderTarget.width;
|
|
409
|
+
const maxHeight = this.renderTarget.height;
|
|
410
|
+
x = math.clamp(Math.floor(x), 0, maxWidth - 1);
|
|
411
|
+
y = math.clamp(Math.floor(y), 0, maxHeight - 1);
|
|
412
|
+
width = Math.floor(Math.max(width, 1));
|
|
413
|
+
width = Math.min(width, maxWidth - x);
|
|
414
|
+
height = Math.floor(Math.max(height, 1));
|
|
415
|
+
height = Math.min(height, maxHeight - y);
|
|
416
|
+
return _rect.set(x, y, width, height);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
decodePixels(pixels, mapping) {
|
|
420
|
+
|
|
421
|
+
const selection = [];
|
|
422
|
+
|
|
423
|
+
// when we decode results from async calls, ignore them if the device is no longer valid
|
|
424
|
+
if (this.deviceValid) {
|
|
425
|
+
|
|
426
|
+
const count = pixels.length;
|
|
427
|
+
for (let i = 0; i < count; i += 4) {
|
|
428
|
+
const r = pixels[i + 0];
|
|
429
|
+
const g = pixels[i + 1];
|
|
430
|
+
const b = pixels[i + 2];
|
|
431
|
+
const a = pixels[i + 3];
|
|
432
|
+
const index = (a << 24 | r << 16 | g << 8 | b) >>> 0;
|
|
433
|
+
|
|
434
|
+
// White is 'no selection
|
|
435
|
+
if (index !== 0xFFFFFFFF) {
|
|
436
|
+
tempSet.add(mapping.get(index));
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// return the content of the set as an array
|
|
441
|
+
tempSet.forEach((meshInstance) => {
|
|
442
|
+
if (meshInstance) {
|
|
443
|
+
selection.push(meshInstance);
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
tempSet.clear();
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
return selection;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
createTexture(name) {
|
|
453
|
+
return Texture.createDataTexture2D(this.device, name, this.width, this.height, PIXELFORMAT_RGBA8);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
allocateRenderTarget() {
|
|
457
|
+
|
|
458
|
+
// TODO: Ideally we'd use a UINT32 texture format and avoid RGBA8 conversion, but WebGL2 does not
|
|
459
|
+
// support clearing render targets of this format, so we'd need a quad based clear solution.
|
|
460
|
+
this.colorBuffer = this.createTexture('pick');
|
|
461
|
+
const colorBuffers = [this.colorBuffer];
|
|
462
|
+
|
|
463
|
+
if (this.depth) {
|
|
464
|
+
// create depth buffer for MRT
|
|
465
|
+
this.depthBuffer = this.createTexture('pick-depth');
|
|
466
|
+
colorBuffers.push(this.depthBuffer);
|
|
467
|
+
|
|
468
|
+
// create a render target for reading the depth buffer
|
|
469
|
+
this.renderTargetDepth = new RenderTarget({
|
|
470
|
+
colorBuffer: this.depthBuffer,
|
|
471
|
+
depth: false
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
this.renderTarget = new RenderTarget({
|
|
476
|
+
colorBuffers: colorBuffers,
|
|
477
|
+
depth: true
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
releaseRenderTarget() {
|
|
482
|
+
this.renderTarget?.destroyTextureBuffers();
|
|
483
|
+
this.renderTarget?.destroy();
|
|
484
|
+
this.renderTarget = null;
|
|
485
|
+
|
|
486
|
+
this.renderTargetDepth?.destroy();
|
|
487
|
+
this.renderTargetDepth = null;
|
|
488
|
+
|
|
489
|
+
this.colorBuffer = null;
|
|
490
|
+
this.depthBuffer = null;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Primes the pick buffer with a rendering of the specified models from the point of view of
|
|
495
|
+
* the supplied camera. Once the pick buffer has been prepared, {@link getSelection} can
|
|
496
|
+
* be called multiple times on the same picker object. Therefore, if the models or camera do
|
|
497
|
+
* not change in any way, {@link prepare} does not need to be called again.
|
|
498
|
+
*
|
|
499
|
+
* @param {CameraComponent} camera - The camera component used to render the scene.
|
|
500
|
+
* @param {Scene} scene - The scene containing the pickable mesh instances.
|
|
501
|
+
* @param {Layer[]} [layers] - Layers from which objects will be picked. If not supplied, all
|
|
502
|
+
* layers of the specified camera will be used.
|
|
503
|
+
*/
|
|
504
|
+
prepare(camera, scene, layers) {
|
|
505
|
+
|
|
506
|
+
if (layers instanceof Layer) {
|
|
507
|
+
layers = [layers];
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// make the render target the right size
|
|
511
|
+
this.renderTarget?.resize(this.width, this.height);
|
|
512
|
+
this.renderTargetDepth?.resize(this.width, this.height);
|
|
513
|
+
|
|
514
|
+
// clear registered meshes mapping
|
|
515
|
+
this.mapping.clear();
|
|
516
|
+
|
|
517
|
+
const renderPass = this.renderPass;
|
|
518
|
+
renderPass.init(this.renderTarget);
|
|
519
|
+
|
|
520
|
+
// set up clears - setClearColor handles MRT and clears all color buffers
|
|
521
|
+
renderPass.setClearColor(Color.WHITE);
|
|
522
|
+
renderPass.depthStencilOps.clearDepth = true;
|
|
523
|
+
|
|
524
|
+
// render the pass to update the render target
|
|
525
|
+
renderPass.update(camera, scene, layers, this.mapping, this.depth);
|
|
526
|
+
renderPass.render();
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Sets the resolution of the pick buffer. The pick buffer resolution does not need to match
|
|
531
|
+
* the resolution of the corresponding frame buffer use for general rendering of the 3D scene.
|
|
532
|
+
* However, the lower the resolution of the pick buffer, the less accurate the selection
|
|
533
|
+
* results returned by {@link getSelection}. On the other hand, smaller pick buffers
|
|
534
|
+
* will yield greater performance, so there is a trade off.
|
|
535
|
+
*
|
|
536
|
+
* @param {number} width - The width of the pick buffer in pixels.
|
|
537
|
+
* @param {number} height - The height of the pick buffer in pixels.
|
|
538
|
+
*/
|
|
539
|
+
resize(width, height) {
|
|
540
|
+
this.width = Math.floor(width);
|
|
541
|
+
this.height = Math.floor(height);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export { Picker };
|