
Mirror Engine API / Picker
Class: Picker
Picker object used to select mesh instances from screen coordinates.
Constructors
new Picker()
new Picker(
app: AppBase,
width: number,
height: number): Picker
Create a new Picker instance.
Parameters
app
AppBase
The application managing this picker instance.
width
number
The width of the pick buffer in pixels.
height
number
The height of the pick buffer in pixels.
Returns
Methods
getSelection()
getSelection(
x: number,
y: number,
width?: number,
height?: number): MeshInstance[]
Return the list of mesh instances selected by the specified rectangle in the previously prepared pick buffer. The rectangle using top-left coordinate system.
Note: This function is not supported on WebGPU. Use Picker#getSelectionAsync instead. Note: This function is blocks the main thread while reading pixels from GPU memory. It's recommended to use Picker#getSelectionAsync instead.
Parameters
x
number
The left edge of the rectangle.
y
number
The top edge of the rectangle.
width?
number
= 1
The width of the rectangle. Defaults to 1.
height?
number
= 1
The height of the rectangle. Defaults to 1.
Returns
An array of mesh instances that are in the selection.
Examples
// Get the selection at the point (10,20)
const selection = picker.getSelection(10, 20)
// Get all models in rectangle with corners at (10,20) and (20,40)
const selection = picker.getSelection(10, 20, 10, 20)
getSelectionAsync()
getSelectionAsync(
x: number,
y: number,
width?: number,
height?: number): Promise<MeshInstance[]>
Return the list of mesh instances selected by the specified rectangle in the previously prepared pick buffer. The rectangle uses top-left coordinate system.
This method is asynchronous and does not block the execution.
Parameters
x
number
The left edge of the rectangle.
y
number
The top edge of the rectangle.
width?
number
= 1
The width of the rectangle. Defaults to 1.
height?
number
= 1
The height of the rectangle. Defaults to 1.
Returns
- Promise that resolves with an array of mesh instances that are in the selection.
Example
// Get the mesh instances at the rectangle with start at (10,20) and size of (5,5)
picker.getSelectionAsync(10, 20, 5, 5).then((meshInstances) => {
console.log(meshInstances)
})
prepare()
prepare(
camera: CameraComponent,
scene: Scene,
layers?: Layer[]): void
Primes the pick buffer with a rendering of the specified models from the point of view of the supplied camera. Once the pick buffer has been prepared, Picker#getSelection can be called multiple times on the same picker object. Therefore, if the models or camera do not change in any way, Picker#prepare does not need to be called again.
Parameters
camera
The camera component used to render the scene.
scene
The scene containing the pickable mesh instances.
layers?
Layer
[]
Layers from which objects will be picked. If not supplied, all layers of the specified camera will be used.
Returns
void
resize()
resize(width: number, height: number): void
Sets the resolution of the pick buffer. The pick buffer resolution does not need to match the resolution of the corresponding frame buffer use for general rendering of the 3D scene. However, the lower the resolution of the pick buffer, the less accurate the selection results returned by Picker#getSelection. On the other hand, smaller pick buffers will yield greater performance, so there is a trade off.
Parameters
width
number
The width of the pick buffer in pixels.
height
number
The height of the pick buffer in pixels.
Returns
void