Advanced shadowmapping/diffusionmapping

Helpdesk Real 3D Main area

What I am going to discuss here are several uses of the same thing. All are based on a very simple RPL program, and can produce the following effects:

General shadowmapping/diffusionmaping techniques

I don't actually know what is the correct term for this, shadowmapping or diffusionmapping. In Real 3D there is no way of controlling the diffusion property of the material other than by mapping a black object with a color texture which has a nonfull effect (via the effect slider). Mapping this property (like LightWave uses diffusion mapping to add details on a colortextured surface) will require a little bit of tricking Real 3D to do what we want. On page ***** in the main manual there is a formula which descripes what Real 3D does when the shadowmap checkbox is activated. Unfortunately this only work properly with colored object and not when assosiated with texture mapping. This formula will be the basis of what we are going to do to make it work with textured objects as well. Create the following program in a texteditor, and save it in the rpl directory with the name ShadowMap1.rpl:

: SHA1GET
R FETCH iSha1R STORE
G FETCH iSha1G STORE
B FETCH iSha1B STORE
;

: SHA1SET
iSha1R FETCH R FETCH * 255 / R STORE
iSha1G FETCH G FETCH * 255 / G STORE
iSha1B FETCH B FETCH * 255 / B STORE
;

Also we need to do some additions to the startup.rpl file. Open the file in a texteditor and scroll down (not far) until you find a line that says something like this: 'VARIABLE iTOG_PARPER 0 iTOG_PARPER STORE' and just above it, enter the following:

VARIABLE iSha1R
VARIABLE iSha1G
VARIABLE iSha1B

Now, start Real 3D and the new variables defined in the startup.rpl can now be used as a temporary holding place for a color. Open the material editor and make a material called getcolor with an appropriate texturemap defining the color. click on the find field to define the handler program and select shadowmap1.rpl file you just created. Switch the color handler to RPL and type in SHA1GET in the expression field. Set the effect to 1 and hit apply. Create a second material by renaming this one to setcol. Select an appropriate shadowmap (greyscale works best, but using colormaps can give some nice effects), i.e. the panels texture. Change the color expression field to SHA1SET, set the effect slider to max and hit apply. Create some objects and map them with the materials you just defined, and, voila.

Some explanations may be appropriate at this point. When we call the SHA1GET word in the first material, it copies the red, green, and blue components of the texturemap into some variables. Since the effect is set to 1 the effect of this colormap when mapped onto some objects is so small that it is not of any notice. A value of 0 would trigger Real 3D to skip the entire definition and nothing would have happened. Increasing the value would increase the blending, which we are trying to avoid. When we call the SHA1SET word in the second material, it fetches the values from the variables and from the components in the shadowmap texture and multiplies them. It then divides the answer by 255 resulting in a range from 0 to 255 which off course is what we want. The end result is that the colormap is 'filtered' through the shadowmap (meaning that if the shadowmap is red, only the red colors of the colormap is shown). So if the shadowmap is a grey image, the color would have its brightness controlled by it. As this method works with colorized shadowmaps, it is a bit more flexible than the usual diffusion map used around, as these are normally based on greyscale images. Below is a couple of images describing the effect of shadowmapping/diffusionmapping.

Examples of color and shadow mapping combined

Basic ColorMap ShadowMap
Fig. 1. The very simple basic model Fig. 2. Textured with a colormap Fig. 3. Filtered though a shadowmap
Specularity Redefinition BumpAdded
Fig. 4. Shadowmap control the specularity Fig. 5. Redefine reflected to white Fig. 6. Shadowmap used as bumpmap also

In case you didn't notice, this is an extremely powerful method of adding detail to a very simple object. This polyhedron anded together with a simple ellipsoid kinda looks like a spaceshit. Okay, it isn't that good, but you get the picture.

The following section(s) may not have been tested in time of the release due to lack of time, but you get the idea

Combinations with other properties

By the way, the example above is a great illustration to generate the kind of pictures produced by Lightwave (if the shadowmap have only grey shades). The 'whiteness' of the shadowmap defines the diffusion (producing Lightwave's diffuse map), and setting the redefine reflected color to white produces evenly distributed specularity no matter what the color is. To make the typical Lightwave specularity brightness map look, switch on the brill map, set the Scope handler to formula, and enter 'sb=br,br=0' in the formula field. However, sometimes it is desireable to make the colormap define the specularity brightness (instead of the diffusion map). Since the effect of the colormap is now set to 1, using the normal specular brightness mapping technique will produce no noticable effect. So we need to read the value in the first material, store them in variables, read the variables in the second, and store them in the correct places. To do this, append the following to the 'program':

: SPB1GET
br FFETCH fSpeBr1 FSTORE
0.0 br FSTORE
;

: SPB1SET
fSpeBr1 FFETCH sb FSTORE
;

This will of course require that you define the fSpeBr1 variable somewhere in the startup.r3d file by writing 'FVARIABLE fSpeBr1' close to the others you wrote before. Also, we use brilliance map to define the specular brightness, so set the brilliance toggle in the getcolor material. Set Scope handler to RPL, and enter 'SPB1GET' in the expression field. Switch to the setcolor material, set Scope handler to RPL and enter 'SPB1SET' in the expression field. What happens is that the blue component (if my memory serves me correct, it might be the green one?) of the colormap also defines the specular brightness.

Now it is time to change one of the lines, just to give you an idea of how to scale the effect. The following requires that the colorcomponent is represented in full (giving galues from 0 to 255). Locate the line that says 'br FFETCH fSpeBr1 FSTORE' in the program and replace it with one of the following:

100.0 br FFETCH F- fSpeBr1 FSTORE                 ( inverts the specular effect
sb FFETCH br FFETCH F+ fSpeBr1 FSTORE             ( add to normal spec. br.
br FFETCH 0.5 F* fSpeBr1 FSTORE                   ( rescales, new is 0 to 128
br FFETCH 0.25 F* 50.0 F+ fSpeBr1 FSTORE          ( rescales, new is 50 to 114
100.0 br FFETCH 0.25 F* 50.0 F+ F- fSpeBr1 FSTORE ( inverts, rescales, new is 114 to 50

And so on, and so on... As you can see, the possibilities are limited only by the imagination, and the rendertime available :-).

Making fake shadows

There is one major issue concerning the use of diffusionmapping to generate fake shadows, and that is the fact that this method totally overrides the ambient light setting. This can be both a blessing and a curse, depending on how you think of it. On the positive side, you can have lots of ambient light in the scene and still get very prominent shadows if required (avoiding a lot of hazzle setting up lights). On the negative side, it is required that the image used has to be finetuned for the scene to get a 'faked photorealistic' (I know, odd combination) result. In other words, the darkest grey have to be the same as the ambient light setting, so make different shadowmaps for each object with varied 'strenght'. I make a shadowmap in the following way:

Remember that white gives no effect where as black give a total shadow, so the image should contain 'full white' where there are no shadows, and grey to black where there are supposed to be shadows. Below is a picture I made using this technique, but also notice how the colored spots in the shadowmap controls the output of the colormap. For a full sized (1024*768) version, check out the Real 3D ftp-site. There should be a file in the images dir called ShadowMapExample.jpg. I try to keep things small (320*240) here. Even though this way of making shadows takes longer time to render than no shadows at all, this is a lot faster than if I was to render with a rectangular (area) light that cast shadows with a lightsamples value of about 4.

Helpdesk Real 3D Main area