Using lights and cookie textures to simulate a cloudy day

As it can be seen in many first-person shooters and survival horror games, lights and shadows can add a great deal of realism to a scene, helping immensely to create the right atmosphere for the game. In this recipe, we will create a cloudy outdoor environment using cookie textures. Cookie textures work as masks for lights. It functions by adjusting the intensity of the light projection to the cookie texture's alpha channel. This allows for a silhouette effect (just think of the bat-signal) or, as in this particular case, subtle variations that give a filtered quality to the lighting.

Getting ready

If you don't have access to an image editor, or prefer to skip the texture map elaboration in order to focus on the implementation, please use the image file called cloudCookie.tga, which is provided inside the 1362_06_01 folder.

How to do it...

To simulate a cloudy outdoor environment, follow these steps:

  1. In your image editor, create a new 512 x 512 pixel image.
  2. Using black as the foreground color and white as the background color, apply the Clouds filter (in Photoshop, this is done by navigating to the Filter | Render | Clouds menu).
    How to do it...

    Note

    Learning about the Alpha channel is useful, but you could get the same result without it. Skip steps 3 to 7, save your image as cloudCookie.png and, when changing texture type in step 9, leave Alpha from Greyscale checked.

  3. Select your entire image and copy it.
  4. Open the Channels window (in Photoshop, this can be done by navigating to the Window | Channels menu).
  5. There should be three channels: Red, Green, and Blue. Create a new channel. This will be the Alpha channel.
  6. In the Channels window, select the Alpha 1 channel and paste your image into it.
    How to do it...
  7. Save your image file as cloudCookie.PSD or TGA.
  8. Import your image file to Unity and select it in the Project view.
  9. From the Inspector view, change its Texture Type to Cookie and its Light Type to Directional. Then, click on Apply, as shown:
    How to do it...
  10. We will need a surface to actually see the lighting effect. You can either add a plane to your scene (via navigating to the GameObject | 3D Object | Plane menu), or create a Terrain (menu option GameObject | 3D Object | Terrain) and edit it, if you so you wish.
  11. Let's add a light to our scene. Since we want to simulate sunlight, the best option is to create a Directional Light. You can do this through the drop-down menu named Create | Light | Directional Light in the Hierarchy view.
  12. Using the Transform component of the Inspector view, reset the light's Position to X: 0, Y: 0, Z: 0 and its Rotation to X: 90; Y: 0; Z: 0.
  13. In the Cookie field, select the cloudCookie texture that you imported earlier. Change the Cookie Size field to 80, or a value that you feel is more appropriate for the scene's dimension. Please leave Shadow Type as No Shadows.
    How to do it...
  14. Now, we need a script to translate our light and, consequently, the Cookie projection. Using the Create drop-down menu in the Project view, create a new C# Script named MovingShadows.cs.
  15. Open your script and replace everything with the following code:
    using UnityEngine;
    using System.Collections;
    
    public class MovingShadows : MonoBehaviour{
      public float windSpeedX;
      public float windSpeedZ;
      private float lightCookieSize;
      private Vector3 initPos;
    
      void Start(){
        initPos = transform.position;
        lightCookieSize = GetComponent<Light>().cookieSize;
      }
    
      void Update(){
        Vector3 pos = transform.position;
        float xPos= Mathf.Abs (pos.x);
        float zPos= Mathf.Abs (pos.z);
        float xLimit = Mathf.Abs(initPos.x) + lightCookieSize;
        float zLimit = Mathf.Abs(initPos.z) + lightCookieSize;
    
        if (xPos >= xLimit)
          pos.x = initPos.x;
    
        if (zPos >= zLimit)
          pos.z = initPos.z;
    
        transform.position = pos;
        float windX = Time.deltaTime * windSpeedX;
        float windZ = Time.deltaTime * windSpeedZ;
        transform.Translate(windX, 0, windZ, Space.World);
      }
    }
  16. Save your script and apply it to the Directional Light.
  17. Select the Directional Light. In the Inspector view, change the parameters Wind Speed X and Wind Speed Z to 20 (you can change these values as you wish, as shown).
    How to do it...
  18. Play your scene. The shadows will be moving.

How it works...

With our script, we are telling the Directional Light to move across the X and Z axis, causing the Light Cookie texture to be displaced as well. Also, we reset the light object to its original position whenever it traveled a distance that was either equal to or greater than the Light Cookie Size. The light position must be reset to prevent it from traveling too far, causing problems in real-time render and lighting. The Light Cookie Size parameter is used to ensure a smooth transition.

The reason we are not enabling shadows is because the light angle for the X axis must be 90 degrees (or there will be a noticeable gap when the light resets to the original position). If you want dynamic shadows in your scene, please add a second Directional Light.

There's more...

In this recipe, we have applied a cookie texture to a Directional Light. But what if we were using the Spot or Point Lights?

Creating Spot Light cookies

Unity documentation has an excellent tutorial on how to make the Spot Light cookies. This is great to simulate shadows coming from projectors, windows, and so on. You can check it out at http://docs.unity3d.com/Manual/HOWTO-LightCookie.html.

Creating Point Light Cookies

If you want to use a cookie texture with a Point Light, you'll need to change the Light Type in the Texture Importer section of the Inspector.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.198.81