private void GetBVHVerticesAndIndices(BVHNode bvh, int level = 0) { var vertBase = _bvhVerts.Count; var corners = bvh.Bounds.GetCorners(); if (level == 9) { _bvhVerts.AddRange(corners.Select(c => { var color = Color.White; switch (_aabCount % 4) { case 1: color = Color.Blue; break; case 2: color = Color.Magenta; break; case 3: color = Color.Yellow; break; } return new VertexPC(c, color); })); _aabCount++; _bvhIndices.AddRange( new[] {0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 4, 0, 5, 1, 7, 3, 6, 2}.Select( i => i + vertBase)); } if (bvh.Children!=null) { foreach (var child in bvh.Children) { GetBVHVerticesAndIndices(child, level+1); } } }
private BVHNode BuildBvh(Vector2 tl, Vector2 br) { var minMaxY = GetMinMaxY(tl, br); var minX = tl.X*Info.CellSpacing - Width/2; var maxX = br.X*Info.CellSpacing - Width/2; var minZ = -tl.Y * Info.CellSpacing + Depth / 2; var maxZ = -br.Y * Info.CellSpacing + Depth / 2; var bvh = new BVHNode {Bounds = new BoundingBox(new Vector3(minX, minMaxY.X, minZ), new Vector3(maxX, minMaxY.Y, maxZ))}; var width = (int)Math.Floor((br.X - tl.X)/2); var depth = (int)Math.Floor((br.Y - tl.Y)/2); if (width >= 4 && depth >= 4) { bvh.Children = new[] { BuildBvh(tl, new Vector2(tl.X + width, tl.Y + depth)), BuildBvh(new Vector2(tl.X + width, tl.Y), new Vector2(br.X, tl.Y + depth) ), BuildBvh(new Vector2(tl.X, tl.Y+depth), new Vector2(tl.X+depth, br.Y) ), BuildBvh(new Vector2(tl.X+width, tl.Y+depth), br ) }; } return bvh; }