def _get_compositing_scene(cfg, op, show_label=False): cfg.aspect_ratio = (1, 1) cfg.duration = 6 # We can not use a circle geometry because the whole areas must be # rasterized for the compositing to work, so instead we build 2 overlapping # quad into which we draw colored circles, offsetted with an animation. # Alternatively, we could use a RTT. quad = ngl.Quad(corner=(-1, -1, 0), width=(2, 0, 0), height=(0, 2, 0)) prog = ngl.Program(vertex=_VERTEX, fragment=_FRAGMENT) prog.update_vert_out_vars(uv=ngl.IOVec2()) A_off_kf = ( ngl.AnimKeyFrameVec2(0, (-1 / 3, 0)), ngl.AnimKeyFrameVec2(cfg.duration / 2, (1 / 3, 0)), ngl.AnimKeyFrameVec2(cfg.duration, (-1 / 3, 0)), ) B_off_kf = ( ngl.AnimKeyFrameVec2(0, (1 / 3, 0)), ngl.AnimKeyFrameVec2(cfg.duration / 2, (-1 / 3, 0)), ngl.AnimKeyFrameVec2(cfg.duration, (1 / 3, 0)), ) A_off = ngl.AnimatedVec2(A_off_kf) B_off = ngl.AnimatedVec2(B_off_kf) A = ngl.Render(quad, prog, label="A") A.update_frag_resources(color=ngl.UniformVec3(value=COLORS.azure), off=A_off) B = ngl.Render(quad, prog, label="B", blending=op) B.update_frag_resources(color=ngl.UniformVec3(value=COLORS.orange), off=B_off) bg = ngl.RenderColor(blending="dst_over") # draw A in current FBO, then draw B with the current operator, and # then result goes over the white background ret = ngl.Group(children=(A, B, bg)) if show_label: label_h = 1 / 4 label_pad = 0.1 label = ngl.Text( op, fg_color=COLORS.black, bg_color=(0.8, 0.8, 0.8), bg_opacity=1, box_corner=(label_pad / 2 - 1, 1 - label_h - label_pad / 2, 0), box_width=(2 - label_pad, 0, 0), box_height=(0, label_h, 0), ) ret.add_children(label) return ret
def velocity_circle_distort_2d(cfg): cfg.duration = 4.0 cfg.aspect_ratio = (1, 1) coords = list(equilateral_triangle_coords()) coords.append(coords[0]) pos_kf = [ ngl.AnimKeyFrameVec2(cfg.duration * i / 3.0, pos[0:2], "exp_in_out") for i, pos in enumerate(coords) ] anim = ngl.AnimatedVec2(pos_kf) velocity = ngl.VelocityVec2(anim) vert = textwrap.dedent("""\ void main() { float distort_max = 0.1; float velocity_l = length(velocity); float direction_l = length(ngl_position); vec2 normed_velocity = velocity_l == 0.0 ? vec2(0.0) : -velocity / velocity_l; vec2 normed_direction = direction_l == 0.0 ? vec2(0.0) : ngl_position.xy / 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(translate, 0.0, 0.0) + vec4(-distort * velocity, 0.0, 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, translate=anim) return shape
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.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 _get_anim_kf(key_cls, data): t0, t1, t2 = 0, ANIM_DURATION / 2., ANIM_DURATION 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(