As it can be seen in many first-person shooters and survival horror games, lights and shadows can add a great deal of complexity to a scene, helping immensely to create the right atmosphere for the game. In this recipe, we will create a cloudy outdoor environment, lighting it using cookie textures.
If you don't have access to a image editor or prefer to skip the texture map elaboration in order to focus on the implementation, please use the cloudCookie.tga
image file provided in the 0423_03_05
folder.
To simulate a cloudy outdoor environment, follow these steps:
cloudCookie.PSD
or cloudCookie.TGA
.0
, 0
, 0
and its Rotation to 90
, 0
, 0
.MovingShadows
.using UnityEngine; using System.Collections; public class MovingShadows : MonoBehaviour { public float windSpeedX; public float windSpeedZ; public float lightCookieSize; // make it equal to the light's Cookie Size parameter in the inspector private Vector3 initPos; void Start() { initPos = transform.position; } void Update() { if (Mathf.Abs(transform.position.x) >= Mathf.Abs(initPos.x) + lightCookieSize) { Vector3 pos = transform.position; pos.x = initPos.x; transform.position = pos; } else { transform.Translate(Time.deltaTime * windSpeedX, 0, 0, Space.World); } if (Mathf.Abs(transform.position.z) >= Mathf.Abs(initPos.z) + lightCookieSize) { Vector3 pos = transform.position; pos.z = initPos.z; transform.position = pos; } else { transform.Translate(0, 0, Time.deltaTime * windSpeedZ, Space.World); } } }
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 travels a distance equal to or greater than the Light Cookie Size parameter. The light position must be reset to prevent it from travelling 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.
In this recipe we have applied a cookie texture to a directional light. But what if we were using Spot Light or Point Light cookies?
Unity documentation has an excellent tutorial on how to make Spot Light cookies. This is great to simulate shadows coming from projectors, windows, and so on. You can check it out at http://unity3d.com/support/documentation/Manual/HOWTO-LightCookie.html.
18.224.54.255