Ejemplo n.º 1
0
        void Update()
        {
            if (OceanRenderer.Instance == null || OceanRenderer.Instance.CollisionProvider == null)
            {
                return;
            }

            // Even if only a single ray trace is desired, this still must be called every frame until Trace() returns true
            _rayTrace.Init(transform.position, transform.forward);
            if (_rayTrace.Trace(out float dist))
            {
                var endPos = transform.position + transform.forward * dist;
                Debug.DrawLine(transform.position, endPos, Color.green);
                VisualiseCollisionArea.DebugDrawCross(endPos, 2f, Color.green, 0f);
            }
            else
            {
                Debug.DrawLine(transform.position, transform.position + transform.forward * 50f, Color.red);
            }
        }
Ejemplo n.º 2
0
        void FixedUpdate()
        {
            UnityEngine.Profiling.Profiler.BeginSample("SimpleFloatingObject.FixedUpdate");

            if (OceanRenderer.Instance == null)
            {
                UnityEngine.Profiling.Profiler.EndSample();
                return;
            }

            var collProvider = OceanRenderer.Instance.CollisionProvider;
            var position     = transform.position;

            var normal = Vector3.up; var waterSurfaceVel = Vector3.zero;

            _sampleHeightHelper.Init(transform.position, _objectWidth, true);
            _sampleHeightHelper.Sample(ref _displacementToObject, ref normal, ref waterSurfaceVel);

            var undispPos = transform.position - _displacementToObject;

            undispPos.y = OceanRenderer.Instance.SeaLevel;

            if (_debugDraw)
            {
                VisualiseCollisionArea.DebugDrawCross(undispPos, 1f, Color.red);
            }

            {
                _sampleFlowHelper.Init(transform.position, ObjectWidth);

                Vector2 surfaceFlow = Vector2.zero;
                _sampleFlowHelper.Sample(ref surfaceFlow);
                waterSurfaceVel += new Vector3(surfaceFlow.x, 0, surfaceFlow.y);
            }

            if (_debugDraw)
            {
                Debug.DrawLine(transform.position + 5f * Vector3.up, transform.position + 5f * Vector3.up + waterSurfaceVel,
                               new Color(1, 1, 1, 0.6f));
            }

            var velocityRelativeToWater = _rb.velocity - waterSurfaceVel;

            var dispPos = undispPos + _displacementToObject;

            if (_debugDraw)
            {
                VisualiseCollisionArea.DebugDrawCross(dispPos, 4f, Color.white);
            }

            float height = dispPos.y;

            float bottomDepth = height - transform.position.y + _raiseObject;

            _inWater = bottomDepth > 0f;
            if (!_inWater)
            {
                UnityEngine.Profiling.Profiler.EndSample();
                return;
            }

            var buoyancy = -Physics.gravity.normalized * _buoyancyCoeff * bottomDepth * bottomDepth * bottomDepth;

            _rb.AddForce(buoyancy, ForceMode.Acceleration);


            // apply drag relative to water
            var forcePosition = _rb.position + _forceHeightOffset * Vector3.up;

            _rb.AddForceAtPosition(Vector3.up * Vector3.Dot(Vector3.up, -velocityRelativeToWater) * _dragInWaterUp, forcePosition, ForceMode.Acceleration);
            _rb.AddForceAtPosition(transform.right * Vector3.Dot(transform.right, -velocityRelativeToWater) * _dragInWaterRight, forcePosition, ForceMode.Acceleration);
            _rb.AddForceAtPosition(transform.forward * Vector3.Dot(transform.forward, -velocityRelativeToWater) * _dragInWaterForward, forcePosition, ForceMode.Acceleration);

            FixedUpdateOrientation(normal);

            UnityEngine.Profiling.Profiler.EndSample();
        }
Ejemplo n.º 3
0
        void FixedUpdate()
        {
            UnityEngine.Profiling.Profiler.BeginSample("SimpleFloatingObject.FixedUpdate");

            if (OceanRenderer.Instance == null)
            {
                UnityEngine.Profiling.Profiler.EndSample();
                return;
            }

            // Trigger processing of displacement textures that have come back this frame. This will be processed
            // anyway in Update(), but FixedUpdate() is earlier so make sure it's up to date now.
            if (OceanRenderer.Instance._simSettingsAnimatedWaves.CollisionSource == SimSettingsAnimatedWaves.CollisionSources.OceanDisplacementTexturesGPU && GPUReadbackDisps.Instance)
            {
                GPUReadbackDisps.Instance.ProcessRequests();
            }

            var collProvider = OceanRenderer.Instance.CollisionProvider;
            var position     = transform.position;

            var normal = Vector3.up; var waterSurfaceVel = Vector3.zero;

            _sampleHeightHelper.Init(transform.position, _objectWidth);
            _sampleHeightHelper.Sample(ref _displacementToObject, ref normal, ref waterSurfaceVel);

            var undispPos = transform.position - _displacementToObject;

            undispPos.y = OceanRenderer.Instance.SeaLevel;

            if (_debugDraw)
            {
                VisualiseCollisionArea.DebugDrawCross(undispPos, 1f, Color.red);
            }

            if (QueryFlow.Instance)
            {
                _sampleFlowHelper.Init(transform.position, ObjectWidth);

                Vector2 surfaceFlow = Vector2.zero;
                _sampleFlowHelper.Sample(ref surfaceFlow);
                waterSurfaceVel += new Vector3(surfaceFlow.x, 0, surfaceFlow.y);
            }

            if (_debugDraw)
            {
                Debug.DrawLine(transform.position + 5f * Vector3.up, transform.position + 5f * Vector3.up + waterSurfaceVel,
                               new Color(1, 1, 1, 0.6f));
            }

            var velocityRelativeToWater = _rb.velocity - waterSurfaceVel;

            var dispPos = undispPos + _displacementToObject;

            if (_debugDraw)
            {
                VisualiseCollisionArea.DebugDrawCross(dispPos, 4f, Color.white);
            }

            float height = dispPos.y;

            float bottomDepth = height - transform.position.y + _raiseObject;

            _inWater = bottomDepth > 0f;
            if (!_inWater)
            {
                UnityEngine.Profiling.Profiler.EndSample();
                return;
            }

            var buoyancy = -Physics.gravity.normalized * _buoyancyCoeff * bottomDepth * bottomDepth * bottomDepth;

            _rb.AddForce(buoyancy, ForceMode.Acceleration);


            // apply drag relative to water
            var forcePosition = _rb.position + _forceHeightOffset * Vector3.up;

            _rb.AddForceAtPosition(Vector3.up * Vector3.Dot(Vector3.up, -velocityRelativeToWater) * _dragInWaterUp, forcePosition, ForceMode.Acceleration);
            _rb.AddForceAtPosition(transform.right * Vector3.Dot(transform.right, -velocityRelativeToWater) * _dragInWaterRight, forcePosition, ForceMode.Acceleration);
            _rb.AddForceAtPosition(transform.forward * Vector3.Dot(transform.forward, -velocityRelativeToWater) * _dragInWaterForward, forcePosition, ForceMode.Acceleration);

            FixedUpdateOrientation(normal);

            UnityEngine.Profiling.Profiler.EndSample();
        }