def bunny_base(scene, scene_path, **kwargs): camera = lm.load_camera( 'camera_main', 'pinhole', { 'position': [-0.191925, 2.961061, 4.171464], 'center': [-0.185709, 2.478091, 3.295850], 'up': [0, 1, 0], 'vfov': 28.841546, 'aspect': 16 / 9 }) scene.add_primitive({'camera': camera.loc()}) model = lm.load_model( 'model_obj', 'wavefrontobj', {'path': os.path.join(scene_path, 'bunny', 'bunny_with_planes.obj')}) mat_diffuse_white = lm.load_material('mat_diffuse_white', 'diffuse', {'Kd': [.8, .8, .8]}) # floor tex = lm.load_texture( 'tex_floor', 'bitmap', {'path': os.path.join(scene_path, 'bunny', 'default.png')}) mat_floor = lm.load_material('mat_floor', 'diffuse', {'mapKd': tex.loc()}) scene.add_primitive({ 'mesh': model.make_loc('mesh_2'), 'material': mat_floor.loc() }) # bunny if 'mat_knob' in kwargs: scene.add_primitive({ 'mesh': model.make_loc('mesh_1'), 'material': kwargs['mat_knob'] })
def portal_box_dragon_2(scene, scene_path, no_light=False, **kwargs): base_path = os.path.join(scene_path, 'dragon_in_box') g = lm.load_asset_group('dragon', 'default') # Load dragon scene camera = lm.load_camera('camera_main', 'pinhole', position=[-0.191925, 2.961061, 4.171464], center=[-0.185709, 2.478091, 3.295850], up=[0, 1, 0], vfov=28.841546, aspect=16 / 9) scene.add_primitive(camera=camera) # Model model = g.load_model('model_obj', 'wavefrontobj', path=os.path.join(base_path, 'dragon_with_plane.obj')) # Floor tex = lm.load_texture('tex_floor', 'bitmap', path=os.path.join(base_path, 'default.png')) mat_floor = lm.load_material('mat_floor', 'diffuse', mapKd=tex) scene.add_primitive(mesh=model.make_loc('mesh_2'), material=mat_floor) # Dragon mat = lm.load_material('mat_ut', 'glossy', Ks=[.8, .8, .8], ax=0.2, ay=0.2) #mat_white = g.load_material('mat_white', 'diffuse', Kd=[.8,.8,.8]) scene.add_primitive(mesh=model.make_loc('mesh_4'), material=mat) # Light if not no_light: Le = 1000 light_env = g.load_light('light_env', 'envconst', Le=[Le, Le, Le]) scene.add_primitive(light=light_env) # Cube mat = g.load_material('mat_white', 'diffuse', Kd=[.8, .8, .8]) cube = g.load_mesh('cube', 'wavefrontobj', path=os.path.join(base_path, 'cube.obj')) scene.add_primitive(mesh=cube, material=mat) # Lid lid = g.load_mesh('lid', 'wavefrontobj', path=os.path.join(base_path, 'lid.obj')) scene.add_primitive(mesh=lid, material=mat) # Portal meshes portal_mesh = g.load_mesh('portal', 'wavefrontobj', path=os.path.join(base_path, 'portal.obj')) portal_mesh_2 = g.load_mesh('portal2', 'wavefrontobj', path=os.path.join(base_path, 'portal2.obj')) return [portal_mesh, portal_mesh_2]
def sphere(scene, scene_path): camera = lm.load_camera( 'camera_main', 'pinhole', { 'position': [0, 2, 5], 'center': [0, 1, 0], 'up': [0, 1, 0], 'vfov': 30, 'aspect': 16 / 9 }) scene.add_primitive({'camera': camera.loc()}) model = lm.load_model('model_obj', 'wavefrontobj', {'path': os.path.join(scene_path, 'sphere.obj')}) mat_diffuse_white = lm.load_material('mat_diffuse_white', 'diffuse', {'Kd': [.8, .8, .8]}) # floor tex = lm.load_texture('tex_floor', 'bitmap', {'path': os.path.join(scene_path, 'default.png')}) mat_floor = lm.load_material('mat_floor', 'diffuse', {'mapKd': tex.loc()}) scene.add_primitive({ 'mesh': model.make_loc('mesh_1'), 'material': mat_floor.loc() }) # sphere # mat = lm.load_material('mat_ut', 'glass', { # 'Ni': 2 # }) mat = lm.load_material('mat_ut', 'mirror', {}) # mat = mat_diffuse_white # mat = lm.load_material('mat_ut', 'mask', {}) scene.add_primitive({ 'mesh': model.make_loc('mesh_3'), 'material': mat.loc() }) # Light source Ke = 10 mat_black = lm.load_material('mat_black', 'diffuse', {'Kd': [0, 0, 0]}) light = lm.load_light('light', 'area', { 'Ke': [Ke, Ke, Ke], 'mesh': model.make_loc('mesh_2') }) scene.add_primitive({ 'mesh': model.make_loc('mesh_2'), 'material': mat_black.loc(), 'light': light.loc() })
def plane_emitter(scene, scene_path): """ A scene containing only a single area light. The scene is useful to test the most simpelst configuration. """ camera = lm.load_camera( 'camera', 'pinhole', { 'position': [0, 0, 5], 'center': [0, 0, 0], 'up': [0, 1, 0], 'vfov': 30, 'aspect': 16 / 9 }) mesh = lm.load_mesh( 'mesh', 'raw', { 'ps': [-1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1], 'ns': [0, 0, 1], 'ts': [0, 0, 1, 0, 1, 1, 0, 1], 'fs': { 'p': [0, 1, 2, 0, 2, 3], 'n': [0, 0, 0, 0, 0, 0], 't': [0, 1, 2, 0, 2, 3] } }) material = lm.load_material('mat_black', 'diffuse', {'Kd': [0, 0, 0]}) light = lm.load_light('light', 'area', { 'Ke': [1, 1, 1], 'mesh': mesh.loc() }) scene.add_primitive({'camera': camera.loc()}) scene.add_primitive({ 'mesh': mesh.loc(), 'material': material.loc(), 'light': light.loc() })
def bunny_with_area_light(scene, scene_path, **kwargs): bunny_base(scene, scene_path, **kwargs) # Light source Ke = 10 mat_black = lm.load_material('mat_black', 'diffuse', {'Kd': [0, 0, 0]}) light = lm.load_light('light', 'area', { 'Ke': [Ke, Ke, Ke], 'mesh': '$.assets.model_obj.mesh_3' }) scene.add_primitive({ 'mesh': '$.assets.model_obj.mesh_3', 'material': mat_black.loc(), 'light': light.loc() })
def mitsuba_knob_with_area_light(scene, scene_path, **kwargs): mitsuba_knob_base(scene, scene_path, **kwargs) # Area light Ke = 10 model_light = lm.load_model( 'model_light', 'wavefrontobj', {'path': os.path.join(scene_path, 'mitsuba_knob', 'light.obj')}) mat_black = lm.load_material('mat_black', 'diffuse', {'Kd': [0, 0, 0]}) light = lm.load_light('light', 'area', { 'Ke': [Ke, Ke, Ke], 'mesh': model_light.make_loc('mesh_1') }) scene.add_primitive({ 'mesh': model_light.make_loc('mesh_1'), 'material': mat_black.loc(), 'light': light.loc() })
def mitsuba_knob_base(scene, scene_path, **kwargs): # Camera camera = lm.load_camera( 'camera_main', 'pinhole', { 'position': [0, 4, 5], 'center': [0, 0, -1], 'up': [0, 1, 0], 'vfov': 30, 'aspect': 16 / 9 #'vfov': 100 }) scene.add_primitive({'camera': camera.loc()}) # Model model = lm.load_model( 'model_obj', 'wavefrontobj', {'path': os.path.join(scene_path, 'mitsuba_knob', 'mitsuba.obj')}) mat_diffuse_white = lm.load_material('mat_diffuse_white', 'diffuse', {'Kd': [.8, .8, .8]}) scene.add_primitive({ 'mesh': model.make_loc('mesh_4'), #'material': mat_diffuse_white.loc() 'material': model.make_loc('backdrop') }) if 'mat_knob' in kwargs: scene.add_primitive({ 'mesh': model.make_loc('mesh_5'), 'material': kwargs['mat_inside'] if 'mat_inside' in kwargs else mat_diffuse_white.loc() }) scene.add_primitive({ 'mesh': model.make_loc('mesh_6'), 'material': kwargs['mat_knob'] })
lm.info() # + {"raw_mimetype": "text/restructuredtext", "active": ""} # A plugin can be loaded by :cpp:func:`lm::comp::load_plugin` function where you specify the path to the plugin as an argument. You don't want to specify the extension of the dynamic library because it is inferred according to the platform. # - # Load plugin lm.comp.load_plugin( os.path.join(env.bin_path, 'functest_material_visualize_normal')) # + {"raw_mimetype": "text/restructuredtext", "active": ""} # We can use the loaded extension in the same way as build-in assets using :cpp:func:`lm::asset` function. We feed the material to the obj model to apply the loaded material to the mesh. # + # Custom material material = lm.load_material('visualize_normal_mat', 'visualize_normal', {}) # OBJ model model = lm.load_model( 'obj1', 'wavefrontobj', { 'path': os.path.join(env.scene_path, 'fireplace_room/fireplace_room.obj'), 'base_material': material.loc() }) # + # Film for the rendered image film = lm.load_film('film1', 'bitmap', {'w': 1920, 'h': 1080}) # Pinhole camera camera = lm.load_camera(
def display_image(img, fig_size=15, scale=1): f = plt.figure(figsize=(fig_size, fig_size)) ax = f.add_subplot(111) ax.imshow(np.clip(np.power(img * scale, 1 / 2.2), 0, 1), origin='lower') ax.axis('off') plt.show() # - # ## Scene setup # Create scene accel = lm.load_accel('accel', 'embree') scene = lm.load_scene('scene', 'default', accel=accel) mat = lm.load_material('mat_ut', 'diffuse', Kd=[1, 1, 1]) lmscene.bunny_with_area_light(scene, env.scene_path, mat_knob=mat) scene.build() # ## Rendering # ### Diffse material # # `material::diffuse` lm.load_material('mat_ut', 'diffuse', Kd=[.8, .2, .2]) img = render(scene, 'pt') display_image(img) # ### Glossy material #
'p': [0, 1, 2, 0, 2, 3], 'n': [0, 0, 0, 0, 0, 0], 't': [0, 1, 2, 0, 2, 3] }) accel = lm.load_accel('accel', 'sahbvh') scene = lm.load_scene('scene', 'default', accel=accel) try: # material1 is undefined scene.add_primitive(mesh='$.assets.mesh1', material='$.assets.material1') except Exception: traceback.print_exc() # Define a missing asset lm.load_material('material1', 'diffuse', Kd=[1, 1, 1]) try: # 'material1' is not a valid locator scene.add_primitive(mesh='$.assets.mesh1', material='material1') except Exception: traceback.print_exc() # This is correct scene.add_primitive(mesh='$.assets.mesh1', material='$.assets.material1') # ### Rendering with invalid scene renderer = lm.load_renderer('renderer', 'raycast', scene=scene,
def display_image(img, fig_size=15, scale=1): f = plt.figure(figsize=(fig_size, fig_size)) ax = f.add_subplot(111) ax.imshow(np.clip(np.power(img * scale, 1 / 2.2), 0, 1), origin='lower') ax.axis('off') plt.show() # - # ## Scene setup accel = lm.load_accel('accel', 'embree') scene = lm.load_scene('scene', 'default', accel=accel) mat = lm.load_material('mat_ut', 'glossy', Ks=[.8, .2, .2], ax=0.1, ay=0.1) # ## Rendering # ### Area light # # ``light::area`` scene.reset() lmscene.bunny_with_area_light(scene, env.scene_path, mat_knob=mat.loc()) scene.build() img = render(scene, 'pt') display_image(img) # ### Environment light #
def generate_plane_with_hole(scene, hole_size): """Generate a plane with a hole with configurable hole size.""" camera = lm.load_camera( 'camera_main', 'pinhole', { 'position': [0, 0, 1], 'center': [0, 0, 0], 'up': [0, 1, 0], 'vfov': 60, 'aspect': 1 }) scene.add_primitive({'camera': camera.loc()}) def merge_mesh(p1, f1, p2, f2): """Merge two meshes.""" l = len(p1) return np.concatenate((p1, p2)), np.concatenate((f1, f2 + l)) def make_quad(a, b, z): """Make a quad with two triangles. . - b | / | a - . """ return (np.array([[a[0], a[1], z], [b[0], a[1], z], [b[0], b[1], z], [a[0], b[1], z]]), np.array([[0, 1, 2], [0, 2, 3]])) def create_lm_raw_mesh(name, ps, fs, n): """Create mesh::raw asset from position and faces.""" return lm.load_mesh( name, 'raw', { 'ps': ps.flatten().tolist(), 'ns': n, 'ts': [0, 0], 'fs': { 'p': fs.flatten().tolist(), 'n': np.zeros(fs.size).tolist(), 't': np.zeros(fs.size).tolist() } }) # Common materials mat_black = lm.load_material('mat_black', 'diffuse', {'Kd': [0, 0, 0]}) mat_white = lm.load_material('mat_white', 'diffuse', {'Kd': [1, 1, 1]}) # Mesh and primitives def add_quad_with_hole_mesh(): """ Add mesh with hole. We want to pierce a hole at the center of a quad [-1,1]^2 in the xy plane. The extent of the hole is [-hole_size,hole_size]^2. Note that the function doesn't use indices for the duplicated positions. """ s = 10 p = [[hole_size, hole_size], [s, 0], [s, s], [0, s]] rot = np.array([[0, -1], [1, 0]]) # row major ps = np.array([]).reshape(0, 3) fs = np.array([]).reshape(0, 3) z = 1 for i in range(4): ps, fs = merge_mesh(ps, fs, *make_quad(p[0], p[1], z)) ps, fs = merge_mesh(ps, fs, *make_quad(p[0], p[2], z)) ps, fs = merge_mesh(ps, fs, *make_quad(p[0], p[3], z)) p = p @ rot mesh = create_lm_raw_mesh('quad_with_hole', ps, fs, [0, 0, 1]) scene.add_primitive({'mesh': mesh.loc(), 'material': mat_black.loc()}) # Returns portal vertices hs = hole_size return [[-hs, -hs, z], [hs, -hs, z], [-hs, hs, z]] def add_light(): s = 10 ps, fs = make_quad([-s, -s], [s, s], 5) mesh = create_lm_raw_mesh('quad_light', ps, fs, [0, 0, -1]) Ke = 10 light = lm.load_light('light', 'area', { 'Ke': [Ke, Ke, Ke], 'mesh': mesh.loc() }) scene.add_primitive({ 'mesh': mesh.loc(), 'material': mat_black.loc(), 'light': light.loc() }) pass def add_diffuser(): ps, fs = make_quad([-1, -1], [1, 1], 0) mesh = create_lm_raw_mesh('quad_diffuser', ps, fs, [0, 0, 1]) scene.add_primitive({'mesh': mesh.loc(), 'material': mat_white.loc()}) portal = add_quad_with_hole_mesh() add_light() add_diffuser() return portal
scene = lm.load_scene('scene', 'default', { 'accel': accel.loc() }) try: # material1 is undefined scene.add_primitive({ 'mesh': '$.assets.mesh1', 'material': '$.assets.material1' }) except Exception: traceback.print_exc() # Define a missing asset lm.load_material('material1', 'diffuse', { 'Kd': [1,1,1] }) try: # 'material1' is not a valid locator scene.add_primitive({ 'mesh': '$.assets.mesh1', 'material': 'material1' }) except Exception: traceback.print_exc() # This is correct scene.add_primitive({ 'mesh': '$.assets.mesh1', 'material': '$.assets.material1'
# %load_ext lightmetrica_jupyter lm.init() lm.log.init('jupyter') lm.progress.init('jupyter') lm.info() camera = lm.load_camera('camera_main', 'pinhole', { 'position': [5.101118, 1.083746, -2.756308], 'center': [4.167568, 1.078925, -2.397892], 'up': [0,1,0], 'vfov': 43.001194 }) material = lm.load_material('obj_base_mat', 'diffuse', { 'Kd': [.8,.2,.2] }) model = lm.load_model('model_obj', 'wavefrontobj', { 'path': os.path.join(env.scene_path, 'fireplace_room/fireplace_room.obj'), 'base_material': material.loc() }) accel = lm.load_accel('accel', 'sahbvh', {}) scene = lm.load_scene('scene', 'default', { 'accel': accel.loc() }) scene.add_primitive({ 'camera': camera.loc() }) scene.add_primitive({ 'model': model.loc() })
import lightmetrica as lm # %load_ext lightmetrica_jupyter lm.init() lm.log.init('jupyter') lm.progress.init('jupyter') lm.info() camera = lm.load_camera('camera_main', 'pinhole', position=[5.101118, 1.083746, -2.756308], center=[4.167568, 1.078925, -2.397892], up=[0,1,0], vfov=43.001194, aspect=16/9) material = lm.load_material('obj_base_mat', 'diffuse', Kd=[.8,.2,.2]) model = lm.load_model('model_obj', 'wavefrontobj', path=os.path.join(env.scene_path, 'fireplace_room/fireplace_room.obj'), base_material=material) accel = lm.load_accel('accel', 'sahbvh') scene = lm.load_scene('scene', 'default', accel=accel) scene.add_primitive(camera=camera) scene.add_primitive(model=model) scene.build() film = lm.load_film('film_output', 'bitmap', w=1920, h=1080) renderer = lm.load_renderer('renderer', 'raycast', scene=scene, output=film) renderer.render() img = np.copy(film.buffer()) f = plt.figure(figsize=(10,10))
aspect=16 / 9) # Load mesh with raw vertex data mesh = lm.load_mesh('mesh', 'raw', ps=[-1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1], ns=[0, 0, 1], ts=[0, 0, 1, 0, 1, 1, 0, 1], fs={ 'p': [0, 1, 2, 0, 2, 3], 'n': [0, 0, 0, 0, 0, 0], 't': [0, 1, 2, 0, 2, 3] }) # Material material = lm.load_material('material', 'diffuse', Kd=[1, 1, 1]) # + {"raw_mimetype": "text/restructuredtext", "active": ""} # Next we will create a `scene` asset. The scene asset can also be created :cpp:func:`load_scene` function. Here, we will create ``scene::default`` asset. # A scene internally `uses acceleration structure` for ray-scene intersections, which can be specified by ``accel`` parameter. # - accel = lm.load_accel('accel', 'sahbvh') scene = lm.load_scene('scene', 'default', accel=accel) # + {"raw_mimetype": "text/restructuredtext", "active": ""} # The scene of Lightmetrica is defined by a set of ``primitives``. A primitive specifies an object inside the scene by associating geometries and materials. We can define a primitive by :cpp:func:`lm::Scene::add_primitive` function. # # .. note:: # If you need transformation applied to the geometry, you can use :cpp:func:`lm::Scene::add_transformed_primitive` function. The transformation is given by 4x4 matrix. #
'ts': ts.flatten().tolist(), 'fs': { 'p': fs.flatten().tolist(), 't': fs.flatten().tolist(), 'n': fs.flatten().tolist() } }) camera = lm.load_camera('camera_main', 'pinhole', { 'position': [0, 0, 50], 'center': [0, 0, 0], 'up': [0, 1, 0], 'vfov': 30 }) material = lm.load_material('material_white', 'diffuse', {'Kd': [1, 1, 1]}) film = lm.load_film('film_output', 'bitmap', {'w': 1920, 'h': 1080}) accel = lm.load_accel('accel', 'nanort', {}) scene = lm.load_scene('scene', 'default', {'accel': accel.loc()}) renderer = lm.load_renderer( 'renderer', 'raycast', { 'scene': scene.loc(), 'output': film.loc(), 'visualize_normal': True, 'bg_color': [1, 1, 1] }) # -