Ejemplo n.º 1
0
def square2circle(cfg, square_color=(0.9, 0.1, 0.3, 1.0), circle_color=(1.0, 1.0, 1.0, 1.0)):
    '''Morphing of a square (composed of many vertices) into a circle'''
    cfg.duration = 5
    cfg.aspect_ratio = (1, 1)

    def sqxf(t):  # square x coordinates clockwise starting top-left
        if t < 1/4.: return t*4
        if t < 1/2.: return 1
        if t < 3/4.: return 1.-(t-.5)*4
        return 0

    def sqyf(t):  # square y coordinates clockwise starting top-left
        if t < 1/4.: return 1
        if t < 1/2.: return 1.-(t-.25)*4
        if t < 3/4.: return 0
        return (t-.75)*4

    n = 1024  # number of vertices
    s = 1.25  # shapes scale
    interp = 'exp_in_out'

    center_vertex = [0, 0, 0]
    square_vertices = array.array('f', center_vertex)
    for i in range(n):
        x = (sqxf(i / float(n)) - .5) * s
        y = (sqyf(i / float(n)) - .5) * s
        square_vertices.extend([x, y, 0])

    circle_vertices = array.array('f', center_vertex)
    step = 2 * math.pi / float(n)
    for i in range(n):
        angle = i * step - math.pi/4.
        x = math.sin(angle) * .5 * s
        y = math.cos(angle) * .5 * s
        circle_vertices.extend([x, y, 0])

    indices = array.array('H')
    for i in range(1, n + 1):
        indices.extend([0, i, i + 1])
    indices[-1] = 1

    vertices_animkf = [
            ngl.AnimKeyFrameBuffer(0,               square_vertices),
            ngl.AnimKeyFrameBuffer(cfg.duration/2., circle_vertices, interp),
            ngl.AnimKeyFrameBuffer(cfg.duration,    square_vertices, interp),
    ]
    vertices = ngl.AnimatedBufferVec3(vertices_animkf)

    color_animkf = [
            ngl.AnimKeyFrameVec4(0,               square_color),
            ngl.AnimKeyFrameVec4(cfg.duration/2., circle_color, interp),
            ngl.AnimKeyFrameVec4(cfg.duration,    square_color, interp),
    ]
    ucolor = ngl.AnimatedVec4(color_animkf)

    geom = ngl.Geometry(vertices, indices=ngl.BufferUShort(data=indices))
    p = ngl.Program(vertex=cfg.get_vert('color'), fragment=cfg.get_frag('color'))
    render = ngl.Render(geom, p)
    render.update_frag_resources(color=ucolor)
    return render
Ejemplo n.º 2
0
def buffer_dove(cfg,
                bgcolor1=(.6, 0, 0, 1),
                bgcolor2=(.8, .8, 0, 1),
                bilinear_filtering=True):
    '''Blending of a Render using a Buffer as data source'''
    cfg.duration = 3.

    # Credits: https://icons8.com/icon/40514/dove
    icon_filename = op.join(op.dirname(__file__), 'data', 'icons8-dove.raw')
    cfg.files.append(icon_filename)
    w, h = (96, 96)
    cfg.aspect_ratio = (w, h)

    img_buf = ngl.BufferUBVec4(filename=icon_filename, label='icon raw buffer')

    img_tex = ngl.Texture2D(data_src=img_buf, width=w, height=h)
    if bilinear_filtering:
        img_tex.set_mag_filter('linear')
    quad = ngl.Quad((-.5, -.5, 0.1), (1, 0, 0), (0, 1, 0))
    program = ngl.Program(vertex=cfg.get_vert('texture'),
                          fragment=cfg.get_frag('texture'))
    program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(),
                                 var_uvcoord=ngl.IOVec2())
    render = ngl.Render(quad, program, label='dove')
    render.update_frag_resources(tex0=img_tex)
    render = ngl.GraphicConfig(render,
                               blend=True,
                               blend_src_factor='src_alpha',
                               blend_dst_factor='one_minus_src_alpha',
                               blend_src_factor_a='zero',
                               blend_dst_factor_a='one')

    prog_bg = ngl.Program(vertex=cfg.get_vert('color'),
                          fragment=cfg.get_frag('color'))
    shape_bg = ngl.Circle(radius=.6, npoints=256)
    render_bg = ngl.Render(shape_bg, prog_bg, label='background')
    color_animkf = [
        ngl.AnimKeyFrameVec4(0, bgcolor1),
        ngl.AnimKeyFrameVec4(cfg.duration / 2.0, bgcolor2),
        ngl.AnimKeyFrameVec4(cfg.duration, bgcolor1)
    ]
    ucolor = ngl.AnimatedVec4(color_animkf)
    render_bg.update_frag_resources(color=ucolor)

    return ngl.Group(children=(render_bg, render))
Ejemplo n.º 3
0
        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),
    array_int=lambda data: ngl.BufferInt(data=data),
    array_ivec2=lambda data: ngl.BufferIVec2(data=data),