Hi folks,
I wanna make a function, which can allow me to use a rectangle to pick multiple items in a scene at once.
For achieving this, I find Frame Buffer Picking in the tutrial on the official wesite is a good start. It’s like this
var PickerFramebuffer = pc.createScript('pickerFramebuffer');
// initialize code called once per entity
PickerFramebuffer.prototype.initialize = function() {
// Create a frame buffer picker with a resolution of 1024x1024
this.picker = new pc.Picker(this.app.graphicsDevice, 1024, 1024);
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onSelect, this);
};
PickerFramebuffer.prototype.onSelect = function (event) {
// QQQ: what is canvas? Is it the screen?
var canvas = this.app.graphicsDevice.canvas;
var canvasWidth = parseInt(canvas.clientWidth, 10);
var canvasHeight = parseInt(canvas.clientHeight, 10);
var camera = this.entity.camera.camera;
var scene = this.app.scene;
var picker = this.picker;
picker.prepare(camera, scene);
// QQQ: why do we have to map the mouse coordinates into picker's?
// As far as I can think about the getSelection function is we give it the center, height and width of a rectangle area on screen, and got the meshInstances of entities,
// which model's screen projection can be inside of the rectangle area.
// If I'm right, why not just pass the center, height and width of the rectangle to getSelection function?
// What this mapping does in this case?
// Map the mouse coordinates into picker coordinates and
// query the selection
var selected = picker.getSelection({
x: Math.floor(event.x * (picker.width / canvasWidth)),
y: picker.height - Math.floor(event.y * (picker.height / canvasHeight)),
width: 1024,
height: 1024
});
if (selected.length > 0) {
// Get the graph node used by the selected mesh instance
console.log(selected);
// Bubble up the hierarchy until we find an actual Entity
for(var i = 0; i < selected.length; i += 1){
var entity = selected[i].node;
while (!(entity instanceof pc.Entity) && entity !== null) {
entity = entity.getParent();
}
if (entity) {
//entity.script.pulse.pulse();
console.log(entity.name + "!!!");
}
}
}
};
When I try it with my project, the picking rectangle keeps small even I make the width and height large, so I can’t pick a lot things spreaded on the ground in the scene. I guess I missunderstand something about that script. I got two questions basically with ‘QQQ’ mark in the comment of the code above.
1.what is canvas? Is it the screen?
2.why do we have to map the mouse coordinates into picker’s?
As far as I can think about the getSelection function is we give it the center, height and width of a rectangle area on screen, and got the meshInstances of entities,
which model’s screen projection can be inside of the rectangle area.
If I’m right, why not just pass the center(event.x, event.y), height and width of the rectangle to getSelection function?
What this mapping does in this case?
Here is the link to my project https://playcanvas.com/project/482357/overview/ffff