How to render object with shadows on a transparent background

Sometimes it is necessary to create a render of an object with shadows on a transparent background. This occurs when you need to realistically embed a 3D into the photo or when creating an image to use it as a sprite in a computer game.

There is no special shader in Blender, like “Shadow catcher” in 3D Studio Max, which remains transparent display shadows. However, it is possible to achieve the desired result.

Let’s consider how we can get a shadow render on a transparent background using Blender:

To start let’s create a simple scene with the object, which needs to be rendered together with shadows, the plane on which objects shadows fall and the small and sufficiently remote light source (meshlight) to make clear, precise shadows.

There is a model of the wind turbine on the plane serving as the basis for its shadow. All the scene lights by another plane with the Emission shader. It locates right and above the model. Instead of a wind generator, any other mesh can be used, for example, “Suzanne”, or the default cube.

For work convenience, all objects need to distribute into different layers of the scene. On the first layer of the scene let’s place the plane for the shadow, transfer the wind generator model to the second scene layer, and place the meshlight on the third scene layer.

  1. Select the wind generator model and the meshlight
    1. m – 2 – Enter
  2. Switch to the second layer of the scene
    1. 2
  3. Select the meshlight
    1. m – 3 – Enter
  4. Select all three layers of the scene to access all the objects.

In order to get the desired result – a wind generator with a shadow on a transparent background, we need to divide the whole render to 3 render layers:

  1. Render layer with a background without the wind generator and without its shadow;
  2. Render layer with a background and a shadow falling on it but without the wind generator;
  3. Render layer with the wind generator without any background and shadow

Let’s create the necessary render layers:

  1. In the Properties window, the Render Layers tab, the Scene panel:
    1. Rename the default render layer to “Background”
      1. In the Layer subpanel
        1. Scene
          1. Check all three layers of the scene
        2. Layer
          1. Check only the first scene layer – only the objects located on the first layer of the scene will render on the current render layer – the background plane.
        3. Exclude
          1. Check the second scene layer – selected scene layer with the wind generator will be excluded from the rendering process, so its shadow will not render on the background plane.

            “Background” Render layer
      2. Click on “+” to create a second render layer and name it “Shadows”
        1. In the Layer subpanel
          1. Scene
            1. Check all three layers of the scene
          2. Layer
            1. Check only the first layer
          3. The layer of the scene with the wind generator is not excluded now, so we have a render layer with the background plane and shadow falling on it.

            “Shadows” Render layer
      3. Click on “+” to create a third render layer and name it “Objects”
        1. In the Layer subpanel
          1. Scene
            1. Check all three layers of the scene
          2. Layer
            1. Check only the second scene layer – only the wind generator will be rendered
        2. In the Properties window, the Render tab, the Film panel
          1. Check the checkbox “Transparent” to render the wind generator on a transparent background

            “Objects” Render layer

After creating all the necessary render layers, it remains to correctly connect them in compositing to get the desired result.

To see the immediate result let’s launch the render by pressing the f12 key.

  1. In the main menu switch to the Compositing mode
    1. Check the following checkboxes:
      1. Use Nodes
      2. Backdrop
    2. Add the Composite node (if it was not added by default)
      1. shift+a – Output – Composite
    3. Add the Viewer node. Connecting the outputs of any node with the Image input of the Viewer node, we can display the intermediate results against the background of the compositing window.
      1. shift+a – Output – Viewer
    4. Add three Render Layers nodes – one for each created render layer
      1. shift+a – Input – Render Layers
        1. Specify “Background” render layer in this node
      2. shift+a – Input – Render Layers
        1. Specify “Shadows” render layer in this node
      3. shift+a – Input – Render Layers
        1. Specify “Objects” render layer in this node
    5. Add the Color Mix node
      1. shift+a – Color – Mix
        1. Switch it to Divide mode
        2. Connect the Image output of the Render Layers node (Background) with the second input of the Image of the Divide node
        3. Connect the Image output of the Render Layers node (Shadows) with the first input of the Image of the Divide node
    6. Add the Invert node
      1. shift+a – Color – Invert
      2. Connect the Image output of the Divide node with the Color input of the Invert node
    7. Add the Set Alpha node
      1. shift+a – Converter – Set Alpha
      2. Connect the Color output of the Invert node with the Alpha input of the Set Alpha node
    8. The wind generator shadow on a transparent background is obtained. It remains to combine it with the wind generator itself.
    9. Add the Alpha Over node
      1. shift+a – Color – Alpha Over
      2. Connect the Image output of the Set Alpha node with the first Image input of the Alpha Over node
      3. Connect the Image output of the Render Layers node (Objects) with the second Image input of the Alpha Over node
      4. Connect the Alpha output of the Render Layers node (Objects) with the Factor Input of the Alpha Over node
    10. The node tree is finished, it remains to save the result.
      1. Connect the Image output of the Alpha Over node with the Image input of the Composite node
      2. In the Properties window, the Render tab, the Output panel
        1. Set the file format which supports transparency, for example, PNG.
          1. Enable transparency support – check RGBA
      3. Switch the main window to UV/Image Editor
        1. Select the Render Result
      4. Save finished image
        1. f3

Bonus: using the Shadow path

If the is no meshlights in the scene and only standard Lamp-type lighting sources are used, we can slightly simplify the process by reducing the number of render layers and simplifying the compositing node tree.

  1. To explore this, let’s remove the meshlight plane from the scene and put in its place the standard light source (Sun)
    1. shift+a – Lamp – Sun
    2. Adjust its brightness, dimensions, and direction so that the wind generator falls a clearly visible shadow
  2. In the Properties window, the Render Layers tab, the Scene panel
    1. Delete the first two render layers (Background and Shadows)
    2. The Objects render layer does not need to be changed
    3. Click on “+” to create a new render layer and name it “All”
      1. In the Layer subpanel
        1. Scene
          1. Check all three layers of the scene
        2. Layer
          1. Check all layers of the scene
      2. In the Passes subpanel
        1. On the Shadow checkbox
  3. In the main menu switch to the Compositing mode
    1. Correct the node tree as follows:
      1. Delete the Render Layers node (Shadows)
      2. The Render Layers node (Background) switch to the render layer All
      3. Delete the Divide node
      4. Connect the Image output of the Render Layers node (All) with the Color input of the Invert node

The desired result is obtained. There are only two render layers and the node tree is easier, so the overall render will be faster. However, the Shadow pass is calculated only for standard light sources and is not considered for the meshlights, therefore, if any meshlights are used in the scene, this method will not give proper result.