public TriangleBuffer(Vector3 v1, Vector3 v2, Vector3 v3, Rigidbody rB)
        {
            Vector3 center = (v1 + v2 + v3) / 3f;

            this.velocity      = WaterInteractionUtils.GetTriangleVelocity(center, rB);
            this.submergedArea = 0f;
            this.originalArea  = WaterInteractionUtils.GetTriangleArea(v1, v2, v3);
        }
        public TriangleInfo(VertexInfo vi1, VertexInfo vi2, VertexInfo vi3, int triangleType, Rigidbody rB, float timeStamp)
        {
            Vector3 v1 = vi1.globalVertex;
            Vector3 v2 = vi2.globalVertex;
            Vector3 v3 = vi3.globalVertex;

            this.center = WaterInteractionUtils.GetTriangleCenter(vi1, vi2, vi3, triangleType);

            // beause of simplification. center distance can be positive in submerged triangle
            // if height is positive, don't add to force
            this.height   = WaterPatch.instance.DistanceToWater(this.center, timeStamp);
            this.normal   = Vector3.Cross(v2 - v1, v3 - v1).normalized;
            this.velocity = WaterInteractionUtils.GetTriangleVelocity(this.center, rB);
            if (this.velocity.magnitude < 0.0001f)
            {
                this.cosine = 0;
            }
            else
            {
                this.cosine = Vector3.Dot(this.velocity, this.normal) / this.velocity.magnitude;
            }
            this.area = WaterInteractionUtils.GetTriangleArea(v1, v2, v3);
        }
Exemple #3
0
        private void OnEnable()
        {
            timeStamp             = Time.time;
            SubmergedTriangles    = new List <TriangleInfo>();
            TriangleBufferIndices = new List <int>();

            meshVertices  = Mesh.vertices;
            meshTriangles = Mesh.triangles;

            // Count of triangles is always multiple of 3
            int triangleCount = meshTriangles.Length / 3;

            PreviousBuffers = new TriangleBuffer[triangleCount];
            CurrentBuffers  = new TriangleBuffer[triangleCount];

            TotalArea = 0f;
            for (int triangleIndex = 0; triangleIndex < triangleCount; ++triangleIndex)
            {
                TotalArea += WaterInteractionUtils.GetTriangleArea(
                    transform.TransformPoint(meshVertices[meshTriangles[triangleIndex * 3]]),
                    transform.TransformPoint(meshVertices[meshTriangles[triangleIndex * 3 + 1]]),
                    transform.TransformPoint(meshVertices[meshTriangles[triangleIndex * 3 + 2]]));
            }
        }
Exemple #4
0
        private void CalculateForce()
        {
            float zMin = float.MaxValue;
            float zMax = float.MinValue;

            for (int i = 0; i < triangleBuilder.SubmergedTriangles.Count; ++i)
            {
                TriangleInfo triangle = triangleBuilder.SubmergedTriangles[i];
                if (triangle.center.z < zMin)
                {
                    zMin = triangle.center.z;
                }
                if (triangle.center.z > zMax)
                {
                    zMax = triangle.center.z;
                }
            }

            float logRigidbodyVelocity    = Mathf.Log10(Rigidbody3d.velocity.magnitude);
            float travelLengthScaleFactor = Mathf.Log10(zMax - zMin) * 2;

            for (int i = 0; i < triangleBuilder.SubmergedTriangles.Count; ++i)
            {
                TriangleInfo triangle = triangleBuilder.SubmergedTriangles[i];
                if (triangle.height > 0f)
                {
                    continue;
                }

                float lookUpKey            = (triangle.center.z - zMin) / (zMax - zMin);
                float logFluidTravelLength = curveLookUpTable.LookUp(lookUpKey) * travelLengthScaleFactor;
                float viscosityCoefficient = WaterInteractionUtils.CalculateViscosityCoefficient(logRigidbodyVelocity, logFluidTravelLength, logViscosity);

                int TriangleBufferIndex = triangleBuilder.TriangleBufferIndices[i];

                Vector3 buoyancyForce          = WaterInteractionUtils.BuoyancyForce(densityOfWater, triangle);
                Vector3 viscousWaterResistance = WaterInteractionUtils.ViscousWaterResistance(densityOfWater, viscosityCoefficient, triangle);
                Vector3 pressureDragForce      = WaterInteractionUtils.PressureDragForce(triangle, referenceSpeed, pdCoefficient1, pdCoefficient2, pdFallOffPower, sdCoefficient1, sdCoefficient2, sdFallOffPower);
                Vector3 slammingForce          = WaterInteractionUtils.SlammingForce(triangleBuilder.PreviousBuffers[TriangleBufferIndex], triangleBuilder.CurrentBuffers[TriangleBufferIndex], triangle, maxAcceleration, smRampUpPower, Rigidbody3d.mass, TotalArea);

                Vector3 netForce = Vector3.zero;
                if (debugBuoyancyForce)
                {
                    netForce += buoyancyForce;
                }
                if (debugViscousWaterResistance)
                {
                    netForce += viscousWaterResistance;
                }
                if (debugPressureDrag)
                {
                    netForce += pressureDragForce;
                }
                if (debugSlammingForce)
                {
                    netForce += slammingForce;
                }

                Rigidbody3d.AddForceAtPosition(netForce, triangle.center);

                // Debug.DrawRay(triangle.center, slammingForce, Color.white);
                // Debug.DrawRay(triangle.center, triangle.normal, Color.white);
                // Debug.DrawRay(triangle.center, buoyancyForce, Color.blue);
                // Debug.DrawRay(triangle.center, viscousWaterResistance, Color.green);
            }
        }