def __init__(self, camera, shapes=[], materials=[], area_lights=[], objects=None, envmap=None): self.camera = camera self.envmap = envmap if objects is None: self.shapes = shapes self.materials = materials self.area_lights = area_lights else: # Convert objects to shapes/materials/lights shapes = [] materials = [] area_lights = [] material_dict = {} current_material_id = 0 for obj in objects: mid = -1 if obj.material in material_dict: mid = material_dict[obj.material] else: mid = current_material_id material_dict[obj.material] = current_material_id materials.append(obj.material) current_material_id += 1 if obj.light_intensity is not None: current_shape_id = len(shapes) area_light = pyredner.AreaLight( shape_id=current_shape_id, intensity=obj.light_intensity, two_sided=obj.light_two_sided) area_lights.append(area_light) shape = pyredner.Shape(vertices=obj.vertices, indices=obj.indices, material_id=mid, uvs=obj.uvs, normals=obj.normals, uv_indices=obj.uv_indices, normal_indices=obj.normal_indices, colors=obj.colors) shapes.append(shape) self.shapes = shapes self.materials = materials self.area_lights = area_lights
[1.5, -1.5, 0.0], [1.5, 1.5, 0.0]], dtype=tf.float32) 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,
def parse_shape(node, material_dict, shape_id): if node.attrib['type'] == 'obj' or node.attrib['type'] == 'serialized': to_world = tf.eye(4) serialized_shape_id = 0 mat_id = -1 light_intensity = None filename = '' max_smooth_angle = -1 for child in node: if 'name' in child.attrib: if child.attrib['name'] == 'filename': filename = child.attrib['value'] elif child.attrib['name'] == 'toWorld': to_world = parse_transform(child) elif child.attrib['name'] == 'shapeIndex': serialized_shape_id = int(child.attrib['value']) elif child.attrib['name'] == 'maxSmoothAngle': max_smooth_angle = float(child.attrib['value']) if child.tag == 'ref': mat_id = material_dict[child.attrib['id']] elif child.tag == 'emitter': for grandchild in child: if grandchild.attrib['name'] == 'radiance': light_intensity = parse_vector( grandchild.attrib['value']) if light_intensity.shape[0] == 1: light_intensity = tf.constant([ light_intensity[0], light_intensity[0], light_intensity[0] ]) if node.attrib['type'] == 'obj': _, mesh_list, _ = pyredner.load_obj(filename) # Convert to CPU for rebuild_topology vertices = mesh_list[0][1].vertices.cpu() indices = mesh_list[0][1].indices.cpu() uvs = mesh_list[0][1].uvs normals = mesh_list[0][1].normals uv_indices = mesh_list[0][1].uv_indices normal_indices = mesh_list[0][1].normal_indices if uvs is not None: uvs = uvs.cpu() if normals is not None: normals = normals.cpu() if uv_indices is not None: uv_indices = uv_indices.cpu() else: assert (node.attrib['type'] == 'serialized') mitsuba_tri_mesh = redner.load_serialized(filename, serialized_shape_id) vertices = tf.convert_to_tensor(mitsuba_tri_mesh.vertices) indices = tf.convert_to_tensor(mitsuba_tri_mesh.indices) uvs = tf.convert_to_tensor(mitsuba_tri_mesh.uvs) normals = tf.convert_to_tensor(mitsuba_tri_mesh.normals) if uvs.shape[0] == 0: uvs = None if normals.shape[0] == 0: normals = None # Transform the vertices and normals vertices = tf.concat( (vertices, tf.ones([vertices.shape[0], 1], dtype=tf.float32)), axis=1) vertices = vertices @ tf.transpose(to_world, [0, 1]) vertices = vertices / vertices[:, 3:4] vertices = vertices[:, 0:3] if normals is not None: normals = normals @ (tf.linalg.inv(tf.transpose(to_world, [0, 1]))[:3, :3]) assert (vertices is not None) assert (indices is not None) if max_smooth_angle >= 0: if normals is None: normals = tf.zeros_like(vertices) new_num_vertices = redner.rebuild_topology(\ redner.float_ptr(pyredner.data_ptr(vertices)), redner.int_ptr(pyredner.data_ptr(indices)), redner.float_ptr(pyredner.data_ptr(uvs) if uvs is not None else 0), redner.float_ptr(pyredner.data_ptr(normals) if normals is not None else 0), redner.int_ptr(pyredner.data_ptr(uv_indices) if uv_indices is not None else 0), int(vertices.shape[0]), int(indices.shape[0]), max_smooth_angle) print('Rebuilt topology, original vertices size: {}, new vertices size: {}'.format(\ int(vertices.shape[0]), new_num_vertices)) vertices.resize_(new_num_vertices, 3) if uvs is not None: uvs.resize_(new_num_vertices, 2) if normals is not None: normals.resize_(new_num_vertices, 3) lgt = None if light_intensity is not None: lgt = pyredner.AreaLight(shape_id, light_intensity) return pyredner.Shape(vertices=vertices, indices=indices, uvs=uvs, normals=normals, material_id=mat_id), lgt elif node.attrib['type'] == 'rectangle': indices = tf.constant([[0, 2, 1], [1, 2, 3]], dtype=tf.int32) vertices = tf.constant([[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [1.0, -1.0, 0.0], [1.0, 1.0, 0.0]]) uvs = None normals = None to_world = tf.eye(4) mat_id = -1 light_intensity = None for child in node: if 'name' in child.attrib: if child.attrib['name'] == 'toWorld': to_world = parse_transform(child) if child.tag == 'ref': mat_id = material_dict[child.attrib['id']] elif child.tag == 'emitter': for grandchild in child: if grandchild.attrib['name'] == 'radiance': light_intensity = parse_vector( grandchild.attrib['value']) if light_intensity.shape[0] == 1: light_intensity = tf.constant([ light_intensity[0], light_intensity[0], light_intensity[0] ]) # Transform the vertices and normals vertices = tf.concat( (vertices, tf.convert_to_tensor(np.ones(vertices.shape[0], 1), dtype=tf.float32)), axis=1) vertices = vertices @ tf.transpose(to_world, [0, 1]) vertices = vertices / vertices[:, 3:4] vertices = vertices[:, 0:3] if normals is not None: normals = normals @ (tf.linalg.inv(tf.transpose(to_world, [0, 1]))[:3, :3]) assert (vertices is not None) assert (indices is not None) lgt = None if light_intensity is not None: lgt = pyrender.Light(shape_id, light_intensity) return pyredner.Shape(vertices=vertices, indices=indices, uvs=uvs, normals=normals, material_id=mat_id), lgt else: assert (False)
[[-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, use_resource=True), indices=tf.constant([[0, 1, 2], [1, 3, 2]], dtype=tf.int32), uvs=None, normals=None, material_id=0) # The shape list of the scene shapes = [shape_triangle, shape_light] # Setup light source with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): light = pyredner.AreaLight(shape_id=1, intensity=tf.Variable([20.0, 20.0, 20.0], dtype=tf.float32, use_resource=True)) area_lights = [light] # Construct the scene scene = pyredner.Scene(cam, shapes, materials, area_lights) # Serialize the scene # Here we specify the output channels as "radiance" and "alpha" # Render the scene as our target image. scene_args = pyredner.serialize_scene(\ scene = scene, num_samples = 16, max_bounces = 1, channels = [redner.channels.radiance, redner.channels.alpha]) # Render the scene as our target image.
shape_light = pyredner.Shape(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), indices=tf.constant([[0, 1, 2], [1, 3, 2]], dtype=tf.int32), uvs=None, normals=None, material_id=0) # The shape list of the scene shapes = [shape_triangle, shape_light] # Setup light source with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): light = pyredner.AreaLight(shape_id=1, intensity=tf.Variable([20.0, 20.0, 20.0], dtype=tf.float32)) area_lights = [light] # Construct the scene scene = pyredner.Scene(cam, shapes, materials, area_lights) # Serialize the scene # Here we specify the output channels as "radiance" and "alpha" # Render the scene as our target image. scene_args = pyredner.serialize_scene(\ scene = scene, num_samples = 16, max_bounces = 1, channels = [redner.channels.radiance, redner.channels.alpha]) # Render the scene as our target image.
def parse_shape(node, material_dict, shape_id): if node.attrib['type'] == 'obj' or node.attrib['type'] == 'serialized': to_world = tf.eye(4) serialized_shape_id = 0 mat_id = -1 light_intensity = None filename = '' for child in node: if 'name' in child.attrib: if child.attrib['name'] == 'filename': filename = child.attrib['value'] elif child.attrib['name'] == 'toWorld': to_world = parse_transform(child) elif child.attrib['name'] == 'shapeIndex': serialized_shape_id = int(child.attrib['value']) if child.tag == 'ref': mat_id = material_dict[child.attrib['id']] elif child.tag == 'emitter': for grandchild in child: if grandchild.attrib['name'] == 'radiance': light_intensity = parse_vector(grandchild.attrib['value']) if light_intensity.shape[0] == 1: light_intensity = tf.constant( [light_intensity[0], light_intensity[0], light_intensity[0]]) if node.attrib['type'] == 'obj': _, mesh_list, _ = pyredner.load_obj(filename) vertices = mesh_list[0][1].vertices.cpu() indices = mesh_list[0][1].indices.cpu() uvs = mesh_list[0][1].uvs normals = mesh_list[0][1].normals if uvs is not None: uvs = uvs.cpu() if normals is not None: normals = normals.cpu() else: assert(node.attrib['type'] == 'serialized') mitsuba_tri_mesh = redner.load_serialized(filename, serialized_shape_id) vertices = tf.convert_to_tensor(mitsuba_tri_mesh.vertices) indices = tf.convert_to_tensor(mitsuba_tri_mesh.indices) uvs = tf.convert_to_tensor(mitsuba_tri_mesh.uvs) normals = tf.convert_to_tensor(mitsuba_tri_mesh.normals) if uvs.shape[0] == 0: uvs = None if normals.shape[0] == 0: normals = None # Transform the vertices and normals vertices = tf.concat((vertices, tf.convert_to_tensor(np.ones([vertices.shape[0], 1]), dtype=tf.float32)), axis = 1) vertices = vertices @ tf.transpose(to_world, [0, 1]) vertices = vertices / vertices[:, 3:4] vertices = vertices[:, 0:3] if normals is not None: normals = normals @ (tf.linalg.inv(tf.transpose(to_world, [0, 1]))[:3, :3]) assert(vertices is not None) assert(indices is not None) lgt = None if light_intensity is not None: lgt = pyredner.AreaLight(shape_id, light_intensity) return pyredner.Shape(vertices, indices, uvs, normals, mat_id), lgt elif node.attrib['type'] == 'rectangle': indices = tf.constant([[0, 2, 1], [1, 2, 3]], dtype = tf.int32) vertices = tf.constant([[-1.0, -1.0, 0.0], [-1.0, 1.0, 0.0], [ 1.0, -1.0, 0.0], [ 1.0, 1.0, 0.0]]) uvs = None normals = None to_world = tf.eye(4) mat_id = -1 light_intensity = None for child in node: if 'name' in child.attrib: if child.attrib['name'] == 'toWorld': to_world = parse_transform(child) if child.tag == 'ref': mat_id = material_dict[child.attrib['id']] elif child.tag == 'emitter': for grandchild in child: if grandchild.attrib['name'] == 'radiance': light_intensity = parse_vector(grandchild.attrib['value']) if light_intensity.shape[0] == 1: light_intensity = tf.constant( [light_intensity[0], light_intensity[0], light_intensity[0]]) # Transform the vertices and normals vertices = tf.concat((vertices, tf.convert_to_tensor(np.ones(vertices.shape[0], 1), dtype=tf.float32)), axis = 1) vertices = vertices @ tf.transpose(to_world, [0, 1]) vertices = vertices / vertices[:, 3:4] vertices = vertices[:, 0:3] if normals is not None: normals = normals @ (tf.linalg.inv(tf.transpose(to_world, [0, 1]))[:3, :3]) assert(vertices is not None) assert(indices is not None) lgt = None if light_intensity is not None: lgt = pyrender.Light(shape_id, light_intensity) return shape.Shape(vertices, indices, uvs, normals, mat_id), lgt else: assert(False)