示例#1
0
        private static unsafe TempSection BuildSectionGeometry(
            int sectionIndex, List <MeshConnectivityBuilder.Primitive> primitives, List <int> subTreeNodeIndices, BoundingVolumeHierarchy.Node *nodes, float3[] vertices)
        {
            var section = new TempSection();

            section.Vertices        = vertices.ToList();
            section.Primitives      = new List <Mesh.PrimitiveVertexIndices>();
            section.PrimitivesFlags = new List <Mesh.PrimitiveFlags>();

            foreach (int root in subTreeNodeIndices)
            {
                int *nodesIndexStack = stackalloc int[BoundingVolumeHierarchy.Constants.UnaryStackSize];
                int  stackSize       = 1;
                nodesIndexStack[0] = root;

                do
                {
                    int nodeIndex = nodesIndexStack[--stackSize];
                    ref BoundingVolumeHierarchy.Node node = ref nodes[nodeIndex];

                    if (node.IsLeaf)
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            if (node.IsChildValid(i))
                            {
                                MeshConnectivityBuilder.Primitive p = primitives[node.Data[i]];
                                section.PrimitivesFlags.Add(ConvertPrimitiveFlags(p.Flags));

                                int vertexCount = p.Flags.HasFlag(MeshConnectivityBuilder.PrimitiveFlags.IsTrianglePair) ? 4 : 3;

                                Mesh.PrimitiveVertexIndices sectionPrimitive = new Mesh.PrimitiveVertexIndices();
                                byte *vertexIndices = &sectionPrimitive.A;

                                for (int v = 0; v < vertexCount; v++)
                                {
                                    vertexIndices[v] = (byte)Array.IndexOf(vertices, p.Vertices[v]);
                                }

                                if (vertexCount == 3)
                                {
                                    sectionPrimitive.D = sectionPrimitive.C;
                                }

                                section.Primitives.Add(sectionPrimitive);

                                int primitiveSectionIndex = section.Primitives.Count - 1;

                                // Update primitive index in the BVH.
                                node.Data[i] = (sectionIndex << 8) | primitiveSectionIndex;
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            if (node.IsChildValid(i))
                            {
                                nodesIndexStack[stackSize++] = node.Data[i];
                            }
                        }
                    }
                } while (stackSize > 0);
            }
示例#2
0
        private static unsafe void BuildSectionGeometry(TempSection sections, NativeList <MeshConnectivityBuilder.Primitive> primitives, NativeList <int> subTreeNodeIndices, BoundingVolumeHierarchy.Node *nodes, NativeArray <float3> vertices)
        {
            var sectionIndex = sections.Ranges.Length;

            var newSectionRange = new TempSectionRanges
            {
                VerticesMin        = sections.Vertices.Length,
                PrimitivesFlagsMin = sections.PrimitivesFlags.Length,
                PrimitivesMin      = sections.Primitives.Length
            };

            sections.Vertices.AddRange(vertices);

            int *nodesIndexStack = stackalloc int[BoundingVolumeHierarchy.Constants.UnaryStackSize];

            for (var rootIndex = 0; rootIndex < subTreeNodeIndices.Length; ++rootIndex)
            {
                var root      = subTreeNodeIndices[rootIndex];
                int stackSize = 1;
                nodesIndexStack[0] = root;

                do
                {
                    int nodeIndex = nodesIndexStack[--stackSize];
                    ref BoundingVolumeHierarchy.Node node = ref nodes[nodeIndex];

                    if (node.IsLeaf)
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            if (node.IsChildValid(i))
                            {
                                MeshConnectivityBuilder.Primitive p = primitives[node.Data[i]];
                                sections.PrimitivesFlags.Add(ConvertPrimitiveFlags(p.Flags));

                                int vertexCount = (p.Flags & MeshConnectivityBuilder.PrimitiveFlags.IsTrianglePair) != 0 ? 4 : 3;

                                Mesh.PrimitiveVertexIndices sectionPrimitive = new Mesh.PrimitiveVertexIndices();
                                byte *vertexIndices = &sectionPrimitive.A;

                                for (int v = 0; v < vertexCount; v++)
                                {
                                    vertexIndices[v] = (byte)vertices.IndexOf(p.Vertices[v]);
                                }

                                if (vertexCount == 3)
                                {
                                    sectionPrimitive.D = sectionPrimitive.C;
                                }

                                sections.Primitives.Add(sectionPrimitive);

                                int primitiveSectionIndex = sections.Primitives.Length - newSectionRange.PrimitivesMin - 1;

                                // Update primitive index in the BVH.
                                node.Data[i] = (sectionIndex << 8) | primitiveSectionIndex;
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            if (node.IsChildValid(i))
                            {
                                nodesIndexStack[stackSize++] = node.Data[i];
                            }
                        }
                    }
                } while (stackSize > 0);
            }