Jump to content

How to find the coordinates of a click in the Babylon.js scene?


vinicius.alves
 Share

Recommended Posts

Hello!

I'm starting in Babylon not long ago, I'm looking for some way to find the coordinates of mouse when there is a click on the canvas. I was able to easily find this for a given object, but not for the scene itself (!?) I do not know what could be wrong, it seems silly, but I have not found the solution...

window.addEventListener('DOMContentLoaded', function () {
    var canvas = document.getElementById('canvas');
    var engine = new BABYLON.Engine(canvas, true); 
            
    var createScene = function () {
        var scene = new BABYLON.Scene(engine);
        scene.clearColor = new BABYLON.Color3.White();

        var box = BABYLON.Mesh.CreateBox("Box",4.0,scene);
        var camera = new BABYLON.ArcRotateCamera("arcCam",
                     BABYLON.Tools.ToRadians(45),
                     BABYLON.Tools.ToRadians(45),
                     10.0,box.position,scene);
                     camera.attachControl(canvas,true);

         var light = new BABYLON.PointLight("pointLight",new BABYLON.Vector3(
            0,10,0),scene);
         light.diffuse = new BABYLON.Color3(1,1,1);

         var onpickAction = new BABYLON.ExecuteCodeAction(
         BABYLON.ActionManager.OnPickTrigger,
         function(evt) {
        
            console.log("(",evt.pointerX,",",evt.pointerY,")");  
        
         });

        //doesn't work (???)
        scene.actionManager = new BABYLON.ActionManager(scene);
        scene.actionManager.registerAction(onpickAction);

        //works fine
        box.actionManager = new BABYLON.ActionManager(scene);
        box.actionManager.registerAction(onpickAction);


    return scene;
}
         
            
  var scene = createScene();
  engine.runRenderLoop(function () {
                scene.render();
            });
 });

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...