map/Assets/DayNightCycle_BulitIn.cs
2026-02-02 20:41:46 +09:00

110 lines
3.3 KiB
C#
Raw Permalink Blame History

using UnityEngine;
public class DayNightCycle_BuiltIn : MonoBehaviour
{
[Header("Time")]
[Range(0f, 1f)] public float time01 = 0.25f;
public float dayLengthSeconds = 600f;
[Header("Sun (Directional Light)")]
public Light sun;
public float sunYaw = 170f;
public AnimationCurve sunIntensity = AnimationCurve.EaseInOut(0, 0, 1, 1.2f);
public Gradient sunColor;
[Header("Ambient (Environment Light)")]
public AnimationCurve ambientIntensity = AnimationCurve.EaseInOut(0, 0.2f, 1, 1.0f);
public Gradient ambientColor;
[Header("Fog")]
public bool useFog = true;
public AnimationCurve fogDensity = AnimationCurve.EaseInOut(0, 0.02f, 1, 0.006f);
public Gradient fogColor;
void Reset()
{
sunColor = new Gradient();
sunColor.SetKeys(
new[] {
new GradientColorKey(new Color(0.25f, 0.35f, 0.55f), 0f),
new GradientColorKey(new Color(1.00f, 0.85f, 0.65f), 0.25f),
new GradientColorKey(Color.white, 0.5f),
new GradientColorKey(new Color(1.00f, 0.55f, 0.35f), 0.75f),
},
new[] {
new GradientAlphaKey(1f, 0f),
new GradientAlphaKey(1f, 1f)
}
);
ambientColor = new Gradient();
ambientColor.SetKeys(
new[] {
new GradientColorKey(new Color(0.08f, 0.10f, 0.18f), 0f),
new GradientColorKey(new Color(0.55f, 0.55f, 0.60f), 0.5f),
new GradientColorKey(new Color(0.25f, 0.20f, 0.25f), 0.75f)
},
new[] {
new GradientAlphaKey(1f, 0f),
new GradientAlphaKey(1f, 1f)
}
);
fogColor = new Gradient();
fogColor.SetKeys(
new[] {
new GradientColorKey(new Color(0.05f, 0.07f, 0.12f), 0f),
new GradientColorKey(new Color(0.65f, 0.75f, 0.85f), 0.5f),
new GradientColorKey(new Color(0.40f, 0.25f, 0.20f), 0.75f)
},
new[] {
new GradientAlphaKey(1f, 0f),
new GradientAlphaKey(1f, 1f)
}
);
}
void Update()
{
time01 += Time.deltaTime / Mathf.Max(1f, dayLengthSeconds);
if (time01 >= 1f) time01 -= 1f;
Apply(time01);
}
void Apply(float t)
{
// 1) <20>¾<EFBFBD> ȸ<><C8B8>
if (sun)
{
float sunAngle = t * 360f - 90f;
sun.transform.rotation = Quaternion.Euler(sunAngle, sunYaw, 0f);
// 2) <20>¾<EFBFBD> <20><><EFBFBD><EFBFBD>/<2F><>
sun.intensity = sunIntensity.Evaluate(t);
if (sunColor != null) sun.color = sunColor.Evaluate(t);
}
// 3) Ambient(ȯ<>汤) - Ȯ<><C8AE><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;
Color c = ambientColor.Evaluate(t);
float k = ambientIntensity.Evaluate(t);
RenderSettings.ambientLight = c * k;
// 4) Fog
if (useFog)
{
RenderSettings.fog = true;
RenderSettings.fogColor = fogColor.Evaluate(t);
RenderSettings.fogDensity = fogDensity.Evaluate(t);
}
else
{
RenderSettings.fog = false;
}
// 5) ȯ<><C8AF> <20><><EFBFBD><EFBFBD>
DynamicGI.UpdateEnvironment();
}
}