Skip to content

Commit

Permalink
Add CrestComponent to abstract boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
daleeidd committed Aug 25, 2020
1 parent 43fdadc commit 9536dda
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Crest
/// (directional light, reflections, ambient etc) with the underwater depth. This works with vanilla lighting, but
/// uncommon or custom lighting will require a custom solution (use this for reference).
/// </summary>
public class UnderwaterEnvironmentalLighting : MonoBehaviour
public class UnderwaterEnvironmentalLighting : CrestComponent
{
Light _primaryLight;
float _lightIntensity;
Expand All @@ -27,13 +27,12 @@ public class UnderwaterEnvironmentalLighting : MonoBehaviour

public const float DEPTH_OUTSCATTER_CONSTANT = 0.25f;

void OnEnable()
public override void OnEnable()
{
if (OceanRenderer.Instance == null)
base.OnEnable();

if (!enabled)
{
OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled;
OceanRenderer.OnOceanRendererEnabled += OnOceanRendererEnabled;
enabled = false;
return;
}

Expand Down Expand Up @@ -71,18 +70,6 @@ void OnDisable()
RenderSettings.fogDensity = _fogDensity;
}

private void OnDestroy()
{
// We need this event registered even when this component is disabled. So we unregister only when the
// component is destroyed.
OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled;
}

void OnOceanRendererEnabled()
{
enabled = true;
}

void LateUpdate()
{
if (OceanRenderer.Instance == null)
Expand Down
34 changes: 34 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Helpers/CrestComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Crest Ocean System

// This file is subject to the MIT License as seen in the root of this folder structure (LICENSE)

// Base class for MonoBehaviours.

using UnityEngine;

namespace Crest
{
public abstract class CrestComponent : MonoBehaviour
{
public virtual void OnEnable()
{
// Delayed initialisation.
if (OceanRenderer.Instance == null)
{
OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled;
OceanRenderer.OnOceanRendererEnabled += OnOceanRendererEnabled;
enabled = false;
}
}

void OnOceanRendererEnabled() => enabled = true;

void OnDestroy()
{
// We need this event registered even when this component is disabled. So we unregister only when the
// component is destroyed.
OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled;
}
}
}

11 changes: 11 additions & 0 deletions crest/Assets/Crest/Crest/Scripts/Helpers/CrestComponent.cs.meta

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

Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static void InitStatics()
/// <summary>
/// Attach to a camera to generate a reflection texture which can be sampled in the ocean shader.
/// </summary>
public class OceanPlanarReflection : MonoBehaviour
public class OceanPlanarReflection : CrestComponent
{
[SerializeField] LayerMask _reflectionLayers = 1;
[SerializeField] bool _disableOcclusionCulling = true;
Expand Down Expand Up @@ -117,13 +117,12 @@ public class OceanPlanarReflection : MonoBehaviour
const int CULL_DISTANCE_COUNT = 32;
float[] _cullDistances = new float[CULL_DISTANCE_COUNT];

private void OnEnable()
public override void OnEnable()
{
if (OceanRenderer.Instance == null)
base.OnEnable();

if (!enabled)
{
OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled;
OceanRenderer.OnOceanRendererEnabled += OnOceanRendererEnabled;
enabled = false;
return;
}

Expand All @@ -148,11 +147,6 @@ private void OnEnable()
#endif
}

void OnOceanRendererEnabled()
{
enabled = true;
}

bool RequestRefresh(long frame)
{
if (_lastRefreshOnFrame <= 0 || RefreshPerFrames < 2)
Expand Down Expand Up @@ -399,12 +393,5 @@ private void OnDisable()
_camReflections = null;
}
}

private void OnDestroy()
{
// We need this event registered even when this component is disabled. So we unregister only when the
// component is destroyed.
OceanRenderer.OnOceanRendererEnabled -= OnOceanRendererEnabled;
}
}
}

0 comments on commit 9536dda

Please sign in to comment.