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
resolution=resolution) mat_perlin = pyredner.Material(diffuse_reflectance=diffuse, specular_reflectance=specular, roughness=roughness) with tf.device(pyredner.get_device_name()): mat_black = pyredner.Material( diffuse_reflectance=tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32)) materials = [mat_perlin, mat_black] vertices = tf.Variable([[-1.5, -1.5, 0.0], [-1.5, 1.5, 0.0], [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)
with tf.device(pyredner.get_device_name()): mat_grey = pyredner.Material(diffuse_reflectance=tf.Variable( [0.5, 0.5, 0.5], dtype=tf.float32, use_resource=True)) mat_black = pyredner.Material(diffuse_reflectance=tf.Variable( [0.0, 0.0, 0.0], dtype=tf.float32, use_resource=True)) materials = [mat_grey, mat_black] # tf.constant allocates arrays on host memory for int32 arrays (some tensorflow internal mess), # but pyredner.Shape constructor automatically converts the memory to device if necessary. floor_vertices = tf.Variable([[-2.0, 0.0, -2.0], [-2.0, 0.0, 2.0], [2.0, 0.0, -2.0], [2.0, 0.0, 2.0]], dtype=tf.float32, use_resource=True) floor_indices = tf.constant([[0, 1, 2], [1, 3, 2]], dtype=tf.int32) shape_floor = pyredner.Shape(floor_vertices, floor_indices, None, None, 0) blocker_vertices = tf.Variable([[-0.5, 3.0, -0.5], [-0.5, 3.0, 0.5], [0.5, 3.0, -0.5], [0.5, 3.0, 0.5]], dtype=tf.float32, use_resource=True) blocker_indices = tf.constant([[0, 1, 2], [1, 3, 2]], dtype=tf.int32) shape_blocker = pyredner.Shape(blocker_vertices, blocker_indices, None, None, 0) light_vertices = tf.Variable( [[-0.1, 5, -0.1], [-0.1, 5, 0.1], [0.1, 5, -0.1], [0.1, 5, 0.1]], dtype=tf.float32, use_resource=True) light_indices = tf.constant([[0, 2, 1], [1, 2, 3]], dtype=tf.int32) shape_light = pyredner.Shape(light_vertices, light_indices, None, None, 1) shapes = [shape_floor, shape_blocker, shape_light]
resolution=(256, 256), fisheye=False) with tf.device(pyredner.get_device_name()): mat_grey = pyredner.Material( diffuse_reflectance=tf.Variable([0.4, 0.4, 0.4], dtype=tf.float32), specular_reflectance=tf.Variable([0.5, 0.5, 0.5], dtype=tf.float32), roughness=tf.Variable([0.05], dtype=tf.float32)) 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(camera=cam, shapes=shapes, materials=materials, area_lights=[], envmap=envmap) scene_args = pyredner.serialize_scene(scene=scene, num_samples=256, max_bounces=1)
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)
look_at=look_at, up=up, fov=fov, clip_near=clip_near, resolution=resolution, fisheye=True) with tf.device(pyredner.get_device_name()): mat_grey = pyredner.Material(diffuse_reflectance = \ tf.Variable([0.5, 0.5, 0.5], dtype=tf.float32)) materials = [mat_grey] vertices = tf.Variable( [[-1.7, 1.0, 0.0], [1.0, 1.0, 0.0], [-0.5, -1.0, 0.0]], dtype=tf.float32) indices = tf.constant([[0, 1, 2]], dtype=tf.int32) shape_triangle = pyredner.Shape(vertices, indices, 0) light_vertices = tf.Variable([[-1.0, -1.0, -9.0], [1.0, -1.0, -9.0], [-1.0, 1.0, -9.0], [1.0, 1.0, -9.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, 0) shapes = [shape_triangle, shape_light] with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): light_intensity = tf.Variable([30.0, 30.0, 30.0], dtype=tf.float32) 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,
resolution=(256, 256), fisheye=False) # Setup materials with tf.device(pyredner.get_device_name()): mat_grey = pyredner.Material(diffuse_reflectance=tf.Variable( [0.5, 0.5, 0.5], dtype=tf.float32, use_resource=True)) # The material list of the scene materials = [mat_grey] # Setup geometries shape_triangle = pyredner.Shape(vertices=tf.Variable( [[-1.7, 1.0, 0.0], [1.0, 1.0, 0.0], [-0.5, -1.0, 0.0]], dtype=tf.float32, use_resource=True), indices=tf.constant([[0, 1, 2]], dtype=tf.int32), uvs=None, normals=None, material_id=0) # Setup light source shape 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, use_resource=True), indices=tf.constant([[0, 1, 2], [1, 3, 2]], dtype=tf.int32), uvs=None, normals=None, material_id=0)
resolution = resolution) mat_perlin = pyredner.Material( diffuse_reflectance = diffuse, specular_reflectance = specular, roughness = roughness) with tf.device(pyredner.get_device_name()): mat_black = pyredner.Material( diffuse_reflectance = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32, use_resource=True)) materials = [mat_perlin, mat_black] vertices = tf.Variable([[-1.5,-1.5,0.0], [-1.5,1.5,0.0], [1.5,-1.5,0.0], [1.5,1.5,0.0]], dtype=tf.float32, use_resource=True) 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, use_resource=True) shape_plane = pyredner.Shape(vertices, indices, uvs, None, 0) 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, use_resource=True) light_indices = tf.constant([[0,1,2],[1,3,2]], dtype=tf.int32) shape_light = pyredner.Shape(light_vertices, light_indices, None, None, 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, use_resource=True) # 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)
material_id_map = {} materials = [] count = 0 for key, value in material_map.items(): material_id_map[key] = count count += 1 materials.append(value) # 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])) with tf.device(pyredner.get_device_name()): envmap = pyredner.imread('sunsky.exr') envmap = pyredner.EnvironmentMap(envmap) # Construct the scene scene = pyredner.Scene(cam, shapes, materials, area_lights=[], envmap=envmap) # Serialize the scene # Here we specify the output channels as "depth", "shading_normal" scene_args = pyredner.serialize_scene(scene=scene, num_samples=512, max_bounces=1) img = pyredner.render(0, *scene_args)
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)
with tf.device(pyredner.get_device_name()): mat_green = pyredner.Material( diffuse_reflectance = tf.Variable([0.35, 0.75, 0.35], dtype=tf.float32)) mat_red = pyredner.Material( diffuse_reflectance = tf.Variable([0.75, 0.35, 0.35], dtype=tf.float32)) mat_black = pyredner.Material( diffuse_reflectance = tf.Variable([0.0, 0.0, 0.0], dtype=tf.float32)) materials = [mat_green,mat_red,mat_black] tri0_vertices = tf.Variable( [[-1.7,1.0,0.0], [1.0,1.0,0.0], [-0.5,-1.0,0.0]], dtype=tf.float32) tri1_vertices = tf.Variable( [[-1.0,1.5,1.0], [0.2,1.5,1.0], [0.2,-1.5,1.0]], dtype=tf.float32) tri0_indices = tf.constant([[0, 1, 2]], dtype=tf.int32) tri1_indices = tf.constant([[0, 1, 2]], dtype=tf.int32) shape_tri0 = pyredner.Shape(tri0_vertices, tri0_indices, 0) shape_tri1 = pyredner.Shape(tri1_vertices, tri1_indices, 1) 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, 2) shapes = [shape_tri0, shape_tri1, 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(2, light_intensity) area_lights = [light] scene = pyredner.Scene(cam, shapes, materials, area_lights) scene_args = pyredner.serialize_scene( scene = scene,