def atest_material_manager(self): sam_mgr = SampledManager() register_rgb_shadepoint() runtimes = [Runtime(), Runtime()] mat = Material() mat.load('lambertian', sam_mgr, spectral=False) mat.set_value('diffuse', RGBSpectrum(0.2, 0.3, 0.4)) mgr = MaterialManager() mgr.add('material1', mat) mgr.compile_shaders(sam_mgr, spectral=False) mgr.prepare_shaders(runtimes) code = """ hp = HitPoint() sp = ShadePoint() material_reflectance(hp, sp, 0) spec = sp.material_reflectance """ spec = RGBArg('spec', RGBSpectrum(0.5, 0.5, 0.5)) shader = Shader(code=code, args=[spec]) shader.compile(shaders=[mgr.ref_shader]) shader.prepare(runtimes) shader.execute() s = shader.get_value('spec') ls = RGBSpectrum(0.2, 0.3, 0.4) * (1.0 / math.pi) self.assertAlmostEqual(s.r, ls.r) self.assertAlmostEqual(s.g, ls.g) self.assertAlmostEqual(s.b, ls.b)
def test_rgb_area_light(self): sam_mgr = SampledManager() register_rgb_shadepoint() point = Vector3(0.0, 0.0, 55.92) e1 = Vector3(55.28, 0.0, 0.0) e2 = Vector3(0.0, 54.88, 0.0) normal = Vector3(0.0, 0.0, -1.0) rectangle = Rectangle(point, e1, e2, normal) material = Material() material.load('lambertian_emiter', sam_mgr) e = RGBSpectrum(0.5, 0.5, 0.5) material.set_value('emission', e) runtimes = [Runtime()] lgt = AreaLight(shape=rectangle, material=material) lgt.load('general', sam_mgr, spectral=False) lgt.compile() lgt.prepare(runtimes) ptrs = lgt.shader.get_ptrs() ptr_func = PointerArg('ptr_func', ptrs[0]) spec = RGBArg('spec', RGBSpectrum(0.5, 0.5, 0.5)) wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) pos = Vec3Arg('position', Vector3(0.0, 0.0, 0.0)) n = Vec3Arg('normal', Vector3(0.0, 0.0, 0.0)) pdf = FloatArg('pdf', 0.0) emission = RGBArg('emission', RGBSpectrum(0.0, 0.0, 0.0)) code = """ hp = HitPoint() hp.hit = (4.0, 5, 6) sp = ShadePoint() __light_radiance(hp, sp, ptr_func) spec = sp.light_intensity wi = sp.wi position = sp.light_position normal = sp.light_normal pdf = sp.light_pdf emission = sp.material_emission """ shader = Shader(code=code, args=[ptr_func, wi, spec, pos, n, pdf, emission]) shader.compile() shader.prepare(runtimes) shader.execute() print("Position ", shader.get_value('position')) print("Normal ", shader.get_value('normal')) print("Light pdf ", shader.get_value('pdf')) print("Emission ", shader.get_value('emission')) print("Wi ", shader.get_value('wi')) print("Intensity ", shader.get_value('spec'))
def _parse_rgb(line, color_mgr=None): t = line.split('=', maxsplit=1) name = t.pop(0).strip() if len(t) == 0: if color_mgr is None: return RGBArg(name, RGBSpectrum(0.0, 0.0, 0.0)) else: return arg_from_value(name, color_mgr.zero()) v = t.pop(0).strip().split(',') spectrum = RGBSpectrum(float(v[0]), float(v[1]), float(v[2])) if color_mgr is None: return RGBArg(name, spectrum) else: return arg_from_value(name, color_mgr.convert_spectrum(spectrum))
def test_sampled_point_light(self): sam_mgr = SampledManager() register_sampled_shadepoint(sam_mgr) runtimes = [Runtime()] lgt = GeneralLight() lgt.load('point', sam_mgr, spectral=True) lgt.set_value('intensity', RGBSpectrum(0.3, 0.3, 0.3)) lgt.set_value('position', Vector3(2.0, 2.0, 2.0)) lgt.compile() lgt.prepare(runtimes) ptrs = lgt.shader.get_ptrs() ptr_func = PointerArg('ptr_func', ptrs[0]) spec = SampledArg('spec', sam_mgr.zero()) wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) pos = Vec3Arg('position', Vector3(0.0, 0.0, 0.0)) code = """ hp = HitPoint() hp.hit = (4.0, 5, 6) sp = ShadePoint() __light_radiance(hp, sp, ptr_func) spec = sp.light_intensity wi = sp.wi position = sp.light_position """ shader = Shader(code=code, args=[ptr_func, wi, spec, pos]) shader.compile() shader.prepare(runtimes) shader.execute() wi = Vector3(2.0, 2.0, 2.0) - Vector3(4.0, 5.0, 6.0) wi.normalize() wi_s = shader.get_value('wi') self.assertAlmostEqual(wi.x, wi_s.x) self.assertAlmostEqual(wi.y, wi_s.y) self.assertAlmostEqual(wi.z, wi_s.z) p = Vector3(2.0, 2.0, 2.0) p_s = shader.get_value('position') self.assertAlmostEqual(p.x, p_s.x) self.assertAlmostEqual(p.y, p_s.y) self.assertAlmostEqual(p.z, p_s.z) s = sam_mgr.rgb_to_sampled(RGBSpectrum(0.3, 0.3, 0.3), illum=True) s_s = shader.get_value('spec') for i in range(len(s.samples)): self.assertAlmostEqual(s.samples[i], s_s.samples[i])
def _value_factory(old_val, val, color_mgr, light=False): if isinstance(old_val, Vector3): return Vector3(float(val[0]), float(val[1]), float(val[2])) elif isinstance(old_val, RGBSpectrum): #TODO parsing of spectrum values from file return RGBSpectrum(float(val[0]), float(val[1]), float(val[2])) elif isinstance(old_val, SampledSpectrum): #TODO parsing of spectrum values from file s = RGBSpectrum(float(val[0]), float(val[1]), float(val[2])) return color_mgr.rgb_to_sampled(s, illum=light) elif isinstance(old_val, float): return float(val[0]) else: raise ValueError("Unknown type ", old_val)
def _rgb_luminance_shader(rgb_mgr): code = """ return rgb[0] * 0.212671 + rgb[1] * 0.715160 + rgb[2] * 0.072169 """ rgb = RGBArg('rgb', RGBSpectrum(0.0, 0.0, 0.0)) shader = Shader(code=code, name='luminance', func_args=[rgb], is_func=True) return shader
def test_sampled_to_rgb(self): runtimes = [Runtime()] color_mgr = SampledManager() s_to_rgb = spectrum_to_rgb_shader(color_mgr) s_to_rgb.compile(color_mgr=color_mgr) s_to_rgb.prepare(runtimes) code = """ value = spectrum_to_rgb(color) """ spec = RGBSpectrum(0.3, 0.5, 0.4) spec = color_mgr.rgb_to_sampled(spec) col = SampledArg('color', spec) val = Vec3Arg('value', Vector3(0.0, 0.0, 0.0)) shader = Shader(code=code, args=[col, val]) shader.compile(shaders=[s_to_rgb], color_mgr=color_mgr) shader.prepare(runtimes) shader.execute() value = shader.get_value('value') vv = color_mgr.sampled_to_rgb(spec) self.assertAlmostEqual(value.x, vv.r) self.assertAlmostEqual(value.y, vv.g) self.assertAlmostEqual(value.z, vv.b, places=6)
def sampling_glass(ior, wo, normal): incident = wo * -1.0 n1 = 1.0 n2 = ior ndotwo = normal.dot(wo) if ndotwo < 0.0: normal = normal * -1.0 n1 = ior n2 = 1.0 ret = tir(normal, incident, n1, n2) if ret == 1: # TIR ocur wi = reflect(normal, incident) pdf = 1.0 reflectance = RGBSpectrum(1.0, 1.0, 1.0) print("TIR ocured") else: rnd = random.random() rnd = 0.3 k = 0.5 R = dielectric_reflectance(normal, incident, n1, n2) P = (1.0 - k) * R + 0.5 * k print(R, P, n1, n2) print("ACOS", math.degrees(math.acos(normal.dot(wo)))) # R = 1.0 # P = 1.0 if rnd < P: # reflection ray wi = reflect(normal, incident) pdf = P reflectance = RGBSpectrum(1.0, 1.0, 1.0) * R else: # transmission ray wi = refract(normal, incident, n1, n2) pdf = 1.0 - P T = 1.0 - R eta = (n1 * n1) / (n2 * n2) reflectance = RGBSpectrum(1.0, 1.0, 1.0) * T * eta ndotwi = normal.dot(wi) reflectance = reflectance * (1.0 / abs(ndotwi)) return wi, pdf, reflectance
def _rgb_to_rgb_shader(rgb_mgr): code = """ return float3(rgb[0], rgb[1], rgb[2]) """ rgb = RGBArg('rgb', RGBSpectrum(0.0, 0.0, 0.0)) shader = Shader(code=code, name='spectrum_to_rgb', func_args=[rgb], is_func=True) return shader
def test_material_sampling_manager(self): sam_mgr = SampledManager() register_rgb_shadepoint() runtimes = [Runtime(), Runtime()] mat = Material() mat.load('lambertian', sam_mgr, spectral=False) mat.set_value('diffuse', RGBSpectrum(0.2, 0.3, 0.4)) mgr = MaterialManager() mgr.add('material1', mat) mgr.compile_shaders(sam_mgr, spectral=False) mgr.prepare_shaders(runtimes) code = """ hp = HitPoint() hp.normal = (0.1, 0.4, 0.66) hp.normal = normalize(hp.normal) sp = ShadePoint() material_sampling(hp, sp, 0) pdf = sp.pdf wi = sp.wi spec = sp.material_reflectance """ pdf = FloatArg('pdf', 0.0) wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) spec = RGBArg('spec', RGBSpectrum(0.5, 0.5, 0.5)) shader = Shader(code=code, args=[pdf, wi, spec]) shader.compile(shaders=[mgr.sampling_shader]) shader.prepare(runtimes) shader.execute() s = shader.get_value('pdf') print(s) s = shader.get_value('wi') print(s) s = shader.get_value('spec') print(s) normal = Vector3(0.1, 0.4, 0.66) normal.normalize() print(cos_hemisphere(r1=0.1, r2=0.06, normal=normal, e=1.0))
def test_light_manager(self): sam_mgr = SampledManager() register_rgb_shadepoint() runtimes = [Runtime(), Runtime()] lgt = GeneralLight() lgt.load('point', sam_mgr, spectral=False) lgt2 = GeneralLight() lgt2.load('point', sam_mgr, spectral=False) lgt2.set_value('intensity', RGBSpectrum(0.3, 0.3, 0.3)) lgt2.set_value('position', Vector3(2.0, 2.0, 2.0)) mgr = LightManager() mgr.add('light1', lgt) mgr.add('light2', lgt2) mgr.compile_shaders(sam_mgr, spectral=False) mgr.prepare_shaders(runtimes) code = """ hp = HitPoint() hp.hit = (4.0, 5, 6) sp = ShadePoint() light_radiance(hp, sp, 1) wi = sp.wi n = number_of_lights() """ wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) nlights = IntArg('n', 999) shader = Shader(code=code, args=[wi, nlights]) shader.compile(shaders=[mgr.rad_shader, mgr.nlights_shader]) shader.prepare(runtimes) shader.execute() wi = Vector3(2.0, 2.0, 2.0) - Vector3(4.0, 5.0, 6.0) wi.normalize() wi_s = shader.get_value('wi') self.assertAlmostEqual(wi.x, wi_s.x) self.assertAlmostEqual(wi.y, wi_s.y) self.assertAlmostEqual(wi.z, wi_s.z) self.assertEqual(2, shader.get_value('n'))
def test_rgb_to_rgb(self): runtimes = [Runtime()] color_mgr = RGBManager() s_to_rgb = spectrum_to_rgb_shader(color_mgr) s_to_rgb.compile(color_mgr=color_mgr) s_to_rgb.prepare(runtimes) code = """ value = spectrum_to_rgb(color) """ spec = RGBSpectrum(0.3, 0.5, 0.4) col = RGBArg('color', spec) val = Vec3Arg('value', Vector3(0.0, 0.0, 0.0)) shader = Shader(code=code, args=[col, val]) shader.compile(shaders=[s_to_rgb], color_mgr=color_mgr) shader.prepare(runtimes) shader.execute() value = shader.get_value('value') self.assertAlmostEqual(value.x, 0.3) self.assertAlmostEqual(value.y, 0.5) self.assertAlmostEqual(value.z, 0.4)
def test_rgb_to_rgb_spectrum(self): runtimes = [Runtime()] color_mgr = RGBManager() rgb_to_spec = rgb_to_spectrum_shader(color_mgr) rgb_to_spec.compile(color_mgr=color_mgr) rgb_to_spec.prepare(runtimes) code = """ spec = rgb_to_spectrum(color) """ val = Vector3(0.3, 0.5, 0.7) col = Vec3Arg('color', val) spec = RGBArg('spec', RGBSpectrum(0.0, 0.0, 0.0)) shader = Shader(code=code, args=[col, spec]) shader.compile(shaders=[rgb_to_spec], color_mgr=color_mgr) shader.prepare(runtimes) shader.execute() value = shader.get_value('spec') self.assertAlmostEqual(value.r, val.x) self.assertAlmostEqual(value.g, val.y) self.assertAlmostEqual(value.b, val.z)
def test_rgb_to_sampled_spectrum(self): runtimes = [Runtime()] color_mgr = SampledManager() rgb_to_spec = rgb_to_spectrum_shader(color_mgr) rgb_to_spec.compile(color_mgr=color_mgr) rgb_to_spec.prepare(runtimes) code = """ spec = rgb_to_spectrum(color) """ val = Vector3(0.3, 0.5, 0.7) col = Vec3Arg('color', val) spec = SampledArg('spec', color_mgr.zero()) shader = Shader(code=code, args=[col, spec]) shader.compile(shaders=[rgb_to_spec], color_mgr=color_mgr) shader.prepare(runtimes) shader.execute() value = shader.get_value('spec') vv = color_mgr.rgb_to_sampled(RGBSpectrum(val.x, val.y, val.z)) for i in range(len(value.samples)): self.assertAlmostEqual(value.samples[i], vv.samples[i], places=6)
def _create_material(name, values, renderer, directory): Ka = Kd = Ks = Ke = Ns = Ni = illum = d = Tr = Tf = None map_Ka = map_Kd = map_Ks = map_d = None #TODO sampled spectrum if 'Ka' in values and not _zero_spectrum(values['Ka']): v = values['Ka'] Ka = RGBSpectrum(float(v[0]), float(v[1]), float(v[2])) if 'Kd' in values and not _zero_spectrum(values['Kd']): v = values['Kd'] Kd = RGBSpectrum(float(v[0]), float(v[1]), float(v[2])) if 'Ks' in values and not _zero_spectrum(values['Ks']): v = values['Ks'] Ks = RGBSpectrum(float(v[0]), float(v[1]), float(v[2])) if 'Ke' in values and not _zero_spectrum(values['Ke']): v = values['Ke'] Ke = RGBSpectrum(float(v[0]), float(v[1]), float(v[2])) if 'Ns' in values: Ns = float(values['Ns'][0]) if 'Ni' in values: Ni = float(values['Ni'][0]) if 'd' in values: d = float(values['d'][0]) if 'Tr' in values: Tr = float(values['Tr'][0]) if 'Tf' in values and not _zero_spectrum(values['Tf']): v = values['Tf'] Tf = RGBSpectrum(float(v[0]), float(v[1]), float(v[2])) if 'illum' in values: illum = int(values['illum'][0]) if 'map_Ka' in values: map_Ka = values['map_Ka'][0].strip() if 'map_Kd' in values: map_Kd = values['map_Kd'][0].strip() if 'map_Ks' in values: map_Ks = values['map_Ks'][0].strip() if 'map_d' in values: map_d = values['map_d'][0].strip() mat = Material() if map_Kd is not None: # lambertian texture full_path = os.path.join(directory, map_Kd) img = load_image(full_path) img_factory = lambda: type(img)(1, 1) mat.load('lambertian_texture', renderer.color_mgr, image_factory=img_factory) mat.set_value('texture', img) elif Ke is not None: # lambertian emission mat.load('lambertian_emiter', renderer.color_mgr) mat.set_value('emission', Ke) if Kd is None: Kd = RGBSpectrum(0.0, 0.0, 0.0) mat.set_value('diffuse', Kd) elif Ks is not None: # phong material mat.load('phong2', renderer.color_mgr) mat.set_value('specular', Ks) if Kd is None: Kd = RGBSpectrum(0.0, 0.0, 0.0) mat.set_value('diffuse', Kd) exponent = 1.0 if Ns is None else Ns mat.set_value('exponent', exponent) elif Kd is not None: # simple lambertian material mat.load('lambertian', renderer.color_mgr) mat.set_value('diffuse', Kd) elif Kd is None and Ks is None: #black lambertian mat.load('lambertian', renderer.color_mgr) Kd = RGBSpectrum(0.0, 0.0, 0.0) mat.set_value('diffuse', Kd) else: raise ValueError("Not sutuable material is found in mtl library", name) renderer.materials.add(name, mat)
def atest_material_glass(self): sam_mgr = SampledManager() register_rgb_shadepoint() runtimes = [Runtime()] mat = Material() mat.load('glass', sam_mgr, spectral=False) mat.set_value('ior', 1.5) mgr = MaterialManager() mgr.add('material1', mat) shaders = shaders_functions() for shader in shaders: shader.compile() shader.prepare(runtimes) mgr.compile_shaders(sam_mgr, spectral=False, shaders=shaders) mgr.prepare_shaders(runtimes) code = """ hp = HitPoint() hp.normal = normal sp = ShadePoint() sp.wo = wo material_sampling(hp, sp, 0) pdf = sp.pdf wi = sp.wi spec = sp.material_reflectance """ pdf = FloatArg('pdf', 0.0) wi = Vec3Arg('wi', Vector3(0.0, 0.0, 0.0)) ww = Vector3(5.0, 1.0, 0.0) ww.normalize() wo = Vec3Arg('wo', ww) nn = Vector3(0.0, 1.0, 0.0) nn.normalize() normal = Vec3Arg('normal', nn) spec = RGBArg('spec', RGBSpectrum(0.5, 0.5, 0.5)) shader = Shader(code=code, args=[pdf, wi, wo, normal, spec]) shader.compile(shaders=[mgr.sampling_shader]) shader.prepare(runtimes) shader.execute() s = shader.get_value('wi') print(s) s = shader.get_value('pdf') print(s) s = shader.get_value('spec') print(s) print('------------------------------') print('wo', ww) wi, pdf, ref = sampling_glass(1.5, ww, nn) print("wi", wi) print("pdf", pdf) print("ref", ref) ndotwi = abs(nn.dot(wi)) print("ndotwi", ndotwi) tmp = ndotwi / pdf path_weight = ref * tmp print("path weight", path_weight)