Skip to content

Commit

Permalink
Tried extrapolating collision data forward in time. Worked but not su…
Browse files Browse the repository at this point in the history
…per stable and error is small. Parking for now.
  • Loading branch information
huwb committed May 21, 2018
1 parent e8e65bf commit 8146864
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
/// </summary>
public class OceanHeightDemo : MonoBehaviour
{
public bool _extrapolateForwards = true;

float _lastTime = -1f;
float _lastHeight;

void Update ()
{
float scale = transform.lossyScale.x;
Expand All @@ -17,9 +22,24 @@ void Update ()

if (lod > -1)
{
var wdc = OceanRenderer.Instance.Builder._shapeWDCs[lod];

var pos = transform.position;
var height = OceanRenderer.Instance.Builder._shapeWDCs[lod].GetHeightExpensive(ref pos);
transform.position += Vector3.up * (height - pos.y);
var height = wdc.GetHeightExpensive(ref pos);
var time = wdc.GetCollisionTime();

var targetHeight = height;

if (_extrapolateForwards && _lastTime != -1f && (time - _lastTime) > Mathf.Epsilon)
{
float vel = (height - _lastHeight) / (time - _lastTime);
targetHeight += (OceanRenderer.Instance.ElapsedTime - time) * vel;
}

transform.position += Vector3.up * (targetHeight - pos.y);

_lastHeight = height;
_lastTime = time;
}
}
}
11 changes: 10 additions & 1 deletion src/unity/Assets/Crest/Scripts/WaveDataCam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ struct CollisionRequest
{
public AsyncGPUReadbackRequest _request;
public RenderData _renderData;
public float _time;
}
Queue<CollisionRequest> _requests = new Queue<CollisionRequest>();
const int MAX_REQUESTS = 8;

// collision data
NativeArray<ushort> _collDataNative;
RenderData _collRenderData;
float _collTime;

Material _matOceanDepth;
RenderTexture _rtOceanDepth;
Expand Down Expand Up @@ -147,6 +149,7 @@ void UpdateShapeReadback()
var data = request._request.GetData<ushort>();
data.CopyTo(_collDataNative);
_collRenderData = request._renderData;
_collTime = request._time;

Profiler.EndSample();
}
Expand Down Expand Up @@ -229,6 +232,11 @@ public float GetHeightExpensive(ref Vector3 worldPos)
return posFlatland.y + disp.y;
}

public float GetCollisionTime()
{
return _collTime;
}

public void EnqueueReadbackRequest(RenderTexture target)
{
if (_requests.Count < MAX_REQUESTS)
Expand All @@ -237,7 +245,8 @@ public void EnqueueReadbackRequest(RenderTexture target)
new CollisionRequest
{
_request = AsyncGPUReadback.Request(cam.targetTexture),
_renderData = _renderData
_renderData = _renderData,
_time = OceanRenderer.Instance.ElapsedTime,
}
);
}
Expand Down

0 comments on commit 8146864

Please sign in to comment.