Raycasting a scene with OBJ modelsΒΆ
This example demonstrates how to render a scene with OBJ models using raycasting.
[1]:
import os
import numpy as np
import imageio
%matplotlib inline
import matplotlib.pyplot as plt
import lmfunctest as ft
import lightmetrica as lm
%load_ext lightmetrica_jupyter
[2]:
lm.init()
lm.log.init('logger::jupyter')
lm.progress.init('progress::jupyter')
lm.info()
[I|0.000|114@user ] Lightmetrica -- Version 3.0.0 (rev. fe30e7c) Linux x64
Following is the definition of assets. To load an OBJ model, we can use model::wavefrontobj asset. This asset internally creates meshes and materials by reading the associated MTL file.
[3]:
# Film for the rendered image
lm.asset('film1', 'film::bitmap', {
'w': 1920,
'h': 1080
})
# Pinhole camera
lm.asset('camera1', 'camera::pinhole', {
'position': [5.101118, 1.083746, -2.756308],
'center': [4.167568, 1.078925, -2.397892],
'up': [0,1,0],
'vfov': 43.001194
})
# OBJ model
lm.asset('obj1', 'model::wavefrontobj', {
'path': os.path.join(ft.env.scene_path, 'fireplace_room/fireplace_room.obj')
})
[I|0.011|48@assets ] Loading asset [name='film1']
[I|0.098|48@assets ] Loading asset [name='camera1']
[I|0.099|48@assets ] Loading asset [name='obj1']
[I|0.099|29@objload] Loading OBJ file [path='fireplace_room.obj']
[I|0.099|169@objloa] Loading MTL file [path='fireplace_room.mtl']
[I|0.099|44@texture] Loading texture [path='wood.ppm']
[I|0.202|44@texture] Loading texture [path='leaf.ppm']
[I|0.206|44@texture] Loading texture [path='picture8.ppm']
[I|0.248|44@texture] Loading texture [path='wood5.ppm']
[3]:
'$.assets.obj1'
We can create primitives from the loaded model::wavefrontobj asset by using lm::primitives() function.
[4]:
# Camera
lm.primitive(lm.identity(), {
'camera': lm.asset('camera1')
})
# Create primitives from model asset
lm.primitive(lm.identity(), {
'model': lm.asset('obj1')
})
Executing the renderer will produce the following image.
[5]:
lm.build('accel::sahbvh', {})
lm.render('renderer::raycast', {
'output': lm.asset('film1'),
'color': [0,0,0]
})
[I|0.710|246@scene ] Building acceleration structure [name='accel::sahbvh']
[I|0.711|131@accel_] Flattening scene
[I|0.740|261@accel_] Building
[I|1.508|151@user ] Starting render [name='renderer::raycast']
[6]:
img = np.copy(lm.buffer(lm.asset('film1')))
f = plt.figure(figsize=(15,15))
ax = f.add_subplot(111)
ax.imshow(np.clip(np.power(img,1/2.2),0,1), origin='lower')
plt.show()