shadows.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. // Setup environment
  4. var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0.8, 90, BABYLON.Vector3.Zero(), scene);
  5. camera.lowerBetaLimit = 0.1;
  6. camera.upperBetaLimit = (Math.PI / 2) * 0.9;
  7. camera.lowerRadiusLimit = 30;
  8. camera.upperRadiusLimit = 150;
  9. camera.attachControl(canvas, true);
  10. // light1
  11. var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-1, -2, -1), scene);
  12. light.position = new BABYLON.Vector3(20, 40, 20);
  13. light.intensity = 0.5;
  14. var lightSphere = BABYLON.Mesh.CreateSphere("sphere", 10, 2, scene);
  15. lightSphere.position = light.position;
  16. lightSphere.material = new BABYLON.StandardMaterial("light", scene);
  17. lightSphere.material.emissiveColor = new BABYLON.Color3(1, 1, 0);
  18. // light2
  19. var light2 = new BABYLON.SpotLight("spot02", new BABYLON.Vector3(30, 40, 20),
  20. new BABYLON.Vector3(-1, -2, -1), 1.1, 16, scene);
  21. light2.intensity = 0.5;
  22. var lightSphere2 = BABYLON.Mesh.CreateSphere("sphere", 10, 2, scene);
  23. lightSphere2.position = light2.position;
  24. lightSphere2.material = new BABYLON.StandardMaterial("light", scene);
  25. lightSphere2.material.emissiveColor = new BABYLON.Color3(1, 1, 0);
  26. // Ground
  27. var ground = BABYLON.Mesh.CreateGroundFromHeightMap("ground", "textures/heightMap.png", 100, 100, 100, 0, 10, scene, false);
  28. var groundMaterial = new BABYLON.StandardMaterial("ground", scene);
  29. groundMaterial.diffuseTexture = new BABYLON.Texture("textures/ground.jpg", scene);
  30. groundMaterial.diffuseTexture.uScale = 6;
  31. groundMaterial.diffuseTexture.vScale = 6;
  32. groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
  33. ground.position.y = -2.05;
  34. ground.material = groundMaterial;
  35. // Torus
  36. var torus = BABYLON.Mesh.CreateTorus("torus", 4, 2, 30, scene, false);
  37. // Box
  38. var box = BABYLON.Mesh.CreateBox("box", 3);
  39. box.parent = torus;
  40. // Shadows
  41. var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
  42. shadowGenerator.addShadowCaster(torus);
  43. shadowGenerator.useExponentialShadowMap = true;
  44. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, light2);
  45. shadowGenerator2.addShadowCaster(torus);
  46. shadowGenerator2.usePoissonSampling = true;
  47. ground.receiveShadows = true;
  48. // Animations
  49. var alpha = 0;
  50. scene.registerBeforeRender(function () {
  51. torus.rotation.x += 0.01;
  52. torus.rotation.z += 0.02;
  53. torus.position = new BABYLON.Vector3(Math.cos(alpha) * 30, 10, Math.sin(alpha) * 30);
  54. alpha += 0.01;
  55. });
  56. return scene;
  57. }