physics.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. scene.clearColor = BABYLON.Color3.Purple();
  4. var camera = new BABYLON.FreeCamera("Camera", new BABYLON.Vector3(0, 0, -20), scene);
  5. camera.attachControl(canvas, true);
  6. camera.checkCollisions = true;
  7. camera.applyGravity = true;
  8. camera.setTarget(new BABYLON.Vector3(0, 0, 0));
  9. var light = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(0.2, -1, 0), scene);
  10. light.position = new BABYLON.Vector3(0, 80, 0);
  11. // Material
  12. var materialAmiga = new BABYLON.StandardMaterial("amiga", scene);
  13. materialAmiga.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  14. materialAmiga.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  15. materialAmiga.diffuseTexture.uScale = 5;
  16. materialAmiga.diffuseTexture.vScale = 5;
  17. var materialAmiga2 = new BABYLON.StandardMaterial("amiga", scene);
  18. materialAmiga2.diffuseTexture = new BABYLON.Texture("textures/amiga.jpg", scene);
  19. materialAmiga2.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  20. // Shadows
  21. var shadowGenerator = new BABYLON.ShadowGenerator(2048, light);
  22. // Physics
  23. //scene.enablePhysics(null, new BABYLON.CannonJSPlugin());
  24. scene.enablePhysics(null, new BABYLON.OimoJSPlugin());
  25. // Spheres
  26. var y = 0;
  27. for (var index = 0; index < 100; index++) {
  28. var sphere = BABYLON.Mesh.CreateSphere("Sphere0", 16, 3, scene);
  29. sphere.material = materialAmiga;
  30. sphere.position = new BABYLON.Vector3(Math.random() * 20 - 10, y, Math.random() * 10 - 5);
  31. shadowGenerator.addShadowCaster(sphere);
  32. sphere.physicsImpostor = new BABYLON.PhysicsImpostor(sphere, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 1 }, scene);
  33. y += 2;
  34. }
  35. // Link
  36. var spheres = [];
  37. for (index = 0; index < 10; index++) {
  38. sphere = BABYLON.Mesh.CreateSphere("Sphere0", 16, 1, scene);
  39. spheres.push(sphere);
  40. sphere.material = materialAmiga2;
  41. sphere.position = new BABYLON.Vector3(Math.random() * 20 - 10, y, Math.random() * 10 - 5);
  42. shadowGenerator.addShadowCaster(sphere);
  43. sphere.physicsImpostor = new BABYLON.PhysicsImpostor(sphere, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 1 }, scene);
  44. }
  45. for (index = 0; index < 9; index++) {
  46. spheres[index].setPhysicsLinkWith(spheres[index + 1], new BABYLON.Vector3(0, 0.5, 0), new BABYLON.Vector3(0, -0.5, 0));
  47. }
  48. // Box
  49. var box0 = BABYLON.Mesh.CreateBox("Box0", 3, scene);
  50. box0.position = new BABYLON.Vector3(3, 30, 0);
  51. var materialWood = new BABYLON.StandardMaterial("wood", scene);
  52. materialWood.diffuseTexture = new BABYLON.Texture("textures/crate.png", scene);
  53. materialWood.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  54. box0.material = materialWood;
  55. shadowGenerator.addShadowCaster(box0);
  56. // Compound
  57. var part0 = BABYLON.Mesh.CreateBox("part0", 3, scene);
  58. part0.position = new BABYLON.Vector3(3, 30, 0);
  59. part0.material = materialWood;
  60. var part1 = BABYLON.Mesh.CreateBox("part1", 3, scene);
  61. part1.parent = part0; // We need a hierarchy for compound objects
  62. part1.position = new BABYLON.Vector3(0, 3, 0);
  63. part1.material = materialWood;
  64. shadowGenerator.addShadowCaster(part0);
  65. shadowGenerator.addShadowCaster(part1);
  66. shadowGenerator.useBlurExponentialShadowMap = true;
  67. shadowGenerator.useKernelBlur = true;
  68. shadowGenerator.blurKernel = 32;
  69. // Playground
  70. var ground = BABYLON.Mesh.CreateBox("Ground", 1, scene);
  71. ground.scaling = new BABYLON.Vector3(100, 1, 100);
  72. ground.position.y = -5.0;
  73. ground.checkCollisions = true;
  74. var border0 = BABYLON.Mesh.CreateBox("border0", 1, scene);
  75. border0.scaling = new BABYLON.Vector3(1, 100, 100);
  76. border0.position.y = -5.0;
  77. border0.position.x = -50.0;
  78. border0.checkCollisions = true;
  79. var border1 = BABYLON.Mesh.CreateBox("border1", 1, scene);
  80. border1.scaling = new BABYLON.Vector3(1, 100, 100);
  81. border1.position.y = -5.0;
  82. border1.position.x = 50.0;
  83. border1.checkCollisions = true;
  84. var border2 = BABYLON.Mesh.CreateBox("border2", 1, scene);
  85. border2.scaling = new BABYLON.Vector3(100, 100, 1);
  86. border2.position.y = -5.0;
  87. border2.position.z = 50.0;
  88. border2.checkCollisions = true;
  89. var border3 = BABYLON.Mesh.CreateBox("border3", 1, scene);
  90. border3.scaling = new BABYLON.Vector3(100, 100, 1);
  91. border3.position.y = -5.0;
  92. border3.position.z = -50.0;
  93. border3.checkCollisions = true;
  94. var groundMat = new BABYLON.StandardMaterial("groundMat", scene);
  95. groundMat.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  96. groundMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  97. groundMat.backFaceCulling = false;
  98. ground.material = groundMat;
  99. border0.material = groundMat;
  100. border1.material = groundMat;
  101. border2.material = groundMat;
  102. border3.material = groundMat;
  103. ground.receiveShadows = true;
  104. // Physics
  105. box0.physicsImpostor = new BABYLON.PhysicsImpostor(box0, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 2, friction: 0.4, restitution: 0.3 }, scene);
  106. ground.physicsImpostor = new BABYLON.PhysicsImpostor(ground, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0, friction: 0.5, restitution: 0.7 }, scene);
  107. border0.physicsImpostor = new BABYLON.PhysicsImpostor(border0, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0 }, scene);
  108. border1.physicsImpostor = new BABYLON.PhysicsImpostor(border1, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0 }, scene);
  109. border2.physicsImpostor = new BABYLON.PhysicsImpostor(border2, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0 }, scene);
  110. border3.physicsImpostor = new BABYLON.PhysicsImpostor(border3, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 0 }, scene);
  111. part0.physicsImpostor = new BABYLON.PhysicsImpostor(part0, BABYLON.PhysicsImpostor.BoxImpostor, { mass: 2, friction: 0.4, restitution: 0.3 }, scene);
  112. return scene;
  113. }