blender www.blender.nl
this document is online at http://www.blender.nl/showitem.php?id=57
ALT
 Particle System Shadows Intermediate  Make particle systems cast shadows
2000 11 09 
  Evan Weaver  id57 
Introduction
See Blender Tutorial Guide 2, Chapter 8: Static Particles. Those plants don't cast shadows, and that's pretty dumb-looking when we're aiming for photorealism. But by careful use of image textures and mapping, we can work around that.

By rendering an image texture from the perspective of the light source, changing it to a form of black and white, and subtractively mapping it to the objects that are to receive the shadows, we can achieve a very realistic effect (See Image 0).

Image 0: What we can accomplish
Image 0: What we can accomplish
Step 0: Getting Started
What you need:

- A scene with a particle system. (Grab one off the Tutor Guide 2 CD-ROM if you have it, otherwise you can download it below. Of course the ideal way would be to make your own.

- An object you want it to cast shadows onto.

- A spotlight, set up to cast shadows normally (your non-particle system objects' shadows render as usual).

- The GIMP, the 2D equivalent of Blender, because it's also very free (actually, The GIMP was first, so Blender is the 3D equivalent of it, I guess.) You can use a similar program like Paint Shop Pro or Photoshop if you are comfortable with it, but this tutorial uses The GIMP.

- Not to be upset by someone who starts counting at zero instead of one. :)

Download:
 begin.blend
Image 1: begin.blend before shadowing
Image 1: begin.blend before shadowing
Steps 1-5: Rendering the shadow image
1. Move your spotlight and your particle system to their own separate layer, because we don't want to render any of the background right now. ( Right click on each object, , , click OK)

2. Move to this layer only (), so all that is displayed is the lamp and the plant.

3. Select the spotlight, and press keypad . The spotlight becomes the current camera (see Image 2).

4. Go to render buttons, change sky to premul (unless you don't have a world object defined), and render the current layer at the final resolution. (Premul renders the background as black instead of whatever skies you have set up.)

5. Save the image as something like fake_shadow.tga.

Image 2: The view from the spotlight
Image 2: The view from the spotlight
Steps 5a-5f: Altering the shadow image
5a. Open the image in The GIMP. (There is a Windows version now, so you have no excuse.)

5b. Right-click (on the picture), Image, Colors (depending on GIMP version), Grayscale.

5c. Right-click, Image, Colors, Curves. Adjust the curve so it is at the very bottom on the right, but after about 10 pixels shoots up to the very top. Turn on "Preview". Notice the image is basically black and white now, except, everything that was not black is white, instead of normal black and white conversion (see Image 3). The idea is to make leave the black background completely black (0 0 0), but make the particle systems completely white (255 255 255). So, a dark gray (50 50 50) would become white instead of black like it would if you converted it to two color normally. See Image 3 for "curve" settings, and 4 for the intended result.

5d. Repeat (c) a couple of times until the image is only black and white.

5f. Right-click, File, Save.

Image 3: Your "curves" window should look like this
Image 3: Your "curves" window should look like this
Steps 6-8: Some miscellaneous actions
6. Press tilde (the ~ under escape) to activate all layers again.

7. Hit keypad . The view returns to the camera.

8. Select the floor, wall, or whatever you want to cast shadows onto.

Image 4: Your shadow image should begin like 4A but look like 4B after "curving"
Image 4: Your shadow image should begin like 4A but look like 4B after "curving"
Steps 9-11: Creating the texture
9. Create a new texture in the floor's materials. Set it to image, set the filter to around 4 (adjust later). Load fake_shadow.tga as the texture image. Turn on UseAlpha, and CalcAlpha. (This makes it so the places where the shadow isn't don't affect the rest of the object.)

This texture must be last in the texture order, so that the shadows get put on top of everything else. (Unless, of course, you want to be difficult and fool with stencil and other oddities.)

Image 5: The material settings
Image 5: The material settings


10. Go back to the material buttons. Set the new texture's color percentage slider to 0.500 (Above alpha slider and var slider). This means the material will blend the texture half and half with the underlining colors and textures. Change mix to sub (subtractive instead of blending). Bright spots on the shadow texture are subtracted from the object, causing dark areas, or shadows. (This method avoids white edges on the shadow caused by the filtering process. Took me a while to figure that out...)

11. Set the new textures size x, size y and size z all to 0.100 or so. (This value is the inverse of the distance of the SpotLamp to the objects creating the shadows. Inverse means one divided by the number; i.e. for a lamp 10 units away, 1/10 = 0.100). And here's the key - instead of the default orco mapping type, set the mapping to object and type in the name of your SpotLamp (probably Lamp or Lamp.001; you should change it to something descriptive.) This calculates the texture depending on the position of the lamp.

This procedure must be done for each object that is to receive a shadow, but the texture they all use should be the same (don't "make single user"). All the size and mapping (object) values should be the same in every material belonging to objects that are to receive shadows.




12. Render the entire scene ().

13. Be amazed.

14. Go back and adjust parameters marked "adjust later," and keep repeating steps 12 and 13 until the output looks correct.

15. Save.

Image 6: The final result
Image 6: The final result
And there we are, a particle system with beautiful shadows!

This procedure works only for stills. You could make it work with movies, too, using an animated texture, but that's beyond the scope of this tutorial. And personally I will probably never do it, because my work is all for printing. It's tough to print an animation.

Note for expert users: If you have a ton of objects, adjusting every value in every material is painful. You may want to consider using straight-line ipokeys, and linking each separate material's value to the same ipokey. Then you could simply move the ipokey up or down to alter the value everywhere. This could save you a lot of work in the long run. But you've got to know what your doing...

You can download the final result below.

Here's a shameless plug for the author's Blender website, where you can see a high-resolution version of Image 0, among other things.

Download:
 final.blend