void LateUpdate()
        {
            var ocean = OceanRenderer.Instance;

            if (ocean == null)
            {
                return;
            }

            // Which lod is this object in (roughly)?
            int simsActive;

            if (!LateUpdateCountOverlappingSims(out simsActive, out int simsPresent))
            {
                if (simsPresent == 0)
                {
                    // Counting non-existent sims is expensive - stop updating if none found
                    enabled = false;
                }

                // No sims running - abort. don't bother switching off renderer - camera wont be active
                return;
            }

            var disp = _object.CalculateDisplacementToObject();

            // Set position of interaction
            {
                var dispFlatLand = disp;
                dispFlatLand.y = 0f;
                var velBoat = _object.Velocity;
                velBoat.y          = 0f;
                transform.position = transform.parent.TransformPoint(_localPositionRest) - dispFlatLand;
                transform.rotation = Quaternion.Euler(90f, 0f, 0f);
            }

            // Velocity relative to water
            Vector3 relativeVelocity = LateUpdateComputeVelRelativeToWater(ocean);

            float dt; int steps;

            ocean._lodDataDynWaves.GetSimSubstepData(ocean.DeltaTimeDynamics, out steps, out dt);

            float weight = _weight / simsActive;

            var waterHeight = disp.y + ocean.SeaLevel;

            LateUpdateSphereWeight(waterHeight, ref weight);

            _renderer.GetPropertyBlock(_mpb);

            _mpb.SetVector(sp_velocity, relativeVelocity);
            _mpb.SetFloat(sp_weight, weight);
            _mpb.SetFloat(sp_simDeltaTime, dt);
            _mpb.SetFloat(sp_radius, Radius);

            _renderer.SetPropertyBlock(_mpb);

            _posLast = transform.position;
        }
Example #2
0
        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;
        }
Example #3
0
        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.RB.velocity;

            var rnd = 1f + _noiseAmp * (2f * Mathf.PerlinNoise(_noiseFreq * OceanRenderer.Instance.CurrentTime, 0.5f) - 1f);
            // feed in water velocity
            var vel = (transform.position - _posLast) / Time.deltaTime;

            if (OceanRenderer.Instance._simSettingsFlow != null &&
                OceanRenderer.Instance._simSettingsFlow._readbackData &&
                GPUReadbackFlow.Instance)
            {
                var position     = transform.position;
                var samplingArea = new Rect(position.x, position.z, 0f, 0f);
                GPUReadbackFlow.Instance.GetSamplingData(ref samplingArea, _boat.ObjectWidth, _samplingDataFlow);

                Vector2 surfaceFlow;
                GPUReadbackFlow.Instance.SampleFlow(ref position, _samplingDataFlow, out surfaceFlow);

                vel -= new Vector3(surfaceFlow.x, 0, surfaceFlow.y);

                GPUReadbackFlow.Instance.ReturnSamplingData(_samplingDataFlow);
            }
            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;

            OceanRenderer.Instance._lodDataDynWaves.GetSimSubstepData(Time.deltaTime, out steps, out dt);
            float weight = _boat.InWater ? _weight / simsActive : 0f;

            for (int mati = 0; mati < _dynWavesInput.MaterialCount; mati++)
            {
                var mat = _dynWavesInput.GetMaterial(mati);

                mat.SetVector("_Velocity", vel);
                mat.SetFloat("_Weight", weight);
                mat.SetFloat("_SimDeltaTime", dt);
            }

            _posLast = transform.position;
        }