I want to change the tint of my panoramic skybox for daylight cycle
24. Červen 2024 v 01:19
So i am trying to create a daylight cycle, where it switches between two panoramic images. The change happens slowly one over 2 minutes and the other over 3. During this the fog and light changes color but the image of the sky just slowly fades from one image to another. I would like for the skybox color to change too using the tint option. But how do i do this in my code so that the tint of the sky will match the color of the light and fog? This is in unity 3d btw using C# if that wasn't already obvious.
The code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TimeManager : MonoBehaviour
{
[SerializeField] private Texture2D skyboxNight;
[SerializeField] private Texture2D skyboxDay;
[SerializeField] private Gradient graddientNightToDay;
[SerializeField] private Gradient graddientDayToNight;
[SerializeField] private Light globalLight;
public int minutes;
public int Minutes
{ get { return minutes; } set { minutes = value; OnMinutesChange(value); } }
public int hours = 5;
public int Hours
{ get { return hours; } set { hours = value; OnHoursChange(value); } }
private int days;
public int Days
{ get { return days; } set { days = value; } }
private float tempSecond;
public void Update()
{
tempSecond += Time.deltaTime;
if (tempSecond >= 1)
{
Minutes += 1;
tempSecond = 0;
}
}
private void OnMinutesChange(int value)
{
globalLight.transform.Rotate(Vector3.up, (1f / (1440f / 4f)) * 360f, Space.World);
if (value >= 60)
{
Hours++;
minutes = 0;
}
if (Hours >= 24)
{
Hours = 0;
Days++;
}
}
private void OnHoursChange(int value)
{
if (Hours >= 24)
{
Hours = 0;
Days++;
}
if (value == 6)
{
StartCoroutine(LerpSkybox(skyboxNight, skyboxDay, 120f));
StartCoroutine(LerpLight(graddientNightToDay, 120f));
}
else if (value == 18)
{
StartCoroutine(LerpSkybox(skyboxDay, skyboxNight, 240f));
StartCoroutine(LerpLight(graddientDayToNight, 240f));
}
}
private IEnumerator LerpSkybox(Texture2D a, Texture2D b, float time)
{
RenderSettings.skybox.SetTexture("_Texture1", a);
RenderSettings.skybox.SetTexture("_Texture2", b);
RenderSettings.skybox.SetFloat("_Blend", 0);
for (float i = 0; i < time; i += Time.deltaTime)
{
RenderSettings.skybox.SetFloat("_Blend", i / time);
yield return null;
}
RenderSettings.skybox.SetTexture("_Texture1", b);
}
private IEnumerator LerpLight(Gradient lightGradient, float time)
{
for (float i = 0; i < time; i += Time.deltaTime)
{
globalLight.color = lightGradient.Evaluate(i / time);
RenderSettings.fogColor = globalLight.color;
yield return null;
}
}
} ```