Hi, so I followed the tutorial for entity picking and made a script that generates a few boxes which can then be highlighted by hovering the mouse over them. So far so good, everything works fine as long as I’m looking at the boxes from the front or back (Positive or negative Z-Axis) but if I look at them from the side (Positive or negative X-Axis) they are basically selected inverted. So when I then point the mouse at the one furthest from the camera, the one closest to it highlights and so for all following.
I’m using the script from the tutorial with a few adjustments to what the console Warnings said to change (as some things were deprecated or parameters changed)
MyPickingScript.prototype.onHover = function (event) {
var canvas = this.app.graphicsDevice.canvas;
var canvasWidth = parseInt(canvas.clientWidth, 10);
var canvasHeight = parseInt(canvas.clientHeight, 10);
var camera = this.cameraEntity.camera; //cameraEntity is an attribute to which I assigned the camera entity
var scene = this.app.scene;
var picker = this.picker; //this picker is declared in the init-function as following:
//this.picker = new pc.Picker(this.app, 1024, 1024);
this.app.renderNextFrame = true;
picker.prepare(camera, scene, scene.layers.getLayerByName("AllowPicking"));
// Map the mouse coordinates into picker coordinates and
// query the selection
var selected = picker.getSelection(
Math.floor(event.x * (picker.width / canvasWidth)),
picker.height - Math.floor(event.y * (picker.height / canvasHeight)));
if (selected.length > 0 && selected[0].node) {
// Get the graph node used by the selected mesh instance
var entity = selected[0].node;
// Bubble up the hierarchy until we find an actual Entity
while (!(entity instanceof pc.Entity) && entity !== null) {
entity = entity.getParent();
}
if (entity.getParent() == this.entity) {
this.entity.children.forEach((c)=>{
if(c instanceof pc.Entity)c.model.meshInstances[0].material = this.colliderMat.resource;});
entity.model.meshInstances[0].material = this.colliderMat_hover.resource;
}
}
};
Thanks for the reply - I thought that Entity Picking was the only way to detect a clicked entity without enabling Physics but I can see that the example you linked doesn’t utilise physics either.
Is there any performance advantages/disadvantages using the RayCasting from that example in comparison to Entity Picking?
Could it be the deprecated declaration as Picker.getSelection used to expect an object as parameter? I mean I really don’t know but maybe something changed in the class itself as for example that the class code now automatically does the inversion of the y-axis and maybe before one had to do it ‘manually’ ?