@luma.gl/engine
Version:
3D Engine Components for luma.gl
76 lines • 2.71 kB
JavaScript
// luma.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
/**
* Helper class for using the legacy picking module
*/
export class LegacyPickingManager {
device;
framebuffer = null;
shaderInputs;
constructor(device, shaderInputs) {
this.device = device;
this.shaderInputs = shaderInputs;
}
destroy() {
this.framebuffer?.destroy();
}
getFramebuffer() {
if (!this.framebuffer) {
this.framebuffer = this.device.createFramebuffer({
colorAttachments: ['rgba8unorm'],
depthStencilAttachment: 'depth24plus'
});
}
return this.framebuffer;
}
/** Clear highlighted / picked object */
clearPickState() {
this.shaderInputs.setProps({ picking: { highlightedObjectColor: null } });
}
/** Prepare for rendering picking colors */
beginRenderPass() {
const framebuffer = this.getFramebuffer();
framebuffer.resize(this.device.getCanvasContext().getDevicePixelSize());
this.shaderInputs.setProps({ picking: { isActive: true } });
const pickingPass = this.device.beginRenderPass({
framebuffer,
clearColor: [0, 0, 0, 0],
clearDepth: 1
});
return pickingPass;
}
updatePickState(mousePosition) {
const framebuffer = this.getFramebuffer();
// use the center pixel location in device pixel range
const [pickX, pickY] = this.getPickPosition(mousePosition);
// Read back
const color255 = this.device.readPixelsToArrayWebGL(framebuffer, {
sourceX: pickX,
sourceY: pickY,
sourceWidth: 1,
sourceHeight: 1
});
// console.log(color255);
// Check if we have
let highlightedObjectColor = [...color255].map(x => x / 255);
const isHighlightActive = highlightedObjectColor[0] + highlightedObjectColor[1] + highlightedObjectColor[2] > 0;
if (!isHighlightActive) {
highlightedObjectColor = null;
}
this.shaderInputs.setProps({
picking: { isActive: false, highlightedObjectColor }
});
}
/**
* Get pick position in device pixel range
* use the center pixel location in device pixel range
*/
getPickPosition(mousePosition) {
const devicePixels = this.device.getCanvasContext().cssToDevicePixels(mousePosition);
const pickX = devicePixels.x + Math.floor(devicePixels.width / 2);
const pickY = devicePixels.y + Math.floor(devicePixels.height / 2);
return [pickX, pickY];
}
}
//# sourceMappingURL=legacy-picking-manager.js.map