Exemplo n.º 1
0
 public hitable_list(List <Hitable> l, float t0 = 0f, float t1 = 0f, bool useBvh = true)
 {
     list       = l;
     bvh        = new BVH_Node(l, t0, t1);
     this.usBvh = useBvh;
 }
Exemplo n.º 2
0
    public BVH_Node(List <Hitable> list, float time0, float time1)
    {
        Objlist = list;
        int axis = Mathf.FloorToInt(zRandom.drand() * 3f);

        if (axis == 0)
        {
            list.Sort((a, b) =>
            {
                rtAABB box_left = new rtAABB(), box_right = new rtAABB();
                if (!a.bounding_box(0, 0, ref box_left) || !b.bounding_box(0, 0, ref box_right))
                {
                    Debug.LogError(" no bounding box in bvh_node constructor");
                }
                if (box_left._min.x - box_right._min.x < 0f)
                {
                    return(-1);
                }
                else
                {
                    return(1);
                }
            });
        }
        else if (axis == 1)
        {
            list.Sort((a, b) =>
            {
                rtAABB box_left = new rtAABB(), box_right = new rtAABB();
                if (!a.bounding_box(0, 0, ref box_left) || !b.bounding_box(0, 0, ref box_right))
                {
                    Debug.LogError(" no bounding box in bvh_node constructor");
                }
                if (box_left._min.y - box_right._min.y < 0f)
                {
                    return(-1);
                }
                else
                {
                    return(1);
                }
            });
        }
        else
        {
            list.Sort((a, b) =>
            {
                rtAABB box_left = new rtAABB(), box_right = new rtAABB();
                if (!a.bounding_box(0, 0, ref box_left) || !b.bounding_box(0, 0, ref box_right))
                {
                    Debug.LogError(" no bounding box in bvh_node constructor");
                }
                if (box_left._min.z - box_right._min.z < 0f)
                {
                    return(-1);
                }
                else
                {
                    return(1);
                }
            });
        }

        int n = list.Count;

        if (n == 1)
        {
            left = right = list[0];
        }
        else if (n == 2)
        {
            left  = list[0];
            right = list[1];
        }
        else
        {
            left  = new BVH_Node(list.GetRange(0, n / 2), time0, time1);
            right = new BVH_Node(list.GetRange(n / 2, n - n / 2), time0, time1);
        }
        rtAABB box_l = new rtAABB(), box_r = new rtAABB();

        if (!left.bounding_box(time0, time1, ref box_l) || !right.bounding_box(time0, time1, ref box_r))
        {
            Debug.LogError(" no bounding box in bvh_node constructor");
        }
        box = rtAABB.surrounding_box(box_l, box_r);
    }