Shadows

Heaps has support for real-time shadows. Objects can both cast and receive shadows. This is set at a material level. In order to for shadows to work properly normals need to be calculated for your geometry.

In the following example you can see a simple scene with a couple of spheres and a floor. The spheres both cast and receive shadows while the floor only receives them. For shadows to render correctly your scene will need at least 1 light.

  1. //Create a floor plane where we can view our cast shadows
  2. var floor = new h3d.prim.Cube(10, 10, 0.1);
  3. //Make sure se add normals
  4. floor.addNormals();
  5. floor.translate( -5, -5, 0);
  6. var m = new h3d.scene.Mesh(floor, s3d);
  7. //Enabled lighting
  8. m.material.mainPass.enableLights = true;
  9. //Enable receieiving of shadows. Our floor does not need to cast
  10. m.material.receiveShadows = true;
  11. //Greate some sphere geometry for casting and receiving shadows
  12. var sphere = new h3d.prim.Sphere(1, 32, 24);
  13. //Make sure we add normals
  14. sphere.addNormals();
  15. var p = new h3d.scene.Mesh(sphere, s3d);
  16. p.z = 1;
  17. p.material.mainPass.enableLights = true;
  18. //Enable casting and receiving of shadows
  19. p.material.shadows = true;
  20. p.material.color.setColor(Std.random(0x1000000));
  21. var p2 = new h3d.scene.Mesh(sphere, s3d);
  22. p2.z = 2.5;
  23. p2.x = 1;
  24. p2.y = 1;
  25. p2.scale(0.5);
  26. p2.material.mainPass.enableLights = true;
  27. //Enable casting and receiving of shadows
  28. p2.material.shadows = true;
  29. p2.material.color.setColor(Std.random(0x1000000));
  30. //Set up a light otherwise we don't get any shadows
  31. var directionalLight = new h3d.scene.DirLight(new h3d.Vector(-0.3, -0.2, -1), s3d);
  32. //Move our camera to a position to see the shadows
  33. s3d.camera.pos.set(12, 12, 6);
  34. //Setup a Camera Controller to move the camera
  35. new h3d.scene.CameraController(s3d).loadFromCamera();

Shadow Example

Blurring Shadows

To create a more realistic effect you can blur your shadows. You do so by accessing the shadow pass from the renderer and setting the amount of passes on the blur object.

You can see an example here:

  1. var shadow = s3d.renderer.getPass(h3d.pass.ShadowMap);
  2. //Increasing the amount of passes increases the amount of blur applied to the shadow
  3. shadow.blur.passes = 3;

An example with shadow.blur.passes = 1; Shadow blurring set to 1

An example with shadow.blur.passes = 10; Shadow blurring set to 10