void LateUpdate() { // which lod is this object in (roughly)? var thisRect = new Rect(new Vector2(transform.position.x, transform.position.z), Vector3.zero); var minLod = LodDataMgrAnimWaves.SuggestDataLOD(thisRect); if (minLod == -1) { // outside all lods, nothing to update! return; } // 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); // counting non-existent sims is expensive - stop updating if none found if (simsPresent == 0) { enabled = false; return; } // no sims running - abort. don't bother switching off renderer - camera wont be active if (simsActive == 0) { return; } var disp = _boat.CalculateDisplacementToObject(); transform.position = transform.parent.TransformPoint(_localOffset) - disp + _velocityPositionOffset * _boat.Velocity; var ocean = OceanRenderer.Instance; var rnd = 1f + _noiseAmp * (2f * Mathf.PerlinNoise(_noiseFreq * ocean.CurrentTime, 0.5f) - 1f); // feed in water velocity var vel = (transform.position - _posLast) / ocean.DeltaTimeDynamics; if (ocean.DeltaTimeDynamics < 0.0001f) { vel = Vector3.zero; } if (QueryFlow.Instance) { _sampleFlowHelper.Init(transform.position, _boat.ObjectWidth); Vector2 surfaceFlow = Vector2.zero; _sampleFlowHelper.Sample(ref surfaceFlow); vel -= new Vector3(surfaceFlow.x, 0, surfaceFlow.y); } vel.y *= _weightUpDownMul; var speedKmh = vel.magnitude * 3.6f; if (speedKmh > _teleportSpeed) { // teleport detected vel *= 0f; if (_warnOnTeleport) { Debug.LogWarning("Teleport detected (speed = " + speedKmh.ToString() + "), velocity discarded.", this); } } else if (speedKmh > _maxSpeed) { // limit speed to max vel *= _maxSpeed / speedKmh; if (_warnOnSpeedClamp) { Debug.LogWarning("Speed (" + speedKmh.ToString() + ") exceeded max limited, clamped.", this); } } float dt; int steps; ocean._lodDataDynWaves.GetSimSubstepData(ocean.DeltaTimeDynamics, out steps, out dt); float weight = _boat.InWater ? _weight / simsActive : 0f; _renderer.GetPropertyBlock(_mpb); _mpb.SetVector("_Velocity", vel); _mpb.SetFloat("_Weight", weight); _mpb.SetFloat("_SimDeltaTime", dt); _renderer.SetPropertyBlock(_mpb); _posLast = transform.position; }
void LateUpdate() { if (OceanRenderer.Instance == null) { return; } // which lod is this object in (roughly)? var thisRect = new Rect(new Vector2(transform.position.x, transform.position.z), Vector3.zero); var minLod = LodDataMgrAnimWaves.SuggestDataLOD(thisRect); if (minLod == -1) { // outside all lods, nothing to update! return; } // 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); // counting non-existent sims is expensive - stop updating if none found if (simsPresent == 0) { enabled = false; return; } // no sims running - abort. don't bother switching off renderer - camera wont be active if (simsActive == 0) { return; } transform.position = transform.parent.TransformPoint(_localOffset) + _velocityPositionOffset * _boat.Velocity; var ocean = OceanRenderer.Instance; // feed in water velocity var vel = (transform.position - _posLast) / ocean.DeltaTimeDynamics; if (ocean.DeltaTimeDynamics < 0.0001f) { vel = Vector3.zero; } { _sampleFlowHelper.Init(transform.position, _boat.ObjectWidth); _sampleFlowHelper.Sample(out var surfaceFlow); vel -= new Vector3(surfaceFlow.x, 0, surfaceFlow.y); } vel.y *= _weightUpDownMul; var speedKmh = vel.magnitude * 3.6f; if (speedKmh > _teleportSpeed) { // teleport detected vel *= 0f; if (_warnOnTeleport) { Debug.LogWarning("Teleport detected (speed = " + speedKmh.ToString() + "), velocity discarded.", this); } } else if (speedKmh > _maxSpeed) { // limit speed to max vel *= _maxSpeed / speedKmh; if (_warnOnSpeedClamp) { Debug.LogWarning("Speed (" + speedKmh.ToString() + ") exceeded max limited, clamped.", this); } } var dt = 1f / ocean._lodDataDynWaves.Settings._simulationFrequency; var weight = _boat.InWater ? 1f / simsActive : 0f; _renderer.GetPropertyBlock(_mpb); _mpb.SetVector("_Velocity", vel); _mpb.SetFloat("_SimDeltaTime", dt); // Weighting with this value helps keep ripples consistent for different gravity values var gravityMul = Mathf.Sqrt(ocean._lodDataDynWaves.Settings._gravityMultiplier / 25f); _mpb.SetFloat("_Weight", weight * gravityMul); _renderer.SetPropertyBlock(_mpb); _posLast = transform.position; }