private static void FindSharpComponentSections(VoxelMeshComponent component, NativeList <PackedIndex> componentIndices, NativeList <VoxelMeshComponentVertex> componentVertices,
                                                       NativeList <SharpSection> sharpSections, NativeList <float3> sharpSectionNormals)
        {
            if (component.RegularFeature)
            {
                int   sharpSectionStart = -1;
                float normalSumX        = 0.0f;
                float normalSumY        = 0.0f;
                float normalSumZ        = 0.0f;

                for (int i = 0; i < component.Size * 2; i++)
                {
                    int j = i % component.Size;

                    PackedIndex index = componentIndices[component.Index + j];

                    float3 n = componentVertices[index].Normal;

                    if (index.Is2DSharpFeature)
                    {
                        if (sharpSectionStart != -1)
                        {
                            float3 sharpSectionNormal = math.normalize(new float3(normalSumX, normalSumY, normalSumZ));

                            if (j < sharpSectionStart)
                            {
                                sharpSections.Add(new SharpSection(j, component.Size));
                                sharpSectionNormals.Add(sharpSectionNormal);

                                sharpSections.Add(new SharpSection(1, sharpSectionStart));
                                sharpSectionNormals.Add(sharpSectionNormal);
                            }
                            else
                            {
                                sharpSections.Add(new SharpSection(sharpSectionStart, j));
                                sharpSectionNormals.Add(sharpSectionNormal);
                            }
                        }

                        sharpSectionStart = j;
                        normalSumX        = normalSumY = normalSumZ = 0.0f;
                    }
                    else if (sharpSectionStart != -1 && index.IsNormalSet)
                    {
                        normalSumX += n.x;
                        normalSumY += n.y;
                        normalSumZ += n.z;
                    }
                }
            }
        }
        public static void Tessellate(VoxelMeshComponent component, NativeList <PackedIndex> componentIndices, NativeList <VoxelMeshComponentVertex> componentVertices,
                                      Matrix4x4 transform, NativeList <float3> vertices, int freeTriIndex, NativeList <int> triIndices,
                                      NativeList <float3> normals, NativeList <int> materials, NativeDeduplicationCache dedupeCache)
        {
            float3 triangleFanCenter;

            if (component.Feature)
            {
                triangleFanCenter = component.FeatureVertex;
            }
            else
            {
                triangleFanCenter = float3.zero;
                int vertexCount = 0;
                for (int i = component.Index; i < component.Index + component.Size; i++)
                {
                    triangleFanCenter += (float3)componentVertices[componentIndices[i]].Position;
                    vertexCount++;
                }
                triangleFanCenter *= 1f / vertexCount;
            }

            if (component.RegularFeature)
            {
                var sharpSections       = new NativeList <SharpSection>(Allocator.Temp);
                var sharpSectionNormals = new NativeList <float3>(Allocator.Temp);

                FindSharpComponentSections(component, componentIndices, componentVertices, sharpSections, sharpSectionNormals);

                for (int i = component.Index; i < component.Index + component.Size; i++)
                {
                    PackedIndex packedIndex1             = componentIndices[i];
                    VoxelMeshComponentVertex meshVertex1 = componentVertices[packedIndex1];
                    float3 v1 = meshVertex1.Position;
                    float3 n1 = meshVertex1.Normal;
                    int    m1 = meshVertex1.Material;

                    PackedIndex packedIndex2             = componentIndices[component.Index + ((i + 1 - component.Index) % component.Size)];
                    VoxelMeshComponentVertex meshVertex2 = componentVertices[packedIndex2];
                    float3 v2 = meshVertex2.Position;
                    float3 n2 = meshVertex2.Normal;
                    int    m2 = meshVertex2.Material;

                    float3 faceAverageNormal = float3.zero;
                    if (packedIndex1.IsNormalSet)
                    {
                        faceAverageNormal += n1 / math.length(v1 - triangleFanCenter);
                    }
                    if (packedIndex2.IsNormalSet)
                    {
                        faceAverageNormal += n2 / math.length(v2 - triangleFanCenter);
                    }
                    faceAverageNormal = math.normalize(faceAverageNormal);

                    float3 triangleFanCenterNormal    = float3.zero;
                    bool   triangleFanCenterNormalSet = false;

                    //Find the normal of the section we're in
                    for (int count = sharpSections.Length, j = 0; j < count; j++)
                    {
                        SharpSection section = sharpSections[j];

                        if (i >= section.startIndex && i < section.endIndex)
                        {
                            triangleFanCenterNormal    = sharpSectionNormals[j];
                            triangleFanCenterNormalSet = true;
                        }
                    }

                    if (!triangleFanCenterNormalSet)
                    {
                        triangleFanCenterNormal = faceAverageNormal;
                    }

                    var outV2 = transform.MultiplyPoint(triangleFanCenter);
                    var outN2 = transform.MultiplyVector(triangleFanCenterNormal);

                    var    outV1 = transform.MultiplyPoint(v1);
                    float3 outN1;
                    if (packedIndex1.IsNormalSet)
                    {
                        outN1 = transform.MultiplyVector(n1);
                    }
                    else
                    {
                        outN1 = outN2;
                    }
                    if (Deduplicate(outV1, outN1, m1, freeTriIndex, dedupeCache, out int outI1))
                    {
                        triIndices.Add(outI1);
                    }
                    else
                    {
                        vertices.Add(outV1);
                        normals.Add(outN1);
                        materials.Add(m1);
                        triIndices.Add(freeTriIndex);
                        freeTriIndex++;
                    }

                    if (Deduplicate(outV2, outN2, m1, freeTriIndex, dedupeCache, out int outI2))
                    {
                        triIndices.Add(outI2);
                    }
                    else
                    {
                        vertices.Add(outV2);
                        normals.Add(outN2);
                        materials.Add(m1);
                        triIndices.Add(freeTriIndex);
                        freeTriIndex++;
                    }

                    var    outV3 = transform.MultiplyPoint(v2);
                    float3 outN3;
                    if (packedIndex2.IsNormalSet)
                    {
                        outN3 = transform.MultiplyVector(n2);
                    }
                    else
                    {
                        outN3 = outN2;
                    }
                    if (Deduplicate(outV3, outN3, m2, freeTriIndex, dedupeCache, out int outI3))
                    {
                        triIndices.Add(outI3);
                    }
                    else
                    {
                        vertices.Add(outV3);
                        normals.Add(outN3);
                        materials.Add(m2);
                        triIndices.Add(freeTriIndex);
                        freeTriIndex++;
                    }
                }

                DisposeManaged(sharpSections);
                DisposeManaged(sharpSectionNormals);
            }
            else
            {
                float3 fullAverageNormal = float3.zero;

                for (int i = component.Index; i < component.Index + component.Size; i++)
                {
                    PackedIndex packedIndex = componentIndices[i];
                    if (packedIndex.IsNormalSet)
                    {
                        VoxelMeshComponentVertex meshVertex = componentVertices[packedIndex];
                        fullAverageNormal += meshVertex.Normal / math.length(meshVertex.Position - triangleFanCenter);
                    }
                }

                fullAverageNormal = math.normalize(fullAverageNormal);

                for (int i = component.Index; i < component.Index + component.Size; i++)
                {
                    PackedIndex packedIndex1             = componentIndices[i];
                    VoxelMeshComponentVertex meshVertex1 = componentVertices[packedIndex1];
                    float3 v1 = meshVertex1.Position;
                    float3 n1 = meshVertex1.Normal;
                    int    m1 = meshVertex1.Material;

                    PackedIndex packedIndex2             = componentIndices[component.Index + ((i + 1 - component.Index) % component.Size)];
                    VoxelMeshComponentVertex meshVertex2 = componentVertices[packedIndex2];
                    float3 v2 = meshVertex2.Position;
                    float3 n2 = meshVertex2.Normal;
                    int    m2 = meshVertex2.Material;

                    float3 faceAverageNormal = float3.zero;
                    if (packedIndex1.IsNormalSet)
                    {
                        faceAverageNormal += n1 / math.length(v1 - triangleFanCenter);
                    }
                    if (packedIndex2.IsNormalSet)
                    {
                        faceAverageNormal += n2 / math.length(v2 - triangleFanCenter);
                    }
                    faceAverageNormal = math.normalize(faceAverageNormal);

                    var    outV1 = transform.MultiplyPoint(v1);
                    float3 outN1;
                    if (packedIndex1.IsNormalSet)
                    {
                        outN1 = transform.MultiplyVector(n1);
                    }
                    else
                    {
                        outN1 = transform.MultiplyVector(faceAverageNormal);
                    }
                    if (Deduplicate(outV1, outN1, m1, freeTriIndex, dedupeCache, out int outI1))
                    {
                        triIndices.Add(outI1);
                    }
                    else
                    {
                        vertices.Add(outV1);
                        normals.Add(outN1);
                        materials.Add(m1);
                        triIndices.Add(freeTriIndex);
                        freeTriIndex++;
                    }

                    var outV2 = transform.MultiplyPoint(triangleFanCenter);
                    var outN2 = transform.MultiplyVector(fullAverageNormal);
                    if (Deduplicate(outV2, outN2, m1, freeTriIndex, dedupeCache, out int outI2))
                    {
                        triIndices.Add(outI2);
                    }
                    else
                    {
                        vertices.Add(outV2);
                        normals.Add(outN2);
                        materials.Add(m1);
                        triIndices.Add(freeTriIndex);
                        freeTriIndex++;
                    }

                    var    outV3 = transform.MultiplyPoint(v2);
                    float3 outN3;
                    if (packedIndex2.IsNormalSet)
                    {
                        outN3 = transform.MultiplyVector(n2);
                    }
                    else
                    {
                        outN3 = transform.MultiplyVector(faceAverageNormal);
                    }
                    if (Deduplicate(outV3, outN3, m2, freeTriIndex, dedupeCache, out int outI3))
                    {
                        triIndices.Add(outI3);
                    }
                    else
                    {
                        vertices.Add(outV3);
                        normals.Add(outN3);
                        materials.Add(m2);
                        triIndices.Add(freeTriIndex);
                        freeTriIndex++;
                    }
                }
            }
        }