public void GenerateUnderwaterMesh() { //Reset underWaterTriangleData.Clear(); //Find all the distances to water once because some triangles share vertices, so reuse for (int j = 0; j < _floatObjVertices.Length; j++) { //The coordinate should be in global position Vector3 globalPos = _floatObjTrans.TransformPoint(_floatObjVertices[j]); //Save the global position so we only need to calculate it once here //And if we want to debug we can convert it back to local globalFloatObjVertices[j] = globalPos; _allDistancesToWater[j] = FloatHelper.DistanceToWater(globalPos, _waterHeight); } //Add the triangles that are below the water AddTriangles(); }
public TriangleData(Vector3 p1, Vector3 p2, Vector3 p3, float pwaterHeight) { this.p1 = p1; this.p2 = p2; this.p3 = p3; //Center of the triangle this.center = (p1 + p2 + p3) / 3f; //Distance to the surface from the center of the triangle this.distanceToSurface = Mathf.Abs(FloatHelper.DistanceToWater(this.center, pwaterHeight)); //Normal to the triangle this.normal = Vector3.Cross(p2 - p1, p3 - p1).normalized; //Area of the triangle float a = Vector3.Distance(p1, p2); float c = Vector3.Distance(p3, p1); this.area = (a * c * Mathf.Sin(Vector3.Angle(p2 - p1, p3 - p1) * Mathf.Deg2Rad)) / 2f; }