Files
2026-02-21 16:45:37 +08:00

85 lines
2.0 KiB
C#

using UltimateWater.Internal;
using UnityEngine;
using UnityEngine.Rendering;
namespace UltimateWater
{
[RequireComponent(typeof(Renderer))]
[AddComponentMenu("Ultimate Water/Dynamic/Water Interactive")]
public sealed class WaterInteractive : MonoBehaviour, IWavesInteractive, IDynamicWaterEffects
{
[Tooltip("How much velocity modifies wave amplitude")]
public float Multiplier = 1f;
[SerializeField]
[Tooltip("Disables simulation when the object is outside the camera view frustum, note that this will probably cull the waves from the objects just outside the view")]
private bool _UseViewCulling;
private Matrix4x4 _Previous;
private Renderer _Renderer;
private Material _Material;
private bool _IsVisible;
public void Render(Camera camera, CommandBuffer commandBuffer)
{
if (_Renderer.enabled)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
if (GeometryUtility.TestPlanesAABB(planes, _Renderer.bounds) && (!_UseViewCulling || _IsVisible))
{
_IsVisible = false;
commandBuffer.DrawRenderer(_Renderer, _Material);
}
}
}
public void Enable()
{
}
public void Disable()
{
}
private void OnWillRenderObject()
{
_IsVisible = true;
}
private void Awake()
{
_Material = ShaderUtility.Instance.CreateMaterial(ShaderList.Velocity);
if (!_Material.IsNullReference(this))
{
_Renderer = GetComponent<Renderer>();
if (!_Renderer.IsNullReference(this))
{
_Previous = base.transform.localToWorldMatrix;
}
}
}
private void OnEnable()
{
DynamicWater.AddRenderer(this);
}
private void OnDisable()
{
DynamicWater.RemoveRenderer(this);
}
private void FixedUpdate()
{
Matrix4x4 localToWorldMatrix = base.transform.localToWorldMatrix;
_Material.SetMatrix("_PreviousWorld", _Previous);
_Material.SetMatrix("_CurrentWorld", localToWorldMatrix);
_Material.SetFloat("_Data", Multiplier / Time.fixedDeltaTime);
_Previous = localToWorldMatrix;
}
}
}