Shadowmaps

From PixieWiki

Jump to: navigation, search


[edit] Rendering ShadowMaps


Shadowmaps are rendered in two passes. The first rendering will render the Z(depth) channel and convert the z data to a shadow texture that will get used by the light in the final render pass.

Soft ShadowMap Spotlight Shader:

/*	soft shadow spot light shader
 *
 *      This is a slightly modified version of Pixie's shadowspot shader
 */

light softshadowspot ( float intensity = 1;
                 color lightcolor = 1;
                 point from = point "shader" (0,0,0);
                 point to = point "shader" (0,0,1);
                 string shadowname = "";
                 float blur = 0.01;
                 float samples = 32;
                 float bias = 0.1;
                 float coneangle = radians(30);
                 float conedeltaangle = radians(5);
                 float beamdistribution = 2; ) {
        uniform vector axis = normalize(to-from);

        illuminate (from, axis, coneangle) {
          float cosangle = (L . axis) / length(L);
          float atten = pow (cosangle, beamdistribution) / (L . L);
          atten *= smoothstep (cos(coneangle), cos(coneangle-conedeltaangle),cosangle);

        if (shadowname != "")
          atten *= (1-shadow(shadowname, Ps, "blur", blur, "samples", samples, "bias", bias));

        Cl = atten * intensity * lightcolor;
        }
}


ShadowMap Pass:

FrameBegin 1
      Format 512 512 1
      PixelSamples 4 4
      ShadingInterpolation "smooth"
      # If you would like to see what is being rendered,
      # uncomment the below line.
      Display "shadow" "framebuffer" "rgba"
      # Render the z 'depth' file
      Display "+shadow.depth" "zfile" "z"
      Projection "perspective" "fov" 44

      # This light is only here to show you what your rendering in the framebuffer;
      # it has no effect on the depth channel.
      LightSource "distantlight" 1 "intensity" 1.5 "from" [0 0 0] "to" [0 0 1]
      
      # You need to position the camera where the light be point
      # so you can render from the lights pov.
      # You have to do this for each light that you want a shadowmaps for.
     Translate 0 0 5
      Rotate -45 1 0 0 
      Rotate 180 0 1 0

      WorldBegin
              # You don't need any light data because you won't be using any lights
              # to render.  
 
              Surface "plastic"

              # Ground plane
              AttributeBegin
                      Color 1 1 1
                      Polygon "P" [ -5 0 5 5 0 5 5 0 -5 -5 0 -5 ]
              AttributeEnd

              # Sphere
              AttributeBegin
                      Color 1 1 1
                      Translate -0.7 0.5 0
                      Sphere 0.5 -0.5 0.5 360
              AttributeEnd

              # Box
              AttributeBegin
                      Color 1 1 1
                      Translate 0.3 0.01 0
                      Rotate -30 0 1 0
                      Polygon "P" [ 0 0 0 0 0 1 0 1 1 0 1 0 ] # left side
                      Polygon "P" [ 1 1 0 1 1 1 1 0 1 1 0 0 ] # right side
                      Polygon "P" [ 0 1 0 1 1 0 1 0 0 0 0 0 ] # front side
                      Polygon "P" [ 0 0 1 1 0 1 1 1 1 0 1 1 ] # back side
                      Polygon "P" [ 0 1 1 1 1 1 1 1 0 0 1 0 ] # top
              AttributeEnd

      WorldEnd

# Now you'll tell Pixie to make a shadowmap texture file from the depth rendering
MakeShadow "shadow.depth" "shadow.tx"

FrameEnd


Render Pass:

FrameBegin 1
      Format 400 300 1
      ShadingRate 1
      Display "shadowmap" "framebuffer" "rgba"
      Display "+shadowmap.png" "file" "rgba"
      Projection "perspective" "fov" 22
      Translate 0 -0.5 8
      Rotate -40 1 0 0
      Rotate -20 0 1 0

      WorldBegin
              # This is the light that will use the shadow map.
              # You want to position it in the same location of the camera for
              # the shadowmap render... even though I didn't (it's close enough).
              LightSource "softshadowspot" 1 "intensity" 400.0 "lightcolor" [1.0 1.0 1.0]"coneangle" 40
              "from" [0 15 15] "to" [0 0 0] "blur" 0.02 "samples" 64 "shadowname" "shadow.tx"

              Illuminate 1 1

              Surface "plastic"

              # Ground plane
              AttributeBegin
                      Color 1 1 1
                      Polygon "P" [ -5 0 5 5 0 5 5 0 -5 -5 0 -5 ]
              AttributeEnd

              # Sphere
              AttributeBegin
                      Color 1 1 1
                      Translate -0.7 0.5 0
                      Sphere 0.5 -0.5 0.5 360
              AttributeEnd

              # Box
              AttributeBegin
                      Color 1 1 1
                      Translate 0.3 0.01 0
                      Rotate -30 0 1 0
                      Polygon "P" [ 0 0 0 0 0 1 0 1 1 0 1 0 ] # left side
                      Polygon "P" [ 1 1 0 1 1 1 1 0 1 1 0 0 ] # right side
                      Polygon "P" [ 0 1 0 1 1 0 1 0 0 0 0 0 ] # front side
                      Polygon "P" [ 0 0 1 1 0 1 1 1 1 0 1 1 ] # back side
                      Polygon "P" [ 0 1 1 1 1 1 1 1 0 0 1 0 ] # top
              AttributeEnd

      WorldEnd
FrameEnd