示例#1
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))
示例#2
0
def triangle(cfg, size=0.5):
    '''Rotating triangle with edge coloring specified in a vertex attribute'''
    b = size * math.sqrt(3) / 2.0
    c = size * 1 / 2.
    cfg.duration = 3.
    cfg.aspect_ratio = (1, 1)

    colors_data = array.array(
        'f', [0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0])
    colors_buffer = ngl.BufferVec4(data=colors_data)

    triangle = ngl.Triangle((-b, -c, 0), (b, -c, 0), (0, size, 0))
    p = ngl.Program(fragment=cfg.get_frag('color'),
                    vertex=cfg.get_vert('triangle'))
    p.update_vert_out_vars(color=ngl.IOVec4())
    node = ngl.Render(triangle, p)
    node.update_attributes(edge_color=colors_buffer)
    animkf = [
        ngl.AnimKeyFrameFloat(0, 0),
        ngl.AnimKeyFrameFloat(cfg.duration / 3., -360 / 3., 'exp_in_out'),
        ngl.AnimKeyFrameFloat(2 * cfg.duration / 3., -2 * 360 / 3.,
                              'exp_in_out'),
        ngl.AnimKeyFrameFloat(cfg.duration, -360, 'exp_in_out')
    ]
    node = ngl.Rotate(node, anim=ngl.AnimatedFloat(animkf))
    return node
def triangle(cfg, size=4 / 3):
    """Rotating triangle with edge coloring specified in a vertex attribute"""
    cfg.duration = 3.0
    cfg.aspect_ratio = (1, 1)

    colors_data = array.array("f",
                              [0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0])
    colors_buffer = ngl.BufferVec3(data=colors_data)

    p0, p1, p2 = equilateral_triangle_coords(size)
    triangle = ngl.Triangle(p0, p1, p2)
    p = ngl.Program(fragment=cfg.get_frag("color"),
                    vertex=cfg.get_vert("triangle"))
    p.update_vert_out_vars(color=ngl.IOVec3())
    node = ngl.Render(triangle, p)
    node.update_attributes(edge_color=colors_buffer)
    node.update_frag_resources(opacity=ngl.UniformFloat(1))
    animkf = [
        ngl.AnimKeyFrameFloat(0, 0),
        ngl.AnimKeyFrameFloat(cfg.duration / 3.0, -360 / 3.0, "exp_in_out"),
        ngl.AnimKeyFrameFloat(2 * cfg.duration / 3.0, -2 * 360 / 3.0,
                              "exp_in_out"),
        ngl.AnimKeyFrameFloat(cfg.duration, -360, "exp_in_out"),
    ]
    node = ngl.Rotate(node, angle=ngl.AnimatedFloat(animkf))
    return node
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
示例#5
0
def shape_triangles_mat4_attribute(cfg):
    cfg.aspect_ratio = (1, 1)
    p0, p1, p2 = equilateral_triangle_coords(1)
    geometry = ngl.Triangle(p0, p1, p2)
    matrices = ngl.BufferMat4(data=array.array(
        "f",
        [
            # fmt: off
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            -0.5,
            0.0,
            0.0,
            1.0,
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            0.0,
            0.0,
            0.0,
            1.0,
            0.0,
            0.5,
            0.0,
            0.0,
            1.0,
            # fmt: on
        ],
    ))

    program = ngl.Program(
        vertex=TRIANGLES_MAT4_ATTRIBUTE_VERT,
        fragment=cfg.get_frag("color"),
    )
    render = ngl.Render(geometry, program, nb_instances=2)
    render.update_instance_attributes(matrix=matrices)
    render.update_frag_resources(color=ngl.UniformVec3(value=COLORS.orange),
                                 opacity=ngl.UniformFloat(1))
    return render
示例#6
0
def shape_triangles_mat4_attribute(cfg):
    cfg.aspect_ratio = (1, 1)
    p0, p1, p2 = equilateral_triangle_coords(1)
    geometry = ngl.Triangle(p0, p1, p2)
    matrices = ngl.BufferMat4(data=array.array('f', [
        1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.0,
        -0.5,
        0.0,
        0.0,
        1.0,
        1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.0,
        0.0,
        0.0,
        0.0,
        1.0,
        0.0,
        0.5,
        0.0,
        0.0,
        1.0,
    ]))

    program = ngl.Program(
        vertex=TRIANGLES_MAT4_ATTRIBUTE_VERT,
        fragment=cfg.get_frag('color'),
    )
    render = ngl.Render(geometry, program, nb_instances=2)
    render.update_instance_attributes(matrix=matrices)
    render.update_uniforms(color=ngl.UniformVec4(value=COLORS['orange']))
    return render
def _get_random_geometry(rng):
    circle = lambda rng: ngl.Circle(
        radius=rng.uniform(1 / 4, 3 / 4),
        npoints=rng.randint(5, 25),
    )
    quad = lambda rng: ngl.Quad(
        corner=_get_random_position(rng),
        width=(rng.uniform(-0.5, 0.5), rng.uniform(-0.5, 0.5), 0),
        height=(rng.uniform(-0.5, 0.5), rng.uniform(-0.5, 0.5), 0),
    )
    triangle = lambda rng: ngl.Triangle(
        edge0=_get_random_position(rng),
        edge1=_get_random_position(rng),
        edge2=_get_random_position(rng),
    )
    shape_func = rng.choice((circle, quad, triangle))
    return shape_func(rng)
示例#8
0
def shape_triangle_msaa(cfg, sz=1, color=COLORS.orange):
    cfg.aspect_ratio = (1, 1)
    p0, p1, p2 = equilateral_triangle_coords(sz)
    geometry = ngl.Triangle(p0, p1, p2)
    return _render_shape(geometry, color)
示例#9
0
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