예제 #1
0
def ray_color(r: RayList, world: HittableList, depth: int) \
        -> Tuple[Optional[RayList], Optional[Vec3List], Vec3List]:
    length = len(r)
    if not r.direction().e.any():
        return None, None, Vec3List.new_zero(length)

    # Calculate object hits
    rec_list: HitRecordList = world.hit(r, 0.001, cp.inf)

    # Useful empty arrays
    empty_vec3list = Vec3List.new_zero(length)
    empty_array_float = cp.zeros(length, cp.float32)
    empty_array_bool = cp.zeros(length, cp.bool)
    empty_array_int = cp.zeros(length, cp.int32)

    # Background / Sky
    unit_direction = r.direction().unit_vector()
    sky_condition = Vec3List.from_array((unit_direction.length() > 0)
                                        & (rec_list.material == 0))
    t = (unit_direction.y() + 1) * 0.5
    blue_bg = (Vec3List.from_vec3(Color(1, 1, 1), length).mul_ndarray(1 - t) +
               Vec3List.from_vec3(Color(0.5, 0.7, 1), length).mul_ndarray(t))
    result_bg = Vec3List(cp.where(sky_condition.e, blue_bg.e,
                                  empty_vec3list.e))
    if depth <= 1:
        return None, None, result_bg

    # Material scatter calculations
    materials: Dict[int, Material] = world.get_materials()
    scattered_list = RayList.new_zero(length)
    attenuation_list = Vec3List.new_zero(length)
    for mat_idx in materials:
        mat_condition = (rec_list.material == mat_idx)
        mat_condition_3 = Vec3List.from_array(mat_condition)
        if not mat_condition.any():
            continue

        ray = RayList(
            Vec3List(cp.where(mat_condition_3.e, r.orig.e, empty_vec3list.e)),
            Vec3List(cp.where(mat_condition_3.e, r.dir.e, empty_vec3list.e)))
        rec = HitRecordList(
            Vec3List(
                cp.where(mat_condition_3.e, rec_list.p.e, empty_vec3list.e)),
            cp.where(mat_condition, rec_list.t, empty_array_float),
            cp.where(mat_condition, rec_list.material, empty_array_int),
            Vec3List(
                cp.where(mat_condition_3.e, rec_list.normal.e,
                         empty_vec3list.e)),
            cp.where(mat_condition, rec_list.front_face, empty_array_bool))
        ray, rec, idx_list = compress(ray, rec)

        scattered, attenuation = materials[mat_idx].scatter(ray, rec)
        scattered, attenuation = decompress(scattered, attenuation, idx_list,
                                            length)
        scattered_list += scattered
        attenuation_list += attenuation

    return scattered_list, attenuation_list, result_bg
예제 #2
0
    def __init__(self, p0: Point3, p1: Point3, mat: Material) -> None:
        self.box_min = p0
        self.box_max = p1

        self.sides = HittableList()
        self.sides.add(XYRect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), mat))
        self.sides.add(
            FlipFace(XYRect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), mat)))
        self.sides.add(XZRect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), mat))
        self.sides.add(
            FlipFace(XZRect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), mat)))
        self.sides.add(YZRect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), mat))
        self.sides.add(
            FlipFace(YZRect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), mat)))
예제 #3
0
def earth(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()

    earth_texture = ImageTexture("earthmap.jpg")
    earth_surface = Lambertian(earth_texture)
    globe = Sphere(Point3(0, 0, 0), 2, earth_surface)

    world.add(globe)
    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(0, 0, -5)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 50
    dist_to_focus: float = 10
    aperture: float = 0
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    return world_bvh, cam
예제 #4
0
def ray_color(r: Ray, world: HittableList, depth: int) -> Color:
    if depth <= 0:
        return Color(0, 0, 0)

    rec: Optional[HitRecord] = world.hit(r, 0.001, np.inf)
    if rec is not None:
        scatter_result = rec.material.scatter(r, rec)
        if scatter_result is not None:
            scattered, attenuation = scatter_result
            return attenuation * ray_color(scattered, world, depth - 1)
        return Color(0, 0, 0)

    unit_direction: Vec3 = r.direction().unit_vector()
    t = (unit_direction.y() + 1) * 0.5
    return Color(1, 1, 1) * (1 - t) + Color(0.5, 0.7, 1) * t
예제 #5
0
def simple_light(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()

    pertext = NoiseTexture(4)
    world.add(Sphere(Point3(0, -1000, 0), 1000, Lambertian(pertext)))
    world.add(Sphere(Point3(0, 2, 0), 2, Lambertian(pertext)))

    difflight = DiffuseLight(SolidColor(4, 4, 4))
    world.add(Sphere(Point3(0, 7, 0), 2, difflight))
    world.add(XYRect(3, 5, 1, 3, -2, difflight))

    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(23, 4, 5)
    lookat = Point3(0, 2, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = 10
    aperture: float = 0
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    return world_bvh, cam
예제 #6
0
def two_perlin_spheres(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()

    pertext = NoiseTexture(4)
    world.add(Sphere(Point3(0, -1000, 0), 1000, Lambertian(pertext)))
    world.add(Sphere(Point3(0, 2, 0), 2, Lambertian(pertext)))

    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(13, 2, 3)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = 10
    aperture: float = 0
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    return world_bvh, cam
예제 #7
0
def two_spheres(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()

    checker = CheckerTexture(SolidColor(0.2, 0.3, 0.1),
                             SolidColor(0.9, 0.9, 0.9))
    world.add(Sphere(Point3(0, -10, 0), 10, Lambertian(checker)))
    world.add(Sphere(Point3(0, 10, 0), 10, Lambertian(checker)))

    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(13, 2, 3)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = 10
    aperture: float = 0
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    return world_bvh, cam
예제 #8
0
def random_scene() -> HittableList:
    world = HittableList()

    ground_material = Lambertian(Color(0.5, 0.5, 0.5))
    world.add(Sphere(Point3(0, -1000, 0), 1000, ground_material))

    for a in range(-11, 11):
        for b in range(-11, 11):
            choose_mat = random_float()
            center = Point3(a + 0.9 * random_float(), 0.2,
                            b + 0.9 * random_float())

            if (center - Vec3(4, 0.2, 0)).length() > 0.9:
                if choose_mat < 0.6:
                    # Diffuse
                    albedo = Color.random() * Color.random()
                    sphere_material_diffuse = Lambertian(albedo)
                    world.add(Sphere(center, 0.2, sphere_material_diffuse))
                elif choose_mat < 0.8:
                    # Metal
                    albedo = Color.random(0.5, 1)
                    fuzz = random_float(0, 0.5)
                    sphere_material_metal = Metal(albedo, fuzz)
                    world.add(Sphere(center, 0.2, sphere_material_metal))
                else:
                    # Glass
                    sphere_material_glass = Dielectric(1.5)
                    world.add(Sphere(center, 0.2, sphere_material_glass))

    material_1 = Dielectric(1.5)
    world.add(Sphere(Point3(0, 1, 0), 1, material_1))

    material_2 = Lambertian(Color(0.4, 0.2, 0.1))
    world.add(Sphere(Point3(-4, 1, 0), 1, material_2))

    material_3 = Metal(Color(0.7, 0.6, 0.5), 0)
    world.add(Sphere(Point3(4, 1, 0), 1, material_3))

    return world
예제 #9
0
def three_ball_scene() -> HittableList:
    world = HittableList()
    world.add(Sphere(Point3(0, 0, -1), 0.5, Lambertian(Color(0.1, 0.2, 0.5))))
    world.add(
        Sphere(Point3(0, -100.5, -1), 100, Lambertian(Color(0.8, 0.8, 0))))
    world.add(Sphere(Point3(1, 0, -1), 0.5, Metal(Color(0.8, 0.6, 0.2), 0.3)))
    world.add(Sphere(Point3(-1, 0, -1), 0.5, Dielectric(1.5)))
    world.add(Sphere(Point3(-1, 0, -1), -0.45, Dielectric(1.5)))
    return world
예제 #10
0
def random_scene(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()

    # ground_material = Lambertian(SolidColor(0.5, 0.5, 0.5))
    ground_material = Lambertian(
        CheckerTexture(SolidColor(0.2, 0.3, 0.1), SolidColor(0.9, 0.9, 0.9)))
    world.add(Sphere(Point3(0, -1000, 0), 1000, ground_material))

    for a in range(-11, 11):
        for b in range(-11, 11):
            choose_mat = random_float()
            center = Point3(a + 0.9 * random_float(), 0.2,
                            b + 0.9 * random_float())

            if (center - Vec3(4, 0.2, 0)).length() > 0.9:
                if choose_mat < 0.6:
                    # Diffuse
                    albedo = Color.random() * Color.random()
                    sphere_material_diffuse = Lambertian(SolidColor(albedo))
                    center2 = center + Vec3(0, random_float(0, 0.5), 0)
                    world.add(
                        MovingSphere(center, center2, 0, 1, 0.2,
                                     sphere_material_diffuse))
                elif choose_mat < 0.8:
                    # Metal
                    albedo = Color.random(0.5, 1)
                    fuzz = random_float(0, 0.5)
                    sphere_material_metal = Metal(albedo, fuzz)
                    world.add(Sphere(center, 0.2, sphere_material_metal))
                else:
                    # Glass
                    sphere_material_glass = Dielectric(1.5)
                    world.add(Sphere(center, 0.2, sphere_material_glass))

    material_1 = Dielectric(1.5)
    world.add(Sphere(Point3(0, 1, 0), 1, material_1))

    material_2 = Lambertian(SolidColor(0.4, 0.2, 0.1))
    world.add(Sphere(Point3(-4, 1, 0), 1, material_2))

    material_3 = Metal(Color(0.7, 0.6, 0.5), 0)
    world.add(Sphere(Point3(4, 1, 0), 1, material_3))

    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(13, 2, 3)
    lookat = Point3(0, 0, 0)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = 10
    aperture: float = 0.1
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    return world_bvh, cam
예제 #11
0
def final_scene(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()

    # Ground
    boxes1 = HittableList()
    ground = Lambertian(SolidColor(0.48, 0.83, 0.53))
    boxes_per_side = 20
    for i in range(boxes_per_side):
        for j in range(boxes_per_side):
            w = 100
            x0 = -1000 + i * w
            z0 = -1000 + j * w
            y0 = 0
            x1 = x0 + w
            y1 = random_float(1, 101)
            z1 = z0 + w
            boxes1.add(Box(Point3(x0, y0, x0), Point3(x1, y1, z1), ground))
    world.add(BVHNode(boxes1.objects, time0, time1))

    # Light
    light = DiffuseLight(SolidColor(7, 7, 7))
    world.add(XZRect(123, 423, 147, 412, 554, light))

    # Moving sphere
    center1 = Point3(400, 400, 200)
    center2 = center1 + Vec3(30, 0, 0)
    moving_sphere_material = Lambertian(SolidColor(0.7, 0.3, 0.1))
    world.add(MovingSphere(center1, center2, 0, 1, 50, moving_sphere_material))

    # Dielectric & metal balls
    world.add(Sphere(Point3(260, 150, 45), 50, Dielectric(1.5)))
    world.add(
        Sphere(Point3(0, 150, 145), 50, Metal(Color(0.8, 0.8, 0.9), 10.0)))

    # The subsurface reflection sphere
    boundary = Sphere(Point3(360, 150, 145), 70, Dielectric(1.5))
    world.add(boundary)
    world.add(ConstantMedium(boundary, 0.2, SolidColor(0.2, 0.4, 0.9)))

    # Big thin mist
    mist = Sphere(Point3(0, 0, 0), 5000, Dielectric(1.5))
    world.add(ConstantMedium(mist, 0.0001, SolidColor(1, 1, 1)))

    # Earth and marble ball
    emat = Lambertian(ImageTexture("earthmap.jpg"))
    world.add(Sphere(Point3(400, 200, 400), 100, emat))
    pertext = NoiseTexture(0.1)
    world.add(Sphere(Point3(220, 280, 300), 80, Lambertian(pertext)))

    # Foam
    boxes2 = HittableList()
    white = Lambertian(SolidColor(0.73, 0.73, 0.73))
    ns = 1000
    for j in range(ns):
        boxes2.add(Sphere(Point3.random(0, 165), 10, white))
    world.add(
        Translate(RotateY(BVHNode(boxes2.objects, time0, time1), 15),
                  Vec3(-100, 270, 395)))

    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(478, 278, -600)
    lookat = Point3(278, 278, 0)
    vup = Vec3(0, 1, 0)
    vfov = 40
    dist_to_focus: float = 10
    aperture: float = 0
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    return world_bvh, cam
예제 #12
0
def cornell_box(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()

    # Colors
    red = Lambertian(SolidColor(0.65, 0.05, 0.05))
    white = Lambertian(SolidColor(0.73, 0.73, 0.73))
    green = Lambertian(SolidColor(0.12, 0.45, 0.15))
    light = DiffuseLight(SolidColor(15, 15, 15))

    # Outer box
    world.add(FlipFace(YZRect(0, 555, 0, 555, 555, green)))
    world.add(YZRect(0, 555, 0, 555, 0, red))
    world.add(XZRect(213, 343, 227, 332, 554, light))
    world.add(XZRect(0, 555, 0, 555, 0, white))
    world.add(FlipFace(XZRect(0, 555, 0, 555, 555, white)))
    world.add(FlipFace(XYRect(0, 555, 0, 555, 555, white)))

    # Objects in the box
    box1: Hittable = Box(Vec3(0, 0, 0), Point3(165, 330, 165), white)
    box1 = RotateY(box1, 15)
    box1 = Translate(box1, Point3(265, 0, 295))
    box1 = ConstantMedium(box1, 0.01, SolidColor(0, 0, 0))
    world.add(box1)

    box2: Hittable = Box(Point3(0, 0, 0), Point3(165, 165, 165), white)
    box2 = RotateY(box2, -18)
    box2 = Translate(box2, Point3(130, 0, 65))
    box2 = ConstantMedium(box2, 0.01, SolidColor(1, 1, 1))
    world.add(box2)

    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(278, 278, -800)
    lookat = Point3(278, 278, 0)
    vup = Vec3(0, 1, 0)
    vfov = 40
    dist_to_focus: float = 10
    aperture: float = 0
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    return world_bvh, cam
예제 #13
0
def three_ball_scene(aspect_ratio: float, time0: float, time1: float) \
        -> Tuple[BVHNode, Camera]:
    world = HittableList()
    world.add(
        Sphere(Point3(0, 0, -1), 0.5, Lambertian(SolidColor(0.1, 0.2, 0.5))))
    world.add(
        Sphere(Point3(0, -100.5, -1), 100, Lambertian(SolidColor(0.8, 0.8,
                                                                 0))))
    world.add(Sphere(Point3(1, 0, -1), 0.5, Metal(Color(0.8, 0.6, 0.2), 0.3)))
    world.add(Sphere(Point3(-1, 0, -1), 0.5, Dielectric(1.5)))
    world.add(Sphere(Point3(-1, 0, -1), -0.45, Dielectric(1.5)))
    world_bvh = BVHNode(world.objects, time0, time1)

    lookfrom = Point3(3, 3, 2)
    lookat = Point3(0, 0, -1)
    vup = Vec3(0, 1, 0)
    vfov = 20
    dist_to_focus: float = (lookfrom - lookat).length()
    aperture: float = 0
    cam = Camera(lookfrom, lookat, vup, vfov, aspect_ratio, aperture,
                 dist_to_focus, time0, time1)

    # lookfrom = Point3(13, 2, 3)
    # lookat = Point3(0, 0, 0)
    # vup = Vec3(0, 1, 0)
    # vfov = 20
    # dist_to_focus: float = 10
    # aperture: float = 0.1
    # cam = Camera(
    #     lookfrom, lookat, vup, vfov, aspect_ratio, aperture, dist_to_focus,
    #     time0, time1
    # )

    return world_bvh, cam
예제 #14
0
def ray_color(r: RayList, world: HittableList, depth: int) -> Vec3List:
    length = len(r)
    if not r.direction().e.any():
        return Vec3List.new_zero(length)

    # Calculate object hits
    rec_list: HitRecordList = world.hit(r, 0.001, np.inf)

    # Useful empty arrays
    empty_vec3list = Vec3List.new_zero(length)
    empty_array_float = np.zeros(length, np.float32)
    empty_array_bool = np.zeros(length, np.bool)
    empty_array_int = np.zeros(length, np.int32)

    # Background / Sky
    unit_direction = r.direction().unit_vector()
    sky_condition = Vec3List.from_array((unit_direction.length() > 0)
                                        & (rec_list.material == 0))
    t = (unit_direction.y() + 1) * 0.5
    blue_bg = (Vec3List.from_vec3(Color(1, 1, 1), length).mul_ndarray(1 - t) +
               Vec3List.from_vec3(Color(0.5, 0.7, 1), length).mul_ndarray(t))
    result_bg = Vec3List(np.where(sky_condition.e, blue_bg.e,
                                  empty_vec3list.e))
    if depth <= 1:
        return result_bg

    # Per-material preparations
    materials: Dict[int, Material] = world.get_materials()
    material_dict: Dict[int, Tuple[RayList, HitRecordList]] = dict()
    for mat_idx in materials:
        mat_condition = (rec_list.material == mat_idx)
        mat_condition_3 = Vec3List.from_array(mat_condition)
        if not mat_condition.any():
            continue
        raylist_temp = RayList(
            Vec3List(np.where(mat_condition_3.e, r.orig.e, empty_vec3list.e)),
            Vec3List(np.where(mat_condition_3.e, r.dir.e, empty_vec3list.e)))
        reclist_temp = HitRecordList(
            Vec3List(
                np.where(mat_condition_3.e, rec_list.p.e, empty_vec3list.e)),
            np.where(mat_condition, rec_list.t, empty_array_float),
            np.where(mat_condition, rec_list.material, empty_array_int),
            Vec3List(
                np.where(mat_condition_3.e, rec_list.normal.e,
                         empty_vec3list.e)),
            np.where(mat_condition, rec_list.front_face, empty_array_bool))
        material_dict[mat_idx] = raylist_temp, reclist_temp

    # Material scatter calculations
    scattered_list = RayList.new_zero(length)
    attenuation_list = Vec3List.new_zero(length)
    for key in material_dict:
        ray, rec = material_dict[key]
        ray, rec, idx_list = compress(ray, rec)

        scattered, attenuation = materials[key].scatter(ray, rec)
        scattered, attenuation = decompress(scattered, attenuation, idx_list,
                                            length)
        scattered_list += scattered
        attenuation_list += attenuation
    result_hittable = (attenuation_list *
                       ray_color(scattered_list, world, depth - 1))

    return result_hittable + result_bg
예제 #15
0
class Box(Hittable):
    def __init__(self, p0: Point3, p1: Point3, mat: Material) -> None:
        self.box_min = p0
        self.box_max = p1

        self.sides = HittableList()
        self.sides.add(XYRect(p0.x(), p1.x(), p0.y(), p1.y(), p1.z(), mat))
        self.sides.add(
            FlipFace(XYRect(p0.x(), p1.x(), p0.y(), p1.y(), p0.z(), mat)))
        self.sides.add(XZRect(p0.x(), p1.x(), p0.z(), p1.z(), p1.y(), mat))
        self.sides.add(
            FlipFace(XZRect(p0.x(), p1.x(), p0.z(), p1.z(), p0.y(), mat)))
        self.sides.add(YZRect(p0.y(), p1.y(), p0.z(), p1.z(), p1.x(), mat))
        self.sides.add(
            FlipFace(YZRect(p0.y(), p1.y(), p0.z(), p1.z(), p0.x(), mat)))

    def hit(self, r: Ray, t_min: float, t_max: float) -> Optional[HitRecord]:
        return self.sides.hit(r, t_min, t_max)

    def bounding_box(self, t0: float, t1: float) -> Optional[AABB]:
        return AABB(self.box_min, self.box_max)
def three_ball_scene():
    world = HittableList()
    world.add(
        Sphere(Point3(0, 0, -1), 0.5, Lambertian(Color(0.1, 0.2, 0.5), 1)))
    world.add(
        Sphere(Point3(0, -100.5, -1), 100, Lambertian(Color(0.8, 0.8, 0), 2)))
    world.add(
        Sphere(Point3(1, 0, -1), 0.5, Metal(Color(0.8, 0.6, 0.2), 0.3, 3)))
    material_dielectric = Dielectric(1.5, 4)
    world.add(Sphere(Point3(-1, 0, -1), 0.5, material_dielectric))
    world.add(Sphere(Point3(-1, 0, -1), -0.45, material_dielectric))
    return world