def render_frame(frame_no, frame_count): from handsome.util import save_array_as_image time = 2 * frame_no / (frame_count - 1) if time > 1.: time = 2 - time set_time = load_sampler_lib()['set_time'] set_time(time) canvas = make_canvas({ 'extents' : (512, 512), 'color' : '#fff', }) surface = generate_surface() shader = generate_shader() mesh = generate_mesh(surface, shader) meshes = subdivide_mesh(mesh) for mesh in meshes: cache = render_mesh(mesh) cache.composite_into(canvas) buffer = array_view(canvas.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255 * buffer).astype(np.uint8) frame_path = 'render/008/frame_{:03}.tiff'.format(frame_no) save_array_as_image(pixel_view(buffer), frame_path, 'RGBA') return frame_path
def main(): colors = { 'red' : '#f00', 'white' : '#fff', } float_colors = { } for key, value in colors.items(): color = parse_color(value) colors[key] = np.array([ color ], dtype=Pixel) float_colors[key] = np.array(tuple(c / 255. for c in color), dtype=FloatPixel) image = Tile((0, 0), (512, 512), sample_rate=4, dtype=FloatPixel) image.buffer[:,:] = float_colors['white'] tile_width, tile_height = image.buffer.shape mesh = MicropolygonMesh((1,1)) mesh_width, mesh_height = mesh.buffer.shape margin = 16 width = 128 right = image.shape[0] top = image.shape[1] buffer = np.array([ [ (right - margin - width, top - margin, 1, 1, 1.5, 0, 0, 1), (right - margin , top - margin, 1, 1, 0 , 0, 0, 1) ], [ (margin , margin, 1, 1, .5, 0, 0, 1), (margin + width, margin, 1, 1, 0 , 0, 0, 1) ], ], dtype=np.float32 ) mesh.buffer.view(dtype=(np.float32, 8))[:] = buffer fill_micropolygon_mesh( mesh_width, mesh_height, generate_numpy_begin(mesh.buffer), tile_width, tile_height, generate_numpy_begin(image.coordinate_image), generate_numpy_begin(image.buffer) ) buffer = array_view(image.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255. * buffer).astype(dtype=np.uint8) save_array_as_image(pixel_view(buffer), 'render/002_micropolygon.tiff', 'RGBA')
def generate_texture(): from handsome.util import save_array_as_image scene = generate_texture_scene() canvas = render_scene(scene) buffer = array_view(canvas.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255. * buffer).astype(dtype=np.uint8) save_array_as_image(pixel_view(buffer), 'render/texture.tiff', 'RGBA')
def render_image(scene_path, out_path): from handsome.util import save_array_as_image scene = parse_scene(scene_path) canvas = render_scene(scene) buffer = array_view(canvas.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255. * buffer).astype(dtype=np.uint8) save_array_as_image(pixel_view(buffer), out_path, 'RGBA')
def test_downsample(): tile = Tile((0, 0), (2, 2), dtype=FloatPixel, sample_rate=2) tile.buffer[:, :]["R"] = tile.coordinate_image["x"] tile.buffer[:, :]["G"] = tile.coordinate_image["y"] expected = np.array( [[(0.25, 1.25, 0, 0.0), (1.25, 1.25, 0.0, 0.0)], [(0.25, 0.25, 0, 0.0), (1.25, 0.25, 0.0, 0.0)]], dtype=np.float32, ).transpose((1, 0, 2)) actual = tile.downsample(1) np.testing.assert_array_equal(expected, array_view(actual))
def main(): from handsome.opencl_api import fill_micropolygon_mesh from handsome.util import save_array_as_image, parse_color mesh = generate_mesh() tile = Tile((0, 0), (512, 512), 4, dtype=FloatPixel) fill_micropolygon_mesh(mesh, tile) buffer = array_view(tile.downsample(1)) # buffer = array_view(tile.buffer) buffer = np.clip(buffer, 0., 1.) buffer = (255 * buffer).astype(np.uint8) save_array_as_image(pixel_view(buffer), 'render/009_opencl.tiff', 'RGBA')
def render_frame(frame_no): print('rendering frame {}'.format(frame_no)) image = Tile((0, 0), (512,512), 4, dtype=FloatPixel) image.buffer[:,:] = palette['white'].view(dtype=FloatPixel) center = (256, 256) radius = 192 mesh = star(center, radius) cache = render_mesh(mesh) cache.composite_into(image) buffer = array_view(image.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255. * buffer).astype(dtype=np.uint8) path = 'render/004/frame_{:03}.tiff'.format(frame_no) save_array_as_image(pixel_view(buffer), path, 'RGBA')
def sample_texture(s, t, texture): from math import floor t = 1. - t s, t = t, s if not (0. <= s < 1.): return clear if not (0. <= t < 1.): return clear shape = texture.shape s = s * (shape[0] - 1) s_index = floor(s) s_frac = s - s_index s_index = int(s_index) t = t * (shape[1] - 1) t_index = floor(t) t_frac = t - t_index t_index = int(t_index) texture = array_view(texture) top_left = texture[s_index, t_index,:] bottom_left = texture[s_index, t_index + 1,:] top_right = texture[s_index + 1, t_index,:] bottom_right = texture[s_index + 1, t_index + 1,:] u = s_frac up = 1 - s_frac v = t_frac vp = 1 - t_frac out = up * vp * top_left out += up * v * bottom_left out += u * vp * top_right out += u * v * bottom_right return out
def main(): from handsome.util import save_array_as_image canvas = make_canvas({ 'extents' : (512, 512), 'color' : '#fff', }) surface = generate_surface() shader = generate_texture_shader() mesh = generate_mesh(surface, shader) cache = render_mesh(mesh) cache.composite_into(canvas) buffer = array_view(canvas.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255 * buffer).astype(np.uint8) save_array_as_image(pixel_view(buffer), 'render/006_texture.tiff', 'RGBA')
def render_frame(frame_no): print('rendering frame {}'.format(frame_no)) from math import sin, pi tau = 2. * pi image = Tile((0, 0), (512,512), 4, dtype=FloatPixel) image.buffer[:,:] = palette['white'].view(dtype=FloatPixel) center = (256, 256) radius = 100 surface = circle(center, radius) global transform transform = np.array( [ [ 1., .0 ], [ .5 * sin(tau * frame_no / 60.), 1. ] ], dtype=np.float32 ) meshes = generate_meshes(surface) for mesh in meshes: cache = render_mesh(mesh) cache.composite_into(image) buffer = array_view(image.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255. * buffer).astype(dtype=np.uint8) path = 'render/003/frame_{:03}.tiff'.format(frame_no) save_array_as_image(pixel_view(buffer), path, 'RGBA')
def test_tile_render(): from handsome.MicropolygonMesh import MicropolygonMesh from handsome.Tile import Tile mesh = MicropolygonMesh((1, 1)) mesh.buffer[:,:]['position'] = [ [ (16, 16, 1, 1), (32, 16, 1, 1) ], [ (0, 0, 1, 1), (16, 0, 1, 1) ], ] red = color(1, 0, 0) green = color(0, 1, 0) orange = color(1, 1, 0) black = color(0, 0, 0, 1) zero = color(0, 0, 0, 0) mesh.buffer[:,:]['color'] = [ [ green, orange, ], [ black, red, ], ] mesh_rows, mesh_columns = mesh.buffer.shape tile = Tile((0,0), (32, 16), 1, FloatPixel) tile_rows, tile_columns = tile.buffer.shape fill_micropolygon_mesh( mesh_rows, mesh_columns, generate_numpy_begin(mesh.buffer), generate_numpy_begin(mesh.bounds), tile_rows, tile_columns, tile.bounds, generate_numpy_begin(tile.coordinate_image), generate_numpy_begin(tile.buffer), ) buffer = array_view(tile.buffer) rows, columns, channels = buffer.shape def image(x, y): return buffer[rows - 1 - y, x] low, high = 1/16, 15/16 expected = [ ((0, 0), color(0, 0, 0, 1)), ((15, 0), color(15/16, 0, 0, 1)), ((16, 0), color(0, 0, 0, 0)), ((31, 0), color(0, 0, 0, 0)), ((0, 8), color(0, 0, 0, 0)), ((8,8), color(0, 1/2, 0, 1)), ((16,8), color(1/2, 1/2, 0, 1)), ((31, 8), color(0, 0, 0, 0)), ((0, 15), color(0, 0, 0, 0)), ((15, 15), low * black + high * green), ((30, 15), low * (low * black + high * green) + high * (low * red + high * orange)), ] for index, c in expected: assert points_are_close(image(*index), c)
def extract_meshes_from_line_path(self, line_path): from handsome.MicropolygonMesh import MicropolygonMesh vertices = line_path.vertices line_width = float(line_path.data['width']) / 2. length, _ = vertices.shape points = vertices[:,:3] / vertices[:,3].reshape((length, 1)) mesh = MicropolygonMesh((length - 1, 1)) ptype = np.dtype((vertices.dtype, 4)) positions = mesh.buffer[:,:]['position'].view(dtype=ptype) colors = array_view(mesh.buffer[:,:]['color']) colors[:,0,:] = vertices[:,4:] colors[:,1,:] = vertices[:,4:] start_low, start_high, _ = splay(points[0,:], points[1,:], line_width) positions[0,0,:3] = start_high * vertices[0,3] positions[0,0,3] = vertices[0,3] positions[0,1,:3] = start_low * vertices[0,3] positions[0,1,3] = vertices[0,3] for i, (a, b, c) in enumerate(triwise(points)): index = i + 1 a_low, a_high, ab = splay(a, b, line_width) b_low, b_high, bc = splay(b, c, line_width) try: A = np.array([ ab[:2], -bc[:2] ]).T b = (b_high - a_high)[:2] u, v = np.linalg.solve(A, b) positions[index,0,:3] = (a_high + u * ab) * vertices[index,3] positions[index,0,3] = vertices[index,3] b = (b_low - a_low)[:2] u, v = np.linalg.solve(A, b) positions[index,1,:3] = (a_low + u * ab) * vertices[index,3] positions[index,1,3] = vertices[index,3] except np.linalg.LinAlgError: positions[index,0,:3] = b_high * vertices[index,3] positions[index,0,3] = vertices[index,3] positions[index,1,:3] = b_low * vertices[index,3] positions[index,1,3] = vertices[index,3] end_high, end_low, _ = splay(points[-1,:], points[-2,:], line_width) positions[-1,0,:3] = end_high * vertices[-1,3] positions[-1,0,3] = vertices[-1,3] positions[-1,1,:3] = end_low * vertices[-1,3] positions[-1,1,3] = vertices[-1,3] yield mesh
def test_open_cl(): from handsome.util import save_array_as_image, parse_color from handsome.Scene import make_canvas canvas = make_canvas({ 'extents' : (640, 480), 'color' : '#fff', }) context = cl.create_some_context() queue = cl.CommandQueue(context) mf = cl.mem_flags source = r''' __kernel void shade_square( __global float4 * result_g ) { int y = get_global_id(0), height = get_global_size(0); int x = get_global_id(1), width = get_global_size(1); int index = y * width + x; const float2 center = (float2)(width / 2, height / 2), dimensions = (float2)(height) / 2, upper_left = center - dimensions / 2; float x_blend = ((float)x - upper_left.x) / dimensions.x, y_blend = ((float)y - upper_left.y) / dimensions.y ; float4 upper_left_c = (float4)(1, 1, 1, 1), upper_right_c = (float4)(1, 0, 0, 1), lower_left_c = (float4)(0, 0, 1, 1), lower_right_c = (float4)(0, 0, 0, 1) ; if (0. <= x_blend && x_blend <= 1. && 0. <= y_blend && y_blend <= 1.) { result_g[index] = (1 - x_blend) * (1 - y_blend) * upper_left_c + x_blend * (1 - y_blend) * upper_right_c + (1 - x_blend) * y_blend * lower_left_c + x_blend * y_blend * lower_right_c ; } else{ result_g[index] = (float4)(0, 0, 0, 1); } } ''' program = cl.Program(context, source).build() result = cl.Buffer(context, mf.WRITE_ONLY, canvas.buffer.nbytes) program.shade_square(queue, canvas.buffer.shape, None, result) cl.enqueue_copy(queue, array_view(canvas.buffer), result) buffer = array_view(canvas.downsample(1)) buffer = np.clip(buffer, 0., 1.) buffer = (255 * buffer).astype(np.uint8) save_array_as_image(pixel_view(buffer), 'render/009_opencl.tiff', 'RGBA')