Exemplo n.º 1
0
        static BvhNode CreateBvh(List <Triangle> triangles, int splitCount)
        {
            BvhNode rootNode;

            var triBoundsArray = new NativeArray <TriangleBounds>(triangles.Count, Allocator.Temp);

            {
                for (var i = 0; i < triangles.Count; ++i)
                {
                    var tri = triangles[i];
                    var min = Vector3.Min(Vector3.Min(tri.pos0, tri.pos1), tri.pos2);
                    var max = Vector3.Max(Vector3.Max(tri.pos0, tri.pos1), tri.pos2);

                    var triBounds = new TriangleBounds()
                    {
                        bounds = new Bounds()
                        {
                            min = min, max = max
                        },
                        triangleIndex = i
                    };

                    triBoundsArray[i] = triBounds;
                }

                rootNode = CreateBvhRecursive(triBoundsArray, splitCount);
                //triBounds = triBoundsArray.ToList();
            }
            triBoundsArray.Dispose();

            return(rootNode);
        }
Exemplo n.º 2
0
        protected override void Build()
        {
            var      numTriangles   = m_indices.Length / 3;
            var      triangleBounds = new TriangleBounds[numTriangles];
            var      index          = 0;
            var      nodeList       = new List <TreeNode>();
            TreeNode rootNode;

            rootNode.m_childIndex = -1;
            rootNode.m_triangles  = null;
            var triangleList = new List <int>();

            for (var i = 0; i < numTriangles; ++i)
            {
                triangleList.Add(index);
                var     v0 = m_vertices[m_indices[index++]];
                var     v1 = m_vertices[m_indices[index++]];
                var     v2 = m_vertices[m_indices[index++]];
                Vector3 min, max;
                min.x = Mathf.Min(Mathf.Min(v0.x, v1.x), v2.x);
                min.y = Mathf.Min(Mathf.Min(v0.y, v1.y), v2.y);
                min.z = Mathf.Min(Mathf.Min(v0.z, v1.z), v2.z);
                max.x = Mathf.Max(Mathf.Max(v0.x, v1.x), v2.x);
                max.y = Mathf.Max(Mathf.Max(v0.y, v1.y), v2.y);
                max.z = Mathf.Max(Mathf.Max(v0.z, v1.z), v2.z);
                triangleBounds[i].m_min = min;
                triangleBounds[i].m_max = max;
            }
            BuildMeshTree(nodeList, ref rootNode, m_bounds.center, m_bounds.extents, triangleList, triangleBounds);
            nodeList.Add(rootNode);
#if UNITY_EDITOR && (UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3)
            int            numNodes      = nodeList.Count;
            int[]          childNodes    = new int[numNodes];
            TriangleList[] triangleLists = new TriangleList[numNodes];
            for (int i = 0; i < numNodes; ++i)
            {
                TreeNode node = nodeList[i];
                childNodes[i] = node.m_childIndex;
                if (node.m_triangles != null)
                {
                    triangleLists[i]           = new TriangleList();
                    triangleLists[i].triangles = node.m_triangles;
                }
                else
                {
                    triangleLists[i] = null;
                }
            }
            m_childNodes    = childNodes;
            m_triangleLists = triangleLists;
#endif
            m_treeNodes = nodeList.ToArray();
        }