def test_two_triangle_layers(self): """Checks that two overlapping triangles are accumulated correctly.""" if self._backend == rasterization_backend.RasterizationBackends.OPENGL: self.skipTest( reason='OpenGL rasterizer only supports single layer.') image_width = 32 image_height = 32 vertices = tf.constant( [[[-0.2, -0.2, 0], [0.5, -0.2, 0], [0.5, 0.5, 0], [0.2, -0.2, 0.5], [-0.5, -0.2, 0.5], [-0.5, 0.5, 0.5]]], dtype=tf.float32) triangles = [[0, 1, 2], [3, 5, 4]] colors = tf.constant( [[[0, 1.0, 0, 1.0], [0, 1.0, 0, 1.0], [0, 1.0, 0, 1.0], [1.0, 0, 0, 1.0], [1.0, 0, 0, 1.0], [1.0, 0, 0, 1.0]]], dtype=tf.float32) composite, _, normalized_layers = splat.rasterize_then_splat( vertices, triangles, {'color': colors}, rasterization_test_utils.get_identity_view_projection_matrix(), (image_height, image_width), lambda x: x['color'], num_layers=2, return_extra_buffers=True, backend=self._backend) baseline_image = rasterization_test_utils.load_baseline_image( 'Two_Triangles_Splat_Composite.png') baseline_image = tf.image.resize(baseline_image, (image_height, image_width)) images_near, error_message = rasterization_test_utils.compare_images( self, baseline_image, composite) self.assertTrue(images_near, msg=error_message) for i in range(3): baseline_image = rasterization_test_utils.load_baseline_image( 'Two_Triangles_Splat_Layer_{}.png'.format(i)) image = normalized_layers[:, i, ...] image = tf.image.resize( image, (512, 512), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) images_near, error_message = rasterization_test_utils.compare_images( self, baseline_image, image) self.assertTrue(images_near, msg=error_message)
def test_renders_colored_cube_multilayer(self, num_layers): """Renders a simple colored cube with multiple layers.""" rasterized = rasterization_backend.rasterize( self.cube_vertex_positions, self.cube_triangles, self.projection, (self.image_width, self.image_height), num_layers=num_layers, enable_cull_face=False, backend=rasterization_backend.RasterizationBackends.CPU) vertex_rgb = (self.cube_vertex_positions * 0.5 + 0.5) vertex_rgba = tf.concat([vertex_rgb, tf.ones([1, 8, 1])], axis=-1) rendered = interpolate.interpolate_vertex_attribute( vertex_rgba, rasterized).value image_shape = [1] + rendered.shape[2:] for layer_index in range(num_layers): baseline_image = rasterization_test_utils.load_baseline_image( 'Unlit_Cube_{}_{}.png'.format(0, layer_index), image_shape) images_near, error_message = rasterization_test_utils.compare_images( self, baseline_image, rendered[:, layer_index, ...]) self.assertTrue(images_near, msg=error_message)
def test_renders_colored_cube(self): """Renders a simple colored cube.""" num_layers = 1 rasterized = rasterization_backend.rasterize( self.cube_vertex_positions, self.cube_triangles, self.projection, (self.image_width, self.image_height), num_layers=num_layers, enable_cull_face=False, backend=rasterization_backend.RasterizationBackends.CPU) vertex_rgb = (self.cube_vertex_positions * 0.5 + 0.5) vertex_rgba = tf.concat([vertex_rgb, tf.ones([1, 8, 1])], axis=-1) rendered = interpolate.interpolate_vertex_attribute(vertex_rgba, rasterized).value baseline_image = rasterization_test_utils.load_baseline_image( 'Unlit_Cube_0_0.png', rendered.shape) images_near, error_message = rasterization_test_utils.compare_images( self, baseline_image, rendered) self.assertTrue(images_near, msg=error_message)
def test_render_simple_triangle(self): """Directly renders a rasterized triangle's barycentric coordinates.""" w_vector = tf.constant([1.0, 1.0, 1.0], dtype=tf.float32) clip_init = tf.constant( [[[-0.5, -0.5, 0.8], [0.0, 0.5, 0.3], [0.5, -0.5, 0.3]]], dtype=tf.float32) clip_coordinates = clip_init * tf.reshape(w_vector, [1, 3, 1]) triangles = tf.constant([[0, 1, 2]], dtype=tf.int32) face_culling_enabled = False framebuffer = rasterization_backend.rasterize( clip_coordinates, triangles, tf.eye(4, batch_shape=[1]), (self.IMAGE_WIDTH, self.IMAGE_HEIGHT), face_culling_enabled, self._num_layers, self._backend).layer(0) ones_image = tf.ones([1, self.IMAGE_HEIGHT, self.IMAGE_WIDTH, 1]) rendered_coordinates = tf.concat( [framebuffer.barycentrics.value, ones_image], axis=-1) baseline_image = rasterization_test_utils.load_baseline_image( 'Simple_Triangle.png', rendered_coordinates.shape) images_near, error_message = rasterization_test_utils.compare_images( self, baseline_image, rendered_coordinates) self.assertTrue(images_near, msg=error_message)