shadows.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. // Shadows
  38. var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
  39. shadowGenerator.getShadowMap().renderList.push(torus);
  40. shadowGenerator.useExponentialShadowMap = true;
  41. var shadowGenerator2 = new BABYLON.ShadowGenerator(1024, light2);
  42. shadowGenerator2.getShadowMap().renderList.push(torus);
  43. shadowGenerator2.usePoissonSampling = true;
  44. ground.receiveShadows = true;
  45. // Animations
  46. var alpha = 0;
  47. scene.registerBeforeRender(function () {
  48. torus.rotation.x += 0.01;
  49. torus.rotation.z += 0.02;
  50. torus.position = new BABYLON.Vector3(Math.cos(alpha) * 30, 10, Math.sin(alpha) * 30);
  51. alpha += 0.01;
  52. });
  53. return scene;
  54. }