def random_scene(n=500): list = [] list.append( sphere(vec3(0, -1000, 0), 1000, lambertian(vec3(0.5, 0.5, 0.5)))) for a in range(-11, 11): for b in range(-11, 11): choose_mat = random() center = vec3(a + 0.9 * random(), 0.2, b + 0.9 * random()) if length(center - vec3(4, 0.2, 0)) > 0.9: if choose_mat < 0.8: list.append( sphere( center, 0.2, lambertian( vec3(random() * random(), random() * random(), random() * random())))) elif choose_mat < 0.95: list.append( sphere( center, 0.2, metal( vec3(0.5 * (1 + random()), 0.5 * (1 + random()), 0.5 * (1 + random())), 0.5 * random()))) else: list.append(sphere(center, 0.2, dielectric(1.5))) list.append(sphere(vec3(0, 1, 0), 1.0, dielectric(1.5))) list.append(sphere(vec3(-4, 1, 0), 1.0, lambertian(vec3(0.4, 0.2, 0.1)))) list.append(sphere(vec3(4, 1, 0), 1.0, metal(vec3(0.7, 0.6, 0.5), 0.0))) return list
def two_perlin_spheres() -> Hitable: pertext = noise_texture(0.1) h_list = list() h_list.append( Sphere(np.array((0, -1000, 0)), 1000, material.lambertian(pertext))) h_list.append(Sphere(np.array((0, 2, 0)), 2, material.lambertian(pertext))) return hitable_list(h_list)
def two_spheres() -> Hitable: checker = checker_texture( constant_texture(np.array((0.2, 0.3, 0.1), float)), constant_texture(np.array((0.9, 0.9, 0.9), float))) h_list = list() h_list.append( Sphere(np.array((0, -10, 0)), 10, material.lambertian(checker))) h_list.append( Sphere(np.array((0, 10, 0)), 10, material.lambertian(checker))) return hitable_list(h_list)
def random_scene(): lista = list() lista.append( Sphere( np.array((0, -1000, 0)), 1000, material.lambertian( checker_texture(constant_texture(np.array((0.2, 0.3, 1))), constant_texture(np.array((0.9, 0.9, 0.9))))))) for a in range(-11, 11): for b in range(-11, 11): choose_mat = random() center: np.ndarray = np.array( (a + 0.9 * random(), 0.2, b + 0.9 * random())) if np.linalg.norm(center - np.array((4.0, 0.2, 0.0))) > 0.9: if choose_mat < 0.8: # diffuse lista.append( Sphere( center, 0.2, material.lambertian( constant_texture( np.array((random() * random(), random() * random(), random() * random())))))) elif choose_mat < 0.95: # metal lista.append( Sphere( center, 0.2, material.metal( np.array((0.5 * (1 + random()), 0.5 * (1 + random()), 0.5 * (1 + random()))), 0.5 * random()))) else: lista.append(Sphere(center, 0.2, material.dielectric(1.5))) lista.append(Sphere(np.array((0, 1, 0)), 1, material.dielectric(1.5))) lista.append( Sphere( np.array((-4, 1, 0)), 1, material.lambertian(constant_texture(np.array((0.4, 0.2, 0.1)))))) lista.append( Sphere(np.array((4, 1, 0)), 1, material.metal(np.array((0.7, 0.6, 0.5)), 0.0))) return hitable_list(lista)
def simple_light(): pertext = noise_texture(4) h_list = list() checker = checker_texture( constant_texture(np.array((0.2, 0.3, 0.1), float)), constant_texture(np.array((0.9, 0.9, 0.9), float))) h_list.append( Sphere(np.array((0, -1000, 0)), 1000, material.lambertian(checker))) h_list.append(Sphere(np.array((0, 2, 0)), 2, material.lambertian(pertext))) h_list.append(Sphere(np.array((2, 2, -2)), 2, material.dielectric(1))) h_list.append( moving_sphere(np.array((-2, 2, 2)), np.array((-2, 2, 2)), 0, 1, 2, material.lambertian(pertext))) h_list.append( Sphere(np.array((0, 7, 0)), 2, material.diffuse_light(constant_texture(np.array((4, 4, 4)))))) h_list.append( xy_rect(0, 50, 0, 30, -2, material.diffuse_light(constant_texture(np.array((4, 4, 4)))))) return hitable_list(h_list)
lookfrom: np.ndarray = np.array((3, 0, 2)) lookat: np.ndarray = np.array((0, 0, -1)) dist_to_focus = np.linalg.norm(lookfrom - lookat) aperture = 2 # cam = Camera(np.array((0, 0, 0)), np.array((0, 0, -1)), np.array((0, 1, 0)), 90, nx / ny) cam = Camera(lookfrom, lookat, np.array((0, 1, 0)), 20, float(nx) / float(ny), aperture, dist_to_focus) R = np.cos(np.pi / 4) h_list = list() # h_list.append(Sphere(np.array((-R, 0, -1)), R, material.lambertian(np.array((0., 0., 1.))))) # h_list.append(Sphere(np.array((R, 0, -1)), R, material.lambertian(np.array((1., 0., 0.))))) h_list.append( Sphere(np.array((0, 0, -1)), 0.5, material.lambertian(np.array((0.1, 0.2, 0.5))))) h_list.append( Sphere(np.array((0, -100.5, -1)), 100, material.lambertian(np.array((0.8, 0.8, 0))))) h_list.append( Sphere(np.array((1, 0, -1)), 0.5, material.metal(np.array((0.8, 0.6, 0.8)), 0.3))) h_list.append(Sphere(np.array((-1, 0, -1)), 0.5, material.dielectric(1.5))) # h_list.append(Sphere(np.array((-1, 0, -1)), -0.45, material.dielectric(1.5))) # h_list.append(Sphere(np.array((-5, 2.5, -5)), 2, material.lambertian(np.array((0.1, 0.2, 0.5))))) world: Hitable = hitable_list(h_list) seed(time_ns()) t_init = time_ns() for j in range(ny, 0, -1): for i in range(0, nx):
f.write("P3\n{} {}\n255".format(nx, ny)) lookfrom: np.ndarray = np.array((12, 2, 3)) lookat: np.ndarray = np.array((0, 0, 0)) dist_to_focus = np.linalg.norm(lookfrom - lookat) aperture = 0 # cam = Camera(np.array((0, 0, 0)), np.array((0, 0, -1)), np.array((0, 1, 0)), 90, nx / ny) cam = Camera(lookfrom, lookat, np.array((0, 1, 0)), 40, float(nx) / float(ny), aperture, dist_to_focus, 0.0, 0.1) R = np.cos(np.pi / 4) h_list = list() # h_list.append(Sphere(np.array((-R, 0, -1)), R, material.lambertian(np.array((0., 0., 1.))))) # h_list.append(Sphere(np.array((R, 0, -1)), R, material.lambertian(np.array((1., 0., 0.))))) h_list.append(moving_sphere(np.array((0, 0, -1)), np.array((0, 1, -1)), 0.0, 1.0, 0.5, material.lambertian(constant_texture(np.array((0.1, 0.2, 0.5)))))) h_list.append( Sphere(np.array((0, -100.5, -1)), 100, material.lambertian(constant_texture(np.array((0.8, 0.8, 0)))))) h_list.append(Sphere(np.array((1, 0, -1)), 0.5, material.metal(np.array((1, 1, 1)), 0.0))) # h_list.append(Sphere(np.array((-1, 0, -1)), 0.5, material.dielectric(1.5))) # h_list.append(Sphere(np.array((-1, 0, -1)), -0.45, material.dielectric(1.5))) # h_list.append(Sphere(np.array((-5, 2.5, -5)), 2, material.lambertian(np.array((0.1, 0.2, 0.5))))) # world: Hitable = hitable_list(scenes.random_scene()) world = bvh_node(scenes.simple_light(), 0, 0) # world = scenes.two_perlin_spheres() img = dict() seed(time_ns()) t_init = time_ns() for j in range(ny, 0, -1): for i in range(0, nx): col = np.array((0, 0, 0), float)