Files
2026-03-04 10:03:45 +08:00

266 lines
6.0 KiB
C#

using System.Collections;
using UnityEngine;
namespace Artngame.SKYMASTER
{
[ExecuteInEditMode]
public class WeatherScript : MonoBehaviour
{
public bool useRandSeed;
public int randomSeed = 1;
public CloudScript clouds;
public CloudScript cloudsREFL;
public int size = 512;
public bool useCustomTexture;
public Texture2D customWeatherTexture;
public GameObject weatherVisualiser;
public float blendTime = 30f;
private MeshRenderer weatherVisualiserRenderer;
public RenderTexture rt;
private bool isChangingWeather;
private RenderTexture prevWeatherTexture;
private RenderTexture nextWeatherTexture;
private bool _useUserWeatherTexture;
private Material _BlendMaterial;
private Material _SystemMaterial;
private bool setToReset;
public bool useCustomWeatherTexture
{
get
{
return _useUserWeatherTexture;
}
set
{
if (value != _useUserWeatherTexture)
{
_useUserWeatherTexture = value;
if (_useUserWeatherTexture && customWeatherTexture != null)
{
Graphics.Blit(rt, prevWeatherTexture);
Graphics.Blit(customWeatherTexture, nextWeatherTexture);
StartWeatherTextureChange();
}
else
{
GenerateAndChangeWeatherTexture();
}
}
}
}
public Material BlendMaterial
{
get
{
if (!_BlendMaterial)
{
_BlendMaterial = new Material(Shader.Find("Hidden/WeatherBlender"));
_BlendMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return _BlendMaterial;
}
}
public Material SystemMaterial
{
get
{
if (!_SystemMaterial)
{
_SystemMaterial = new Material(Shader.Find("Hidden/WeatherSystem"));
_SystemMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return _SystemMaterial;
}
}
public void Awake()
{
if (weatherVisualiser != null)
{
weatherVisualiserRenderer = weatherVisualiser.GetComponent<MeshRenderer>();
}
}
private void setWeatherTexture()
{
clouds.CloudMaterial.SetTexture("_WeatherTexture", rt);
if (cloudsREFL != null)
{
cloudsREFL.CloudMaterial.SetTexture("_WeatherTexture", rt);
}
if (weatherVisualiser != null)
{
weatherVisualiserRenderer.sharedMaterial.SetTexture("_MainTex", rt);
}
}
public void StartWeatherTextureChange()
{
if (isChangingWeather)
{
StopCoroutine("LerpWeatherTexture");
}
StartCoroutine("LerpWeatherTexture");
}
public void GenerateWeatherTexture()
{
if (useRandSeed)
{
Random.InitState(randomSeed);
}
SystemMaterial.SetVector("_Randomness", new Vector3(Random.Range(-1000, 1000), Random.Range(-1000, 1000), Random.value * 1.5f - 0.2f));
Graphics.Blit(rt, prevWeatherTexture);
Graphics.Blit(null, rt, SystemMaterial, 0);
Graphics.Blit(rt, nextWeatherTexture);
}
private void LateUpdate()
{
if (setToReset && !useCustomTexture)
{
GenerateAndChangeWeatherTexture();
setToReset = false;
}
if (setToReset && useCustomTexture)
{
GenerateAndChangeWeatherTexture();
GenerateWeatherTexture();
_useUserWeatherTexture = true;
Graphics.Blit(customWeatherTexture, prevWeatherTexture);
Graphics.Blit(prevWeatherTexture, rt);
setToReset = false;
}
}
private void OnEnable()
{
if (!Application.isPlaying && !useCustomTexture)
{
setToReset = true;
}
if (!Application.isPlaying && useCustomTexture)
{
setToReset = true;
}
}
public void GenerateAndChangeWeatherTexture()
{
GenerateWeatherTexture();
if (!useCustomWeatherTexture)
{
if (!Application.isPlaying)
{
Graphics.Blit(nextWeatherTexture, prevWeatherTexture);
Graphics.Blit(nextWeatherTexture, rt);
setWeatherTexture();
}
else
{
StartWeatherTextureChange();
}
}
if (useCustomWeatherTexture)
{
_useUserWeatherTexture = true;
Graphics.Blit(customWeatherTexture, prevWeatherTexture);
Graphics.Blit(prevWeatherTexture, rt);
setWeatherTexture();
}
}
private IEnumerator LerpWeatherTexture()
{
isChangingWeather = true;
for (float t = 0f; t <= blendTime; t += Time.deltaTime * (((double)clouds.globalMultiplier == 0.0) ? blendTime : Mathf.Abs(clouds.globalMultiplier)))
{
BlendMaterial.SetTexture("_PrevWeather", prevWeatherTexture);
BlendMaterial.SetTexture("_NextWeather", nextWeatherTexture);
BlendMaterial.SetFloat("_Alpha", t / blendTime);
Graphics.Blit(null, rt, BlendMaterial, 0);
setWeatherTexture();
yield return null;
}
Graphics.Blit(nextWeatherTexture, rt);
setWeatherTexture();
isChangingWeather = false;
}
private void Start()
{
if ((bool)_BlendMaterial)
{
Object.DestroyImmediate(_BlendMaterial);
}
if ((bool)_SystemMaterial)
{
Object.DestroyImmediate(_SystemMaterial);
}
rt = new RenderTexture(size, size, 0, RenderTextureFormat.ARGB32);
rt.wrapMode = TextureWrapMode.Mirror;
rt.Create();
prevWeatherTexture = new RenderTexture(size, size, 0, RenderTextureFormat.ARGB32);
prevWeatherTexture.wrapMode = TextureWrapMode.Mirror;
prevWeatherTexture.Create();
nextWeatherTexture = new RenderTexture(size, size, 0, RenderTextureFormat.ARGB32);
nextWeatherTexture.wrapMode = TextureWrapMode.Mirror;
nextWeatherTexture.Create();
clouds.CloudMaterial.SetTexture("_WeatherTexture", rt);
if (cloudsREFL != null)
{
cloudsREFL.CloudMaterial.SetTexture("_WeatherTexture", rt);
}
useCustomWeatherTexture = useCustomTexture;
if (useCustomWeatherTexture && customWeatherTexture != null)
{
if (isChangingWeather)
{
StopCoroutine("LerpWeatherTexture");
}
Graphics.Blit(rt, prevWeatherTexture);
Graphics.Blit(customWeatherTexture, nextWeatherTexture);
setWeatherTexture();
}
else
{
GenerateWeatherTexture();
setWeatherTexture();
}
if (Application.isPlaying)
{
GenerateAndChangeWeatherTexture();
}
}
private void Update()
{
useCustomWeatherTexture = useCustomTexture;
if (!Application.isPlaying)
{
setWeatherTexture();
}
}
}
}