Sound on mesh.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var createScene = function () {
  2. // This creates a basic Babylon Scene object (non-mesh)
  3. var scene = new BABYLON.Scene(engine);
  4. // Lights
  5. var light0 = new BABYLON.DirectionalLight("Omni", new BABYLON.Vector3(-2, -5, 2), scene);
  6. var light1 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(2, -5, -2), scene);
  7. // Need a free camera for collisions
  8. var camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(0, -8, -20), scene);
  9. camera.attachControl(canvas, true);
  10. //Ground
  11. var ground = BABYLON.Mesh.CreatePlane("ground", 400.0, scene);
  12. ground.material = new BABYLON.StandardMaterial("groundMat", scene);
  13. ground.material.diffuseColor = new BABYLON.Color3(1, 1, 1);
  14. ground.material.backFaceCulling = false;
  15. ground.position = new BABYLON.Vector3(5, -10, -15);
  16. ground.rotation = new BABYLON.Vector3(Math.PI / 2, 0, 0);
  17. //Simple crate
  18. var box = BABYLON.Mesh.CreateBox("crate", 2, scene);
  19. box.material = new BABYLON.StandardMaterial("Mat", scene);
  20. box.material.diffuseTexture = new BABYLON.Texture("textures/crate.png", scene);
  21. box.position = new BABYLON.Vector3(10, -9, 0);
  22. // Create and load the sound async
  23. var music = new BABYLON.Sound("Violons", "sounds/violons11.wav", scene, function () {
  24. // Call with the sound is ready to be played (loaded & decoded)
  25. // TODO: add your logic
  26. console.log("Sound ready to be played!");
  27. }, { loop: true, autoplay: true });
  28. // Sound will now follow the mesh position
  29. music.attachToMesh(box);
  30. //Set gravity for the scene (G force like, on Y-axis)
  31. scene.gravity = new BABYLON.Vector3(0, -0.9, 0);
  32. // Enable Collisions
  33. scene.collisionsEnabled = true;
  34. //Then apply collisions and gravity to the active camera
  35. camera.checkCollisions = true;
  36. camera.applyGravity = true;
  37. //Set the ellipsoid around the camera (e.g. your player's size)
  38. camera.ellipsoid = new BABYLON.Vector3(1, 1, 1);
  39. //finally, say which mesh will be collisionable
  40. ground.checkCollisions = true;
  41. var alpha = 0;
  42. scene.registerBeforeRender(function () {
  43. // Moving the box will automatically move the associated sound attached to it
  44. box.position = new BABYLON.Vector3(Math.cos(alpha) * 30, -9, Math.sin(alpha) * 30);
  45. alpha += 0.01;
  46. });
  47. return scene;
  48. };