Skip to content

Commit

Permalink
Merge pull request #912 from wave-harmonic/feature/water-body-auto-clip
Browse files Browse the repository at this point in the history
Water body auto clip-include
  • Loading branch information
daleeidd authored Dec 15, 2021
2 parents c4463f8 + 701656c commit 50cdfa8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
75 changes: 75 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/WaterBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UnityEditor;
#endif
using UnityEngine;
using UnityEngine.Rendering;

namespace Crest
{
Expand All @@ -33,6 +34,9 @@ public partial class WaterBody : MonoBehaviour
bool _runValidationOnStart = true;
#pragma warning restore 414

[Tooltip("If clipping is enabled and set to clip everywhere by default, this option will register this water body to ensure its area does not get clipped."), SerializeField]
bool _registerWithClipSurfaceData = true;

public static List<WaterBody> WaterBodies => _waterBodies;
static List<WaterBody> _waterBodies = new List<WaterBody>();

Expand All @@ -43,16 +47,52 @@ public partial class WaterBody : MonoBehaviour
"specified, the default material assigned to the OceanRenderer component will be used.")]
public Material _overrideMaterial = null;

class ClipInput : ILodDataInput
{
Material _renderMat;

// Render to all cascades
public float Wavelength => 0f;
public bool Enabled => true;

public Matrix4x4 _transform;

public ClipInput(WaterBody owner)
{
var rotateQuadFaceUp = Matrix4x4.Rotate(Quaternion.AngleAxis(90, Vector3.right));
_transform = owner.transform.localToWorldMatrix * rotateQuadFaceUp;

_renderMat = new Material(Shader.Find("Crest/Inputs/Clip Surface/Include Area"));
}

public void Draw(CommandBuffer buf, float weight, int isTransition, int lodIdx)
{
buf.DrawMesh(RegisterLodDataInputBase.QuadMesh, _transform, _renderMat);
}
}

ClipInput _clipInput;

private void OnEnable()
{
CalculateBounds();

_waterBodies.Add(this);

// Needs to execute after the Ocean Renderer as Update is stripped from builds.
HandleClipInputRegistration();
}

private void OnDisable()
{
_waterBodies.Remove(this);

if (_clipInput != null)
{
RegisterLodDataInputBase.GetRegistrar(typeof(LodDataMgrClipSurface)).Remove(_clipInput);

_clipInput = null;
}
}

private void CalculateBounds()
Expand All @@ -67,6 +107,30 @@ private void CalculateBounds()
AABB = bounds;
}

void HandleClipInputRegistration()
{
var registered = _clipInput != null;
var shouldBeRegistered = _registerWithClipSurfaceData && OceanRenderer.Instance && OceanRenderer.Instance.CreateClipSurfaceData
&& OceanRenderer.Instance._defaultClippingState == OceanRenderer.DefaultClippingState.EverythingClipped;

if (registered != shouldBeRegistered)
{
if (shouldBeRegistered)
{
_clipInput = new ClipInput(this);

var registrar = RegisterLodDataInputBase.GetRegistrar(typeof(LodDataMgrClipSurface));
registrar.Add(0, _clipInput);
}
else
{
RegisterLodDataInputBase.GetRegistrar(typeof(LodDataMgrClipSurface)).Remove(_clipInput);

_clipInput = null;
}
}
}

#if UNITY_EDITOR
private void Start()
{
Expand All @@ -76,6 +140,17 @@ private void Start()
}
}

private void Update()
{
HandleClipInputRegistration();

if (_clipInput != null)
{
var rotateQuadFaceUp = Matrix4x4.Rotate(Quaternion.AngleAxis(90, Vector3.right));
_clipInput._transform = transform.localToWorldMatrix * rotateQuadFaceUp;
}
}

private void OnDrawGizmosSelected()
{
// Required as we're not normally executing in edit mode
Expand Down
2 changes: 1 addition & 1 deletion crest/Assets/Crest/Crest/Scripts/WaterBody.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/about/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Changed
- Add *Ocean Renderer > Water Body Culling* option so the ocean can ignore culling.
Useful if using *Water Body > Override Material* and still want an ocean.
- Improve multiple *Water Body* overlapping case when *Water Body > Override Material* option is used.
- Water Body adds an inclusion to clipping (ie unclips) if *Default Clipping State* is *Everything Clipped*.

Fixed
^^^^^
Expand Down

0 comments on commit 50cdfa8

Please sign in to comment.