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)
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 import redner # Automatic UV mapping example adapted from tutorial 2 # Use GPU if available pyredner.set_use_gpu( tf.test.is_gpu_available(cuda_only=True, min_cuda_compute_capability=None)) # Set up the pyredner scene for rendering: with tf.device(pyredner.get_device_name()): material_map, mesh_list, light_map = pyredner.load_obj('scenes/teapot.obj') for _, mesh in mesh_list: mesh.normals = pyredner.compute_vertex_normal(mesh.vertices, mesh.indices) mesh.uvs, mesh.uv_indices = pyredner.compute_uvs( mesh.vertices, mesh.indices) # Setup camera with tf.device('/device:cpu:' + str(pyredner.get_cpu_device_id())): cam = pyredner.Camera( position=tf.Variable([0.0, 30.0, 200.0], dtype=tf.float32), look_at=tf.Variable([0.0, 30.0, 0.0], dtype=tf.float32), up=tf.Variable([0.0, 1.0, 0.0], dtype=tf.float32), fov=tf.Variable([45.0], dtype=tf.float32), # in degree clip_near=1e-2, # needs to > 0 resolution=(256, 256),
# 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')
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)