def animated_square(cfg, color=(1, 0.66, 0), rotate=True, scale=True, translate=True): """Animated Translate/Scale/Rotate on a square""" cfg.duration = 5.0 cfg.aspect_ratio = (1, 1) sz = 1 / 3.0 q = ngl.Quad((-sz / 2, -sz / 2, 0), (sz, 0, 0), (0, sz, 0)) node = ngl.RenderColor(color, geometry=q) coords = [(-1, 1), (1, 1), (1, -1), (-1, -1), (-1, 1)] if rotate: animkf = (ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360)) node = ngl.Rotate(node, angle=ngl.AnimatedFloat(animkf)) if scale: animkf = ( ngl.AnimKeyFrameVec3(0, (1, 1, 1)), ngl.AnimKeyFrameVec3(cfg.duration / 2, (2, 2, 2)), ngl.AnimKeyFrameVec3(cfg.duration, (1, 1, 1)), ) node = ngl.Scale(node, factors=ngl.AnimatedVec3(animkf)) if translate: animkf = [] tscale = 1.0 / float(len(coords) - 1) * cfg.duration for i, xy in enumerate(coords): pos = (xy[0] * 0.5, xy[1] * 0.5, 0) t = i * tscale animkf.append(ngl.AnimKeyFrameVec3(t, pos)) node = ngl.Translate(node, vector=ngl.AnimatedVec3(animkf)) return node
def velocity_circle_distort_3d(cfg): cfg.duration = 4.0 cfg.aspect_ratio = (1, 1) coords = list(equilateral_triangle_coords()) coords.append(coords[0]) pos_kf = [ ngl.AnimKeyFrameVec3(cfg.duration * i / 3.0, pos, "exp_in_out") for i, pos in enumerate(coords) ] anim = ngl.AnimatedVec3(pos_kf) velocity = ngl.VelocityVec3(anim) vert = textwrap.dedent("""\ void main() { float distort_max = 0.1; float velocity_l = length(velocity); float direction_l = length(ngl_position); vec3 normed_velocity = velocity_l == 0.0 ? vec3(0.0) : -velocity / velocity_l; vec3 normed_direction = direction_l == 0.0 ? vec3(0.0) : ngl_position / direction_l; float distort = clamp(dot(normed_velocity, normed_direction) / 2.0 * distort_max, 0.0, 1.0); vec4 pos = vec4(ngl_position, 1.0) + vec4(-distort * velocity, 0.0); ngl_out_pos = ngl_projection_matrix * ngl_modelview_matrix * pos; } """) geom = ngl.Circle(radius=0.2, npoints=128) prog = ngl.Program(vertex=vert, fragment=cfg.get_frag("color")) shape = ngl.Render(geom, prog) shape.update_frag_resources(color=ngl.UniformVec3(COLORS.white), opacity=ngl.UniformFloat(1)) shape.update_vert_resources(velocity=velocity) return ngl.Translate(shape, vector=anim)
def animated_uniform(cfg): """Uniform mat4 animated with a transform chain""" m0 = cfg.medias[0] cfg.aspect_ratio = (m0.width, m0.height) q = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0)) m = ngl.Media(m0.filename) t = ngl.Texture2D(data_src=m) p = ngl.Program(vertex=cfg.get_vert("texture"), fragment=cfg.get_frag("matrix-transform")) p.update_vert_out_vars(var_uvcoord=ngl.IOVec2(), var_tex0_coord=ngl.IOVec2()) ts = ngl.Render(q, p) ts.update_frag_resources(tex0=t) scale_animkf = [ ngl.AnimKeyFrameVec3(0, (1, 1, 1)), ngl.AnimKeyFrameVec3(cfg.duration, (0.1, 0.1, 0.1), "quartic_out"), ] s = ngl.Scale(ngl.Identity(), factors=ngl.AnimatedVec3(scale_animkf)) rotate_animkf = [ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360, "exp_out") ] r = ngl.Rotate(s, axis=(0, 0, 1), angle=ngl.AnimatedFloat(rotate_animkf)) u = ngl.UniformMat4(transform=r) ts.update_frag_resources(matrix=u) return ts
def animated_uniform(cfg): '''Uniform mat4 animated with a transform chain''' m0 = cfg.medias[0] cfg.aspect_ratio = (m0.width, m0.height) q = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0)) m = ngl.Media(m0.filename) t = ngl.Texture2D(data_src=m) p = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('matrix-transform')) ts = ngl.Render(q, p) ts.update_textures(tex0=t) scale_animkf = [ ngl.AnimKeyFrameVec3(0, (1, 1, 1)), ngl.AnimKeyFrameVec3(cfg.duration, (0.1, 0.1, 0.1), 'quartic_out') ] s = ngl.Scale(ngl.Identity(), anim=ngl.AnimatedVec3(scale_animkf)) rotate_animkf = [ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360, 'exp_out') ] r = ngl.Rotate(s, axis=(0, 0, 1), anim=ngl.AnimatedFloat(rotate_animkf)) u = ngl.UniformMat4(transform=r) ts.update_uniforms(matrix=u) return ts
def animated_camera(cfg, rotate=True): '''Animated camera around a scene''' g = ngl.Group() q = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0)) m = ngl.Media(cfg.medias[0].filename) t = ngl.Texture2D(data_src=m) program = ngl.Program(vertex=cfg.get_vert('texture'), fragment=cfg.get_frag('texture')) program.update_vert_out_vars(var_uvcoord=ngl.IOVec2(), var_tex0_coord=ngl.IOVec2()) node = ngl.Render(q, program) node.update_frag_resources(tex0=t) g.add_children(node) translate = ngl.Translate(node, vector=(-0.6, 0.8, -1)) g.add_children(translate) translate = ngl.Translate(node, vector=(0.6, 0.8, -1)) g.add_children(translate) translate = ngl.Translate(node, vector=(-0.6, -0.5, -1)) g.add_children(translate) translate = ngl.Translate(node, vector=(0.6, -0.5, -1)) g.add_children(translate) g = ngl.GraphicConfig(g, depth_test=True) camera = ngl.Camera(g) camera.set_eye(0, 0, 2) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_perspective(45.0, cfg.aspect_ratio_float) camera.set_clipping(0.1, 10.0) tr_animkf = [ ngl.AnimKeyFrameVec3(0, (0.0, 0.0, 0.0)), ngl.AnimKeyFrameVec3(10, (0.0, 0.0, 3.0), 'exp_out') ] node = ngl.Translate(ngl.Identity(), anim=ngl.AnimatedVec3(tr_animkf)) if rotate: rot_animkf = [ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360, 'exp_out') ] node = ngl.Rotate(node, axis=(0, 1, 0), anim=ngl.AnimatedFloat(rot_animkf)) camera.set_eye_transform(node) fov_animkf = [ ngl.AnimKeyFrameFloat(0.5, 60.0), ngl.AnimKeyFrameFloat(cfg.duration, 45.0, 'exp_out') ] camera.set_fov_anim(ngl.AnimatedFloat(fov_animkf)) return camera
def color_negative_values_srgb(cfg): cfg.duration = 5 kfs = ( # The elastic_in easing has the special property to undershoot under 0 ngl.AnimKeyFrameVec3(-5, value=(0.0, 0.0, 0.0)), ngl.AnimKeyFrameVec3(5, value=(0.0, 0.0, 1.0), easing="elastic_in"), ) color0 = ngl.AnimatedVec3(keyframes=kfs) color1 = ngl.UniformVec3(value=(-1.0, -1.0, 1.0)) return ngl.RenderGradient(color0=color0, color1=color1, linear=True)
def transform_scale_animated(cfg, factors=(0.7, 1.4, 0)): cfg.aspect_ratio = (1, 1) cfg.duration = 2.0 shape = _transform_shape(cfg) anim = [ ngl.AnimKeyFrameVec3(0, (0, 0, 0)), ngl.AnimKeyFrameVec3(cfg.duration / 2.0, factors), ngl.AnimKeyFrameVec3(cfg.duration, (0, 0, 0)), ] return ngl.Scale(shape, factors=ngl.AnimatedVec3(anim))
def animated_camera(cfg, rotate=True): """Animated camera around a scene""" g = ngl.Group() q = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0)) m = ngl.Media(cfg.medias[0].filename) t = ngl.Texture2D(data_src=m) node = ngl.RenderTexture(t, geometry=q) g.add_children(node) translate = ngl.Translate(node, vector=(-0.6, 0.8, -1)) g.add_children(translate) translate = ngl.Translate(node, vector=(0.6, 0.8, -1)) g.add_children(translate) translate = ngl.Translate(node, vector=(-0.6, -0.5, -1)) g.add_children(translate) translate = ngl.Translate(node, vector=(0.6, -0.5, -1)) g.add_children(translate) g = ngl.GraphicConfig(g, depth_test=True) camera = ngl.Camera(g) camera.set_eye(0, 0, 2) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_clipping(0.1, 10.0) tr_animkf = [ ngl.AnimKeyFrameVec3(0, (0.0, 0.0, 0.0)), ngl.AnimKeyFrameVec3(10, (0.0, 0.0, 3.0), "exp_out") ] node = ngl.Translate(ngl.Identity(), vector=ngl.AnimatedVec3(tr_animkf)) if rotate: rot_animkf = [ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360, "exp_out") ] node = ngl.Rotate(node, axis=(0, 1, 0), angle=ngl.AnimatedFloat(rot_animkf)) camera.set_eye_transform(node) perspective_animkf = [ ngl.AnimKeyFrameVec2(0.5, (60.0, cfg.aspect_ratio_float)), ngl.AnimKeyFrameVec2(cfg.duration, (45.0, cfg.aspect_ratio_float), "exp_out"), ] camera.set_perspective(ngl.AnimatedVec2(perspective_animkf)) return camera
def transform_animated_camera(cfg): cfg.duration = 5. g = ngl.Group() elems = ( ('red', None), ('yellow', (-0.6, 0.8, -1)), ('green', (0.6, 0.8, -1)), ('cyan', (-0.6, -0.5, -1)), ('magenta', (0.6, -0.5, -1)), ) quad = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0)) prog = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) for color, vector in elems: node = ngl.Render(quad, prog) node.update_uniforms(color=ngl.UniformVec4(value=COLORS[color])) if vector: node = ngl.Translate(node, vector=vector) g.add_children(node) g = ngl.GraphicConfig(g, depth_test=True) camera = ngl.Camera(g) camera.set_eye(0, 0, 2) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_perspective(45.0, cfg.aspect_ratio_float) camera.set_clipping(0.1, 10.0) tr_animkf = [ ngl.AnimKeyFrameVec3(0, (0.0, 0.0, 0.0)), ngl.AnimKeyFrameVec3(cfg.duration, (0.0, 0.0, 3.0)) ] eye_transform = ngl.Translate(ngl.Identity(), anim=ngl.AnimatedVec3(tr_animkf)) rot_animkf = [ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360) ] eye_transform = ngl.Rotate(eye_transform, axis=(0, 1, 0), anim=ngl.AnimatedFloat(rot_animkf)) camera.set_eye_transform(eye_transform) fov_animkf = [ ngl.AnimKeyFrameFloat(0.5, 60.0), ngl.AnimKeyFrameFloat(cfg.duration, 45.0) ] camera.set_fov_anim(ngl.AnimatedFloat(fov_animkf)) return camera
def transform_eye_camera(cfg): cfg.duration = 3.0 cfg.aspect_ratio = (1, 1) node = ngl.RenderGradient4(geometry=ngl.Circle(radius=0.7, npoints=128)) animkf = [ ngl.AnimKeyFrameVec3(0, (0, -0.5, 0)), ngl.AnimKeyFrameVec3(cfg.duration / 2, (0, 1, 0)), ngl.AnimKeyFrameVec3(cfg.duration, (0, -0.5, 0)), ] return ngl.Camera(node, eye=ngl.AnimatedVec3(animkf))
def transform_translate_animated(cfg): cfg.aspect_ratio = (1, 1) cfg.duration = 3.0 p0, p1, p2 = equilateral_triangle_coords() anim = [ ngl.AnimKeyFrameVec3(0, p0), ngl.AnimKeyFrameVec3(1 * cfg.duration / 3.0, p1), ngl.AnimKeyFrameVec3(2 * cfg.duration / 3.0, p2), ngl.AnimKeyFrameVec3(cfg.duration, p0), ] shape = _transform_shape(cfg) return ngl.Translate(shape, vector=ngl.AnimatedVec3(anim))
def animated_square(cfg, color=(1, 0.66, 0, 1), rotate=True, scale=True, translate=True): '''Animated Translate/Scale/Rotate on a square''' cfg.duration = 5.0 cfg.aspect_ratio = (1, 1) sz = 1 / 3. q = ngl.Quad((-sz / 2, -sz / 2, 0), (sz, 0, 0), (0, sz, 0)) p = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) node = ngl.Render(q, p) ucolor = ngl.UniformVec4(value=color) node.update_uniforms(color=ucolor) coords = [(-1, 1), (1, 1), (1, -1), (-1, -1), (-1, 1)] if rotate: animkf = (ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360)) node = ngl.Rotate(node, anim=ngl.AnimatedFloat(animkf)) if scale: animkf = (ngl.AnimKeyFrameVec3(0, (1, 1, 1)), ngl.AnimKeyFrameVec3(cfg.duration / 2, (2, 2, 2)), ngl.AnimKeyFrameVec3(cfg.duration, (1, 1, 1))) node = ngl.Scale(node, anim=ngl.AnimatedVec3(animkf)) if translate: animkf = [] tscale = 1. / float(len(coords) - 1) * cfg.duration for i, xy in enumerate(coords): pos = (xy[0] * .5, xy[1] * .5, 0) t = i * tscale animkf.append(ngl.AnimKeyFrameVec3(t, pos)) node = ngl.Translate(node, anim=ngl.AnimatedVec3(animkf)) return node
def transform_animated_camera(cfg): cfg.duration = 5.0 g = ngl.Group() elems = ( # fmt: off (COLORS.red, None), (COLORS.yellow, (-0.6, 0.8, -1)), (COLORS.green, (0.6, 0.8, -1)), (COLORS.cyan, (-0.6, -0.5, -1)), (COLORS.magenta, (0.6, -0.5, -1)), # fmt: on ) quad = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0)) for color, vector in elems: node = ngl.RenderColor(color, geometry=quad) if vector: node = ngl.Translate(node, vector=vector) g.add_children(node) g = ngl.GraphicConfig(g, depth_test=True) camera = ngl.Camera(g) camera.set_eye(0, 0, 2) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_clipping(0.1, 10.0) tr_animkf = [ ngl.AnimKeyFrameVec3(0, (0.0, 0.0, 0.0)), ngl.AnimKeyFrameVec3(cfg.duration, (0.0, 0.0, 3.0)) ] eye_transform = ngl.Translate(ngl.Identity(), vector=ngl.AnimatedVec3(tr_animkf)) rot_animkf = [ ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, 360) ] eye_transform = ngl.Rotate(eye_transform, axis=(0, 1, 0), angle=ngl.AnimatedFloat(rot_animkf)) camera.set_eye_transform(eye_transform) perspective_animkf = [ ngl.AnimKeyFrameVec2(0.5, (60.0, cfg.aspect_ratio_float)), ngl.AnimKeyFrameVec2(cfg.duration, (45.0, cfg.aspect_ratio_float)), ] camera.set_perspective(ngl.AnimatedVec2(perspective_animkf)) return camera
def transform_skew_animated(cfg, angles=(0, -60, 14), axis=(1, 0, 0), anchor=(0, 0.05, -0.5)): cfg.aspect_ratio = (1, 1) cfg.duration = 2.0 shape = _transform_shape(cfg) anim = [ ngl.AnimKeyFrameVec3(0, (0, 0, 0)), ngl.AnimKeyFrameVec3(cfg.duration / 2.0, angles), ngl.AnimKeyFrameVec3(cfg.duration, (0, 0, 0)), ] return ngl.Skew(shape, angles=ngl.AnimatedVec3(anim), axis=axis, anchor=anchor)
def animated_circles(cfg): '''Simple cyclic circles animation''' group = ngl.Group() cfg.duration = 5. cfg.aspect_ratio = (1, 1) radius = 0.2 n = 10 step = 360. / n shape = ngl.Circle(radius=radius, npoints=128) prog = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color')) render = ngl.Render(shape, prog) render.update_uniforms(color=ngl.UniformVec4([1.0] * 4)) for i in range(n): mid_time = cfg.duration / 2.0 start_time = mid_time / (i + 2) end_time = cfg.duration - start_time scale_animkf = [ ngl.AnimKeyFrameVec3(start_time, (0, 0, 0)), ngl.AnimKeyFrameVec3(mid_time, (1.0, 1.0, 1.0), 'exp_out'), ngl.AnimKeyFrameVec3(end_time, (0, 0, 0), 'exp_in'), ] angle = i * step rotate_animkf = [ ngl.AnimKeyFrameFloat(start_time, 0), ngl.AnimKeyFrameFloat(mid_time, angle, 'exp_out'), ngl.AnimKeyFrameFloat(end_time, 0, 'exp_in'), ] tnode = render tnode = ngl.Scale(tnode, anim=ngl.AnimatedVec3(scale_animkf)) tnode = ngl.Translate(tnode, vector=(1 - radius, 0, 0)) tnode = ngl.Rotate(tnode, anim=ngl.AnimatedFloat(rotate_animkf)) group.add_children(tnode) return group
def animated_circles(cfg): """Simple cyclic circles animation""" group = ngl.Group() cfg.duration = 5.0 cfg.aspect_ratio = (1, 1) radius = 0.2 n = 10 step = 360.0 / n shape = ngl.Circle(radius=radius, npoints=128) render = ngl.RenderColor(geometry=shape) for i in range(n): mid_time = cfg.duration / 2.0 start_time = mid_time / (i + 2) end_time = cfg.duration - start_time scale_animkf = [ ngl.AnimKeyFrameVec3(start_time, (0, 0, 0)), ngl.AnimKeyFrameVec3(mid_time, (1.0, 1.0, 1.0), "exp_out"), ngl.AnimKeyFrameVec3(end_time, (0, 0, 0), "exp_in"), ] angle = i * step rotate_animkf = [ ngl.AnimKeyFrameFloat(start_time, 0), ngl.AnimKeyFrameFloat(mid_time, angle, "exp_out"), ngl.AnimKeyFrameFloat(end_time, 0, "exp_in"), ] tnode = render tnode = ngl.Scale(tnode, factors=ngl.AnimatedVec3(scale_animkf)) tnode = ngl.Translate(tnode, vector=(1 - radius, 0, 0)) tnode = ngl.Rotate(tnode, angle=ngl.AnimatedFloat(rotate_animkf)) group.add_children(tnode) return group
def blending_and_stencil(cfg): """Scene using blending and stencil graphic features""" cfg.duration = 5 vertex = cfg.get_vert("color") fragment = cfg.get_frag("color") program = ngl.Program(vertex=vertex, fragment=fragment) circle = ngl.Circle(npoints=256) cloud_color = ngl.UniformVec3(value=(1, 1, 1)) cloud_opacity = ngl.UniformFloat(0.4) main_group = ngl.Group() render = ngl.RenderColor(color=(0.2, 0.6, 1), label="sky") config = ngl.GraphicConfig( render, stencil_test=True, stencil_write_mask=0xFF, stencil_func="always", stencil_ref=1, stencil_read_mask=0xFF, stencil_fail="replace", stencil_depth_fail="replace", stencil_depth_pass="******", ) main_group.add_children(config) render = ngl.RenderColor(color=(1, 0.8, 0), geometry=circle, label="sun") scale = ngl.Scale(render, (0.15, 0.15, 0.0)) translate = ngl.Translate(scale, (0.4, 0.3, 0)) main_group.add_children(translate) cloud_group = ngl.Group(label="clouds") centers = [ (-1.0, 0.85, 0.4), (-0.5, 2.0, 1.0), (0, 0.85, 0.4), (1.0, 1.55, 0.8), (0.6, 0.65, 0.075), (0.5, 1.80, 1.25), ] for center in centers: render = ngl.Render(circle, program, blending="src_over") render.update_frag_resources(color=cloud_color, opacity=cloud_opacity) factor = cfg.rng.random() * 0.4 + center[2] keyframe = cfg.duration * (cfg.rng.random() * 0.4 + 0.2) animkf = ( ngl.AnimKeyFrameVec3(0, (factor, factor, 0)), ngl.AnimKeyFrameVec3(keyframe, (factor + 0.1, factor + 0.1, 0)), ngl.AnimKeyFrameVec3(cfg.duration, (factor, factor, 0)), ) scale = ngl.Scale(render, factors=ngl.AnimatedVec3(animkf)) translate = ngl.Translate(scale, vector=(center[0], center[1], 0)) cloud_group.add_children(translate) config = ngl.GraphicConfig( cloud_group, stencil_test=True, stencil_write_mask=0x0, stencil_func="equal", stencil_ref=1, stencil_read_mask=0xFF, stencil_fail="keep", stencil_depth_fail="keep", stencil_depth_pass="******", ) main_group.add_children(config) camera = ngl.Camera(main_group) camera.set_eye(0.0, 0.0, 2.0) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_orthographic(-cfg.aspect_ratio_float, cfg.aspect_ratio_float, -1.0, 1.0) camera.set_clipping(1.0, 10.0) return camera
def _get_easing_node(cfg, easing, curve_zoom, color_program, nb_points=128): text_vratio = 1 / 8. graph_hpad_ratio = 1 / 16. area_size = 2.0 width, height = area_size, area_size text_height = text_vratio * height pad_height = graph_hpad_ratio * height # Colors hue = random.uniform(0, 0.6) color = list(colorsys.hls_to_rgb(hue, 0.6, 1.0)) + [1] ucolor = ngl.UniformVec4(value=color) graph_bg_ucolor = ngl.UniformVec4(value=(.15, .15, .15, 1)) normed_graph_bg_ucolor = ngl.UniformVec4(value=(0, 0, 0, 1)) line_ucolor = ngl.UniformVec4(value=(1, 1, 1, .4)) # Text legend text = ngl.Text(text=easing, fg_color=color, padding=3, bg_color=(0, 0, 0, 1), box_corner=(-width / 2., height / 2. - text_height, 0), box_width=(width, 0, 0), box_height=(0, text_height, 0), label='%s legend' % easing) # Graph drawing area (where the curve may overflow) graph_size = area_size - text_height - pad_height * 2 graph_block = _block(graph_size, graph_size, color_program, corner=(-graph_size / 2, -(graph_size + text_height) / 2, 0), color=graph_bg_ucolor) # Normed area of the graph normed_graph_size = graph_size * curve_zoom normed_graph_block = _block(normed_graph_size, normed_graph_size, color_program, corner=(-normed_graph_size / 2, -(normed_graph_size + text_height) / 2, 0), color=normed_graph_bg_ucolor) # Curve easing_name, easing_args = _easing_split(easing) curve_scale_factor = graph_size / area_size * curve_zoom vertices_data = array.array('f') for i in range(nb_points + 1): t = i / float(nb_points) v = ngl.easing_evaluate(easing_name, t, easing_args) x = curve_scale_factor * (t * width - width / 2.) y = curve_scale_factor * (v * height - height / 2.) y -= text_height / 2. vertices_data.extend([x, y, 0]) vertices = ngl.BufferVec3(data=vertices_data) geometry = ngl.Geometry(vertices, topology='line_strip') curve = ngl.Render(geometry, color_program, label='%s curve' % easing) curve.update_uniforms(color=ucolor) # Value cursor y = 2 / 3. * pad_height x = y * math.sqrt(3) cursor_geometry = ngl.Triangle((-x, y, 0), (0, 0, 0), (-x, -y, 0)) cursor = ngl.Render(cursor_geometry, color_program, label='%s cursor' % easing) cursor.update_uniforms(color=ucolor) # Horizontal value line hline_data = array.array('f', (0, 0, 0, graph_size, 0, 0)) hline_vertices = ngl.BufferVec3(data=hline_data) hline_geometry = ngl.Geometry(hline_vertices, topology='line_strip') hline = ngl.Render(hline_geometry, color_program, label='%s value line' % easing) hline.update_uniforms(color=line_ucolor) # Value animation (cursor + h-line) value_x = -graph_size / 2. value_y = (-text_height - normed_graph_size) / 2. value_animkf = ( ngl.AnimKeyFrameVec3(0, (value_x, value_y, 0)), ngl.AnimKeyFrameVec3(cfg.duration, (value_x, value_y + normed_graph_size, 0), easing_name, easing_args), ) value_anim = ngl.Group(children=(hline, cursor)) value_anim = ngl.Translate(value_anim, anim=ngl.AnimatedVec3(value_animkf), label='%s value anim' % easing) # Vertical time line vline_data = array.array('f', (0, 0, 0, 0, graph_size, 0)) vline_vertices = ngl.BufferVec3(data=vline_data) vline_geometry = ngl.Geometry(vline_vertices, topology='line_strip') vline = ngl.Render(vline_geometry, color_program, label='%s time line' % easing) vline.update_uniforms(color=line_ucolor) # Time animation (v-line only) time_x = -normed_graph_size / 2. time_y = (-text_height - graph_size) / 2. time_animkf = (ngl.AnimKeyFrameVec3(0, (time_x, time_y, 0)), ngl.AnimKeyFrameVec3( cfg.duration, (time_x + normed_graph_size, time_y, 0))) time_anim = ngl.Translate(vline, anim=ngl.AnimatedVec3(time_animkf), label='%s time anim' % easing) group = ngl.Group(label='%s block' % easing) group.add_children(text, graph_block, normed_graph_block, curve, value_anim, time_anim) return group
def blending_and_stencil(cfg): '''Scene using blending and stencil graphic features''' cfg.duration = 5 random.seed(0) fragment = cfg.get_frag('color') program = ngl.Program(fragment=fragment) circle = ngl.Circle(npoints=256) cloud_color = ngl.UniformVec4(value=(1, 1, 1, 0.4)) main_group = ngl.Group() quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0)) render = ngl.Render(quad, program, label='sky') render.update_uniforms(color=ngl.UniformVec4(value=(0.2, 0.6, 1, 1))) config = ngl.GraphicConfig(render, stencil_test=True, stencil_write_mask=0xFF, stencil_func='always', stencil_ref=1, stencil_read_mask=0xFF, stencil_fail='replace', stencil_depth_fail='replace', stencil_depth_pass='******') main_group.add_children(config) render = ngl.Render(circle, program, label='sun') render.update_uniforms(color=ngl.UniformVec4(value=(1, 0.8, 0, 1))) scale = ngl.Scale(render, (0.15, 0.15, 0.0)) translate = ngl.Translate(scale, (0.4, 0.3, 0)) main_group.add_children(translate) cloud_group = ngl.Group(label='clouds') centers = [ (-1.0, 0.85, 0.4), (-0.5, 2.0, 1.0), ( 0, 0.85, 0.4), ( 1.0, 1.55, 0.8), ( 0.6, 0.65, 0.075), ( 0.5, 1.80, 1.25), ] for center in centers: render = ngl.Render(circle, program) render.update_uniforms(color=cloud_color) factor = random.random() * 0.4 + center[2] keyframe = cfg.duration * (random.random() * 0.4 + 0.2) animkf = (ngl.AnimKeyFrameVec3(0, (factor, factor, 0)), ngl.AnimKeyFrameVec3(keyframe, (factor + 0.1, factor + 0.1, 0)), ngl.AnimKeyFrameVec3(cfg.duration, (factor, factor, 0))) scale = ngl.Scale(render, anim=ngl.AnimatedVec3(animkf)) translate = ngl.Translate(scale, vector=(center[0], center[1], 0)) cloud_group.add_children(translate) config = ngl.GraphicConfig(cloud_group, blend=True, blend_src_factor='src_alpha', blend_dst_factor='one_minus_src_alpha', blend_src_factor_a='zero', blend_dst_factor_a='one', stencil_test=True, stencil_write_mask=0x0, stencil_func='equal', stencil_ref=1, stencil_read_mask=0xFF, stencil_fail='keep', stencil_depth_fail='keep', stencil_depth_pass='******') main_group.add_children(config) camera = ngl.Camera(main_group) camera.set_eye(0.0, 0.0, 2.0) camera.set_center(0.0, 0.0, 0.0) camera.set_up(0.0, 1.0, 0.0) camera.set_orthographic(-cfg.aspect_ratio_float, cfg.aspect_ratio_float, -1.0, 1.0) camera.set_clipping(1.0, 10.0) return camera
if data is None: _, v1, v2, _ = gen_floats(4) return [key_cls(t0, v1), key_cls(t1, v2), key_cls(t2, v1)] return [ key_cls(t0, data), key_cls(t1, data[::-1]), key_cls(t2, data), ] FUNCS = dict( animated_float=lambda data: ngl.AnimatedFloat(keyframes=_get_anim_kf( ngl.AnimKeyFrameFloat, data)), animated_vec2=lambda data: ngl.AnimatedVec2(keyframes=_get_anim_kf( ngl.AnimKeyFrameVec2, data)), animated_vec3=lambda data: ngl.AnimatedVec3(keyframes=_get_anim_kf( ngl.AnimKeyFrameVec3, data)), animated_vec4=lambda data: ngl.AnimatedVec4(keyframes=_get_anim_kf( ngl.AnimKeyFrameVec4, data)), animated_buffer_float=lambda data: ngl.AnimatedBufferFloat( keyframes=_get_anim_kf(ngl.AnimKeyFrameBuffer, data)), animated_buffer_vec2=lambda data: ngl.AnimatedBufferVec2( keyframes=_get_anim_kf(ngl.AnimKeyFrameBuffer, data)), animated_buffer_vec3=lambda data: ngl.AnimatedBufferVec3( keyframes=_get_anim_kf(ngl.AnimKeyFrameBuffer, data)), animated_buffer_vec4=lambda data: ngl.AnimatedBufferVec4( keyframes=_get_anim_kf(ngl.AnimKeyFrameBuffer, data)), animated_quat_mat4=lambda data: ngl.AnimatedQuat( keyframes=_get_anim_kf(ngl.AnimKeyFrameQuat, data), as_mat4=True), animated_quat_vec4=lambda data: ngl.AnimatedQuat( keyframes=_get_anim_kf(ngl.AnimKeyFrameQuat, data), as_mat4=False), array_float=lambda data: ngl.BufferFloat(data=data),