Skip to content

Commit

Permalink
Merge pull request #829 from wave-harmonic/feature/multiple-oceans-2
Browse files Browse the repository at this point in the history
Multiple Oceans: Connect underwater to ocean instance
  • Loading branch information
daleeidd authored May 14, 2021
2 parents 73c3128 + eca9f89 commit 55c79f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
6 changes: 4 additions & 2 deletions crest/Assets/Crest/Crest/Scripts/Helpers/UnderwaterEffect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public partial class UnderwaterEffect : MonoBehaviour
OceanRenderer _ocean;
PropertyWrapperMPB _mpb;
Renderer _rend;
Camera _camera;

readonly int sp_HeightOffset = Shader.PropertyToID("_HeightOffset");

Expand All @@ -59,12 +60,13 @@ private void Start()
#endif
_rend = GetComponent<Renderer>();

_camera = GetComponentInParent<Camera>();

// Render before the surface mesh
_rend.sortingOrder = _overrideSortingOrder ? _overridenSortingOrder : -LodDataMgr.MAX_LOD_COUNT - 1;
GetComponent<MeshFilter>().sharedMesh = Mesh2DGrid(0, 2, -0.5f, -0.5f, 1f, 1f, GEOM_HORIZ_DIVISIONS, 1);

// TODO - how to connect camera/underwater effect with ocean?
_ocean = OceanRenderer.AnyInstance;
_ocean = OceanRenderer.GetInstance(_camera);

#if UNITY_EDITOR
if (EditorApplication.isPlaying && !Validate(_ocean, ValidatedHelper.DebugLog))
Expand Down
25 changes: 15 additions & 10 deletions crest/Assets/Crest/Crest/Scripts/OceanRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,21 @@ public static OceanRenderer ClosestInstance(Vector3 position)
return result;
}

public static OceanRenderer GetInstance(Camera camera)
{
if (_instances.Count == 0) return null;

for (int i = 0; i < _instances.Count; i++)
{
if (_instances[i].Viewpoint == camera.transform)
{
return _instances[i];
}
}

return null;
}

static List<OceanRenderer> _instances = new List<OceanRenderer>();

// We are computing these values to be optimal based on the base mesh vertex density.
Expand Down Expand Up @@ -1084,16 +1099,6 @@ public bool Validate(OceanRenderer ocean, ValidatedHelper.ShowMessage showMessag
isValid = false;
}

// OceanRenderer
if (FindObjectsOfType<OceanRenderer>().Length > 1)
{
showMessage
(
"Multiple OceanRenderer scripts detected in open scenes, this is not typical - usually only one OceanRenderer is expected to be present.",
ValidatedHelper.MessageType.Warning, ocean
);
}

// ShapeGerstnerBatched
var gerstners = FindObjectsOfType<ShapeGerstnerBatched>();
if (gerstners.Length == 0)
Expand Down

0 comments on commit 55c79f8

Please sign in to comment.