Intersections.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 70, new BABYLON.Vector3(5, 0, 0), scene);
  4. camera.attachControl(canvas, true);
  5. // Material
  6. var matPlan = new BABYLON.StandardMaterial("matPlan1", scene);
  7. matPlan.backFaceCulling = false;
  8. matPlan.emissiveColor = new BABYLON.Color3(0.2, 1, 0.2);
  9. var matBB = new BABYLON.StandardMaterial("matBB", scene);
  10. matBB.emissiveColor = new BABYLON.Color3(1, 1, 1);
  11. matBB.wireframe = true;
  12. // Intersection point
  13. var pointToIntersect = new BABYLON.Vector3(-30, 0, 0);
  14. var origin = BABYLON.Mesh.CreateSphere("origin", 4, 0.3, scene);
  15. origin.position = pointToIntersect;
  16. origin.material = matPlan;
  17. // Create two planes
  18. var plan1 = BABYLON.Mesh.CreatePlane("plane1", 20, scene);
  19. plan1.position = new BABYLON.Vector3(13, 0, 0);
  20. plan1.rotation.x = -Math.PI / 4;
  21. plan1.material = matPlan;
  22. var plan2 = BABYLON.Mesh.CreatePlane("plane2", 20, scene);
  23. plan2.position = new BABYLON.Vector3(-13, 0, 0);
  24. plan2.rotation.x = -Math.PI / 4;
  25. plan2.material = matPlan;
  26. // AABB - Axis aligned bounding box
  27. var planAABB = BABYLON.Mesh.CreateBox("AABB", 20, scene);
  28. planAABB.material = matBB;
  29. planAABB.position = new BABYLON.Vector3(13, 0, 0);
  30. planAABB.scaling = new BABYLON.Vector3(1, Math.cos(Math.PI / 4), Math.cos(Math.PI / 4));
  31. // OBB - Object boundind box
  32. var planOBB = BABYLON.Mesh.CreateBox("OBB", 20, scene);
  33. planOBB.scaling = new BABYLON.Vector3(1, 1, 0.05);
  34. planOBB.parent = plan2;
  35. planOBB.material = matBB;
  36. // Balloons
  37. var balloon1 = BABYLON.Mesh.CreateSphere("balloon1", 10, 2.0, scene);
  38. var balloon2 = BABYLON.Mesh.CreateSphere("balloon2", 10, 2.0, scene);
  39. var balloon3 = BABYLON.Mesh.CreateSphere("balloon3", 10, 2.0, scene);
  40. balloon1.material = new BABYLON.StandardMaterial("matBallon", scene);
  41. balloon2.material = new BABYLON.StandardMaterial("matBallon", scene);
  42. balloon3.material = new BABYLON.StandardMaterial("matBallon", scene);
  43. balloon1.position = new BABYLON.Vector3(6, 5, 0);
  44. balloon2.position = new BABYLON.Vector3(-6, 5, 0);
  45. balloon3.position = new BABYLON.Vector3(-30, 5, 0);
  46. //Animation
  47. var alpha = Math.PI;
  48. scene.registerBeforeRender(function () {
  49. //Balloon 1 intersection -- Precise = false
  50. if (balloon1.intersectsMesh(plan1, false)) {
  51. balloon1.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
  52. } else {
  53. balloon1.material.emissiveColor = new BABYLON.Color3(1, 1, 1);
  54. }
  55. //Balloon 2 intersection -- Precise = true
  56. if (balloon2.intersectsMesh(plan2, true)) {
  57. balloon2.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
  58. } else {
  59. balloon2.material.emissiveColor = new BABYLON.Color3(1, 1, 1);
  60. }
  61. //balloon 3 intersection on single point
  62. if (balloon3.intersectsPoint(pointToIntersect)) {
  63. balloon3.material.emissiveColor = new BABYLON.Color3(1, 0, 0);
  64. } else {
  65. balloon3.material.emissiveColor = new BABYLON.Color3(1, 1, 1);
  66. }
  67. alpha += 0.01;
  68. balloon1.position.y += Math.cos(alpha) / 10;
  69. balloon2.position.y = balloon1.position.y;
  70. balloon3.position.y = balloon1.position.y;
  71. });
  72. return scene;
  73. }