Realtime Global Illumination Day/Night Cycle

確認済のバージョン: 5

-

難易度: 中級

Using Unity 5 and the standard shader with physically based lighting, this tutorial shows how to create demo with a simple day/night cycle.

Realtime Global Illumination Day/Night Cycle

中級 Graphics

This lesson highlights the use of realtime global illumination, reflection probes, emissive materials and one single directional light. Nothing is baked. There is no ambient lighting. The reflections on the floor are from a single reflection probe. The picture that lights up is a an emissive material. Every non-directional light is a physical bounce.

The Demo

The final demo can be found here: https://www.youtube.com/watch?v=Ukkiqe_75d0

AutoIntensity

Code snippet

using UnityEngine;
using System.Collections;

public class AutoIntensity : MonoBehaviour {

    public Gradient nightDayColor;

    public float maxIntensity = 3f;
    public float minIntensity = 0f;
    public float minPoint = -0.2f;

    public float maxAmbient = 1f;
    public float minAmbient = 0f;
    public float minAmbientPoint = -0.2f;


    public Gradient nightDayFogColor;
    public AnimationCurve fogDensityCurve;
    public float fogScale = 1f;

    public float dayAtmosphereThickness = 0.4f;
    public float nightAtmosphereThickness = 0.87f;

    public Vector3 dayRotateSpeed;
    public Vector3 nightRotateSpeed;

    float skySpeed = 1;


    Light mainLight;
    Skybox sky;
    Material skyMat;

    void Start () 
    {
    
        mainLight = GetComponent<Light>();
        skyMat = RenderSettings.skybox;

    }

    void Update () 
    {
    
        float tRange = 1 - minPoint;
        float dot = Mathf.Clamp01 ((Vector3.Dot (mainLight.transform.forward, Vector3.down) - minPoint) / tRange);
        float i = ((maxIntensity - minIntensity) * dot) + minIntensity;

        mainLight.intensity = i;

        tRange = 1 - minAmbientPoint;
        dot = Mathf.Clamp01 ((Vector3.Dot (mainLight.transform.forward, Vector3.down) - minAmbientPoint) / tRange);
        i = ((maxAmbient - minAmbient) * dot) + minAmbient;
        RenderSettings.ambientIntensity = i;

        mainLight.color = nightDayColor.Evaluate(dot);
        RenderSettings.ambientLight = mainLight.color;

        RenderSettings.fogColor = nightDayFogColor.Evaluate(dot);
        RenderSettings.fogDensity = fogDensityCurve.Evaluate(dot) * fogScale;

        i = ((dayAtmosphereThickness - nightAtmosphereThickness) * dot) + nightAtmosphereThickness;
        skyMat.SetFloat ("_AtmosphereThickness", i);

        if (dot > 0) 
            transform.Rotate (dayRotateSpeed * Time.deltaTime * skySpeed);
        else
            transform.Rotate (nightRotateSpeed * Time.deltaTime * skySpeed);

        if (Input.GetKeyDown (KeyCode.Q)) skySpeed *= 0.5f;
        if (Input.GetKeyDown (KeyCode.E)) skySpeed *= 2f;


    }
}

SetSunLight

Code snippet

using UnityEngine;
using System.Collections;

public class SetSunLight : MonoBehaviour {

    public Renderer lightwall;

    Material sky;

    public Renderer water;

    public Transform stars;
    public Transform worldProbe;

    // Use this for initialization
    void Start () 
    {

        sky = RenderSettings.skybox;

    }

    bool lighton = false;

    // Update is called once per frame
    void Update () 
    {

        stars.transform.rotation = transform.rotation;

        if (Input.GetKeyDown(KeyCode.T))
        {

            lighton = !lighton;

        }


        if (lighton)
        {
            Color final = Color.white * Mathf.LinearToGammaSpace(5);
            lightwall.material.SetColor("_EmissionColor", final);
            DynamicGI.SetEmissive(lightwall, final);
        }
        else
        {
            Color final = Color.white * Mathf.LinearToGammaSpace(0);
            lightwall.material.SetColor("_EmissionColor", final);
            DynamicGI.SetEmissive(lightwall, final);
        }
    
        Vector3 tvec = Camera.main.transform.position;
        worldProbe.transform.position = tvec;

        water.material.mainTextureOffset = new Vector2(Time.time / 100, 0);
        water.material.SetTextureOffset("_DetailAlbedoMap", new Vector2(0, Time.time / 80));

    }
}

Glen Rhodes

コミュニティー作者

Glen Rhodes has been developing games professionally since 1994, when he started out developing games for the original Sony Playstation at Gray Matter Inc. In the years since, Glen has developed games for many clients including Disney, MTV, Nickelodeon, Honda and more. Glen is also a published author, having authored or co-authored 10 books, including many titles such as "Macromedia Flash Professional 8 Game Development", 2006. Glen has spoken at several conferences on game development. In recent years, Glen has moved from Flash into Unity 3D. Glen is the Director of Creative Technology at Picture This Today, where he is in charge of the development of 3D real estate visualization software in Unity. Glen is also an accomplished composer, having written music all his life. From 2006 until 2011 Glen wrote music for the Oprah Winfrey Show.

関連するチュートリアル