コード例 #1
0
def parse_scene(node):
    cam = None
    resolution = None
    materials = []
    material_dict = {}
    shapes = []
    lights = []
    for child in node:
        if child.tag == 'sensor':
            cam = parse_camera(child)
        elif child.tag == 'bsdf':
            node_id, material = parse_material(child)
            if node_id is not None:
                material_dict[node_id] = len(materials)
                materials.append(material)
        elif child.tag == 'shape':
            shape, light = parse_shape(child, material_dict, len(shapes))
            shapes.append(shape)
            if light is not None:
                lights.append(light)
    return pyredner.Scene(cam, shapes, materials, lights)
コード例 #2
0
    indices = tf.constant([[0, 1, 2], [1, 3, 2]], dtype=tf.int32)
    uvs = tf.Variable([[0.05, 0.05], [0.05, 0.95], [0.95, 0.05], [0.95, 0.95]],
                      dtype=tf.float32)
    shape_plane = pyredner.Shape(vertices, indices, 0, uvs)
    light_vertices = tf.Variable([[-1.0, -1.0, -7.0], [1.0, -1.0, -7.0],
                                  [-1.0, 1.0, -7.0], [1.0, 1.0, -7.0]],
                                 dtype=tf.float32)
    light_indices = tf.constant([[0, 1, 2], [1, 3, 2]], dtype=tf.int32)
    shape_light = pyredner.Shape(light_vertices, light_indices, 1)
shapes = [shape_plane, shape_light]
with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())):
    light_intensity = tf.Variable([20.0, 20.0, 20.0], dtype=tf.float32)
# The first argument is the shape id of the light
light = pyredner.AreaLight(1, light_intensity)
area_lights = [light]
scene = pyredner.Scene(cam, shapes, materials, area_lights)
scene_args = pyredner.serialize_scene(scene=scene,
                                      num_samples=16,
                                      max_bounces=1)

# Render our target
img = pyredner.render(0, *scene_args)
pyredner.imwrite(img, 'results/test_svbrdf/target.exr')
pyredner.imwrite(img, 'results/test_svbrdf/target.png')
target = pyredner.imread('results/test_svbrdf/target.exr')

# Our initial guess is three gray textures
with tf.device(pyredner.get_device_name()):
    diffuse_tex = tf.Variable(tf.ones((256, 256, 3), dtype=np.float32) * 0.5,
                              trainable=True)
    specular_tex = tf.Variable(tf.ones((256, 256, 3), dtype=np.float32) * 0.5,
コード例 #3
0
ファイル: test_envmap.py プロジェクト: zybo19971/redner
with tf.device(pyredner.get_device_name()):
    vertices, indices, uvs, normals = pyredner.generate_sphere(128, 64)
    shape_sphere = pyredner.Shape(vertices=vertices,
                                  indices=indices,
                                  uvs=uvs,
                                  normals=normals,
                                  material_id=0)
shapes = [shape_sphere]

with tf.device(pyredner.get_device_name()):
    envmap = pyredner.imread('sunsky.exr')
    envmap = pyredner.EnvironmentMap(envmap)
scene = pyredner.Scene(camera=cam,
                       shapes=shapes,
                       materials=materials,
                       area_lights=[],
                       envmap=envmap)
scene_args = pyredner.serialize_scene(scene=scene,
                                      num_samples=256,
                                      max_bounces=1)

img = pyredner.render(0, *scene_args)
pyredner.imwrite(img, 'results/test_envmap/target.exr')
pyredner.imwrite(img, 'results/test_envmap/target.png')
target = pyredner.imread('results/test_envmap/target.exr')

with tf.device(pyredner.get_device_name()):
    envmap_texels = tf.Variable(0.5 * tf.ones([32, 64, 3], dtype=tf.float32),
                                trainable=True)
    envmap = pyredner.EnvironmentMap(tf.abs(envmap_texels))
コード例 #4
0
# Tensorflow by default allocates all GPU memory, leaving very little for rendering.
# We set the environment variable TF_FORCE_GPU_ALLOW_GROWTH to true to enforce on demand
# memory allocation to reduce page faults.
import os

os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
import tensorflow as tf

tf.compat.v1.enable_eager_execution()  # redner only supports eager mode
import pyredner_tensorflow as pyredner

objects = pyredner.load_obj('scenes/teapot.obj', return_objects=True)
camera = pyredner.automatic_camera_placement(objects, resolution=(512, 512))
scene = pyredner.Scene(camera=camera, objects=objects)

light = pyredner.PointLight(position=(camera.position + tf.constant(
    (0.0, 0.0, 100.0))),
                            intensity=tf.constant((20000.0, 30000.0, 20000.0)))

img = pyredner.render_deferred(scene=scene, lights=[light])
pyredner.imwrite(img,
                 'results/test_compute_vertex_normals/no_vertex_normal.exr')

for obj in objects:
    obj.normals = pyredner.compute_vertex_normal(obj.vertices, obj.indices,
                                                 'max')
scene = pyredner.Scene(camera=camera, objects=objects)
img = pyredner.render_deferred(scene=scene, lights=[light])
pyredner.imwrite(img,
                 'results/test_compute_vertex_normals/max_vertex_normal.exr')
コード例 #5
0
materials = [mat_grey]

with tf.device(pyredner.get_device_name()):
    vertices, indices, uvs, normals = pyredner.generate_sphere(128, 64)
    shape_sphere = pyredner.Shape(vertices=vertices,
                                  indices=indices,
                                  uvs=uvs,
                                  normals=normals,
                                  material_id=0)
shapes = [shape_sphere]

with tf.device(pyredner.get_device_name()):
    envmap = pyredner.imread('sunsky.exr')
    envmap = pyredner.EnvironmentMap(envmap)
scene = pyredner.Scene(cam, shapes, materials, [], envmap)
scene_args = pyredner.serialize_scene(scene=scene,
                                      num_samples=256,
                                      max_bounces=1)

img = pyredner.render(0, *scene_args)
pyredner.imwrite(img, 'results/test_envmap/target.exr')
pyredner.imwrite(img, 'results/test_envmap/target.png')
target = pyredner.imread('results/test_envmap/target.exr')

with tf.device(pyredner.get_device_name()):
    envmap_texels = tf.Variable(0.5 * tf.ones([32, 64, 3], dtype=tf.float32),
                                use_resource=True,
                                trainable=True)
    envmap = pyredner.EnvironmentMap(tf.abs(envmap_texels))
scene = pyredner.Scene(cam, shapes, materials, [], envmap)
コード例 #6
0
ファイル: test_g_buffer.py プロジェクト: zybo19971/redner
# Setup geometries
shapes = []
with tf.device(pyredner.get_device_name()):
    for mtl_name, mesh in mesh_list:
        shapes.append(
            pyredner.Shape(vertices=mesh.vertices,
                           indices=mesh.indices,
                           uvs=mesh.uvs,
                           normals=mesh.normals,
                           material_id=material_id_map[mtl_name]))

# We don't setup any light source here

# Construct the scene
scene = pyredner.Scene(cam, shapes, materials, area_lights=[], envmap=None)
# Serialize the scene
# Here we specify the output channels as "depth", "shading_normal"
scene_args = pyredner.serialize_scene(
    scene=scene,
    num_samples=16,
    max_bounces=0,
    channels=[redner.channels.depth, redner.channels.shading_normal])

# Render. The first argument is the seed for RNG in the renderer.
img = pyredner.render(0, *scene_args)
# Save the images.
depth = img[:, :, 0]
normal = img[:, :, 1:4]
pyredner.imwrite(depth, 'results/test_g_buffer/target_depth.exr')
pyredner.imwrite(depth,
コード例 #7
0
# Tensorflow by default allocates all GPU memory, leaving very little for rendering.
# We set the environment variable TF_FORCE_GPU_ALLOW_GROWTH to true to enforce on demand
# memory allocation to reduce page faults.
import os
os.environ['TF_FORCE_GPU_ALLOW_GROWTH'] = 'true'
import tensorflow as tf
tf.compat.v1.enable_eager_execution()
import pyredner_tensorflow as pyredner

vertices, indices, uvs, normals = pyredner.generate_sphere(64, 128)
m = pyredner.Material(diffuse_reflectance=tf.constant((0.5, 0.5, 0.5)))
obj = pyredner.Object(vertices=vertices,
                      indices=indices,
                      uvs=uvs,
                      normals=normals,
                      material=m)
cam = pyredner.automatic_camera_placement([obj], resolution=(480, 640))
scene = pyredner.Scene(objects=[obj], camera=cam)

img = pyredner.render_g_buffer(
    scene, channels=[pyredner.channels.uv, pyredner.channels.shading_normal])
uv_img = tf.concat([img[:, :, :2], tf.zeros((480, 640, 1))], axis=2)
normal_img = img[:, :, 2:]
pyredner.imwrite(uv_img, 'results/test_sphere/uv.png')
pyredner.imwrite(normal_img, 'results/test_sphere/normal.png')
コード例 #8
0
checkerboard_texture = pyredner.imread('scenes/teapot.png')
mat_checkerboard = pyredner.Material(\
    diffuse_reflectance = checkerboard_texture)
mat_black = pyredner.Material(\
    diffuse_reflectance = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32))

plane = pyredner.Object(vertices=tf.Variable([[-1.0, -1.0, 0.0],
                                              [-1.0, 1.0,
                                               0.0], [1.0, -1.0, 0.0],
                                              [1.0, 1.0, 0.0]]),
                        indices=tf.constant([[0, 1, 2], [1, 3, 2]],
                                            dtype=tf.int32),
                        uvs=tf.Variable([[0.05, 0.05], [0.05, 0.95],
                                         [0.95, 0.05], [0.95, 0.95]]),
                        material=mat_checkerboard)
scene = pyredner.Scene(camera=cam, objects=[plane])
img = pyredner.render_albedo(scene=scene)
pyredner.imwrite(img, 'results/test_camera_distortion/target.exr')
pyredner.imwrite(img, 'results/test_camera_distortion/target.png')
# Read the target image we just saved.
target = pyredner.imread('results/test_camera_distortion/target.exr')

cam.distortion_params = tf.Variable(tf.zeros(8), trainable=True)
scene = pyredner.Scene(camera=cam, objects=[plane])
img = pyredner.render_albedo(scene=scene)
pyredner.imwrite(img, 'results/test_camera_distortion/init.exr')
pyredner.imwrite(img, 'results/test_camera_distortion/init.png')

# Optimize for triangle vertices.
optimizer = tf.compat.v1.train.AdamOptimizer(1e-3)
for t in range(200):