[Memo-Unity] 21. AnimationCurve

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: ,


AnimationCurve

AnimationCurve

AnimationCurve








๋ฐค๋‚ฎ ๊ตฌํ˜„

AnimationCurve

DayNightCycle.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DayNightCycle : MonoBehaviour
{
    [Range(0.0f, 1.0f)]
    public float time;
    public float fullDayLength;
    public float startTime = 0.4f;
    private float timeRate;
    public Vector3 noon;

    [Header("Sun")]
    public Light sun;
    public Gradient sunColor;
    public AnimationCurve sunIntensity;

    [Header("Moon")]
    public Light moon;
    public Gradient moonColor;
    public AnimationCurve moonIntensity;

    [Header("Other Lighting")]
    public AnimationCurve lightingIntensityMultiplier;
    public AnimationCurve reflectionIntensityMultiplier;

    private void Start()
    {
        timeRate = 1.0f / fullDayLength;
        time = startTime;
    }

    private void Update()
    {
        time = (time + timeRate * Time.deltaTime) % 1.0f;

        UpdateLighting(sun, sunColor, sunIntensity);
        UpdateLighting(moon, moonColor, moonIntensity);

        RenderSettings.ambientIntensity = lightingIntensityMultiplier.Evaluate(time);
        RenderSettings.reflectionIntensity = reflectionIntensityMultiplier.Evaluate(time);

    }

    void UpdateLighting(Light lightSource, Gradient colorGradiant, AnimationCurve intensityCurve)
    {
        float intensity = intensityCurve.Evaluate(time);

        lightSource.transform.eulerAngles = (time - (lightSource == sun ? 0.25f : 0.75f)) * noon * 4.0f;
        lightSource.color = colorGradiant.Evaluate(time);
        lightSource.intensity = intensity;

        GameObject go = lightSource.gameObject;
        if (lightSource.intensity == 0 && go.activeInHierarchy)
            go.SetActive(false);
        else if (lightSource.intensity > 0 && !go.activeInHierarchy)
            go.SetActive(true);
    }
}



UpdateLighting
UpdateLighting(Light lightSource, Gradient colorGradiant, AnimationCurve intensityCurve)

float intensity = intensityCurve.Evaluate(time);

  • ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ปค๋ธŒ์—์„œ ์‹œ๊ฐ„๋ณ„(x์ถ•) y์˜ ๊ฐ’์„ intensity ์— ์ €์žฅ image
  • lightSource.transform.eulerAngles = (time - (lightSource == sun ? 0.25f : 0.75f)) * noon * 4.0f;
  • ํ–‡๋น›์˜ ๊ฐ๋„๋ฅผ ์‹œ๊ฐ„๋ณ„๋กœ ์กฐ์ •
  • lightSource.color = colorGradiant.Evaluate(time);
  • ์‹œ๊ฐ„์— ๋”ฐ๋ฅธ ์ƒ‰์ƒ๊ฐ’
  • if, else if ๋ฌธ
  • sun ์˜ intensity ๊ฐ€ 0 ์ผ๋•Œ time ์ด 0 ~ 0.2, 0.8 ~ 1.0 ์ผ๋•Œ sun setActive(False)
  • image
  • Moon ์˜ intensity ๊ฐ€ 0 ์ผ๋•Œ time ์ด 0.3 ~ 0.7 ์ผ๋•Œ setActive(False)
  • image




[Unity] AnimationCurve TOP


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ