2017-08-27 74 views
0

不久前,我開始在巴比倫,我正在尋找一些方法來查找鼠標在畫布上點擊時的座標。我能夠很容易地找到一個給定的對象,但不是現場本身(!?)我不知道什麼可能是錯的,它似乎很愚蠢,但我還沒有找到解決方案...如何在Babylon.js場景中找到點擊的座標?

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(); 
 
    }); 
 
});
<!DOCTYPE html> 
 
<html lang="en"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Title</title> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/babylonjs/2.5.0/babylon.js"></script> 
 
    <style> 
 
     #canvas { 
 
      width: 100%; 
 
      height: 100%; 
 
     } 
 
    </style> 
 
</head> 
 
<body> 
 
    <canvas id="canvas"></canvas> 
 
</body> 
 
</html>

回答