using System.Collections.Generic;
using Unity.Mathematics;
namespace UnityEngine.Rendering.HighDefinition
{
///
/// Water decal component.
///
[HDRPHelpURL("water-decals-and-masking-in-the-water-system")]
[ExecuteInEditMode]
public partial class WaterDecal : MonoBehaviour
{
internal enum PassType
{
Deformation = 0,
Foam = 1,
SimulationMask = 2,
LargeCurrent = 3,
RipplesCurrent = 4,
}
#region General
///
/// The scaling mode to apply to this Foam Generator.
///
[Tooltip("Specify the scaling mode")]
public DecalScaleMode scaleMode = DecalScaleMode.InheritFromHierarchy;
///
/// Specifies the size of the deformer in meters.
///
public Vector2 regionSize = new Vector2(20.0f, 20.0f);
///
/// Specifies the amplitude of the deformation.
///
public float amplitude = 2.0f;
///
/// Specifies the dimmer for the surface foam.
///
[Range(0.0f, 1.0f)]
public float surfaceFoamDimmer = 1.0f;
///
/// Specifies a dimmer for the deep foam.
///
[Range(0.0f, 1.0f)]
public float deepFoamDimmer = 1.0f;
/// Scale that should be used for rendering and handles.
internal float3 effectiveScale => scaleMode == DecalScaleMode.InheritFromHierarchy ? transform.lossyScale : Vector3.one;
#endregion
#region Material Management
///
/// Specifies the resolution when written inside the atlas.
///
public Vector2Int resolution = new Vector2Int(128, 128);
///
/// Frequency of update of the Material in the atlas.
///
[Tooltip("Frequency of update of the Material in the atlas.")]
public CustomRenderTextureUpdateMode updateMode = CustomRenderTextureUpdateMode.OnLoad;
///
/// Specifies the material used for the water decal.
///
[Tooltip("Specifies the material used for the water decal.")]
public Material material = null;
internal int updateCount = 0;
///
/// Triggers a render of the material in the deformer atlas.
///
public void RequestUpdate()
{
updateCount++;
}
internal MaterialPropertyBlock mpb;
///
/// Override per-deformer material parameters. This is more memory efficient than having one complete distinct Material per deformer but is recommended when only a few properties of a Material overriden.
///
/// Property block with values you want to override.
public void SetPropertyBlock(MaterialPropertyBlock properties)
{
mpb = properties;
}
///
/// Returns true if the Deformer has a material property block attached via SetPropertyBlock.
///
/// Returns true if the Deformer has a material property block attached via SetPropertyBlock.
public bool HasPropertyBlock()
{
return mpb != null;
}
internal bool IsValidMaterial()
{
#if UNITY_EDITOR
return material != null && material.GetTag("ShaderGraphTargetId", false, null) == "WaterDecalSubTarget";
#else
return material != null;
#endif
}
internal int GetMaterialAtlasingId()
{
// If material has a property block, we can't reuse the atlas slot
return HasPropertyBlock() ? GetInstanceID(): material.GetInstanceID();
}
#endregion
#region Instance Management
internal static List instances = new List();
internal static void RegisterInstance(WaterDecal decal)
{
instances.Add(decal);
}
internal static void UnregisterInstance(WaterDecal decal)
{
if (instances.Contains(decal))
instances.Remove(decal);
}
internal void Reset()
{
regionSize = new Vector2(10f, 10.0f);
resolution = new Vector2Int(256, 256);
scaleMode = DecalScaleMode.InheritFromHierarchy;
updateMode = CustomRenderTextureUpdateMode.OnLoad;
updateCount++;
material = GraphicsSettings.GetRenderPipelineSettings().waterDecalMaterial;
amplitude = 2.0f;
surfaceFoamDimmer = 1.0f;
deepFoamDimmer = 1.0f;
}
#endregion
#region MonoBehavior Methods
private void OnEnable()
{
RegisterInstance(this);
if (updateMode == CustomRenderTextureUpdateMode.OnLoad)
updateCount++;
}
private void OnDisable()
{
UnregisterInstance(this);
}
#endregion
}
}