Hi,
I’m working with the examples but can’t add detect when an entity was clicked event to be triggered (using raycast).
I want to do that programmatically and not through the designer.
A simple code that I could add to the code below will be appreciated:
var box = new pc.fw.Entity();
application.context.systems.model.addComponent(box, {
type: "box",
});
application.context.root.addChild(box);
Thanks for the quick response.
I’ve tried to implement this in my test without luck.
I also mentioned not doing it through the designer.
This is the closest I’ve come to getting a mesh instance which according to docs has no methods and therefore I have no idea what to do with.
context.mouse = mouse;
mouse.on(pc.input.EVENT_MOUSEDOWN, function (event) {
var from = camera.getPosition();
var to = camera.camera.screenToWorld(event.x, event.y, camera.camera.farClip);
var device = new pc.gfx.Device(canvas);
var picker = new pc.scene.Picker(device,1000,1000);
picker.scene = context.scene;
var selection = picker.getSelection({
x:mousex,
y:mousey
});
You need to call picker.prepare(camera, scene) to before you call getSelection(). This renders the scene to the pick buffer. So you need to call this again if you scene changes (perhaps every frame)
The co-ords to getSelection need to be scaled by the picker-canvas ratio and y is inverted
The object selected is a mesh instance, which contains node which is the SceneGraph object. Use this to find the parent entity.
Thanks a lot for the example, I got it to work.
Maybe a little more documentation regarding coding without the designer would’ve been helpful,
Regardless you have a real good product here
This comes as inheritance from OpenGL actually having bottom-left corner as 0,0 UV coordinates.
It is a bit confusing as DirectX is actually top-left. And most environments, like web, are top-left too.
So yes, it was missed at the time, and now it perhaps can’t be changed, as it would break things, but we could create another method that would work with top-left coords.