def hit(self, ray, t0, t1, rec): org = ray.origin.clone() dir = ray.direction.clone() axis = vm.Vec3.zaxis() if self.axis == RECT_AXIS_TYPE_XZ: org = vm.Point3(org.x(), org.z(), org.y()) dir = vm.Vec3(dir.x(), dir.z(), dir.y()) axis = vm.Vec3.yaxis() elif self.axis == RECT_AXIS_TYPE_YZ: org = vm.Point3(org.y(), org.z(), org.x()) dir = vm.Vec3(dir.y(), dir.z(), dir.x()) axis = vm.Vec3.xaxis() t = (self.k - org.z()) / dir.z() if t < t0 or t > t1: return False x = org.x() + t * dir.x() y = org.y() + t * dir.y() if x < self.x0 or x > self.x1 or y < self.y0 or y > self.y1: return False rec.u = (x - self.x0) / (self.x1 - self.x0) rec.v = (y - self.y0) / (self.y1 - self.y0) rec.t = t rec.m = self.material.clone() rec.p = ray.at(t) rec.n = axis return True
def build_random(self): ny, nx, _ = self.image.shape self.camera = Camera.lookat(vm.Point3(13., 2., 3.), vm.Point3(), vm.Vec3.yaxis(), 20., float(nx) / float(ny)) self.world = ShapeList() N = 11 for i in range(-N, N): for j in range(-N, N): rnd = vm.Float3.random() choose_mat = rnd.x() ofx = rnd.y() ofz = rnd.z() center = vm.Point3( float(i) + 0.9 * ofx, 0.2, float(j) + 0.9 * ofz) if (center - vm.Point3(4., 0.2, 0.)).length() > 0.9: if choose_mat < 0.8: self.world.add(ShapeBuilder().color_texture( vm.Color.random()).lambertian().sphere( center, 0.2).build()) elif choose_mat < 0.95: self.world.add(ShapeBuilder().color_texture( vm.Color.random()).metal(0.5 * rnd.x()).sphere( center, 0.2).build()) else: self.world.add(ShapeBuilder().dielectric(1.5).sphere( center, 0.2).build()) self.world.add(ShapeBuilder().color_texture( vm.Color.full(0.5)).lambertian().sphere(vm.Point3(0., -1000., -1.), 1000.).build())
def random(self, o): if self.axis != RECT_AXIS_TYPE_XZ: return vm.Vec3.xaxis() rnd = vm.Vec3.random() x = self.x0 + rnd.x() * (self.x1 - self.x0) y = self.y0 + rnd.y() * (self.y1 - self.y0) random_point = vm.Point3(x, y, self.k) if self.axis == RECT_AXIS_TYPE_XZ: random_point = vm.Point3(x, self.k, y) elif self.axis == RECT_AXIS_TYPE_YZ: random_point = vm.Point3(self.k, x, y) return random_point - o
def random(self, o): n = len(self.shapes) if n == 0: return vm.Point3() index = int(float(n) * vm.Float3.random().x()) if index > 0 and index >= n: index = n - 1 return self.shapes[index].random(o)
def build(self): self.camera = Camera(vm.Point3(), vm.Vec3(4., 0., 0.), vm.Vec3(0., 2., 0.), vm.Vec3(-2., -1., -1.)) self.world = ShapeList() self.world.add(ShapeBuilder().color_texture(vm.Color( 0.1, 0.2, 0.5)).lambertian().sphere(vm.Point3(0.6, 0, -1.), 0.5).build()) # self.world.add(ShapeBuilder() # .image_texture('assets/brick_diffuse.jpg') # .lambertian() # .sphere(vm.Point3(0.6, 0, -1.), 0.5) # .build()) # self.world.add(ShapeBuilder() # .color_texture(vm.Color.full(10.0)) # .diffuse_light() # .sphere(vm.Point3(-0.6, 0, -1.), 0.5) # .build()) self.world.add(ShapeBuilder().color_texture( vm.Color.full(0.8)).metal(0.2).sphere(vm.Point3(-0.6, 0, -1.), 0.5).build()) # self.world.add(ShapeBuilder() # .dielectric(1.5) # .sphere(vm.Point3(-0.6, 0, -1.), 0.5) # .build()) # self.world.add(ShapeBuilder() # .dielectric(1.5) # .sphere(vm.Point3(-0.6, 0, -1.), -0.45) # .build()) # self.world.add(ShapeBuilder() # .color_texture(vm.Color.full(0.8)) # .metal(0.2) # .sphere(vm.Point3(0., -0.35, -0.8), 0.15) # .build()) self.world.add(ShapeBuilder().color_texture(vm.Color( 10., 0., 0.)).diffuse_light().rect_xy(-2., 2., 0.2, 2., -2.).build()) self.world.add(ShapeBuilder().checker_texture( vm.Color(0.8, 0.8, 0.), vm.Color(0.8, 0.2, 0.), 10.).lambertian().sphere(vm.Point3(0., -100.5, -1.), 100.).build())
def build_cornell_box(self): ny, nx, _ = self.image.shape self.camera = Camera.lookat(vm.Point3(278., 278., -800.), vm.Point3(278., 278., 0.), vm.Vec3.yaxis(), 40., float(nx) / float(ny)) self.world = ShapeList() self.world.add(ShapeBuilder().color_texture(vm.Color( 0.12, 0.45, 0.15)).lambertian().rect_yz(0., 555., 0., 555., 555.).flip_face().build()) self.world.add(ShapeBuilder().color_texture(vm.Color( 0.65, 0.05, 0.05)).lambertian().rect_yz(0., 555., 0., 555., 0.).build()) self.world.add(ShapeBuilder().color_texture( vm.Color.full(15.)).diffuse_light().rect_xz( 213., 343., 227., 332., 554.).flip_face().build()) self.world.add(ShapeBuilder().color_texture( vm.Color.full(0.73)).lambertian().rect_xz( 0., 555., 0., 555., 555.).flip_face().build()) self.world.add(ShapeBuilder().color_texture( vm.Color.full(0.73)).lambertian().rect_xz(0., 555., 0., 555., 0.).build()) self.world.add(ShapeBuilder().color_texture( vm.Color.full(0.73)).lambertian().rect_xy( 0., 555., 0., 555., 555.).flip_face().build()) self.world.add(ShapeBuilder().dielectric(1.5).sphere( vm.Point3(190., 90., 190.), 90.).build()) self.world.add( ShapeBuilder().color_texture(vm.Color.full(0.73)).lambertian().box( vm.Point3(), vm.Point3(165., 330., 165.)).rotate( vm.Vec3.yaxis(), 15.).translate(vm.Point3(265., 0., 295.)).build()) self.light = ShapeList() self.light.add(ShapeBuilder().default_material().rect_xz( 213., 343., 227., 332., 554.).build()) self.light.add(ShapeBuilder().default_material().sphere( vm.Point3(190., 90., 190.), 90.).build()) self.bgcolor = vm.Color()
def __init__(self): self.t = 0. self.p = vm.Point3() self.n = vm.Vec3.xaxis() self.u = 0. self.v = 0.
def __init__(self): self.ray = Ray(vm.Point3(), vm.Vec3.xaxis()) self.albedo = vm.Color() self.pdf = None self.is_specular = False