forked from mitsuba-renderer/mitsuba2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
starting creating python code examples
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
01_render_scene/*.jpg | ||
01_render_scene/*.exr | ||
02_depth_integrator/*.jpg | ||
02_depth_integrator/*.exr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import os | ||
|
||
from mitsuba.core import Bitmap, Struct, Thread | ||
from mitsuba.core.xml import load_file | ||
|
||
|
||
SCENE_DIR = '../../../resources/data/scenes/' | ||
filename = os.path.join(SCENE_DIR, 'cbox/cbox.xml') | ||
|
||
# Append the directory containing the scene to the "file resolver". | ||
# This is needed since the scene specifies meshes and textures | ||
# using relative paths. | ||
directory_name = os.path.dirname(filename) | ||
Thread.thread().file_resolver().append(directory_name) | ||
|
||
# Load the actual scene | ||
scene = load_file(filename) | ||
|
||
# Call the scene's integrator to render the loaded scene | ||
scene.integrator().render(scene) | ||
|
||
# After rendering, the rendered data is stored in the film | ||
film = scene.sensor().film() | ||
|
||
# Write out rendering as high dynamic range OpenEXR file | ||
film.bitmap().convert(Bitmap.ERGB, Struct.EFloat32, srgb_gamma=False).write('cbox.exr') | ||
|
||
# Write out a tonemapped JPG of the same rendering | ||
film.bitmap().convert(Bitmap.ERGB, Struct.EUInt8, srgb_gamma=True).write('cbox.jpg') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os | ||
import numpy as np | ||
|
||
from mitsuba.core import Bitmap, Struct, Thread | ||
from mitsuba.core.xml import load_file | ||
|
||
from mitsuba.core import Bitmap, EDebug, Log | ||
from mitsuba.render import RadianceSample3fX, ImageBlock | ||
|
||
SCENE_DIR = '../../../resources/data/scenes/' | ||
filename = os.path.join(SCENE_DIR, 'cbox/cbox.xml') | ||
|
||
# Append the directory containing the scene to the "file resolver". | ||
# This is needed since the scene specifies meshes and textures | ||
# using relative paths. | ||
directory_name = os.path.dirname(filename) | ||
Thread.thread().file_resolver().append(directory_name) | ||
|
||
# Load the actual scene | ||
scene = load_file(filename) | ||
|
||
# Instead of calling the scene's integrator, we build our own small integrator | ||
# This integrator simply computes the depth values per pixel | ||
sensor = scene.sensor() | ||
film = sensor.film() | ||
film_size = film.crop_size() | ||
spp = 32 | ||
|
||
# Sample pixel positions in the image plane | ||
# Inside of each pixels, we randomly choose starting locations for our rays | ||
n_rays = film_size[0] * film_size[1] * spp | ||
pos = np.arange(n_rays) / spp | ||
scale = np.array([1.0 / film_size[0], 1.0 / film_size[1]]) | ||
pos = np.stack([pos % int(film_size[0]), pos / int(film_size[0])], axis=1) | ||
position_sample = pos + np.random.rand(n_rays, 2).astype(np.float32) | ||
|
||
active = np.ones(n_rays).astype(np.bool) | ||
|
||
# Sample rays starting from the camera sensor | ||
rays, weights = sensor.sample_ray_differential( | ||
time=0, | ||
sample1=np.random.rand(n_rays).astype(np.float32), | ||
sample2=position_sample * scale, | ||
sample3=np.random.rand(n_rays, 2).astype(np.float32), | ||
active=active) | ||
|
||
# Intersect rays with the scene geometry | ||
surface_interaction = scene.ray_intersect(rays) | ||
|
||
# Given intersection, compute the final pixel values as the depth t | ||
# of the sampled surface interaction | ||
result = surface_interaction.t | ||
result[~surface_interaction.is_valid()] = 0 # set values to zero if no intersection occured | ||
|
||
|
||
# Accumulate values into an image block | ||
rfilter = film.reconstruction_filter() | ||
block = ImageBlock(Bitmap.EXYZAW, film.crop_size(), warn=False, filter=rfilter, border=False) | ||
result = np.stack([result, result, result, result], axis=1) # Workaround since ImageBlock assumes 4 values | ||
block.put(position_sample, rays.wavelengths, result, 1) | ||
|
||
# Write out the result from the ImageBlock | ||
block.bitmap().convert(Bitmap.EY, Struct.EFloat32, srgb_gamma=False).write('depth.exr') | ||
|