93 lines
2.4 KiB
C#
93 lines
2.4 KiB
C#
using UnityEngine;
|
|
|
|
namespace AQUAS
|
|
{
|
|
public class AQUAS_Caustics : MonoBehaviour
|
|
{
|
|
public float fps;
|
|
|
|
public Texture2D[] frames;
|
|
|
|
public float maxCausticDepth;
|
|
|
|
private int frameIndex;
|
|
|
|
private Projector projector;
|
|
|
|
private float currentScale;
|
|
|
|
private float aspectRatio;
|
|
|
|
[HideInInspector]
|
|
public float waterLevel;
|
|
|
|
[HideInInspector]
|
|
public bool overrideWaterLevel;
|
|
|
|
private void Start()
|
|
{
|
|
projector = GetComponent<Projector>();
|
|
NextFrame();
|
|
InvokeRepeating("NextFrame", 1f / fps, 1f / fps);
|
|
projector.material.SetFloat("_WaterLevel", base.transform.parent.transform.position.y);
|
|
projector.material.SetFloat("_DepthFade", base.transform.parent.transform.position.y - maxCausticDepth);
|
|
currentScale = base.transform.parent.localScale.z;
|
|
aspectRatio = base.transform.parent.localScale.x / base.transform.parent.localScale.z;
|
|
waterLevel = base.transform.parent.position.y;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (overrideWaterLevel && base.transform.parent == null)
|
|
{
|
|
projector.material.SetFloat("_DepthFade", waterLevel - maxCausticDepth);
|
|
}
|
|
else
|
|
{
|
|
if (base.transform.parent == null)
|
|
{
|
|
return;
|
|
}
|
|
projector.material.SetFloat("_DepthFade", base.transform.parent.transform.position.y - maxCausticDepth);
|
|
}
|
|
if (overrideWaterLevel)
|
|
{
|
|
currentScale = 1000f;
|
|
GetComponent<Projector>().orthographicSize = currentScale;
|
|
}
|
|
else
|
|
{
|
|
if (base.transform.parent == null)
|
|
{
|
|
return;
|
|
}
|
|
currentScale = Mathf.Max(base.transform.parent.localScale.x, base.transform.parent.localScale.z) * 5f;
|
|
if (currentScale != base.transform.parent.localScale.z)
|
|
{
|
|
GetComponent<Projector>().orthographicSize = currentScale;
|
|
}
|
|
if (aspectRatio != base.transform.parent.localScale.x / base.transform.parent.localScale.z)
|
|
{
|
|
aspectRatio = base.transform.parent.localScale.x / base.transform.parent.localScale.z;
|
|
GetComponent<Projector>().aspectRatio = aspectRatio;
|
|
}
|
|
}
|
|
if (overrideWaterLevel)
|
|
{
|
|
projector.material.SetFloat("_WaterLevel", waterLevel);
|
|
}
|
|
else if (waterLevel != base.transform.parent.position.y)
|
|
{
|
|
waterLevel = base.transform.parent.position.y;
|
|
projector.material.SetFloat("_WaterLevel", waterLevel);
|
|
}
|
|
}
|
|
|
|
private void NextFrame()
|
|
{
|
|
projector.material.SetTexture("_Texture", frames[frameIndex]);
|
|
frameIndex = (frameIndex + 1) % frames.Length;
|
|
}
|
|
}
|
|
}
|