public int UpdateSides(OutputMesh builder, CPNPolygon polygon, int trPosition,
                               int innerVerticesPosition)
        {
            CPNSideEdge[] polylines = polygon.sideEdges;

            first[0] = innerVerticesPosition;
            first[1] = innerVerticesPosition + (MH - 2);
            first[2] = innerVerticesPosition + (MV - 1) * (MH - 1) - 1;
            first[3] = innerVerticesPosition + (MV - 2) * (MH - 1);

            move[0] = 1;
            move[1] = (MH - 1);
            move[2] = -1;
            move[3] = -(MH - 1);

            count[0] = MH - 1;
            count[1] = MV - 1;
            count[2] = MH - 1;
            count[3] = MV - 1;

            for (int i = 0; i < 4; i++)
            {
                if (polylines[i].GetN() > 1)
                {
                    LinearMeshIndicesArray          lmi = new LinearMeshIndicesArray(first[i], move[i], count[i], builder);
                    NetPolylineInternalIndicesArray npi = new NetPolylineInternalIndicesArray(polylines[i], builder);
                    trPosition = MeshStructures.CreateSideTriangles(builder, lmi, npi, trPosition);
                }
                else
                {
                    LinearMeshIndicesArray  lmi = new LinearMeshIndicesArray(first[i], move[i], count[i], builder);
                    NetPolylineIndicesArray npi = new NetPolylineIndicesArray(polylines[i], builder);
                    trPosition = MeshStructures.CreateSideTriangles(builder, lmi, npi, trPosition);
                }

                int prev = i == 0 ? 3 : i - 1;
                if (polylines[i].GetN() > 1 && polylines[prev].GetN() > 1)
                {
                    trPosition = builder.WriteQuad(trPosition, polylines[i].GetIndex(0), polylines[i].GetIndex(1), first[i],
                                                   polylines[prev].GetBackIndex(1));
                }
                else if (polylines[i].GetN() > 1)
                {
                    trPosition = builder.WriteTriangle(trPosition, polylines[i].GetIndex(0), polylines[i].GetIndex(1),
                                                       first[i]);
                }
                else if (polylines[prev].GetN() > 1)
                {
                    trPosition = builder.WriteTriangle(trPosition, polylines[i].GetIndex(0), first[i],
                                                       polylines[prev].GetBackIndex(1));
                }

                // System.err.println("trPosition on sides " + trPosition);
            }

            return(trPosition);
        }
Пример #2
0
        int WritePolygons(OutputMesh mesh, int position)
        {
            int geometriesCount = this.curvedPolygonsNet.GetGeometriesCount();

            CPNPolygon[][] polygons                = tessellationOutput.GetPolygons();
            int[][]        polygonsProfile         = tessellationOutput.GetPolygonsProfile();
            int[][]        polygonsVerticesProfile = tessellationOutput.GetPolygonsVerticesProfile();

            for (int k = 0; k < geometriesCount; k++)
            {
                mesh.SetGeometry(k);
                CPNGeometry geometry = this.curvedPolygonsNet.GetGeometries()[k];

                int geomPolygonsCount = subSet == null?geometry.GetPolygonsCount() : subSet.polygons[k].Length;

                for (int i = 0; i < geomPolygonsCount; i++)
                {
                    int        index = subSet == null ? i : subSet.polygons[k][i];
                    int        countEffectiveSize = polygons[k][index].sideEdges.Length;
                    CPNPolygon polygonData        = polygons[k][index];

                    if (countEffectiveSize > 2 && countEffectiveSize < TESSELLATION_PROCESS_NET_INTERPOLATORS &&
                        !polygonData.skip)
                    {
                        polygonData.computeSideEdgesSizes();
                        ICPNetInterpolator netInterpolator = manager.GetSchema(polygonData.schemaIndex).
                                                             interpolators[countEffectiveSize];

                        netInterpolator.UdpdateContent(mesh, polygonData, polygonsVerticesProfile[k][index],
                                                       polygonsProfile[k][index]);
                    }
                }
                position += polygonsVerticesProfile[k][geometry.GetPolygonsCount()];

                int     triangleIndex  = polygonsProfile[k][geometry.GetPolygonsCount()];
                int     trianglesCount = geometry.GetTrianglesCount();
                short[] triangles      = geometry.GetTriangles();
                for (int i = 0; i < trianglesCount; i++)
                {
                    mesh.WriteTriangle(triangleIndex, triangles[3 * i], triangles[3 * i + 1], triangles[3 * i + 2]);
                    triangleIndex++;
                }
                int     quadsCount = geometry.GetQuadsCount();
                short[] quads      = geometry.GetQuads();
                for (int i = 0; i < quadsCount; i++)
                {
                    mesh.WriteQuad(triangleIndex, quads[3 * i], quads[3 * i + 1], quads[3 * i + 2], quads[3 * i + 3]);
                    triangleIndex += 2;
                }
            }

            return(position);
        }
Пример #3
0
        public static int CreateSideTriangles2(OutputMesh builder,
                                               IMeshIndicesArray inside, IMeshIndicesArray outside,
                                               int intTrIndex)
        {
            Vector3[] vs = builder.GetVertices();

            int   outerEdgeLength = outside.Count();
            int   innerEdgeLength = inside.Count();
            int   outerIndex      = 0;
            int   innerIndex      = 0;
            float dtInner         = innerEdgeLength > 1 ? 1.0f / (innerEdgeLength - 1) : 1;
            float dtOuter         = outerEdgeLength > 1 ? 1.0f / (outerEdgeLength - 1) : 1;

            float kIn  = 0;
            float kOut = 1;

            if (outerEdgeLength > 1 && innerEdgeLength > 1)
            {
                Vector3 A = vs[outside.GetIndex()];
                Vector3 B = vs[outside.GetNext()];
                Vector3 C = vs[inside.GetIndex()];
                Vector3 D = vs[outside.GetAtIndex(outerEdgeLength - 2)];
                Vector3 E = vs[outside.GetAtIndex(outerEdgeLength - 1)];
                Vector3 F = vs[inside.GetAtIndex(innerEdgeLength - 1)];

                kIn  = Vector3.Dot(C - A, B - A) / Vector3.Dot(B - A, B - A);
                kOut = Vector3.Dot(F - D, E - D) / Vector3.Dot(E - D, E - D);

                float rect = 1.0f / (outerEdgeLength - 1);
                kIn  = kIn * rect;
                kOut = 1 - (1 - kOut) * rect;
            }

            while (innerIndex < innerEdgeLength - 1 || outerIndex < outerEdgeLength - 1)
            {
                if (innerIndex < innerEdgeLength - 1)
                {
                    if (outerIndex < outerEdgeLength - 1)
                    {
                        int id1 = outside.GetIndex();
                        int id2 = inside.GetIndex();
                        int id3 = outside.GetNext();
                        int id4 = inside.GetNext();

                        float tOuter = dtOuter * (outerIndex);
                        float tInner = dtInner * (innerIndex) * (kOut - kIn) + kIn;

                        if (tInner <= tOuter)
                        {
                            intTrIndex = builder.WriteTriangle(intTrIndex, id1, id4, id2);
                            innerIndex++;
                            inside.Move();
                        }
                        else
                        {
                            intTrIndex = builder.WriteTriangle(intTrIndex, id1, id3, id2);
                            outerIndex++;
                            outside.Move();
                        }
                    }
                    else
                    {
                        int id1 = outside.GetIndex();
                        int id2 = inside.GetIndex();
                        int id4 = inside.GetNext();

                        intTrIndex = builder.WriteTriangle(intTrIndex, id1, id4, id2);
                        innerIndex++;
                        inside.Move();
                    }
                }
                else
                {
                    int id1 = outside.GetIndex();
                    int id2 = inside.GetIndex();
                    int id3 = outside.GetNext();
                    intTrIndex = builder.WriteTriangle(
                        intTrIndex, id1, id3, id2);
                    outerIndex++;
                    outside.Move();
                }
            }
            return(intTrIndex);
        }
Пример #4
0
        public static int CreateSideTriangles(OutputMesh builder,
                                              IMeshIndicesArray inside, IMeshIndicesArray outside,
                                              int intTrIndex)
        {
            /*Vector3[] vs = builder.GetVertices();
             * int outerEdgeLength = outside.Count();
             * int innerEdgeLength = inside.Count();
             * int outerIndex = 0;
             * int innerIndex = 0;
             *
             * while (innerIndex < innerEdgeLength - 1 || outerIndex < outerEdgeLength - 1)
             * {
             *  if (innerIndex < innerEdgeLength - 1)
             *  {
             *      if (outerIndex < outerEdgeLength - 1)
             *      {
             *          int id1 = outerIndex;
             *          int id2 = innerIndex;
             *          int id3 = outside.GetNext();
             *          int id4 = inside.GetNext();
             *
             *          Vector3 P1 = vs[id1];
             *          Vector3 P2 = vs[id2];
             *          Vector3 P3 = vs[id3];
             *          Vector3 P4 = vs[id4];
             *
             *          float dInner = (P4 - P1).sqrMagnitude;
             *          float dOuter = (P3 - P2).sqrMagnitude;
             *
             *          if (dInner < dOuter) {
             *              intTrIndex = builder.WriteTriangle(intTrIndex, id1, id4, id2);
             *              innerIndex++;
             *              inside.Move();
             *          } else {
             *              intTrIndex = builder.WriteTriangle(intTrIndex, id1, id3, id2);
             *              outerIndex++;
             *              outside.Move();
             *
             *          }
             *      }
             *      else
             *      {
             *
             *          int id1 = outerIndex;
             *          int id2 = innerIndex;
             *          int id4 = inside.GetNext();
             *
             *          intTrIndex = builder.WriteTriangle(intTrIndex, id1, id4, id2);
             *          innerIndex++;
             *          inside.Move();
             *      }
             *  }
             *  else
             *  {
             *      int id1 = outerIndex;
             *      int id2 = innerIndex;
             *      int id3 = outside.GetNext();
             *      intTrIndex = builder.WriteTriangle(
             *          intTrIndex, id1, id3, id2);
             *      outerIndex++;
             *      outside.Move();
             *  }
             * }
             *
             * return intTrIndex;*/


            Vector3[] vs = builder.GetVertices();

            int outerEdgeLength = outside.Count();
            int innerEdgeLength = inside.Count();
            int outerIndex      = 0;
            int innerIndex      = 0;

            while (innerIndex < innerEdgeLength - 1 || outerIndex < outerEdgeLength - 1)
            {
                if (innerIndex < innerEdgeLength - 1)
                {
                    if (outerIndex < outerEdgeLength - 1)
                    {
                        int id1 = outside.GetIndex();
                        int id2 = inside.GetIndex();
                        int id3 = outside.GetNext();
                        int id4 = inside.GetNext();

                        Vector3 P1 = vs[id1];
                        Vector3 P2 = vs[id2];
                        Vector3 P3 = vs[id3];
                        Vector3 P4 = vs[id4];

                        float dInner = (P4 - P1).sqrMagnitude;
                        float dOuter = (P3 - P2).sqrMagnitude;

                        if (dInner <= dOuter)
                        {
                            intTrIndex = builder.WriteTriangle(intTrIndex, id1, id4, id2);
                            innerIndex++;
                            inside.Move();
                        }
                        else
                        {
                            intTrIndex = builder.WriteTriangle(intTrIndex, id1, id3, id2);
                            outerIndex++;
                            outside.Move();
                        }
                    }
                    else
                    {
                        int id1 = outside.GetIndex();
                        int id2 = inside.GetIndex();
                        int id4 = inside.GetNext();

                        intTrIndex = builder.WriteTriangle(intTrIndex, id1, id4, id2);
                        innerIndex++;
                        inside.Move();
                    }
                }
                else
                {
                    int id1 = outside.GetIndex();
                    int id2 = inside.GetIndex();
                    int id3 = outside.GetNext();
                    intTrIndex = builder.WriteTriangle(
                        intTrIndex, id1, id3, id2);
                    outerIndex++;
                    outside.Move();
                }
            }
            return(intTrIndex);
        }
        public void CreateTriangleTessellation(OutputMesh mesh, int internalsIndex, int facesIndex,
                                               CPNPolygon polygon)
        {
            // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
            // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
            // internalsIndex:%d facesIndex:%d", internalsIndex, facesIndex);
            // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR

            CPNSideEdge[] polylines = polygon.sideEdges;
            RetrieveInfos(polygon);

            int trPosition = facesIndex;

            if (M == 1)
            {
                trPosition = mesh.WriteTriangle(trPosition, polylines[0].GetIndex(0), polylines[1].GetIndex(0),
                                                polylines[2].GetIndex(0));
                return;
            }
            else if (M == 2)
            {
                // int[] MS=new int[3];
                MS[0] = polylines[0].GetN();
                MS[1] = polylines[1].GetN();
                MS[2] = polylines[2].GetN();
                for (int i = 0; i < 3; i++)
                {
                    int prev = i == 0 ? 2 : i - 1;
                    if (MS[prev] == 2)
                    {
                        // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                        // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                        // M == 2 i:%d trPosition:%d writing CASE 1",i,trPosition);
                        // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                        trPosition = mesh.WriteTriangle(trPosition,
                                                        polylines[i].GetIndex(0), polylines[i].GetIndex(1),
                                                        polylines[prev].GetBackIndex(1));
                    }
                    else if (MS[i] == 2)
                    {
                        int other = (i == 2 ? 0 : i + 1);
                        if (MS[other] == 2)
                        {
                            // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                            // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                            // M == 2 i:%d trPosition:%d writing CASE 2", i,
                            // trPosition);
                            // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                            trPosition = mesh.WriteTriangle(trPosition, polylines[i].GetIndex(0), polylines[i].GetIndex(1),
                                                            polylines[other].GetIndex(1));
                        }
                        else
                        {
                            // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                            // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                            // M == 2 i:%d trPosition:%d writing CASE 3", i,
                            // trPosition);
                            // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                            trPosition = mesh.WriteTriangle(trPosition, polylines[i].GetIndex(0), polylines[i].GetIndex(1),
                                                            polylines[other].GetIndex(1));
                        }
                    }
                }
                if (MS[0] == 2 && MS[1] == 2 && MS[2] == 2)
                {
                    // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                    // M == 2 writing CASE 4");
                    // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    trPosition = mesh.WriteTriangle(trPosition, polylines[0].GetIndex(1), polylines[1].GetIndex(1),
                                                    polylines[2].GetIndex(1));
                }

                // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                // if(facesIndex+trianglesN!=trPosition)
                // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                // ERROR on trianglesN facesIndex:%d trianglesN:%d trPosition:%d",
                // facesIndex, trianglesN, trPosition);
                // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR

                return;
            }

            int innerVerticesPosition = internalsIndex;
            int rowPosition1          = innerVerticesPosition;
            int rowPosition2          = innerVerticesPosition + M - 2;

            // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
            // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
            // writing Internals innerVerticesPosition:%d rowPosition1:%d
            // rowPosition2:%d", innerVerticesPosition, rowPosition1, rowPosition2);
            // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR

            for (int i = 0; i < M - 3; i++)
            {
                for (int j_ = 0; j_ < M - 4 - i; j_++)
                {
                    trPosition = mesh.WriteTriangle(trPosition, rowPosition1 + j_, rowPosition1 + j_ + 1, rowPosition2 + j_);
                    trPosition = mesh.WriteTriangle(trPosition, rowPosition2 + j_, rowPosition1 + j_ + 1,
                                                    rowPosition2 + j_ + 1);
                }

                int j = M - 4 - i;

                trPosition = mesh.WriteTriangle(trPosition, rowPosition1 + j, rowPosition1 + j + 1, rowPosition2 + j);

                rowPosition1 = rowPosition2;
                rowPosition2 = rowPosition2 + (M - 3 - i);
                // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                // writing Internals rowPosition1:%d rowPosition2:%d", rowPosition1,
                // rowPosition2);
                // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
            }

            first[0] = innerVerticesPosition;
            first[1] = innerVerticesPosition + M - 3;
            first[2] = innerVerticesPosition + (((M - 1) * (M - 2)) >> 1) - 1;

            move[0] = 1;
            move[1] = M - 2;
            move[2] = -1;

            deltaMove[0] = 0;
            deltaMove[1] = -1;
            deltaMove[2] = -1;

            int count = M - 2;

            // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
            // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
            // preparing Sides count:%d first:[%d,%d,%d] move:[%d,%d,%d]
            // deltaMove:[%d,%d,%d]",
            // count, first[0], first[1], first[2], move[0], move[1], move[2],
            // deltaMove[0], deltaMove[1], deltaMove[2]);
            // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR

            for (int i = 0; i < 3; i++)
            {
                int N = polylines[i].GetN();
                if (N >= 2)
                {
                    // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                    // side Triangle case A trPosition:%d", trPosition);
                    // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    meshIndicesArray.Setup(first[i], move[i], deltaMove[i], count, mesh,
                                           TriangleMeshStructure.DEFAULT_VERTICES_LAYER);

                    NetPolylineInternalIndicesArray npi = new NetPolylineInternalIndicesArray(polylines[i], mesh);
                    trPosition = MeshStructures.CreateSideTriangles(mesh, meshIndicesArray, npi, trPosition);
                }
                else if (N == 1)
                {
                    // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                    // side Triangle case B trPosition:%d (Begin)", trPosition);
                    // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    meshIndicesArray.Setup(first[i], move[i], deltaMove[i], count, mesh, DEFAULT_VERTICES_LAYER);

                    NetPolylineIndicesArray npi = new NetPolylineIndicesArray(polylines[i], mesh);
                    trPosition = MeshStructures.CreateSideTriangles(mesh, meshIndicesArray, npi, trPosition);

                    // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                    // side Triangle case B trPosition:%d (End)", trPosition);
                    // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                }

                int prev  = i == 0 ? 2 : i - 1;
                int prevN = polylines[prev].GetN();
                if (N > 1 && prevN > 1)
                {
                    // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                    // writing corner Index as Quad a:%d b:%d c:%d d:%d",
                    // polylines[i].getIndex(1),first[i],
                    // polylines[prev].getBackIndex(1), polylines[i].getIndex(0));
                    // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR

                    if (prevN + N < 1.5f * M)
                    {
                        trPosition = mesh.WriteQuad(trPosition, polylines[i].GetIndex(0), polylines[i].GetIndex(1),
                                                    first[i], polylines[prev].GetBackIndex(1));
                    }
                    else
                    {
                        trPosition = mesh.WriteQuad(trPosition, polylines[i].GetIndex(1), first[i],
                                                    polylines[prev].GetBackIndex(1), polylines[i].GetIndex(0));
                    }
                }
                else if (polylines[i].GetN() > 1)
                {
                    // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                    // writing corner Index as Triangle a:%d b:%d c:%d ",
                    // polylines[i].getIndex(0), polylines[i].getIndex(1),
                    // first[i]);
                    // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR

                    trPosition = mesh.WriteTriangle(trPosition, polylines[i].GetIndex(0), polylines[i].GetIndex(1),
                                                    first[i]);
                }
                else if (polylines[prev].GetN() > 1)
                {
                    // #ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
                    // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
                    // writing corner Index as Triangle a:%d b:%d c:%d ",
                    // polylines[i].getIndex(0),
                    // first[i],polylines[i].getBackIndex(1));
                    // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR

                    trPosition = mesh.WriteTriangle(trPosition, polylines[i].GetIndex(0), first[i],
                                                    polylines[prev].GetBackIndex(1));
                }
            }

            // #
            // ifdef SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
            // if(facesIndex+trianglesN!=trPosition)
            //
            // printf("\nSFCPNTrianglesMeshStructure.createTriangleTessellation
            // ERROR on trianglesN facesIndex:%d trianglesN:%d trPosition:%d",
            // facesIndex, trianglesN, trPosition);
            // #endif //SF_RAW_DEBUG_TRIANGLE_TESSELLATOR
        }
        public void UdpdateContent(OutputMesh mesh, CPNPolygon buildingPolygonData, int internalsIndex,
                                   int facesIndex, bool doUpdateStructure = true)
        {
            RetrieveInfos(buildingPolygonData);

            this.step = 1.0f / M;

            totalInterpolationStep = M / relativeTriangleFactorX;
            interpolationSteps     = (int)(totalInterpolationStep);
            if (interpolationSteps * relativeTriangleFactorX < M)
            {
                interpolationSteps++;
            }

            Vector3 vertex = Vector3.zero;
            Vector3 uv     = Vector3.zero;

            int centerPiecePosition = 0;

            int verticesLayer = 0;// mesh.FindLayer(BufferType.VERTICES);

            //int normalsLayer = mesh.FindLayer(BufferType.NORMALS);
            //cornerSurfacesSet.SetNormalsLayer(normalsLayer);

            CPNSideEdge[] polylines = buildingPolygonData.sideEdges;

            for (int i = 0; i < sides; i++)
            {
                buffers[i].writeWithGuide(polylines[i], interpolationSteps, step * relativeTriangleFactorX, mesh, evaluator);
                backBuffers[i].writeWithGuideBack(polylines[i], interpolationSteps, step * relativeTriangleFactorX, mesh, evaluator);
            }

            prepareMemory(M, polylines, mesh);

            //int position = internalsIndex;

            int triangleSize = ((M)*(M + 1)) >> 1;

            for (int corner = 0; corner < sides; corner++)
            {
                int next = corner == sides - 1 ? 0 : corner + 1;

                float p1x = cos_[corner];
                float p2x = cos_[next];
                float p1y = sin_[corner];
                float p2y = sin_[next];

                int relativeMemoryIndex = triangleSize * corner + M;

                for (int i = 1; i <= M - 1; i++)
                {
                    for (int j = 0; j <= M - 1 - i; j++)
                    {
                        float innerU = step * j;
                        float innerV = step * i;
                        float innerW = 1 - innerU - innerV;

                        float innerX_ = innerW * p1x + innerU * p2x;
                        float innerY_ = innerW * p1y + innerU * p2y;

                        vertex = EvalVertex(mesh, buildingPolygonData, innerX_, innerY_, out uv);

                        memory.vertices[relativeMemoryIndex] = vertex;
                        memory.uv[relativeMemoryIndex]       = uv;
                        relativeMemoryIndex++;
                        //position++;
                    }
                }
            }

            vertex = EvalVertex(mesh, buildingPolygonData, 0, 0, out uv);
            //mesh.SetValue(0, position, vertex);
            memory.vertices[triangleSize * sides] = vertex;
            memory.uv[triangleSize * sides]       = uv;
            //position++;



            //Phase 2 Normals and Tangents Evaluation

            int position = internalsIndex;

            for (int corner = 0; corner < sides; corner++)
            {
                int prev = corner == 0 ? sides - 1 : corner - 1;
                int next = corner == sides - 1 ? 0 : corner + 1;
                int relativeMemoryIndex          = triangleSize * corner;
                int nextPatchRelativeMemoryIndex = triangleSize * next;
                int prevPatchRelativeMemoryIndex = triangleSize * prev;
                slicePosition[corner] = position;
                for (int i = 1; i <= M - 1; i++)
                {
                    for (int j = 0; j <= M - 1 - i; j++)
                    {
                        int rowIndex     = relativeMemoryIndex + M * i - (((i) * (i - 1)) >> 1);
                        int prevRowIndex = relativeMemoryIndex + M * (i - 1) - (((i - 1) * (i - 2)) >> 1);
                        int nextRowIndex = relativeMemoryIndex + M * (i + 1) - (((i + 1) * (i)) >> 1);

                        int index = rowIndex + j;
                        vertex = memory.vertices[index];
                        uv     = memory.uv[index];

                        int duIndexA = 0, duIndexB = 0, dvIndexA = 0, dvIndexB = 0;
                        if (j > 0 && j < M - 1 - i)
                        {
                            duIndexA = prevRowIndex + j;
                            duIndexB = nextRowIndex + j;
                            dvIndexA = nextRowIndex + j - 1;
                            dvIndexB = prevRowIndex + j + 1;
                        }
                        else if (i < M - 1)
                        {
                            if (j == M - 1 - i)
                            {
                                int nextPatchnextRowIndex = nextPatchRelativeMemoryIndex + M * (i + 1) - (((i + 1) * (i)) >> 1);

                                duIndexA = prevRowIndex + j;
                                duIndexB = nextPatchnextRowIndex;
                                dvIndexA = nextRowIndex + j - 1;
                                dvIndexB = prevRowIndex + j + 1;
                            }
                            else if (j == 0)
                            {
                                int prevPatchprevRowIndex = prevPatchRelativeMemoryIndex + M * (i - 1) - (((i - 1) * (i - 2)) >> 1);

                                duIndexA = prevRowIndex + j;
                                duIndexB = nextRowIndex + j;
                                dvIndexA = prevPatchprevRowIndex + M - i;
                                dvIndexB = prevRowIndex + j + 1;
                            }
                        }
                        else
                        {
                            int prevPatchprevRowIndex = prevPatchRelativeMemoryIndex + M * (i - 1) - (((i - 1) * (i - 2)) >> 1);

                            duIndexA = prevRowIndex + j;
                            duIndexB = triangleSize * sides;
                            dvIndexA = prevPatchprevRowIndex + M - i;
                            dvIndexB = prevRowIndex + j + 1;
                        }


                        Vector3 dSdu   = memory.vertices[duIndexB] - memory.vertices[duIndexA];
                        Vector3 dSdv   = memory.vertices[dvIndexB] - memory.vertices[dvIndexA];
                        Vector3 normal = Vector3.Cross(dSdu, dSdv).normalized;

                        //Tangent
                        Vector3 dTxdu   = memory.uv[duIndexB] - memory.uv[duIndexA];
                        Vector3 dTxdv   = memory.uv[dvIndexB] - memory.uv[dvIndexA];
                        Vector3 tangent = getTangent(dSdu, dSdv, dTxdu, dTxdv);

                        mesh.SetPNUV(position, vertex, normal, uv, tangent);

                        //relativeMemoryIndex++;
                        position++;
                    }
                }
            }

            {
                int duIndexA = 2 * triangleSize - 1;
                int duIndexB = 4 * triangleSize - 1;
                int dvIndexA = triangleSize - 1;
                int dvIndexB = 3 * triangleSize - 1;
                vertex = memory.vertices[triangleSize * sides];
                uv     = memory.uv[triangleSize * sides];

                Vector3 dSdu   = memory.vertices[duIndexB] - memory.vertices[duIndexA];
                Vector3 dSdv   = memory.vertices[dvIndexB] - memory.vertices[dvIndexA];
                Vector3 normal = Vector3.Cross(dSdu, dSdv).normalized;

                //Tangent
                Vector3 dTxdu   = memory.uv[duIndexB] - memory.uv[duIndexA];
                Vector3 dTxdv   = memory.uv[dvIndexB] - memory.uv[dvIndexA];
                Vector3 tangent = getTangent(dSdu, dSdv, dTxdu, dTxdv);
                mesh.SetPNUV(position, vertex, normal, uv, tangent);
            }

            centerPiecePosition = position;
            position++;



            int trPosition = facesIndex;

            for (int corner = 0; corner < sides; corner++)
            {
                int next = corner == sides - 1 ? 0 : corner + 1;

                if (M > 1)
                {
                    int rowPosition1     = slicePosition[corner];
                    int rowPosition2     = slicePosition[corner] + M - 1;
                    int rowPosition1Next = slicePosition[next];
                    int rowPosition2Next = slicePosition[next] + M - 1;
                    for (int i = 0; i < M - 2; i++)
                    {
                        for (int j_ = 0; j_ < M - 3 - i; j_++)
                        {
                            trPosition = mesh.WriteTriangle(trPosition, rowPosition1 + j_, rowPosition2 + j_, rowPosition1 + j_ + 1);
                            trPosition = mesh.WriteTriangle(trPosition, rowPosition1 + j_ + 1, rowPosition2 + j_, rowPosition2 + j_ + 1);
                        }
                        int j = M - 3 - i;
                        trPosition = mesh.WriteTriangle(trPosition, rowPosition1 + j,
                                                        rowPosition2 + j, rowPosition1 + j + 1);

                        trPosition = mesh.WriteTriangle(trPosition, rowPosition2 + j,
                                                        rowPosition2Next, rowPosition1 + j + 1);

                        trPosition = mesh.WriteTriangle(trPosition, rowPosition1 + j + 1,
                                                        rowPosition2Next, rowPosition1Next);

                        rowPosition1     = rowPosition2;
                        rowPosition2     = rowPosition2 + M - 2 - i;
                        rowPosition1Next = rowPosition2Next;
                        rowPosition2Next = rowPosition2Next + M - 2 - i;
                    }
                    trPosition = mesh.WriteTriangle(trPosition, rowPosition1, centerPiecePosition, rowPosition1Next);

                    meshIndicesArray.Setup(slicePosition[corner], 1, M, slicePosition[next], mesh, verticesLayer);

                    int polylineIndex = sides - 1 - corner;

                    NetPolylineIndicesArray npi = new NetPolylineIndicesArray(polylines[polylineIndex], mesh, true);
                    trPosition = MeshStructures.CreateSideTriangles(mesh, npi, meshIndicesArray,
                                                                    trPosition);
                }
                else

                {
                    trPosition = mesh.WriteTriangle(trPosition, polylines[corner].GetIndex(0), centerPiecePosition, polylines[corner].GetBackIndex(0));
                }
            }
        }