Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore what multiple ocean instances would mean #650

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void Start()
{
_rb = GetComponent<Rigidbody>();

if (OceanRenderer.Instance == null)
if (OceanRenderer.AnyInstance == null)
{
enabled = false;
return;
Expand All @@ -75,15 +75,16 @@ void Start()

void FixedUpdate()
{
if (OceanRenderer.Instance == null)
var ocean = OceanRenderer.ClosestInstance(transform.position);
if (ocean == null)
{
return;
}

UnityEngine.Profiling.Profiler.BeginSample("BoatAlignNormal.FixedUpdate");

_sampleHeightHelper.Init(transform.position, _boatWidth, true);
var height = OceanRenderer.Instance.SeaLevel;
var height = ocean.SeaLevel;

_sampleHeightHelper.Sample(out Vector3 disp, out var normal, out var waterSurfaceVel);

Expand Down
4 changes: 2 additions & 2 deletions crest/Assets/Crest/Crest-Examples/BoatDev/Scripts/LerpCam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class LerpCam : MonoBehaviour

void Update()
{
if (OceanRenderer.Instance == null)
if (OceanRenderer.AnyInstance == null)
{
return;
}
Expand All @@ -28,7 +28,7 @@ void Update()
var targetPos = _targetPos.position;
targetPos.y = Mathf.Max(targetPos.y, h + _minHeightAboveWater);

transform.position = Vector3.Lerp(transform.position, targetPos, _lerpAlpha * OceanRenderer.Instance.DeltaTime * 60f);
transform.position = Vector3.Lerp(transform.position, targetPos, _lerpAlpha * OceanRenderer.AnyInstance.DeltaTime * 60f);
transform.LookAt(_targetLookatPos.position + _lookatOffset * Vector3.up);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void Start()
{
_rdwi = GetComponent<RegisterDynWavesInput>();

if (OceanRenderer.Instance == null || !OceanRenderer.Instance.CreateDynamicWaveSim || _rdwi == null)
if (OceanRenderer.AnyInstance == null || !OceanRenderer.AnyInstance.CreateDynamicWaveSim || _rdwi == null)
{
enabled = false;
return;
Expand All @@ -33,14 +33,15 @@ void Start()

void Update()
{
if (OceanRenderer.Instance == null)
var ocean = OceanRenderer.AnyInstance;
if (ocean == null)
{
return;
}

if (_animate)
{
float t = OceanRenderer.Instance.CurrentTime;
float t = OceanRenderer.AnyInstance.CurrentTime;
if (t < _warmUp)
return;
t -= _warmUp;
Expand All @@ -50,7 +51,7 @@ void Update()

// which lod is this object in (roughly)?
Rect thisRect = new Rect(new Vector2(transform.position.x, transform.position.z), Vector3.zero);
int minLod = LodDataMgrAnimWaves.SuggestDataLOD(thisRect);
int minLod = LodDataMgrAnimWaves.SuggestDataLOD(ocean, thisRect);
if (minLod == -1)
{
// outside all lods, nothing to update!
Expand All @@ -60,15 +61,15 @@ void Update()
// how many active wave sims currently apply to this object - ideally this would eliminate sims that are too
// low res, by providing a max grid size param
int simsPresent, simsActive;
LodDataMgrDynWaves.CountWaveSims(minLod, out simsPresent, out simsActive);
LodDataMgrDynWaves.CountWaveSims(ocean, minLod, out simsPresent, out simsActive);
if (simsPresent == 0)
{
enabled = false;
return;
}

float dt;
OceanRenderer.Instance._lodDataDynWaves.GetSimSubstepData(OceanRenderer.Instance.DeltaTimeDynamics, out _, out dt);
ocean._lodDataDynWaves.GetSimSubstepData(ocean.DeltaTimeDynamics, out _, out dt);

_rend.GetPropertyBlock(_mpb);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@ public class OceanSampleDisplacementDemo : MonoBehaviour

void Update()
{
if (OceanRenderer.Instance == null)
var ocean = OceanRenderer.ClosestInstance(transform.position);
if (ocean == null)
{
return;
}

if (_trackCamera)
{
var height = Mathf.Abs(Camera.main.transform.position.y - OceanRenderer.Instance.SeaLevel);
var height = Mathf.Abs(Camera.main.transform.position.y - ocean.SeaLevel);
var lookAngle = Mathf.Max(Mathf.Abs(Camera.main.transform.forward.y), 0.001f);
var offset = height / lookAngle;
_markerPos[0] = Camera.main.transform.position + Camera.main.transform.forward * offset;
_markerPos[1] = Camera.main.transform.position + Camera.main.transform.forward * offset + _samplesRadius * Vector3.right;
_markerPos[2] = Camera.main.transform.position + Camera.main.transform.forward * offset + _samplesRadius * Vector3.forward;
}

var collProvider = OceanRenderer.Instance.CollisionProvider;
var collProvider = ocean.CollisionProvider;

var status = collProvider.Query(GetHashCode(), _minGridSize, _markerPos, _resultDisps, _resultNorms, _resultVels);

Expand All @@ -56,7 +57,7 @@ void Update()
}

var query = _markerPos[i];
query.y = OceanRenderer.Instance.SeaLevel;
query.y = ocean.SeaLevel;

var disp = _resultDisps[i];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ public class UnderwaterEnvironmentalLighting : MonoBehaviour

void Start()
{
if (OceanRenderer.Instance == null)
// todo - how to connect this to correct ocean instance?
if (OceanRenderer.AnyInstance == null)
{
enabled = false;
return;
}

_primaryLight = OceanRenderer.Instance._primaryLight;
_primaryLight = OceanRenderer.AnyInstance._primaryLight;

// Store lighting settings
if (_primaryLight)
Expand All @@ -47,13 +48,13 @@ void Start()
_fogDensity = RenderSettings.fogDensity;

// Check to make sure the property exists. We might be using a test material.
if (!OceanRenderer.Instance.OceanMaterial.HasProperty("_DepthFogDensity"))
if (!OceanRenderer.AnyInstance.OceanMaterial.HasProperty("_DepthFogDensity"))
{
enabled = false;
return;
}

Color density = OceanRenderer.Instance.OceanMaterial.GetColor("_DepthFogDensity");
Color density = OceanRenderer.AnyInstance.OceanMaterial.GetColor("_DepthFogDensity");
_averageDensity = (density.r + density.g + density.b) / 3f;
}

Expand All @@ -71,13 +72,13 @@ void OnDisable()

void LateUpdate()
{
if (OceanRenderer.Instance == null)
if (OceanRenderer.AnyInstance == null)
{
return;
}

float depthMultiplier = Mathf.Exp(_averageDensity *
Mathf.Min(OceanRenderer.Instance.ViewerHeightAboveWater * DEPTH_OUTSCATTER_CONSTANT, 0f));
Mathf.Min(OceanRenderer.AnyInstance.ViewerHeightAboveWater * DEPTH_OUTSCATTER_CONSTANT, 0f));

// Darken environmental lighting when viewer underwater
if (_primaryLight)
Expand Down
14 changes: 9 additions & 5 deletions crest/Assets/Crest/Crest-Examples/Whirlpool/Scripts/Whirlpool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void UpdateMaterials()

void Start()
{
if (OceanRenderer.Instance == null)
if (OceanRenderer.AnyInstance == null)
{
enabled = false;
return;
Expand Down Expand Up @@ -79,12 +79,16 @@ void AddInput<RegisterInputType>(Material material, float radius) where Register

void Update()
{
if (OceanRenderer.Instance == null)
for (var i = 0; i < OceanRenderer.InstanceCount; i++)
{
return;
}
var ocean = OceanRenderer.GetInstance(i);
if (ocean == null)
{
continue;
}

OceanRenderer.Instance.ReportMaxDisplacementFromShape(0f, _amplitude, 0f);
ocean.ReportMaxDisplacementFromShape(0f, _amplitude, 0f);
}

UpdateMaterials();
}
Expand Down
6 changes: 2 additions & 4 deletions crest/Assets/Crest/Crest/Scripts/BuildCommandBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ void BuildLodData(OceanRenderer ocean, CommandBuffer buf)
/// <summary>
/// Construct the command buffer and attach it to the camera so that it will be executed in the render.
/// </summary>
public void BuildAndExecute()
public void BuildAndExecute(OceanRenderer ocean)
{
if (OceanRenderer.Instance == null) return;

if (_buf == null)
{
_buf = new CommandBuffer();
Expand All @@ -98,7 +96,7 @@ public void BuildAndExecute()

_buf.Clear();

BuildLodData(OceanRenderer.Instance, _buf);
BuildLodData(ocean, _buf);

// This will execute at the beginning of the frame before the graphics queue
Graphics.ExecuteCommandBuffer(_buf);
Expand Down
14 changes: 9 additions & 5 deletions crest/Assets/Crest/Crest/Scripts/Collision/QueryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public abstract class QueryBase
const int s_maxRequests = 4;
const int s_maxGuids = 1024;

protected OceanRenderer _ocean;

protected virtual ComputeShader ShaderProcessQueries => _shaderProcessQueries;
ComputeShader _shaderProcessQueries;
PropertyWrapperComputeStandalone _wrapper;
Expand Down Expand Up @@ -215,13 +217,15 @@ public enum QueryStatus
InvalidDtForVelocity = 16,
}

public QueryBase()
public QueryBase(OceanRenderer ocean)
{
_ocean = ocean;

_dataArrivedAction = new System.Action<AsyncGPUReadbackRequest>(DataArrived);

if (_maxQueryCount != OceanRenderer.Instance._lodDataAnimWaves.Settings.MaxQueryCount)
if (_maxQueryCount != ocean._lodDataAnimWaves.Settings.MaxQueryCount)
{
_maxQueryCount = OceanRenderer.Instance._lodDataAnimWaves.Settings.MaxQueryCount;
_maxQueryCount = ocean._lodDataAnimWaves.Settings.MaxQueryCount;
_queryPosXZ_minGridSize = new Vector3[_maxQueryCount];
}

Expand Down Expand Up @@ -307,7 +311,7 @@ protected bool UpdateQueryPoints(int i_ownerHash, float i_minSpatialLength, Vect
// The smallest wavelengths should repeat no more than twice across the smaller spatial length. Unless we're
// in the last LOD - then this is the best we can do.
float minWavelength = i_minSpatialLength / 2f;
float minGridSize = minWavelength / OceanRenderer.Instance.MinTexelsPerWave;
float minGridSize = minWavelength / _ocean.MinTexelsPerWave;

if (countPts + segment.x > _queryPosXZ_minGridSize.Length)
{
Expand Down Expand Up @@ -392,7 +396,7 @@ protected bool RetrieveResults(int guid, Vector3[] displacements, float[] height
// Retrieve Result heights
if (heights != null)
{
var seaLevel = OceanRenderer.Instance.SeaLevel;
var seaLevel = _ocean.SeaLevel;
for (int i = 0; i < countPoints; i++)
{
heights[i] = seaLevel + _queryResults[i + segment.x].y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ public class QueryDisplacements : QueryBase, ICollProvider
protected override string QueryShaderName => "QueryDisplacements";
protected override string QueryKernelName => "CSMain";

public QueryDisplacements(OceanRenderer ocean) : base(ocean)
{
}

protected override void BindInputsAndOutputs(PropertyWrapperComputeStandalone wrapper, ComputeBuffer resultsBuffer)
{
OceanRenderer.Instance._lodDataAnimWaves.BindResultData(wrapper);
ShaderProcessQueries.SetTexture(_kernelHandle, sp_LD_TexArray_AnimatedWaves, OceanRenderer.Instance._lodDataAnimWaves.DataTexture);
_ocean._lodDataAnimWaves.BindResultData(wrapper);
ShaderProcessQueries.SetTexture(_kernelHandle, sp_LD_TexArray_AnimatedWaves, _ocean._lodDataAnimWaves.DataTexture);
ShaderProcessQueries.SetBuffer(_kernelHandle, sp_ResultDisplacements, resultsBuffer);
}

Expand Down
10 changes: 7 additions & 3 deletions crest/Assets/Crest/Crest/Scripts/Collision/QueryFlow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ public class QueryFlow : QueryBase, IFlowProvider
protected override string QueryShaderName => "QueryFlow";
protected override string QueryKernelName => "CSMain";

public QueryFlow(OceanRenderer ocean) : base(ocean)
{
}

protected override void BindInputsAndOutputs(PropertyWrapperComputeStandalone wrapper, ComputeBuffer resultsBuffer)
{
if (OceanRenderer.Instance._lodDataFlow != null)
if (_ocean._lodDataFlow != null)
{
OceanRenderer.Instance._lodDataFlow.BindResultData(wrapper);
ShaderProcessQueries.SetTexture(_kernelHandle, sp_LD_TexArray_Flow, OceanRenderer.Instance._lodDataFlow.DataTexture);
_ocean._lodDataFlow.BindResultData(wrapper);
ShaderProcessQueries.SetTexture(_kernelHandle, sp_LD_TexArray_Flow, _ocean._lodDataFlow.DataTexture);
}
else
{
Expand Down
12 changes: 8 additions & 4 deletions crest/Assets/Crest/Crest/Scripts/Collision/RayTraceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Crest
/// </summary>
public class RayTraceHelper
{
OceanRenderer _ocean;

Vector3[] _queryPos;
Vector3[] _queryResult;

Expand Down Expand Up @@ -53,6 +55,8 @@ public RayTraceHelper(float rayLength, float rayStepSize = 2f)
/// <param name="i_rayDirection">World space ray direction</param>
public void Init(Vector3 i_rayOrigin, Vector3 i_rayDirection)
{
_ocean = OceanRenderer.ClosestInstance(i_rayOrigin);

for (int i = 0; i < _queryPos.Length; i++)
{
_queryPos[i] = i_rayOrigin + i * _rayStepSize * i_rayDirection;
Expand Down Expand Up @@ -82,9 +86,9 @@ public bool Trace(out float o_distance)
return false;
}

var status = OceanRenderer.Instance.CollisionProvider.Query(GetHashCode(), _minLength, _queryPos, _queryResult, null, null);
var status = _ocean.CollisionProvider.Query(GetHashCode(), _minLength, _queryPos, _queryResult, null, null);

if (!OceanRenderer.Instance.CollisionProvider.RetrieveSucceeded(status))
if (!_ocean.CollisionProvider.RetrieveSucceeded(status))
{
_valid = false;
return false;
Expand All @@ -94,8 +98,8 @@ public bool Trace(out float o_distance)
// the ray crosses the surface, the distance to the intersection is interpolated from the heights.
for (int i = 1; i < _queryPos.Length; i++)
{
var height0 = _queryResult[i - 1].y + OceanRenderer.Instance.SeaLevel - _queryPos[i - 1].y;
var height1 = _queryResult[i].y + OceanRenderer.Instance.SeaLevel - _queryPos[i].y;
var height0 = _queryResult[i - 1].y + _ocean.SeaLevel - _queryPos[i - 1].y;
var height1 = _queryResult[i].y + _ocean.SeaLevel - _queryPos[i].y;

if (Mathf.Sign(height0) != Mathf.Sign(height1))
{
Expand Down
Loading