pointer events handling.js 5.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
  4. camera.setTarget(BABYLON.Vector3.Zero());
  5. camera.attachControl(canvas, true);
  6. var light = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(0, 1, 0), scene);
  7. light.intensity = 0.7;
  8. var sphere = BABYLON.Mesh.CreateSphere("sphere1", 16, 2, scene);
  9. sphere.position.y = 1;
  10. var ground = BABYLON.Mesh.CreateGround("ground1", 6, 6, 2, scene);
  11. scene.exclusiveDoubleMode = false;
  12. scene.onPrePointerObservable.add( function(pointerInfo, eventState) {
  13. console.log('%c PrePointerObservable: pointer down', 'background: red; color: white');
  14. //pointerInfo.skipOnPointerObservable = true;
  15. }, BABYLON.PointerEventTypes.POINTERDOWN, false);
  16. scene.onPrePointerObservable.add( function(pointerInfo, eventState) {
  17. console.log('%c PrePointerObservable: pointer up', 'background: red; color: white');
  18. // pointerInfo.skipOnPointerObservable = true;
  19. }, BABYLON.PointerEventTypes.POINTERUP, false);
  20. scene.onPrePointerObservable.add( function(pointerInfo, eventState) {
  21. console.log('%c PrePointerObservable: pointer pick: ' + pointerInfo.pickInfo.pickedMesh.name, 'background: red; color: white');
  22. }, BABYLON.PointerEventTypes.POINTERPICK, false);
  23. scene.onPrePointerObservable.add( function(pointerInfo, eventState) {
  24. console.log('%c PrePointerObservable: pointer tap', 'background: red; color: white');
  25. }, BABYLON.PointerEventTypes.POINTERTAP, false);
  26. scene.onPrePointerObservable.add( function(pointerInfo, eventState) {
  27. console.log('%c PrePointerObservable: pointer double tap', 'background: red; color: white');
  28. }, BABYLON.PointerEventTypes.POINTERDOUBLETAP, false);
  29. scene.onPointerObservable.add( function(pointerInfo, eventState) {
  30. console.log('%c PointerObservable: pointer down', 'background: blue; color: white');
  31. }, BABYLON.PointerEventTypes.POINTERDOWN, false);
  32. scene.onPointerObservable.add( function(pointerInfo, eventState) {
  33. console.log('%c PointerObservable: pointer up', 'background: blue; color: white');
  34. }, BABYLON.PointerEventTypes.POINTERUP, false);
  35. scene.onPointerObservable.add( function(pointerInfo, eventState) {
  36. console.log('%c PointerObservable: pointer pick: ' + pointerInfo.pickInfo.pickedMesh.name, 'background: blue; color: white');
  37. }, BABYLON.PointerEventTypes.POINTERPICK, false);
  38. scene.onPointerObservable.add( function(pointerInfo, eventState) {
  39. console.log('%c PointerObservable: pointer tap', 'background: blue; color: white');
  40. }, BABYLON.PointerEventTypes.POINTERTAP, false);
  41. scene.onPointerObservable.add( function(pointerInfo, eventState) {
  42. console.log('%c PointerObservable: pointer double tap', 'background: blue; color: white');
  43. }, BABYLON.PointerEventTypes.POINTERDOUBLETAP, false);
  44. var meshes = [sphere, ground];
  45. for (var i=0; i< meshes.length; i++) {
  46. let mesh = meshes[i];
  47. mesh.actionManager = new BABYLON.ActionManager(scene);
  48. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnLongPressTrigger, (function(mesh) {
  49. console.log("%c ActionManager: long press : " + mesh.name, 'background: green; color: white');
  50. }).bind(this, mesh)));
  51. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnLeftPickTrigger, (function(mesh) {
  52. console.log("%c ActionManager: left pick: " + mesh.name, 'background: green; color: white');
  53. }).bind(this, mesh)));
  54. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnRightPickTrigger, (function(mesh) {
  55. console.log("%c ActionManager: right pick: " + mesh.name, 'background: green; color: white');
  56. }).bind(this, mesh)));
  57. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnCenterPickTrigger, (function(mesh) {
  58. console.log("%c ActionManager: center pick: " + mesh.name, 'background: green; color: white');
  59. }).bind(this, mesh)));
  60. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickTrigger, (function(mesh) {
  61. console.log("%c ActionManager: pick : " + mesh.name, 'background: green; color: white');
  62. }).bind(this, mesh)));
  63. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickDownTrigger, (function(mesh) {
  64. console.log("%c ActionManager: pick down : " + mesh.name, 'background: green; color: white');
  65. }).bind(this, mesh)));
  66. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickUpTrigger, (function(mesh) {
  67. console.log("%c ActionManager: pick up : " + mesh.name, 'background: green; color: white');
  68. }).bind(this, mesh)));
  69. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnDoublePickTrigger, (function(mesh) {
  70. console.log("%c ActionManager: double pick : " + mesh.name, 'background: green; color: white');
  71. }).bind(this, mesh)));
  72. mesh.actionManager.registerAction(new BABYLON.ExecuteCodeAction(BABYLON.ActionManager.OnPickOutTrigger, (function(mesh) {
  73. console.log("%c ActionManager: pick out : " + mesh.name, 'background: green; color: white');
  74. }).bind(this, mesh)));
  75. }
  76. return scene;
  77. };