def buffer_dove(cfg,
                bgcolor1=(0.6, 0, 0),
                bgcolor2=(0.8, 0.8, 0),
                bilinear_filtering=True):
    """Blending of a Render using a Buffer as data source"""
    cfg.duration = 3.0

    # Credits: https://icons8.com/icon/40514/dove
    # (Raw data is the premultiplied)
    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((-0.5, -0.5, 0.1), (1, 0, 0), (0, 1, 0))
    render = ngl.RenderTexture(img_tex, geometry=quad, blending="src_over")

    shape_bg = ngl.Circle(radius=0.6, npoints=256)
    color_animkf = [
        ngl.AnimKeyFrameColor(0, bgcolor1),
        ngl.AnimKeyFrameColor(cfg.duration / 2.0, bgcolor2),
        ngl.AnimKeyFrameColor(cfg.duration, bgcolor1),
    ]
    ucolor = ngl.AnimatedColor(color_animkf)
    render_bg = ngl.RenderColor(ucolor, geometry=shape_bg, label="background")

    return ngl.Group(children=(render_bg, render))
示例#2
0
 def scene_func(cfg):
     cfg.duration = 5
     color_animkf = [
         # Start at t=1 and end 1s earlier so that it tests the underflow
         # and overflow of the animation
         ngl.AnimKeyFrameColor(1, c0),
         ngl.AnimKeyFrameColor(cfg.duration - 1, c1),
     ]
     ucolor = ngl.AnimatedColor(color_animkf, space=space)
     return ngl.RenderColor(ucolor)
示例#3
0
def square2circle(cfg,
                  square_color=(0.9, 0.1, 0.3),
                  circle_color=(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.0:
            return t * 4
        if t < 1 / 2.0:
            return 1
        if t < 3 / 4.0:
            return 1.0 - (t - 0.5) * 4
        return 0

    def sqyf(t):  # square y coordinates clockwise starting top-left
        if t < 1 / 4.0:
            return 1
        if t < 1 / 2.0:
            return 1.0 - (t - 0.25) * 4
        if t < 3 / 4.0:
            return 0
        return (t - 0.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)) - 0.5) * s
        y = (sqyf(i / float(n)) - 0.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.0
        x = math.sin(angle) * 0.5 * s
        y = math.cos(angle) * 0.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.0, circle_vertices, interp),
        ngl.AnimKeyFrameBuffer(cfg.duration, square_vertices, interp),
    ]
    vertices = ngl.AnimatedBufferVec3(vertices_animkf)

    color_animkf = [
        ngl.AnimKeyFrameColor(0, square_color),
        ngl.AnimKeyFrameColor(cfg.duration / 2.0, circle_color, interp),
        ngl.AnimKeyFrameColor(cfg.duration, square_color, interp),
    ]
    ucolor = ngl.AnimatedColor(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, opacity=ngl.UniformFloat(1))
    return render