Skip to content

Commit

Permalink
Merge pull request #899 from wave-harmonic/feature/clip-surface-input…
Browse files Browse the repository at this point in the history
…-inverted-option

Clip surface input inverted option
  • Loading branch information
daleeidd authored Sep 5, 2021
2 parents 2ed0afc + 2c545f7 commit 644cc8c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public enum Primitive
[SerializeField, Predicated("_mode", inverted: true, Mode.Primitive), DecoratedField]
Primitive _primitive = Primitive.Cube;

[Tooltip("Order (ascending) that this input will be rendered into the clip surface data.")]
[SerializeField, Predicated("_mode", inverted: true, Mode.Primitive), DecoratedField]
int _order = 0;

[Tooltip("Removes clip surface data instead of adding it.")]
[SerializeField, Predicated("_mode", inverted: true, Mode.Primitive), DecoratedField]
bool _inverted = false;

[Header("3D Clipping Options")]

[Tooltip("Prevents inputs from cancelling each other out when aligned vertically. It is imperfect so custom logic might be needed for your use case.")]
Expand All @@ -75,6 +83,7 @@ public enum Primitive

static int sp_DisplacementSamplingIterations = Shader.PropertyToID("_DisplacementSamplingIterations");
static readonly int sp_SignedDistanceShapeMatrix = Shader.PropertyToID("_SignedDistanceShapeMatrix");
static readonly int sp_BlendOp = Shader.PropertyToID("_BlendOp");

Material _signedDistancedMaterial;
Primitive _activePrimitive;
Expand Down Expand Up @@ -110,6 +119,20 @@ protected override void Update()
#endif
}

protected override bool GetQueue(out int queue)
{
// Support queue for primitives.
if (_mode == Mode.Primitive)
{
queue = _order;
return true;
}
else
{
return base.GetQueue(out queue);
}
}

void InitializeSignedDistanceMaterial()
{
if (_signedDistancedMaterial == null)
Expand Down Expand Up @@ -214,6 +237,19 @@ private void LateUpdate()
{
_renderer.GetPropertyBlock(_mpb.materialPropertyBlock);
}
else
{
if (_inverted)
{
_signedDistancedMaterial.EnableKeyword("_INVERTED");
}
else
{
_signedDistancedMaterial.DisableKeyword("_INVERTED");
}

_signedDistancedMaterial.SetInt(sp_BlendOp, (int)(_inverted ? BlendOp.Min : BlendOp.Max));
}

_mpb.SetInt(LodDataMgr.sp_LD_SliceIndex, lodIdx);
_mpb.SetInt(sp_DisplacementSamplingIterations, (int)_animatedWavesDisplacementSamplingIterations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public abstract class RegisterLodDataInput<LodDataType> : RegisterLodDataInputBa

int _registeredQueueValue = int.MinValue;

bool GetQueue(out int queue)
protected virtual bool GetQueue(out int queue)
{
var rend = GetComponent<Renderer>();
if (rend && rend.sharedMaterial != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

Shader "Crest/Inputs/Clip Surface/Convex Hull"
{
Properties
{
[Toggle] _Inverted("Inverted", Float) = 0
}

SubShader
{
ZWrite Off
Expand All @@ -28,6 +33,7 @@ Shader "Crest/Inputs/Clip Surface/Convex Hull"

CBUFFER_START(CrestPerOceanInput)
uint _DisplacementSamplingIterations;
float _Inverted;
CBUFFER_END

struct Attributes
Expand Down Expand Up @@ -66,7 +72,7 @@ Shader "Crest/Inputs/Clip Surface/Convex Hull"
{
clip(-1);
}
return float4(1, 0, 0, 1);
return float4(_Inverted == 0, 0, 0, 1);
}
ENDCG
}
Expand All @@ -86,8 +92,10 @@ Shader "Crest/Inputs/Clip Surface/Convex Hull"
#include "../../OceanHelpersNew.hlsl"
#include "../../OceanHelpersDriven.hlsl"


CBUFFER_START(CrestPerOceanInput)
uint _DisplacementSamplingIterations;
float _Inverted;
CBUFFER_END

struct Attributes
Expand Down Expand Up @@ -126,7 +134,7 @@ Shader "Crest/Inputs/Clip Surface/Convex Hull"
{
clip(-1);
}
return float4(0, 0, 0, 1);
return float4(_Inverted, 0, 0, 1);
}
ENDCG
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Shader "Hidden/Crest/Inputs/Clip Surface/Signed Distance"
{
ZWrite Off
ColorMask R
BlendOp Max
BlendOp [_BlendOp]

Pass
{
Expand All @@ -21,6 +21,7 @@ Shader "Hidden/Crest/Inputs/Clip Surface/Signed Distance"
#pragma fragment Frag

#pragma multi_compile_local _SPHERE _CUBE
#pragma multi_compile_local _ _INVERTED

#include "UnityCG.cginc"
#include "../../OceanGlobals.hlsl"
Expand Down Expand Up @@ -89,7 +90,11 @@ Shader "Hidden/Crest/Inputs/Clip Surface/Signed Distance"
float signedDistance = signedDistanceSphere(positionWS);
#endif

return float4(1.0 - signedDistance, 0.0, 0.0, 1.0);
#if !_INVERTED
signedDistance = 1.0 - signedDistance;
#endif

return float4(signedDistance, 0.0, 0.0, 1.0);
}
ENDCG
}
Expand Down
2 changes: 1 addition & 1 deletion docs/about/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Changed
.. bullet_list::

- Add *Dynamic Waves* reflections from *Ocean Depth Cache* geometry.

- Add inverted option to *Clip Surface* signed-distance primitives and convex hulls which removes clipping.


4.13
Expand Down

0 comments on commit 644cc8c

Please sign in to comment.