drag and drop.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, new BABYLON.Vector3(0, 0, 0), scene);
  4. camera.setPosition(new BABYLON.Vector3(20, 200, 400));
  5. camera.lowerBetaLimit = 0.1;
  6. camera.upperBetaLimit = (Math.PI / 2) * 0.99;
  7. camera.lowerRadiusLimit = 150;
  8. scene.clearColor = new BABYLON.Color3(0, 0, 0);
  9. // Light
  10. var light = new BABYLON.PointLight("omni", new BABYLON.Vector3(0, 50, 0), scene);
  11. // Ground
  12. var ground = BABYLON.Mesh.CreateGround("ground", 1000, 1000, 1, scene, false);
  13. var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
  14. groundMaterial.specularColor = BABYLON.Color3.Black();
  15. ground.material = groundMaterial;
  16. // Meshes
  17. var redSphere = BABYLON.Mesh.CreateSphere("red", 32, 20, scene);
  18. var redMat = new BABYLON.StandardMaterial("ground", scene);
  19. redMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  20. redMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  21. redMat.emissiveColor = BABYLON.Color3.Red();
  22. redSphere.material = redMat;
  23. redSphere.position.y = 10;
  24. redSphere.position.x -= 100;
  25. var greenBox = BABYLON.Mesh.CreateBox("green", 20, scene);
  26. var greenMat = new BABYLON.StandardMaterial("ground", scene);
  27. greenMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  28. greenMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  29. greenMat.emissiveColor = BABYLON.Color3.Green();
  30. greenBox.material = greenMat;
  31. greenBox.position.z -= 100;
  32. greenBox.position.y = 10;
  33. var blueBox = BABYLON.Mesh.CreateBox("blue", 20, scene);
  34. var blueMat = new BABYLON.StandardMaterial("ground", scene);
  35. blueMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  36. blueMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  37. blueMat.emissiveColor = BABYLON.Color3.Blue();
  38. blueBox.material = blueMat;
  39. blueBox.position.x += 100;
  40. blueBox.position.y = 10;
  41. var purpleDonut = BABYLON.Mesh.CreateTorus("red", 30, 10, 32, scene);
  42. var purpleMat = new BABYLON.StandardMaterial("ground", scene);
  43. purpleMat.diffuseColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  44. purpleMat.specularColor = new BABYLON.Color3(0.4, 0.4, 0.4);
  45. purpleMat.emissiveColor = BABYLON.Color3.Purple();
  46. purpleDonut.material = purpleMat;
  47. purpleDonut.position.y = 10;
  48. purpleDonut.position.z += 100;
  49. // Events
  50. var canvas = engine.getRenderingCanvas();
  51. var startingPoint;
  52. var currentMesh;
  53. var getGroundPosition = function () {
  54. // Use a predicate to get position on the ground
  55. var pickinfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh == ground; });
  56. if (pickinfo.hit) {
  57. return pickinfo.pickedPoint;
  58. }
  59. return null;
  60. }
  61. var onPointerDown = function (evt) {
  62. if (evt.button !== 0) {
  63. return;
  64. }
  65. // check if we are under a mesh
  66. var pickInfo = scene.pick(scene.pointerX, scene.pointerY, function (mesh) { return mesh !== ground; });
  67. if (pickInfo.hit) {
  68. currentMesh = pickInfo.pickedMesh;
  69. startingPoint = getGroundPosition(evt);
  70. if (startingPoint) { // we need to disconnect camera from canvas
  71. setTimeout(function () {
  72. camera.detachControl(canvas);
  73. }, 0);
  74. }
  75. }
  76. }
  77. var onPointerUp = function () {
  78. if (startingPoint) {
  79. camera.attachControl(canvas, true);
  80. startingPoint = null;
  81. return;
  82. }
  83. }
  84. var onPointerMove = function (evt) {
  85. if (!startingPoint) {
  86. return;
  87. }
  88. var current = getGroundPosition(evt);
  89. if (!current) {
  90. return;
  91. }
  92. var diff = current.subtract(startingPoint);
  93. currentMesh.position.addInPlace(diff);
  94. startingPoint = current;
  95. }
  96. canvas.addEventListener("pointerdown", onPointerDown, false);
  97. canvas.addEventListener("pointerup", onPointerUp, false);
  98. canvas.addEventListener("pointermove", onPointerMove, false);
  99. scene.onDispose = function () {
  100. canvas.removeEventListener("pointerdown", onPointerDown);
  101. canvas.removeEventListener("pointerup", onPointerUp);
  102. canvas.removeEventListener("pointermove", onPointerMove);
  103. }
  104. return scene;
  105. };