def gradient_eval(cfg, mode="ramp", c0=(1, 0.5, 0.5), c1=(0.5, 1, 0.5)):
    """Animate a gradient and objects using CPU evaluation"""

    pos_res = dict(t=ngl.Time())
    pos0_x = ngl.EvalFloat("sin( 0.307*t - 0.190)", resources=pos_res)
    pos0_y = ngl.EvalFloat("sin( 0.703*t - 0.957)", resources=pos_res)
    pos1_x = ngl.EvalFloat("sin(-0.236*t + 0.218)", resources=pos_res)
    pos1_y = ngl.EvalFloat("sin(-0.851*t - 0.904)", resources=pos_res)

    trf0 = ngl.EvalVec3("x", "y", "0", resources=dict(x=pos0_x, y=pos0_y))
    trf1 = ngl.EvalVec3("x", "y", "0", resources=dict(x=pos1_x, y=pos1_y))

    geom = ngl.Circle(radius=0.02, npoints=64)
    p0 = ngl.RenderColor(color=(1 - c0[0], 1 - c0[1], 1 - c0[2]),
                         geometry=geom)
    p1 = ngl.RenderColor(color=(1 - c1[0], 1 - c1[1], 1 - c1[2]),
                         geometry=geom)
    p0 = ngl.Scale(p0, factors=(1 / cfg.aspect_ratio_float, 1, 1))
    p1 = ngl.Scale(p1, factors=(1 / cfg.aspect_ratio_float, 1, 1))
    p0 = ngl.Translate(p0, vector=trf0)
    p1 = ngl.Translate(p1, vector=trf1)

    pos0 = ngl.EvalVec2("x/2+.5", ".5-y/2", resources=dict(x=pos0_x, y=pos0_y))
    pos1 = ngl.EvalVec2("x/2+.5", ".5-y/2", resources=dict(x=pos1_x, y=pos1_y))
    grad = ngl.RenderGradient(pos0=pos0,
                              pos1=pos1,
                              mode=mode,
                              color0=c0,
                              color1=c1)

    return ngl.Group(children=(grad, p0, p1))
Example #2
0
def rtt_clear_attachment_with_timeranges(cfg):
    cfg.aspect_ratio = (1, 1)

    # Time-disabled full screen white quad
    render = ngl.RenderColor(COLORS.white)

    time_range_filter = ngl.TimeRangeFilter(render)
    time_range_filter.add_ranges(ngl.TimeRangeModeNoop(0))

    # Intermediate no-op RTT to force the use of a different render pass internally
    texture = ngl.Texture2D(width=32, height=32)
    rtt_noop = ngl.RenderToTexture(ngl.Identity(), [texture])

    # Centered rotating quad
    quad = ngl.Quad((-0.5, -0.5, 0), (1, 0, 0), (0, 1, 0))
    render = ngl.RenderColor(COLORS.orange, geometry=quad)

    animkf = [ngl.AnimKeyFrameFloat(0, 0), ngl.AnimKeyFrameFloat(cfg.duration, -360)]
    render = ngl.Rotate(render, angle=ngl.AnimatedFloat(animkf))

    group = ngl.Group(children=(time_range_filter, rtt_noop, render))

    # Root RTT
    texture = ngl.Texture2D(width=512, height=512)
    rtt = ngl.RenderToTexture(group, [texture])

    # Full screen render of the root RTT result
    render = ngl.RenderTexture(texture)

    return ngl.Group(children=(rtt, render))
def _get_userlive_switch_func():
    scene0 = ngl.RenderColor(COLORS.white, geometry=ngl.Circle())
    scene1 = ngl.RenderColor(COLORS.red, geometry=ngl.Quad())
    scene2 = ngl.RenderColor(COLORS.azure, geometry=ngl.Triangle())

    switch0 = ngl.UserSwitch(
        ngl.Scale(scene0, factors=(1 / 3, 1 / 3, 1 / 3), anchor=(-1, 0, 0)))
    switch1 = ngl.UserSwitch(ngl.Scale(scene1, factors=(1 / 2, 1 / 2, 1 / 2)))
    switch2 = ngl.UserSwitch(
        ngl.Scale(scene2, factors=(1 / 3, 1 / 3, 1 / 3), anchor=(1, 0, 0)))

    def keyframes_callback(t_id):
        # Build a "random" composition of switches
        switch0.set_enabled(t_id % 2 == 0)
        switch1.set_enabled(t_id % 3 == 0)
        switch2.set_enabled(t_id % 4 == 0)

    @test_fingerprint(nb_keyframes=10,
                      keyframes_callback=keyframes_callback,
                      tolerance=1,
                      exercise_serialization=False)
    @scene(s0=scene.Bool(), s1=scene.Bool(), s2=scene.Bool())
    def scene_func(cfg, s0_enabled=True, s1_enabled=True, s2_enabled=True):
        cfg.aspect_ratio = (1, 1)
        switch0.set_enabled(s0_enabled)
        switch1.set_enabled(s1_enabled)
        switch2.set_enabled(s2_enabled)
        return ngl.Group(children=(switch0, switch1, switch2))

    return scene_func
Example #4
0
def _rtt_load_attachment():
    background = ngl.RenderColor(COLORS.white)
    render = ngl.RenderColor(COLORS.orange)

    texture = ngl.Texture2D(width=16, height=16)
    rtt = ngl.RenderToTexture(render, [texture])

    texture_noop = ngl.Texture2D(width=16, height=16)
    rtt_noop = ngl.RenderToTexture(render, [texture_noop])

    quad = ngl.Quad((0, 0, 0), (1, 0, 0), (0, 1, 0))
    foreground = ngl.RenderTexture(texture, geometry=quad)

    return ngl.Group(children=(background, rtt, rtt_noop, foreground))
Example #5
0
def _path_scene(cfg, path, points=None, controls=None, easing="linear"):
    cfg.aspect_ratio = (1, 1)

    anim_kf = [
        ngl.AnimKeyFrameFloat(0, 0),
        ngl.AnimKeyFrameFloat(cfg.duration, 1, easing),
    ]

    geom = ngl.Circle(radius=0.03, npoints=32)
    shape = ngl.RenderColor(COLORS.orange, geometry=geom)

    moving_shape = ngl.Translate(shape, vector=ngl.AnimatedPath(anim_kf, path))

    objects = []

    if points:
        debug_points = {f"P{i}": p[:2] for i, p in enumerate(points)}
        objects.append(get_debug_points(cfg, debug_points))

    if controls:
        debug_controls = {f"C{i}": p[:2] for i, p in enumerate(controls)}
        objects.append(get_debug_points(cfg, debug_controls,
                                        color=COLORS.cyan))

    objects.append(moving_shape)

    return ngl.Group(children=objects)
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))
def _get_random_render(cfg, rng, t0, t1, enable_computes):
    color = lambda rng: ngl.RenderColor(
        color=_get_random_animated_color(rng, t0, t1),
        opacity=_get_random_animated_opacity(rng, t0, t1),
        geometry=_get_random_geometry(rng),
        blending="src_over",
    )
    gradient = lambda rng: ngl.RenderGradient(
        color0=_get_random_animated_color(rng, t0, t1),
        color1=_get_random_animated_color(rng, t0, t1),
        opacity0=_get_random_animated_opacity(rng, t0, t1),
        opacity1=_get_random_animated_opacity(rng, t0, t1),
        pos0=(rng.random(), rng.random()),
        pos1=(rng.random(), rng.random()),
        mode=rng.choice(("ramp", "radial")),
        geometry=_get_random_geometry(rng),
        blending="src_over",
    )
    texture = lambda rng: _get_random_rendertexture(cfg, rng)
    text = lambda rng: _get_random_text(cfg, rng)
    compute = lambda rng: _get_random_compute(cfg, rng, t0, t1)
    render_funcs = [
        color,
        gradient,
        text,
        texture,
    ]
    if enable_computes:
        render_funcs.append(compute)
    return rng.choice(render_funcs)(rng)
Example #8
0
def _get_background_circles(circle, positions, bcolor):
    blend_bg = ngl.Group()
    render = ngl.RenderColor(bcolor, geometry=circle)
    for position in positions:
        trender = ngl.Translate(render, position + (0.0, ))
        blend_bg.add_children(trender)
    return blend_bg
Example #9
0
def velocity_triangle_rotate(cfg):
    cfg.duration = 5.0
    cfg.aspect_ratio = (1, 1)

    anim_kf = [
        ngl.AnimKeyFrameFloat(0, 0),
        ngl.AnimKeyFrameFloat(cfg.duration, 360 * 3, "circular_in_out"),
    ]
    anim = ngl.AnimatedFloat(anim_kf)
    velocity = ngl.VelocityFloat(anim)

    frag = textwrap.dedent("""\
    void main()
    {
        float v = clamp(velocity / 3000., 0.0, 1.0);
        ngl_out_color = vec4(v, v / 2.0, 0.0, 1.0);
    }
    """)

    p0, p1, p2 = equilateral_triangle_coords(2.0)
    triangle = ngl.RenderColor(COLORS.white, geometry=ngl.Triangle(p0, p1, p2))
    triangle = ngl.Rotate(triangle, angle=anim)

    prog_c = ngl.Program(vertex=cfg.get_vert("color"), fragment=frag)
    circle = ngl.Render(ngl.Circle(radius=1.0, npoints=128), prog_c)
    circle.update_frag_resources(velocity=velocity)
    return ngl.Group(children=(circle, triangle))
Example #10
0
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
Example #11
0
def _get_live_shared_uniform_scene(cfg, color, debug_positions):
    group = ngl.Group()
    for i in range(2):
        quad = ngl.Quad((-1 + i, -1 + i, 0), (1, 0, 0), (0, 1, 0))
        render = ngl.RenderColor(color, geometry=quad)
        group.add_children(render)
    if debug_positions:
        group.add_children(get_debug_points(cfg, _SHARED_UNIFORM_CUEPOINTS))
    return group
Example #12
0
def _get_blending_base_objects():
    circle = ngl.Circle(radius=_CIRCLE_RADIUS, npoints=100)
    positions = _equilateral_triangle_coords(_CIRCLE_RADIUS * 2.0 / 3.0)
    colored_circles = ngl.Group(label="colored circles")
    for position, color in zip(positions, _CIRCLES_COLORS):
        render = ngl.RenderColor(color, geometry=circle)
        render = ngl.Translate(render, position + (0.0, ))
        colored_circles.add_children(render)
    return colored_circles, circle, positions
Example #13
0
def shape_diamond_colormask(_):
    color_write_masks = ("r+g+b+a", "r+g+a", "g+b+a", "r+b+a")
    geometry = ngl.Circle(npoints=5)
    render = ngl.RenderColor(COLORS.white, geometry=geometry)
    scenes = [
        ngl.GraphicConfig(render, color_write_mask=cwm)
        for cwm in color_write_masks
    ]
    return autogrid_simple(scenes)
def _block(w, h, color, corner=None):
    block_width = (w, 0, 0)
    block_height = (0, h, 0)
    block_corner = (-w / 2.0, -h / 2.0, 0) if corner is None else corner
    block_quad = ngl.Quad(corner=block_corner,
                          width=block_width,
                          height=block_height)
    block_render = ngl.RenderColor(color=color, geometry=block_quad)
    return block_render
Example #15
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)
Example #16
0
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 _render_quad(cfg,
                 corner=(-1, -1, 0),
                 width=(2, 0, 0),
                 height=(0, 2, 0),
                 color=(1, 1, 1),
                 opacity=1):
    quad = ngl.Quad(corner, width, height)
    return ngl.RenderColor(color,
                           opacity=opacity,
                           geometry=quad,
                           blending="src_over")
def texture_scissor(cfg):
    cfg.aspect_ratio = (1, 1)

    render = ngl.RenderColor(COLORS.orange)
    graphic_config = ngl.GraphicConfig(render,
                                       scissor_test=True,
                                       scissor=(32, 32, 32, 32))
    texture = ngl.Texture2D(width=64, height=64)
    rtt = ngl.RenderToTexture(graphic_config, [texture],
                              clear_color=(0, 0, 0, 1))
    render = ngl.RenderTexture(texture)
    return ngl.Group(children=(rtt, render))
Example #19
0
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 texture_clear_and_scissor(cfg):
    render = ngl.RenderColor(COLORS.white)
    graphic_config = ngl.GraphicConfig(render,
                                       scissor_test=True,
                                       scissor=(0, 0, 0, 0),
                                       color_write_mask="")

    texture = ngl.Texture2D(width=64, height=64)
    rtt = ngl.RenderToTexture(ngl.Identity(), [texture],
                              clear_color=list(COLORS.orange) + [1])
    render = ngl.RenderTexture(texture)

    return ngl.Group(children=(graphic_config, rtt, render))
def _get_userlive_select_func():
    # We point on the same underlying render to test the different render paths
    render = ngl.RenderColor(COLORS.white, opacity=0.5, geometry=ngl.Quad())
    below = ngl.Translate(render, vector=(0.5 - 2 / 3, 0.5 - 1 / 3, 0))
    above = ngl.Translate(render, vector=(0.5 - 1 / 3, 0.5 - 2 / 3, 0))

    # Additive blending (for premultiplied values): lighten
    gc0 = ngl.GraphicConfig(
        above,
        blend=True,
        blend_src_factor="one",
        blend_dst_factor="one",
        blend_src_factor_a="one",
        blend_dst_factor_a="one",
    )

    # Multiply blending (for premultiplied values): darken
    gc1 = ngl.GraphicConfig(
        above,
        blend=True,
        blend_src_factor="zero",
        blend_dst_factor="src_color",
        blend_src_factor_a="zero",
        blend_dst_factor_a="src_alpha",
    )

    # Select has 3 branches: simple over blending, additive blending, multiply
    # blending
    select = ngl.UserSelect(branches=(above, gc0, gc1))

    def keyframes_callback(t_id):
        # 4 states: the for the 3 blending branches and one extra for nothing
        # (branch ID overflow). We remain on the each state for 2 frames.
        select.set_branch((t_id // 2) % 4)

    @test_fingerprint(nb_keyframes=8,
                      keyframes_callback=keyframes_callback,
                      tolerance=1,
                      exercise_serialization=False)
    @scene(branch=scene.Range([0, 3]))
    def scene_func(cfg, branch=0):
        cfg.aspect_ratio = (1, 1)
        select.set_branch(branch)
        return ngl.Group(children=(below, select))

    return scene_func
Example #22
0
def compare(cfg, scene0, scene1, xsplit):
    """
    Compare two scenes by splitting them vertically. The scene generated by
    this function relies on the stencil buffer, and thus can not work properly
    if one of the compared scenes uses it.
    """

    # Fill the stencil buffer with 1's on the left side, and 0's on the right
    scene_mask = ngl.RenderColor()
    scene_mask = ngl.Scale(scene_mask,
                           factors=(xsplit, 1, 1),
                           anchor=(-1, 0, 0))
    scene_mask = ngl.GraphicConfig(
        scene_mask,
        color_write_mask=
        "",  # no need to write the color themselves, only the stencil
        stencil_test=True,
        stencil_ref=1,
        stencil_read_mask=0,
        stencil_write_mask=0xFF,
        stencil_fail="replace",
        stencil_depth_fail="replace",
        stencil_depth_pass="******",
    )

    # Draw the 1st scene where the stencil is equal to 1 (left)
    scene0 = ngl.GraphicConfig(scene0, stencil_func="equal")

    # Draw the 2nd scene where the stencil is not equal to 1 (right)
    scene1 = ngl.GraphicConfig(scene1, stencil_func="notequal")

    stencil_test = ngl.GraphicConfig(
        ngl.Group(children=(scene0, scene1)),
        stencil_test=True,
        stencil_ref=1,
        stencil_read_mask=0xFF,
        stencil_write_mask=0,
        stencil_fail="keep",
        stencil_depth_fail="keep",
        stencil_depth_pass="******",
    )

    return ngl.Group(children=(scene_mask, stencil_test))
def get_debug_points(cfg,
                     points,
                     radius=0.025,
                     color=COLORS.green,
                     text_size=(0.1, 0.1)):
    g = ngl.Group(label="Debug circles")
    circle = ngl.Circle(radius=radius)
    circle_render = ngl.RenderColor(color, geometry=circle)
    box_w = (text_size[0], 0, 0)
    box_h = (0, text_size[1], 0)
    for pos_name, position in points.items():
        text = ngl.Text(pos_name,
                        box_width=box_w,
                        box_height=box_h,
                        bg_opacity=0,
                        valign="top")
        text = ngl.Translate(text, (1 + radius, 1 - radius - text_size[1], 0))
        point = ngl.Group(children=(circle_render, text))
        point = ngl.Translate(point, list(position) + [0])
        g.add_children(point)
    return g
Example #24
0
def fibo(cfg, n=8):
    """Fibonacci with a recursive tree (nodes inherit transforms)"""
    cfg.duration = 5.0
    cfg.aspect_ratio = (1, 1)

    fib = [0, 1, 1]
    for i in range(2, n):
        fib.append(fib[i] + fib[i - 1])
    fib = fib[::-1]

    shift = 1 / 3.0  # XXX: what's the exact math here?
    shape_scale = 1.0 / ((2.0 - shift) * sum(fib))

    orig = (-shift, -shift, 0)
    g = None
    root = None
    for i, x in enumerate(fib[:-1]):
        w = x * shape_scale
        gray = 1.0 - i / float(n)
        color = (gray, gray, gray)
        q = ngl.Quad(orig, (w, 0, 0), (0, w, 0))
        render = ngl.RenderColor(color, geometry=q)

        new_g = ngl.Group()
        animkf = [
            ngl.AnimKeyFrameFloat(0, 90),
            ngl.AnimKeyFrameFloat(cfg.duration / 2, -90, "exp_in_out"),
            ngl.AnimKeyFrameFloat(cfg.duration, 90, "exp_in_out"),
        ]
        rot = ngl.Rotate(new_g, anchor=orig, angle=ngl.AnimatedFloat(animkf))
        if g:
            g.add_children(rot)
        else:
            root = rot
        g = new_g
        new_g.add_children(render)
        orig = (orig[0] + w, orig[1] + w, 0)

    assert root is not None
    return root
Example #25
0
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
Example #26
0
def shape_geometry_with_renderother(cfg):
    cfg.aspect_ratio = (1, 1)

    vertices_data = array.array("f")
    uvcoords_data = array.array("f")
    boundaries = (
        (-1, 0, 0, 1),
        (0, 1, 0, 1),
        (-1, 0, -1, 0),
        (0, 1, -1, 0),
    )
    for xmin, xmax, ymin, ymax in boundaries:
        x0, y0 = cfg.rng.uniform(xmin, xmax), cfg.rng.uniform(ymin, ymax)
        x1, y1 = cfg.rng.uniform(xmin, xmax), cfg.rng.uniform(ymin, ymax)
        vertices_data.extend((0, 0, 0, x0, y0, 0, x1, y1, 0))
        uvcoords_data.extend((0, 0, x0, y0, x1, y1))

    geometry = ngl.Geometry(
        vertices=ngl.BufferVec3(data=vertices_data),
        uvcoords=ngl.BufferVec2(data=uvcoords_data),
        topology="triangle_list",
    )

    return ngl.RenderColor(geometry=geometry)
Example #27
0
def _render_shape(geometry, color):
    return ngl.RenderColor(color, geometry=geometry)
Example #28
0
 def scene_func(_):
     return ngl.RenderColor(color=ngl.UniformColor(c, space=space))
Example #29
0
def _get_scene(geometry=None):
    return ngl.RenderColor(geometry=ngl.Quad() if geometry is None else geometry)
Example #30
0
def _transform_shape(cfg, w=0.75, h=0.45):
    geometry = ngl.Quad(corner=(-w / 2.0, -h / 2.0, 0),
                        width=(w, 0, 0),
                        height=(0, h, 0))
    return ngl.RenderColor(COLORS.rose, geometry=geometry)