Example #1
0
        public bool RayQuery(NativeBVHTree.Ray ray)
        {
            var invD = 1 / ray.direction;

            return(IntersectionUtils.DoesOverlap(LowerBound, UpperBound, ref ray, invD));
        }
        public void RaycastQuery(Ray ray, NativeList <int> results)
        {
            ray.direction = math.normalize(ray.direction);
            var invD = math.rcp(ray.direction);

            if (nodes[rootIndex[0]]->isLeaf)
            {
                RayLeaf(ref this, rootIndex[0]);
                return;
            }

            var stack = stackalloc int[256];

            stack[0] = rootIndex[0];
            var top = 1;

            while (top > 0)
            {
                var index = stack[--top];
                var node  = nodes[index];

                var child1 = nodes[node->child1];
                var child2 = nodes[node->child2];
                if (child1->isLeaf && IntersectionUtils.DoesOverlap(ref child1->box, ref ray, invD))
                {
                    RayLeaf(ref this, node->child1);
                }
                if (child2->isLeaf && IntersectionUtils.DoesOverlap(ref child2->box, ref ray, invD))
                {
                    RayLeaf(ref this, node->child2);
                }

                var result = node->grandchildrenAabbs.Raycast(ray, invD);

                ProcessResult(ref this, 0, child1->child1);
                ProcessResult(ref this, 1, child1->child2);
                ProcessResult(ref this, 2, child2->child1);
                ProcessResult(ref this, 3, child2->child2);

                void ProcessResult(ref NativeBVHTree tree, int childId, int id)
                {
                    if (result[childId])
                    {
                        var child = tree.nodes[id];
                        if (child->isLeaf)
                        {
                            RayLeaf(ref tree, id);
                        }
                        else
                        {
                            stack[top++] = id;
                        }
                    }
                }
            }

            void RayLeaf(ref NativeBVHTree tree, int id)
            {
                var child = tree.nodes[id];

                if ((ray.layerMask & child->leaf.layer) == 0 && tree.CastRay(ray, ref child->leaf))
                {
                    results.Add(id);
                }
            }
        }