shadows.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var CreateShadowsTestScene = function (engine) {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, BABYLON.Vector3.Zero(), scene);
  4. var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0, -1, -0.2), scene);
  5. var light2 = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(-1, -2, -1), scene);
  6. light.position = new BABYLON.Vector3(0, 30, 0);
  7. light2.position = new BABYLON.Vector3(10, 20, 10);
  8. light.intensity = 0.6;
  9. light2.intensity = 0.6;
  10. camera.setPosition(new BABYLON.Vector3(-20, 20, 0));
  11. // Skybox
  12. var skybox = BABYLON.Mesh.CreateBox("skyBox", 1000.0, scene);
  13. var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
  14. skyboxMaterial.backFaceCulling = false;
  15. skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("Scenes/Customs/skybox/night", scene);
  16. skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
  17. skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  18. skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  19. skybox.material = skyboxMaterial;
  20. // Ground
  21. var ground = BABYLON.Mesh.CreateGround("ground", 1000, 1000, 1, scene, false);
  22. var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
  23. groundMaterial.diffuseTexture = new BABYLON.Texture("/Assets/grass.jpg", scene);
  24. groundMaterial.diffuseTexture.uScale = 60;
  25. groundMaterial.diffuseTexture.vScale = 60;
  26. groundMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  27. groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  28. ground.position.y = -2.05;
  29. ground.material = groundMaterial;
  30. // Torus
  31. var torus = BABYLON.Mesh.CreateTorus("torus", 8, 2, 32, scene, false);
  32. torus.position.y = 6.0;
  33. var torus2 = BABYLON.Mesh.CreateTorus("torus2", 4, 1, 32, scene, false);
  34. torus2.position.y = 6.0;
  35. var torusMaterial = new BABYLON.StandardMaterial("torus", scene);
  36. torusMaterial.diffuseTexture = new BABYLON.Texture("/Scenes/Customs/rocks.dds", scene);
  37. torusMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
  38. torusMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  39. torus.material = torusMaterial;
  40. torus2.material = torusMaterial;
  41. // Shadows
  42. var shadowGenerator = new BABYLON.ShadowGenerator(512, light);
  43. shadowGenerator.getShadowMap().renderList.push(torus);
  44. shadowGenerator.getShadowMap().renderList.push(torus2);
  45. var shadowGenerator2 = new BABYLON.ShadowGenerator(512, light2);
  46. shadowGenerator2.getShadowMap().renderList.push(torus);
  47. shadowGenerator2.getShadowMap().renderList.push(torus2);
  48. shadowGenerator2.useVarianceShadowMap = false;
  49. ground.receiveShadows = true;
  50. var beforeRenderFunction = function () {
  51. // Camera
  52. if (camera.beta < 0.1)
  53. camera.beta = 0.1;
  54. else if (camera.beta > (Math.PI / 2) * 0.99)
  55. camera.beta = (Math.PI / 2) * 0.99;
  56. if (camera.radius > 150)
  57. camera.radius = 150;
  58. if (camera.radius < 5)
  59. camera.radius = 5;
  60. };
  61. scene.registerBeforeRender(beforeRenderFunction);
  62. // Animations
  63. scene.registerBeforeRender(function () {
  64. torus.rotation.x += 0.01;
  65. torus.rotation.z += 0.02;
  66. torus2.rotation.x += 0.02;
  67. torus2.rotation.y += 0.01;
  68. });
  69. return scene;
  70. };