Пример #1
0
def transform_scale(cfg, factors=(0.7, 1.4, 0)):
    cfg.aspect_ratio = (1, 1)
    shape = _transform_shape(cfg)
    return ngl.Scale(shape, factors)
Пример #2
0
def transform_scale_anchor(cfg, factors=(0.7, 1.4, 0),
                           anchor=(-0.4, 0.5, 0.7)):
    cfg.aspect_ratio = (1, 1)
    shape = _transform_shape(cfg)
    return ngl.Scale(shape, factors, anchor=anchor)
Пример #3
0
def blending_and_stencil(cfg):
    '''Scene using blending and stencil graphic features'''
    cfg.duration = 5
    random.seed(0)
    fragment = cfg.get_frag('color')

    program = ngl.Program(fragment=fragment)
    circle = ngl.Circle(npoints=256)
    cloud_color = ngl.UniformVec4(value=(1, 1, 1, 0.4))

    main_group = ngl.Group()

    quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0))
    render = ngl.Render(quad, program, label='sky')
    render.update_uniforms(color=ngl.UniformVec4(value=(0.2, 0.6, 1, 1)))
    config = ngl.GraphicConfig(render,
                               stencil_test=True,
                               stencil_write_mask=0xFF,
                               stencil_func='always',
                               stencil_ref=1,
                               stencil_read_mask=0xFF,
                               stencil_fail='replace',
                               stencil_depth_fail='replace',
                               stencil_depth_pass='******')
    main_group.add_children(config)

    render = ngl.Render(circle, program, label='sun')
    render.update_uniforms(color=ngl.UniformVec4(value=(1, 0.8, 0, 1)))

    scale = ngl.Scale(render, (0.15, 0.15, 0.0))
    translate = ngl.Translate(scale, (0.4, 0.3, 0))
    main_group.add_children(translate)

    cloud_group = ngl.Group(label='clouds')

    centers = [
        (-1.0, 0.85, 0.4),
        (-0.5, 2.0,  1.0),
        (   0, 0.85, 0.4),
        ( 1.0, 1.55, 0.8),
        ( 0.6, 0.65, 0.075),
        ( 0.5, 1.80, 1.25),
    ]

    for center in centers:
        render = ngl.Render(circle, program)
        render.update_uniforms(color=cloud_color)

        factor = random.random() * 0.4 + center[2]
        keyframe = cfg.duration * (random.random() * 0.4 + 0.2)
        animkf = (ngl.AnimKeyFrameVec3(0,            (factor,       factor,       0)),
                  ngl.AnimKeyFrameVec3(keyframe,     (factor + 0.1, factor + 0.1, 0)),
                  ngl.AnimKeyFrameVec3(cfg.duration, (factor,       factor,       0)))
        scale = ngl.Scale(render, anim=ngl.AnimatedVec3(animkf))

        translate = ngl.Translate(scale, vector=(center[0], center[1], 0))
        cloud_group.add_children(translate)

    config = ngl.GraphicConfig(cloud_group,
                               blend=True,
                               blend_src_factor='src_alpha',
                               blend_dst_factor='one_minus_src_alpha',
                               blend_src_factor_a='zero',
                               blend_dst_factor_a='one',
                               stencil_test=True,
                               stencil_write_mask=0x0,
                               stencil_func='equal',
                               stencil_ref=1,
                               stencil_read_mask=0xFF,
                               stencil_fail='keep',
                               stencil_depth_fail='keep',
                               stencil_depth_pass='******')
    main_group.add_children(config)

    camera = ngl.Camera(main_group)
    camera.set_eye(0.0, 0.0, 2.0)
    camera.set_center(0.0, 0.0, 0.0)
    camera.set_up(0.0, 1.0, 0.0)
    camera.set_orthographic(-cfg.aspect_ratio_float, cfg.aspect_ratio_float, -1.0, 1.0)
    camera.set_clipping(1.0, 10.0)

    return camera
Пример #4
0
def _get_live_trf_spec():

    t0 = ngl.Identity()
    t1 = ngl.Transform(t0)
    t2 = ngl.Translate(t1)
    t3 = ngl.Rotate(t2)
    t4 = ngl.Scale(t3)
    t5 = ngl.RotateQuat(t4)
    t6 = ngl.Skew(t5)

    return [
        dict(
            name="m4",
            type="mat4",
            category="single",
            func=lambda data: ngl.UniformMat4(data, transform=t6),
            livechange=(
                # fmt: off
                lambda: t1.set_matrix(  # trf_step=1
                    0.1,
                    0.2,
                    0.0,
                    0.0,
                    0.0,
                    0.3,
                    0.4,
                    0.0,
                    0.0,
                    0.0,
                    0.5,
                    0.0,
                    0.8,
                    0.7,
                    0.0,
                    0.6,
                ),
                lambda: t2.set_vector(0.2, 0.7, -0.4),  # trf_step=2
                lambda: t3.set_angle(123.4),  # trf_step=3
                lambda: t4.set_factors(0.7, 1.4, 0.2),  # trf_step=4
                lambda: t5.set_quat(0, 0, -0.474, 0.880),  # trf_step=5
                lambda: t6.set_angles(0, 50, -145),  # trf_step=6
                lambda: t6.set_angles(0, 0, 0),
                lambda: t5.set_quat(0, 0, 0, 1),
                lambda: t4.set_factors(1, 1, 1),
                lambda: t3.set_angle(0),
                lambda: t2.set_vector(0, 0, 0),
                lambda: t1.set_matrix(
                    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.0,
                    0.0,
                    0.0,
                    1.0,
                )
                # fmt: on
            ),
        ),
    ]
Пример #5
0
def _get_random_compute(cfg, rng, t0, t1):
    count = 10

    vertex_shader = textwrap.dedent("""
        void main()
        {
            vec4 position = vec4(ngl_position, 1.0) + vec4(pos.data[ngl_instance_index], 0.0, 0.0);
            ngl_out_pos = ngl_projection_matrix * ngl_modelview_matrix * position;
            var_tex0_coord = (tex0_coord_matrix * vec4(ngl_uvcoord, 0.0, 1.0)).xy;
        }
        """)

    funcs = ("snake", "circle", "spread")

    compute_shader = textwrap.dedent("""
        #define TAU (2.0 * 3.14159265358979323846)

        vec2 snake(float i)
        {
            return vec2(i * 2.0 - 1.0, sin((time + i) * TAU));
        }

        vec2 circle(float i)
        {
            float angle = (time + i) * TAU;
            return vec2(sin(angle), cos(angle));
        }

        vec2 spread(float i)
        {
            float angle = i * TAU;
            return vec2(sin(angle), cos(angle)) * (time + i);
        }

        void main()
        {
            uint wg = gl_WorkGroupID.x;
            float i = float(wg) / float(gl_NumWorkGroups.x - 1U);
            pos.data[wg] = FUNC(i);
        }
        """.replace("FUNC", rng.choice(funcs)))

    pos = ngl.Block(fields=[ngl.BufferVec2(count=count, label="data")])

    time_animkf = [ngl.AnimKeyFrameFloat(t0, 0), ngl.AnimKeyFrameFloat(t1, 1)]

    compute_prog = ngl.ComputeProgram(compute_shader, workgroup_size=(1, 1, 1))
    compute_prog.update_properties(pos=ngl.ResourceProps(writable=True))

    compute = ngl.Compute(workgroup_count=(count, 1, 1), program=compute_prog)
    compute.update_resources(time=ngl.AnimatedFloat(time_animkf), pos=pos)

    geometry = _get_random_geometry(rng)
    program = ngl.Program(vertex=vertex_shader,
                          fragment=cfg.get_frag("texture"))
    program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2())
    render = ngl.Render(geometry,
                        program,
                        nb_instances=count,
                        blending="src_over")
    render.update_frag_resources(tex0=_get_random_texture(cfg, rng))
    render.update_vert_resources(pos=pos)
    render = ngl.Scale(render, factors=(0.5, 0.5, 0))

    return ngl.Group(children=(compute, render))
Пример #6
0
def _get_rtt_scene(cfg,
                   features='depth',
                   texture_ds_format=None,
                   samples=0,
                   mipmap_filter='none',
                   sample_depth=False):
    cfg.duration = 10
    cfg.aspect_ratio = (1, 1)
    cube = _get_cube()
    program = ngl.Program(vertex=_RENDER_CUBE_VERT, fragment=_RENDER_CUBE_FRAG)
    program.update_vert_out_vars(var_normal=ngl.IOVec3())
    render = ngl.Render(cube, program)
    render = ngl.Scale(render, (0.5, 0.5, 0.5))

    for i in range(3):
        rot_animkf = ngl.AnimatedFloat([
            ngl.AnimKeyFrameFloat(0, 0),
            ngl.AnimKeyFrameFloat(cfg.duration, 360 * (i + 1))
        ])
        axis = [int(i == x) for x in range(3)]
        render = ngl.Rotate(render, axis=axis, anim=rot_animkf)

    config = ngl.GraphicConfig(render, depth_test=True)

    camera = ngl.Camera(
        config,
        eye=(0.0, 0.0, 3.0),
        center=(0.0, 0.0, 0.0),
        up=(0.0, 1.0, 0.0),
        perspective=(45.0, cfg.aspect_ratio_float),
        clipping=(1.0, 10.0),
    )

    size = 1024
    texture_depth = None
    if texture_ds_format:
        texture_depth = ngl.Texture2D(width=size,
                                      height=size,
                                      format=texture_ds_format)

    texture = ngl.Texture2D(
        width=size,
        height=size,
        min_filter='linear',
        mipmap_filter=mipmap_filter,
    )
    rtt = ngl.RenderToTexture(
        camera,
        [texture],
        features=features,
        depth_texture=texture_depth,
        samples=samples,
        clear_color=(0, 0, 0, 1),
    )

    quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0))
    if sample_depth:
        program = ngl.Program(vertex=cfg.get_vert('texture'),
                              fragment=_RENDER_DEPTH)
        program.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(),
                                     var_uvcoord=ngl.IOVec2())
        render = ngl.Render(quad, program)
        render.update_frag_resources(tex0=texture_depth)
    else:
        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)
        render.update_frag_resources(tex0=texture)
    return ngl.Group(children=(rtt, render))
Пример #7
0
def character_ttf(cfg):
    """The 'g' glyph from Noto Sans Black"""
    cfg.duration = 5
    keyframes = [
        # fmt: off
        ngl.PathKeyMove(to=(0.375912, 1, 0)),
        ngl.PathKeyBezier2(control=(0.483577, 1, 0),
                           to=(0.550182, 0.972036, 0)),
        ngl.PathKeyBezier2(control=(0.616788, 0.944073, 0),
                           to=(0.657847, 0.902736, 0)),
        ngl.PathKeyLine(to=(0.665146, 0.902736, 0)),
        ngl.PathKeyLine(to=(0.691606, 0.987842, 0)),
        ngl.PathKeyLine(to=(1, 0.987842, 0)),
        ngl.PathKeyLine(to=(1, 0.297872, 0)),
        ngl.PathKeyBezier2(control=(1, 0.151976, 0),
                           to=(0.864051, 0.0759878, 0)),
        ngl.PathKeyBezier2(control=(0.729015, 0, 0), to=(0.448905, 0, 0)),
        ngl.PathKeyBezier2(control=(0.322993, 0, 0),
                           to=(0.22719, 0.00911854, 0)),
        ngl.PathKeyBezier2(control=(0.132299, 0.0176292, 0),
                           to=(0.044708, 0.0401216, 0)),
        ngl.PathKeyLine(to=(0.044708, 0.238298, 0)),
        ngl.PathKeyBezier2(control=(0.138686, 0.212158, 0),
                           to=(0.222628, 0.199392, 0)),
        ngl.PathKeyBezier2(control=(0.306569, 0.186018, 0),
                           to=(0.42792, 0.186018, 0)),
        ngl.PathKeyBezier2(control=(0.643248, 0.186018, 0),
                           to=(0.643248, 0.288146, 0)),
        ngl.PathKeyLine(to=(0.643248, 0.300304, 0)),
        ngl.PathKeyBezier2(control=(0.643248, 0.33617, 0),
                           to=(0.654197, 0.386018, 0)),
        ngl.PathKeyLine(to=(0.643248, 0.386018, 0)),
        ngl.PathKeyBezier2(control=(0.605839, 0.345289, 0),
                           to=(0.541058, 0.316109, 0)),
        ngl.PathKeyBezier2(control=(0.476277, 0.28693, 0),
                           to=(0.370438, 0.28693, 0)),
        ngl.PathKeyBezier2(control=(0.205292, 0.28693, 0),
                           to=(0.10219, 0.377508, 0)),
        ngl.PathKeyBezier2(control=(0, 0.468693, 0), to=(0, 0.642553, 0)),
        ngl.PathKeyBezier2(control=(0, 0.817021, 0),
                           to=(0.104015, 0.908207, 0)),
        ngl.PathKeyBezier2(control=(0.208942, 1, 0), to=(0.375912, 1, 0)),
        ngl.PathKeyMove(to=(0.510036, 0.815805, 0)),
        ngl.PathKeyBezier2(control=(0.362226, 0.815805, 0),
                           to=(0.362226, 0.638906, 0)),
        ngl.PathKeyBezier2(control=(0.362226, 0.549544, 0),
                           to=(0.399635, 0.51003, 0)),
        ngl.PathKeyBezier2(control=(0.437044, 0.471125, 0),
                           to=(0.515511, 0.471125, 0)),
        ngl.PathKeyBezier2(control=(0.601277, 0.471125, 0),
                           to=(0.635949, 0.507599, 0)),
        ngl.PathKeyBezier2(control=(0.67062, 0.544073, 0),
                           to=(0.67062, 0.617629, 0)),
        ngl.PathKeyLine(to=(0.67062, 0.646201, 0)),
        ngl.PathKeyBezier2(control=(0.67062, 0.72766, 0),
                           to=(0.637774, 0.771429, 0)),
        ngl.PathKeyBezier2(control=(0.605839, 0.815805, 0),
                           to=(0.510036, 0.815805, 0)),
        # fmt: on
    ]

    path = ngl.Path(keyframes)
    return ngl.Scale(_path_scene(cfg, path),
                     factors=(2, 2, 0),
                     anchor=(1, 1, 0))
Пример #8
0
def character_otf(cfg):
    """The 'g' glyph from Adobe Source Code Pro Black"""
    cfg.duration = 5
    keyframes = [
        # fmt: off
        ngl.PathKeyMove(to=(0.423713, 0, 0)),
        ngl.PathKeyBezier3(control1=(0.775735, 0, 0),
                           control2=(1, 0.103331, 0),
                           to=(1, 0.253569, 0)),
        ngl.PathKeyBezier3(control1=(1, 0.381373, 0),
                           control2=(0.856618, 0.434398, 0),
                           to=(0.608456, 0.434398, 0)),
        ngl.PathKeyLine(to=(0.448529, 0.434398, 0)),
        ngl.PathKeyBezier3(control1=(0.342831, 0.434398, 0),
                           control2=(0.301471, 0.445275, 0),
                           to=(0.301471, 0.476547, 0)),
        ngl.PathKeyBezier3(control1=(0.301471, 0.495581, 0),
                           control2=(0.308824, 0.503739, 0),
                           to=(0.331801, 0.516655, 0)),
        ngl.PathKeyBezier3(control1=(0.376838, 0.509857, 0),
                           control2=(0.41636, 0.507138, 0),
                           to=(0.450368, 0.507138, 0)),
        ngl.PathKeyBezier3(control1=(0.673713, 0.507138, 0),
                           control2=(0.847426, 0.573759, 0),
                           to=(0.847426, 0.738273, 0)),
        ngl.PathKeyBezier3(control1=(0.847426, 0.766145, 0),
                           control2=(0.83364, 0.793338, 0),
                           to=(0.820772, 0.810333, 0)),
        ngl.PathKeyLine(to=(0.98989, 0.810333, 0)),
        ngl.PathKeyLine(to=(0.98989, 0.983005, 0)),
        ngl.PathKeyLine(to=(0.610294, 0.983005, 0)),
        ngl.PathKeyBezier3(control1=(0.5625, 0.993882, 0),
                           control2=(0.506434, 1, 0),
                           to=(0.450368, 1, 0)),
        ngl.PathKeyBezier3(control1=(0.231618, 1, 0),
                           control2=(0.0340074, 0.919103, 0),
                           to=(0.0340074, 0.746431, 0)),
        ngl.PathKeyBezier3(control1=(0.0340074, 0.665534, 0),
                           control2=(0.0900735, 0.601632, 0),
                           to=(0.161765, 0.568321, 0)),
        ngl.PathKeyLine(to=(0.161765, 0.562882, 0)),
        ngl.PathKeyBezier3(control1=(0.0965074, 0.528892, 0),
                           control2=(0.0487132, 0.479266, 0),
                           to=(0.0487132, 0.431679, 0)),
        ngl.PathKeyBezier3(control1=(0.0487132, 0.373215, 0),
                           control2=(0.0900735, 0.337186, 0),
                           to=(0.143382, 0.312033, 0)),
        ngl.PathKeyLine(to=(0.143382, 0.306594, 0)),
        ngl.PathKeyBezier3(control1=(0.0487132, 0.273283, 0),
                           control2=(0, 0.229776, 0),
                           to=(0, 0.172672, 0)),
        ngl.PathKeyBezier3(control1=(0, 0.0475867, 0),
                           control2=(0.190257, 0, 0),
                           to=(0.423713, 0, 0)),
        ngl.PathKeyMove(to=(0.450368, 0.648538, 0)),
        ngl.PathKeyBezier3(control1=(0.387868, 0.648538, 0),
                           control2=(0.337316, 0.67981, 0),
                           to=(0.337316, 0.746431, 0)),
        ngl.PathKeyBezier3(control1=(0.337316, 0.810333, 0),
                           control2=(0.387868, 0.840925, 0),
                           to=(0.450368, 0.840925, 0)),
        ngl.PathKeyBezier3(control1=(0.511949, 0.840925, 0),
                           control2=(0.565257, 0.810333, 0),
                           to=(0.565257, 0.746431, 0)),
        ngl.PathKeyBezier3(control1=(0.565257, 0.67981, 0),
                           control2=(0.511949, 0.648538, 0),
                           to=(0.450368, 0.648538, 0)),
        ngl.PathKeyMove(to=(0.474265, 0.147519, 0)),
        ngl.PathKeyBezier3(control1=(0.348346, 0.147519, 0),
                           control2=(0.261949, 0.167233, 0),
                           to=(0.261949, 0.211421, 0)),
        ngl.PathKeyBezier3(control1=(0.261949, 0.231135, 0),
                           control2=(0.276654, 0.245411, 0),
                           to=(0.308824, 0.261727, 0)),
        ngl.PathKeyBezier3(control1=(0.340993, 0.256288, 0),
                           control2=(0.378676, 0.253569, 0),
                           to=(0.452206, 0.253569, 0)),
        ngl.PathKeyLine(to=(0.545956, 0.253569, 0)),
        ngl.PathKeyBezier3(control1=(0.639706, 0.253569, 0),
                           control2=(0.693015, 0.248131, 0),
                           to=(0.693015, 0.211421, 0)),
        ngl.PathKeyBezier3(control1=(0.693015, 0.172672, 0),
                           control2=(0.600184, 0.147519, 0),
                           to=(0.474265, 0.147519, 0)),
        # fmt: on
    ]

    path = ngl.Path(keyframes)
    return ngl.Scale(_path_scene(cfg, path),
                     factors=(2, 2, 0),
                     anchor=(1, 1, 0))
def blending_and_stencil(cfg):
    """Scene using blending and stencil graphic features"""
    cfg.duration = 5
    vertex = cfg.get_vert("color")
    fragment = cfg.get_frag("color")

    program = ngl.Program(vertex=vertex, fragment=fragment)
    circle = ngl.Circle(npoints=256)
    cloud_color = ngl.UniformVec3(value=(1, 1, 1))
    cloud_opacity = ngl.UniformFloat(0.4)

    main_group = ngl.Group()

    render = ngl.RenderColor(color=(0.2, 0.6, 1), label="sky")
    config = ngl.GraphicConfig(
        render,
        stencil_test=True,
        stencil_write_mask=0xFF,
        stencil_func="always",
        stencil_ref=1,
        stencil_read_mask=0xFF,
        stencil_fail="replace",
        stencil_depth_fail="replace",
        stencil_depth_pass="******",
    )
    main_group.add_children(config)

    render = ngl.RenderColor(color=(1, 0.8, 0), geometry=circle, label="sun")

    scale = ngl.Scale(render, (0.15, 0.15, 0.0))
    translate = ngl.Translate(scale, (0.4, 0.3, 0))
    main_group.add_children(translate)

    cloud_group = ngl.Group(label="clouds")

    centers = [
        (-1.0, 0.85, 0.4),
        (-0.5, 2.0, 1.0),
        (0, 0.85, 0.4),
        (1.0, 1.55, 0.8),
        (0.6, 0.65, 0.075),
        (0.5, 1.80, 1.25),
    ]

    for center in centers:
        render = ngl.Render(circle, program, blending="src_over")
        render.update_frag_resources(color=cloud_color, opacity=cloud_opacity)

        factor = cfg.rng.random() * 0.4 + center[2]
        keyframe = cfg.duration * (cfg.rng.random() * 0.4 + 0.2)
        animkf = (
            ngl.AnimKeyFrameVec3(0, (factor, factor, 0)),
            ngl.AnimKeyFrameVec3(keyframe, (factor + 0.1, factor + 0.1, 0)),
            ngl.AnimKeyFrameVec3(cfg.duration, (factor, factor, 0)),
        )
        scale = ngl.Scale(render, factors=ngl.AnimatedVec3(animkf))

        translate = ngl.Translate(scale, vector=(center[0], center[1], 0))
        cloud_group.add_children(translate)

    config = ngl.GraphicConfig(
        cloud_group,
        stencil_test=True,
        stencil_write_mask=0x0,
        stencil_func="equal",
        stencil_ref=1,
        stencil_read_mask=0xFF,
        stencil_fail="keep",
        stencil_depth_fail="keep",
        stencil_depth_pass="******",
    )
    main_group.add_children(config)

    camera = ngl.Camera(main_group)
    camera.set_eye(0.0, 0.0, 2.0)
    camera.set_center(0.0, 0.0, 0.0)
    camera.set_up(0.0, 1.0, 0.0)
    camera.set_orthographic(-cfg.aspect_ratio_float, cfg.aspect_ratio_float,
                            -1.0, 1.0)
    camera.set_clipping(1.0, 10.0)

    return camera