Exemplo n.º 1
0
        public static void DisableCamCollision(TriangleClassification?classification = null)
        {
            List <uint> triangleAddresses = GetLevelTriangleAddresses();

            triangleAddresses.ForEach(address =>
            {
                float ynorm = Config.Stream.GetSingle(address + TriangleOffsetsConfig.NormY);
                TriangleClassification triClassification = CalculateClassification(ynorm);
                if (classification == null || classification == triClassification)
                {
                    ButtonUtilities.DisableCamCollisionForTriangle(address);
                }
            });
        }
Exemplo n.º 2
0
        public static void NeutralizeTriangles(TriangleClassification?classification = null)
        {
            List <uint> triangleAddresses = GetLevelTriangleAddresses();

            triangleAddresses.ForEach(address =>
            {
                float ynorm = Config.Stream.GetSingle(address + TriangleOffsetsConfig.NormY);
                TriangleClassification triClassification = CalculateClassification(ynorm);
                if (classification == null || classification == triClassification)
                {
                    ButtonUtilities.NeutralizeTriangle(new List <uint>()
                    {
                        address
                    });
                }
            });
        }
Exemplo n.º 3
0
        public static uint GetCurrentTriangle(TriangleClassification classification)
        {
            switch (classification)
            {
            case TriangleClassification.Wall:
                return(Config.Stream.GetUInt(MarioConfig.StructAddress + MarioConfig.WallTriangleOffset));

            case TriangleClassification.Floor:
                return(Config.Stream.GetUInt(MarioConfig.StructAddress + MarioConfig.FloorTriangleOffset));

            case TriangleClassification.Ceiling:
                return(Config.Stream.GetUInt(MarioConfig.StructAddress + MarioConfig.CeilingTriangleOffset));

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public override float GetSizeForOrthographicView(TriangleClassification classification)
        {
            switch (classification)
            {
            case TriangleClassification.Wall:
                return(50);

            case TriangleClassification.Floor:
                return(78);

            case TriangleClassification.Ceiling:
                return(160);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
        public override Color GetColorForOrthographicView(TriangleClassification classification)
        {
            switch (classification)
            {
            case TriangleClassification.Wall:
                return(Color.Green);

            case TriangleClassification.Floor:
                return(Color.Blue);

            case TriangleClassification.Ceiling:
                return(Color.Red);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 6
0
        public static double GetSignedDistanceFromPointToLine(
            double pX, double pZ, double v1X, double v1Z, double v2X, double v2Z, double v3X, double v3Z, int p1Index, int p2Index,
            TriangleClassification classification, bool?misalignmentOffsetNullable = null)
        {
            pX = PuUtilities.GetRelativeCoordinate(pX);
            pZ = PuUtilities.GetRelativeCoordinate(pZ);

            double[] vX = new double[] { v1X, v2X, v3X };
            double[] vZ = new double[] { v1Z, v2Z, v3Z };

            double p1X = vX[p1Index - 1];
            double p1Z = vZ[p1Index - 1];
            double p2X = vX[p2Index - 1];
            double p2Z = vZ[p2Index - 1];

            double dist                   = MoreMath.GetDistanceFromPointToLine(pX, pZ, p1X, p1Z, p2X, p2Z);
            bool   leftOfLine             = MoreMath.IsPointLeftOfLine(pX, pZ, p1X, p1Z, p2X, p2Z);
            bool   floorTri               = MoreMath.IsPointLeftOfLine(v3X, v3Z, v1X, v1Z, v2X, v2Z);
            bool   onSideOfLineTowardsTri = floorTri == leftOfLine;
            double signedDist             = dist * (onSideOfLineTowardsTri ? 1 : -1);

            bool misalignmentOffset = misalignmentOffsetNullable ?? SavedSettingsConfig.UseMisalignmentOffsetForDistanceToLine;

            if (misalignmentOffset && classification != TriangleClassification.Wall)
            {
                if (p1X == p2X)
                {
                    bool thirdPointOnLeft = p1Z >= p2Z == floorTri;
                    if ((thirdPointOnLeft && p1X >= 0) || (!thirdPointOnLeft && p1X <= 0))
                    {
                        signedDist += 1;
                    }
                }
                else if (p1Z == p2Z)
                {
                    bool thirdPointOnTop = p1X <= p2X == floorTri;
                    if ((thirdPointOnTop && p1Z >= 0) || (!thirdPointOnTop && p1Z <= 0))
                    {
                        signedDist += 1;
                    }
                }
            }

            return(signedDist);
        }
Exemplo n.º 7
0
        public TriangleListForm(
            MapLevelTriangleObjectI levelTriangleObject,
            TriangleClassification classification,
            List <uint> triAddressList)
        {
            InitializeComponent();

            _levelTriangleObject = levelTriangleObject;
            _triAddressList      = triAddressList;
            _lastRemoveTime      = 0;

            Text = classification + " Triangle List";
            labelNumTriangles.Text  = _triAddressList.Count + " Triangles";
            FormClosing            += (sender, e) => TriangleListFormClosing();
            buttonSort.Click       += (sender, e) => RefreshAndSort();
            buttonAnnihilate.Click += (sender, e) => Annihilate();
            buttonInject.Click     += (sender, e) => Inject();
            buttonRemove.Click     += (sender, e) => Remove();

            RefreshAndSort();
        }
Exemplo n.º 8
0
        public static List <uint> GetTriangleAddressesInCell(int cellX, int cellZ, bool staticPartition, TriangleClassification classification)
        {
            uint partitionAddress = staticPartition ? TriangleConfig.StaticTrianglePartitionAddress : TriangleConfig.DynamicTrianglePartitionAddress;
            int  typeInt;

            switch (classification)
            {
            case TriangleClassification.Wall:
                typeInt = 2;
                break;

            case TriangleClassification.Floor:
                typeInt = 0;
                break;

            case TriangleClassification.Ceiling:
                typeInt = 1;
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            int  typeSize = 2 * 4;
            int  xSize    = 3 * typeSize;
            int  zSize    = 16 * xSize;
            uint address  = (uint)(partitionAddress + cellZ * zSize + cellX * xSize + typeInt * typeSize);

            address = Config.Stream.GetUInt(address);

            List <uint> output = new List <uint>();

            while (address != 0)
            {
                uint triAddress = Config.Stream.GetUInt(address + 4);
                output.Add(triAddress);
                address = Config.Stream.GetUInt(address);
            }
            return(output);
        }
Exemplo n.º 9
0
 public static List <uint> GetTriangleAddressesInMarioCell(bool staticPartition, TriangleClassification classification)
 {
     (int cellX, int cellZ) = GetMarioCell();
     return(GetTriangleAddressesInCell(cellX, cellZ, staticPartition, classification));
 }
Exemplo n.º 10
0
 public static List <uint> GetTriangleAddressesForPosition(float x, float z, bool staticPartition, TriangleClassification classification)
 {
     (int cellX, int cellZ) = GetCell(x, z);
     return(GetTriangleAddressesInCell(cellX, cellZ, staticPartition, classification));
 }
Exemplo n.º 11
0
        public TriangleStruct(uint triangleAddress)
        {
            Address = triangleAddress;

            SurfaceType        = Config.Stream.GetUInt16(triangleAddress + TriangleOffsetsConfig.SurfaceType);
            ExertionForceIndex = Config.Stream.GetByte(triangleAddress + TriangleOffsetsConfig.ExertionForceIndex);
            ExertionAngle      = Config.Stream.GetByte(triangleAddress + TriangleOffsetsConfig.ExertionAngle);
            Flags = Config.Stream.GetByte(triangleAddress + TriangleOffsetsConfig.Flags);
            Room  = Config.Stream.GetByte(triangleAddress + TriangleOffsetsConfig.Room);

            YMin = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.YMin);
            YMax = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.YMax);

            X1 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.X1);
            Y1 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.Y1);
            Z1 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.Z1);
            X2 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.X2);
            Y2 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.Y2);
            Z2 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.Z2);
            X3 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.X3);
            Y3 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.Y3);
            Z3 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.Z3);

            NormX      = Config.Stream.GetSingle(triangleAddress + TriangleOffsetsConfig.NormX);
            NormY      = Config.Stream.GetSingle(triangleAddress + TriangleOffsetsConfig.NormY);
            NormZ      = Config.Stream.GetSingle(triangleAddress + TriangleOffsetsConfig.NormZ);
            NormOffset = Config.Stream.GetSingle(triangleAddress + TriangleOffsetsConfig.NormOffset);

            AssociatedObject = Config.Stream.GetUInt32(triangleAddress + TriangleOffsetsConfig.AssociatedObject);

            Classification = TriangleUtilities.CalculateClassification(NormY);

            XProjection     = (Flags & TriangleOffsetsConfig.XProjectionMask) != 0;
            BelongsToObject = (Flags & TriangleOffsetsConfig.BelongsToObjectMask) != 0;
            NoCamCollision  = (Flags & TriangleOffsetsConfig.NoCamCollisionMask) != 0;

            FieldValueList = new List <object> {
                "0x" + Address.ToString("X8"),
                Classification,
                SurfaceType,
                ExertionForceIndex,
                ExertionAngle,
                "0x" + Flags.ToString("X2"),
                XProjection,
                BelongsToObject,
                NoCamCollision,
                Room,
                YMin,
                YMax,
                X1,
                Y1,
                Z1,
                X2,
                Y2,
                Z2,
                X3,
                Y3,
                Z3,
                NormX,
                NormY,
                NormZ,
                NormOffset,
                "0x" + AssociatedObject.ToString("X8"),
            };
        }
Exemplo n.º 12
0
        private TriangleDataModel(uint triangleAddress)
        {
            Address = triangleAddress;

            SurfaceType        = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.SurfaceType);
            ExertionForceIndex = Config.Stream.GetByte(triangleAddress + TriangleOffsetsConfig.ExertionForceIndex);
            ExertionAngle      = Config.Stream.GetByte(triangleAddress + TriangleOffsetsConfig.ExertionAngle);
            Flags = Config.Stream.GetByte(triangleAddress + TriangleOffsetsConfig.Flags);
            Room  = Config.Stream.GetByte(triangleAddress + TriangleOffsetsConfig.Room);

            YMinMinus5 = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.YMinMinus5);
            YMaxPlus5  = Config.Stream.GetInt16(triangleAddress + TriangleOffsetsConfig.YMaxPlus5);

            X1 = TriangleOffsetsConfig.GetX1(triangleAddress);
            Y1 = TriangleOffsetsConfig.GetY1(triangleAddress);
            Z1 = TriangleOffsetsConfig.GetZ1(triangleAddress);
            X2 = TriangleOffsetsConfig.GetX2(triangleAddress);
            Y2 = TriangleOffsetsConfig.GetY2(triangleAddress);
            Z2 = TriangleOffsetsConfig.GetZ2(triangleAddress);
            X3 = TriangleOffsetsConfig.GetX3(triangleAddress);
            Y3 = TriangleOffsetsConfig.GetY3(triangleAddress);
            Z3 = TriangleOffsetsConfig.GetZ3(triangleAddress);

            NormX      = Config.Stream.GetSingle(triangleAddress + TriangleOffsetsConfig.NormX);
            NormY      = Config.Stream.GetSingle(triangleAddress + TriangleOffsetsConfig.NormY);
            NormZ      = Config.Stream.GetSingle(triangleAddress + TriangleOffsetsConfig.NormZ);
            NormOffset = Config.Stream.GetSingle(triangleAddress + TriangleOffsetsConfig.NormOffset);

            AssociatedObject = Config.Stream.GetUInt32(triangleAddress + TriangleOffsetsConfig.AssociatedObject);

            Classification = TriangleUtilities.CalculateClassification(NormY);

            XProjection     = (Flags & TriangleOffsetsConfig.XProjectionMask) != 0;
            BelongsToObject = (Flags & TriangleOffsetsConfig.BelongsToObjectMask) != 0;
            NoCamCollision  = (Flags & TriangleOffsetsConfig.NoCamCollisionMask) != 0;

            Description             = TableConfig.TriangleInfo.GetDescription(SurfaceType);
            Slipperiness            = TableConfig.TriangleInfo.GetSlipperiness(SurfaceType) ?? 0;
            SlipperinessDescription = TableConfig.TriangleInfo.GetSlipperinessDescription(SurfaceType);
            FrictionMultiplier      = TableConfig.TriangleInfo.GetFrictionMultiplier(SurfaceType);
            SlopeAccel      = TableConfig.TriangleInfo.GetSlopeAccel(SurfaceType);
            SlopeDecelValue = TableConfig.TriangleInfo.GetSlopeDecelValue(SurfaceType);
            Exertion        = TableConfig.TriangleInfo.GetExertion(SurfaceType) ?? false;

            FieldValueList = new List <object> {
                HexUtilities.FormatValue(Address, 8),
                Classification,
                HexUtilities.FormatValue(SurfaceType, 2),
                Description,
                HexUtilities.FormatValue(Slipperiness, 2),
                SlipperinessDescription,
                Exertion,
                ExertionForceIndex,
                ExertionAngle,
                HexUtilities.FormatValue(Flags, 2),
                XProjection,
                BelongsToObject,
                NoCamCollision,
                Room,
                YMinMinus5,
                YMaxPlus5,
                X1,
                Y1,
                Z1,
                X2,
                Y2,
                Z2,
                X3,
                Y3,
                Z3,
                NormX,
                NormY,
                NormZ,
                NormOffset,
                HexUtilities.FormatValue(AssociatedObject, 8),
            };
        }
Exemplo n.º 13
0
        void SliceMeshOnAxis(Mesh activeMesh, bool gizmosPass, SlicerAxisData activeAxisData)
        {
            Gizmos.matrix = transform.localToWorldMatrix;

            Vector3 axis = Vector3.zero;

            axis[activeAxisData.AxisIndex] = 1;

            Vector3[] vertices  = activeMesh.vertices;
            Vector2[] uv        = activeMesh.uv;
            Vector3[] normals   = activeMesh.normals;
            Vector4[] tangents  = activeMesh.tangents;
            int[]     triangles = activeMesh.triangles;
            // Second copy so that we can modify triangles buffer while iterating through it
            int[] newTriangles = activeMesh.triangles;

            List <NewTriangleWith1NewVertex> additionalTrianglesWith1NewVertices = new List <NewTriangleWith1NewVertex>();
            List <NewTriangleWith2NewVertex> additionalTrianglesWith2NewVertices = new List <NewTriangleWith2NewVertex>();

            NativeArray <float3> verticesNativeArray = new NativeArray <float3>(vertices.Length, Allocator.TempJob,
                                                                                NativeArrayOptions.UninitializedMemory);

            for (int i = 0; i < vertices.Length; i++)
            {
                verticesNativeArray[i] = vertices[i];
            }

            NativeArray <int>[] classificationArrays = new NativeArray <int> [2];

            // Loop through all the planes and classify the source vertices relative to each plane
            for (var planeIndex = 0; planeIndex < 2; planeIndex++)
            {
                Plane plane = activeAxisData.CalculatePlane(planeIndex);

                classificationArrays[planeIndex] = new NativeArray <int>(vertices.Length, Allocator.TempJob,
                                                                         NativeArrayOptions.UninitializedMemory);

                var jobData = new Classifier.ClassifyVerticesAgainstPlane()
                {
                    vertices             = verticesNativeArray,
                    classificationResult = classificationArrays[planeIndex],
                    planeDistance        = plane.distance,
                    planeNormal          = plane.normal
                };

                var handle = jobData.Schedule(vertices.Length, 128);
                handle.Complete(); // Block until all jobs are done
            }

            for (var planeIndex = 0; planeIndex < 2; planeIndex++)
            {
                Plane plane = activeAxisData.CalculatePlane(planeIndex);

                var classificationArray = classificationArrays[planeIndex];
                for (int i = 0; i < triangles.Length / 3; i++)
                {
                    int index1 = triangles[i * 3 + 0];
                    int index2 = triangles[i * 3 + 1];
                    int index3 = triangles[i * 3 + 2];

                    TriangleClassification triangleClassification =
                        Classifier.ClassifyTriangle(index1, index2, index3, classificationArray);

                    if (triangleClassification == TriangleClassification.Straddle)
                    {
                        int classificationSum = (classificationArray[index1] + classificationArray[index2] +
                                                 classificationArray[index3]);

                        if (classificationSum == 1 + 1 - 1 || // Two in front
                            classificationSum == 1 - 1 - 1)    // Two behind
                        {
                            // Which out which vertex is isolated at the back of the plane
                            int isolatedIndex = -1;
                            for (int j = 0; j < 3; j++)
                            {
                                if (classificationArray[triangles[i * 3 + j]] != classificationSum)
                                {
                                    isolatedIndex = j;
                                    break;
                                }
                            }

                            Debug.Assert(isolatedIndex >= 0 && isolatedIndex <= 2);

                            int indexA = (isolatedIndex + 1) % 3;
                            int indexB = (isolatedIndex + 2) % 3;

                            // Categorise the three points in the triangle, as the one isolated behind the plane, then the other two in winding order
                            Vector3 isolatedPoint = vertices[triangles[i * 3 + isolatedIndex]];
                            Vector3 pointA        = vertices[triangles[i * 3 + indexA]];
                            Vector3 pointB        = vertices[triangles[i * 3 + indexB]];

                            // Calculate the normalized intersection along each edge from the isolated point
                            float interpolantA = plane.GetPlaneIntersectionInterpolant(isolatedPoint, pointA);
                            float interpolantB = plane.GetPlaneIntersectionInterpolant(isolatedPoint, pointB);

                            Vector3 newPointA = Vector3.Lerp(isolatedPoint, pointA, interpolantA);
                            Vector3 newPointB = Vector3.Lerp(isolatedPoint, pointB, interpolantB);

                            Vector3 newUVA = Vector2.Lerp(uv[triangles[i * 3 + isolatedIndex]],
                                                          uv[triangles[i * 3 + indexA]], interpolantA);
                            Vector3 newUVB = Vector2.Lerp(uv[triangles[i * 3 + isolatedIndex]],
                                                          uv[triangles[i * 3 + indexB]], interpolantB);

                            Vector3 newNormalA = Vector3.Lerp(normals[triangles[i * 3 + isolatedIndex]],
                                                              normals[triangles[i * 3 + indexA]], interpolantA);
                            Vector3 newNormalB = Vector3.Lerp(normals[triangles[i * 3 + isolatedIndex]],
                                                              normals[triangles[i * 3 + indexB]], interpolantB);

                            Vector3 newTangentA = Vector3.Lerp(tangents[triangles[i * 3 + isolatedIndex]],
                                                               tangents[triangles[i * 3 + indexA]], interpolantA);
                            Vector3 newTangentB = Vector3.Lerp(tangents[triangles[i * 3 + isolatedIndex]],
                                                               tangents[triangles[i * 3 + indexB]], interpolantB);

#if SHOW_DEBUG_CLIPPING
                            // ORIGINAL TRIANGLE
                            if (gizmosPass)
                            {
                                Vector3 point1 = vertices[index1];
                                Vector3 point2 = vertices[index2];
                                Vector3 point3 = vertices[index3];
                                Gizmos.color = Color.green;
                                GizmoHelper.DrawTriangle(point1, point2, point3);
                            }
#endif

                            Vector3 transformedOffset = activeAxisData.GetTransformedOffset(planeIndex);

                            // NEW CLIPPED TRIANGLE
                            if (classificationSum == 1 + 1 - 1 || // Two in front
                                classificationSum == 1 - 1 - 1)    // Two behind
                            {
                                additionalTrianglesWith1NewVertices.Add(new NewTriangleWith1NewVertex()
                                {
                                    ExistingIndex1 = triangles[i * 3 + indexA],
                                    ExistingIndex2 = triangles[i * 3 + indexB],

                                    NewVertexPosition1 = newPointA + transformedOffset,
                                    NewVertexUV1       = newUVA,
                                    NewVertexNormal1   = newNormalA,
                                    NewVertexTangent1  = newTangentA,

                                    Flipped = false,
                                });

                                // Add other triangle
                                additionalTrianglesWith2NewVertices.Add(new NewTriangleWith2NewVertex()
                                {
                                    ExistingIndex1 = triangles[i * 3 + indexB],

                                    NewVertexPosition1 = newPointA + transformedOffset,
                                    NewVertexUV1       = newUVA,
                                    NewVertexNormal1   = newNormalA,
                                    NewVertexTangent1  = newTangentA,

                                    NewVertexPosition2 = newPointB + transformedOffset,
                                    NewVertexUV2       = newUVB,
                                    NewVertexNormal2   = newNormalB,
                                    NewVertexTangent2  = newTangentB,

                                    Flipped = false,
                                });

#if SHOW_DEBUG_CLIPPING
                                if (gizmosPass)
                                {
                                    Gizmos.color = Color.white;
                                    GizmoHelper.DrawTriangle(pointB, pointA, newPointA);
                                    GizmoHelper.DrawTriangle(pointB, newPointA, newPointB);
                                }
#endif
                            }

                            if (classificationSum == 1 + 1 - 1 || // Two in front
                                classificationSum == 1 - 1 - 1)    // Two behind
                            {
                                additionalTrianglesWith2NewVertices.Add(new NewTriangleWith2NewVertex()
                                {
                                    ExistingIndex1 = triangles[i * 3 + isolatedIndex],

                                    NewVertexPosition1 = newPointA + transformedOffset,
                                    NewVertexUV1       = newUVA,
                                    NewVertexNormal1   = newNormalA,
                                    NewVertexTangent1  = newTangentA,

                                    NewVertexPosition2 = newPointB + transformedOffset,
                                    NewVertexUV2       = newUVB,
                                    NewVertexNormal2   = newNormalB,
                                    NewVertexTangent2  = newTangentB,

                                    Flipped = true,
                                });
                            }

                            // DRAW SPLIT LINE
//                            if (gizmosPass)
//                            {
//                                // Transformed
//                                Gizmos.color = Color.blue;
//                                Gizmos.DrawLine(newPointA + transformedOffset, newPointB + transformedOffset);
//                            }
                        }
                        else
                        {
                            throw new NotSupportedException("Unexpected and unhandled classification sum");
                        }
                    }

                    if (triangleClassification == TriangleClassification.Straddle)
                    {
                        // Zero out the indices for the straddling triangles as they are being replaced with new triangles
                        // Note: This is wasteful and in the future we should reuse these indices for one of the new triangles
                        newTriangles[i * 3 + 0] = 0;
                        newTriangles[i * 3 + 1] = 0;
                        newTriangles[i * 3 + 2] = 0;
                    }
                }
            }

            float sourceInset = sourceMesh.bounds.size[activeAxisData.AxisIndex] *
                                (activeAxisData.TotalInsetProportion);
            float scale = (size[activeAxisData.AxisIndex] - sourceInset) /
                          (sourceMesh.bounds.size[activeAxisData.AxisIndex] - sourceInset);
            Vector3 scaleOffset = axis * (activeAxisData.GetInset(0) *
                                          sourceMesh.bounds.size[activeAxisData.AxisIndex] - (activeAxisData.GetInset(1)) *
                                          sourceMesh.bounds.size[activeAxisData.AxisIndex]) / 2f
                                  + sourceMesh.bounds.center;

            for (int v = 0; v < vertices.Length; v++)
            {
                bool allBehind = true;
                for (var planeIndex = 0; planeIndex < 2; planeIndex++)
                {
                    if (classificationArrays[planeIndex][v] == -1)
                    {
                        vertices[v] += activeAxisData.GetTransformedOffset(planeIndex);
                        allBehind    = false;
                    }
                }

                if (allBehind)
                {
                    var vertex = vertices[v];

                    //scaleOffset = activeAxisData.GetInset(2) - activeAxisData.GetInset(1);
                    vertex -= scaleOffset;
                    vertex[activeAxisData.AxisIndex] *= scale;
                    vertex     += scaleOffset;
                    vertices[v] = vertex;
                }
            }

            int baseTriangleCount = triangles.Length; // Before additional triangles start getting taken into account
            int baseVertexCount   = vertices.Length;  // Before additional triangles start getting taken into account

            int additionalTriangleCount =
                additionalTrianglesWith1NewVertices.Count + additionalTrianglesWith2NewVertices.Count;

            // Resize the triangle indices to accomodate the additional triangles we're adding
            Array.Resize(ref newTriangles, baseTriangleCount + (additionalTriangleCount * 3));

            // Resize the vertex attribute buffers to accomodate the additional triangles we're adding
            Array.Resize(ref vertices,
                         baseVertexCount + additionalTrianglesWith1NewVertices.Count +
                         additionalTrianglesWith2NewVertices.Count * 2);
            Array.Resize(ref uv,
                         baseVertexCount + additionalTrianglesWith1NewVertices.Count +
                         additionalTrianglesWith2NewVertices.Count * 2);
            Array.Resize(ref normals,
                         baseVertexCount + additionalTrianglesWith1NewVertices.Count +
                         additionalTrianglesWith2NewVertices.Count * 2);
            Array.Resize(ref tangents,
                         baseVertexCount + additionalTrianglesWith1NewVertices.Count +
                         additionalTrianglesWith2NewVertices.Count * 2);

            // Add in new triangles with one new vertex
            for (var index = 0; index < additionalTrianglesWith1NewVertices.Count; index++)
            {
                NewTriangleWith1NewVertex newTriangleWith1NewVertex = additionalTrianglesWith1NewVertices[index];

                vertices[baseVertexCount + index] = newTriangleWith1NewVertex.NewVertexPosition1;
                uv[baseVertexCount + index]       = newTriangleWith1NewVertex.NewVertexUV1;
                normals[baseVertexCount + index]  = newTriangleWith1NewVertex.NewVertexNormal1;
                tangents[baseVertexCount + index] = newTriangleWith1NewVertex.NewVertexTangent1;

                if (newTriangleWith1NewVertex.Flipped)
                {
                    newTriangles[baseTriangleCount + index * 3 + 2] = newTriangleWith1NewVertex.ExistingIndex1;
                    newTriangles[baseTriangleCount + index * 3 + 1] = newTriangleWith1NewVertex.ExistingIndex2;
                    newTriangles[baseTriangleCount + index * 3 + 0] = baseVertexCount + index;
                }
                else
                {
                    newTriangles[baseTriangleCount + index * 3 + 0] = newTriangleWith1NewVertex.ExistingIndex1;
                    newTriangles[baseTriangleCount + index * 3 + 1] = newTriangleWith1NewVertex.ExistingIndex2;
                    newTriangles[baseTriangleCount + index * 3 + 2] = baseVertexCount + index;
                }
            }

            int offset = additionalTrianglesWith1NewVertices.Count; // Offset to accomodate the new triangles we added

            // Add in new triangles with two new vertices
            for (var index = 0; index < additionalTrianglesWith2NewVertices.Count; index++)
            {
                NewTriangleWith2NewVertex newTriangleWith2NewVertex = additionalTrianglesWith2NewVertices[index];

                vertices[baseVertexCount + offset + index * 2 + 0] = newTriangleWith2NewVertex.NewVertexPosition1;
                uv[baseVertexCount + offset + index * 2 + 0]       = newTriangleWith2NewVertex.NewVertexUV1;
                normals[baseVertexCount + offset + index * 2 + 0]  = newTriangleWith2NewVertex.NewVertexNormal1;
                tangents[baseVertexCount + offset + index * 2 + 0] = newTriangleWith2NewVertex.NewVertexTangent1;

                vertices[baseVertexCount + offset + index * 2 + 1] = newTriangleWith2NewVertex.NewVertexPosition2;
                uv[baseVertexCount + offset + index * 2 + 1]       = newTriangleWith2NewVertex.NewVertexUV2;
                normals[baseVertexCount + offset + index * 2 + 1]  = newTriangleWith2NewVertex.NewVertexNormal2;
                tangents[baseVertexCount + offset + index * 2 + 1] = newTriangleWith2NewVertex.NewVertexTangent2;

                if (newTriangleWith2NewVertex.Flipped)
                {
                    newTriangles[baseTriangleCount + offset * 3 + index * 3 + 0] =
                        newTriangleWith2NewVertex.ExistingIndex1;
                    newTriangles[baseTriangleCount + offset * 3 + index * 3 + 1] =
                        baseVertexCount + offset + index * 2 + 0;
                    newTriangles[baseTriangleCount + offset * 3 + index * 3 + 2] =
                        baseVertexCount + offset + index * 2 + 1;
                }
                else
                {
                    newTriangles[baseTriangleCount + offset * 3 + index * 3 + 2] =
                        newTriangleWith2NewVertex.ExistingIndex1;
                    newTriangles[baseTriangleCount + offset * 3 + index * 3 + 1] =
                        baseVertexCount + offset + index * 2 + 0;
                    newTriangles[baseTriangleCount + offset * 3 + index * 3 + 0] =
                        baseVertexCount + offset + index * 2 + 1;
                }
            }

            activeMesh.vertices  = vertices;
            activeMesh.uv        = uv;
            activeMesh.normals   = normals;
            activeMesh.tangents  = tangents;
            activeMesh.triangles = newTriangles;

            // Cleanup the native arrays
            verticesNativeArray.Dispose();

            for (var planeIndex = 0; planeIndex < 2; planeIndex++)
            {
                classificationArrays[planeIndex].Dispose();
            }
        }
Exemplo n.º 14
0
 public void Setup()
 {
     triangle = new TriangleClassification();
 }