using UnityEngine.Experimental.Rendering;
using Unity.Collections;
using Unity.Mathematics;
using static Unity.Mathematics.math;
namespace UnityEngine.Rendering.HighDefinition
{
public partial class WaterSurface
{
#region Water Deformation
///
/// Specifies if the water surfaces supports local deformations.
///
public bool deformation = false;
///
/// Specifies the resolution of the deformation texture used to represent the deformation area.
///
[Tooltip("Specifies the resolution of the deformation texture used to represent the deformation area.")]
public WaterDecalRegionResolution deformationRes = WaterDecalRegionResolution.Resolution512;
#endregion
///
/// Function that returns the deformation buffer for the water surface. If the feature is disabled or the resource is not available the function returns null.
///
///
/// A single channgel texture that holds the surface deformation of the water surface.
public Texture GetDeformationBuffer()
{
return deformation ? deformationBuffer : null;
}
// GPU deformation data
internal RTHandle deformationBuffer = null;
internal RTHandle deformationSGBuffer = null;
// CPU Deformation data
internal AsyncTextureSynchronizer deformationBufferSychro = new AsyncTextureSynchronizer(GraphicsFormat.R16_SFloat);
void FillWaterDeformationData(ref WaterSimSearchData wsd)
{
var system = HDRenderPipeline.currentPipeline.waterSystem;
// Water Mask
if (system.m_ActiveDeformation && deformation && deformationBufferSychro.TryGetBuffer(out var deformationBuffer) && deformationBuffer.Length > 0)
{
wsd.activeDeformation = true;
wsd.deformationBuffer = deformationBuffer;
wsd.deformationResolution = deformationBufferSychro.CurrentResolution();
}
else
{
wsd.activeDeformation = false;
wsd.deformationBuffer = system.m_DefaultDeformationBuffer;
}
Matrix4x4 worldToWater = simulation.rendering.worldToWaterMatrix;
wsd.waterForwardXZ = float2(worldToWater.m00, worldToWater.m02);
}
internal void CheckDeformationResources()
{
if (deformation)
{
int resolution = (int)deformationRes;
if (deformationBuffer != null && deformationBuffer.rt.width != resolution)
ReleaseDeformationResources();
if (deformationBuffer == null)
{
deformationBuffer = RTHandles.Alloc(resolution, resolution, 1, dimension: TextureDimension.Tex2D, colorFormat: GraphicsFormat.R16_SFloat, enableRandomWrite: true, wrapMode: TextureWrapMode.Clamp, name: "Water Deformation");
deformationSGBuffer = RTHandles.Alloc(resolution, resolution, 1, dimension: TextureDimension.Tex2D, colorFormat: GraphicsFormat.R16G16_SFloat, enableRandomWrite: true, wrapMode: TextureWrapMode.Clamp, name: "Water Deformation SG");
}
}
else if (deformationBuffer != null)
ReleaseDeformationResources();
}
internal void ReleaseDeformationResources()
{
RTHandles.Release(deformationBuffer);
RTHandles.Release(deformationSGBuffer);
deformationBuffer = null;
deformationSGBuffer = null;
// CPU Simulation data
deformationBufferSychro.ReleaseATSResources();
}
internal Texture GetDeformationBuffer(WaterSystem system, bool frameSetting, Texture defaultValue = null)
{
return frameSetting && system.m_ActiveDeformation && deformation ? deformationBuffer : defaultValue;
}
internal Texture GetDeformationNormalBuffer(WaterSystem system, bool frameSetting, Texture defaultValue = null)
{
return frameSetting && system.m_ActiveDeformation && deformation ? deformationSGBuffer : defaultValue;
}
}
}