def texture_data_animated(cfg, dim=8):
    cfg.duration = 3.0
    nb_kf = int(cfg.duration)
    buffers = [get_random_color_buffer(cfg.rng, dim) for i in range(nb_kf)]
    random_animkf = []
    time_scale = cfg.duration / float(nb_kf)
    for i, buf in enumerate(buffers + [buffers[0]]):
        random_animkf.append(ngl.AnimKeyFrameBuffer(i * time_scale, buf))
    random_buffer = ngl.AnimatedBufferVec4(keyframes=random_animkf)
    random_tex = ngl.Texture2D(data_src=random_buffer, width=dim, height=dim)
    return ngl.RenderTexture(random_tex)
示例#2
0
def texture_data_animated(cfg, dim=8):
    cfg.duration = 3.0
    random.seed(0)
    nb_kf = int(cfg.duration)
    buffers = [get_random_color_buffer(dim) for i in range(nb_kf)]
    random_animkf = []
    time_scale = cfg.duration / float(nb_kf)
    for i, buf in enumerate(buffers + [buffers[0]]):
        random_animkf.append(ngl.AnimKeyFrameBuffer(i * time_scale, buf))
    random_buffer = ngl.AnimatedBufferVec4(keyframes=random_animkf)
    random_tex = ngl.Texture2D(data_src=random_buffer, width=dim, height=dim)
    quad = ngl.Quad((-1, -1, 0), (2, 0, 0), (0, 2, 0))
    prog = ngl.Program(vertex=cfg.get_vert('texture'),
                       fragment=cfg.get_frag('texture'))
    prog.update_vert_out_vars(var_tex0_coord=ngl.IOVec2(),
                              var_uvcoord=ngl.IOVec2())
    render = ngl.Render(quad, prog)
    render.update_frag_resources(tex0=random_tex)
    return render
示例#3
0
    def cropboard(cfg, dim_clr=3, dim_cut=9):
        cfg.duration = 5.0 + 1.0

        nb_kf = 2
        buffers = [
            get_random_color_buffer(cfg.rng, dim_clr) for _ in range(nb_kf)
        ]
        random_animkf = []
        time_scale = cfg.duration / float(nb_kf)
        for i, buf in enumerate(buffers + [buffers[0]]):
            random_animkf.append(ngl.AnimKeyFrameBuffer(i * time_scale, buf))
        random_buffer = ngl.AnimatedBufferVec4(keyframes=random_animkf)
        random_tex = ngl.Texture2D(data_src=random_buffer,
                                   width=dim_clr,
                                   height=dim_clr)

        kw = kh = 1.0 / dim_cut
        qw = qh = 2.0 / dim_cut

        p = ngl.Program(vertex=cfg.get_vert("cropboard"),
                        fragment=cfg.get_frag("texture"))
        p.update_vert_out_vars(var_tex0_coord=ngl.IOVec2())

        uv_offset_buffer = array.array("f")
        translate_a_buffer = array.array("f")
        translate_b_buffer = array.array("f")

        if set_indices:
            indices = array.array("H", [0, 2, 1, 1, 3, 0])
            indices_buffer = ngl.BufferUShort(data=indices)

            vertices = array.array(
                "f",
                [
                    # fmt: off
                    0,
                    0,
                    0,
                    qw,
                    qh,
                    0,
                    qw,
                    0,
                    0,
                    0,
                    qh,
                    0,
                    # fmt: on
                ],
            )

            uvcoords = array.array(
                "f",
                [
                    # fmt: off
                    0,
                    1.0,
                    kw,
                    1.0 - kh,
                    kw,
                    1.0,
                    0,
                    1.0 - kh,
                    # fmt: on
                ],
            )

            vertices_buffer = ngl.BufferVec3(data=vertices)
            uvcoords_buffer = ngl.BufferVec2(data=uvcoords)

            q = ngl.Geometry(vertices=vertices_buffer,
                             uvcoords=uvcoords_buffer,
                             indices=indices_buffer)
        else:
            q = ngl.Quad(
                corner=(0, 0, 0),
                width=(qw, 0, 0),
                height=(0, qh, 0),
                uv_corner=(0, 0),
                uv_width=(kw, 0),
                uv_height=(0, kh),
            )

        for y in range(dim_cut):
            for x in range(dim_cut):
                uv_offset = [x * kw, (y + 1.0) * kh - 1.0]
                src = [cfg.rng.uniform(-2, 2), cfg.rng.uniform(-2, 2)]
                dst = [x * qw - 1.0, 1.0 - (y + 1.0) * qh]

                uv_offset_buffer.extend(uv_offset)
                translate_a_buffer.extend(src)
                translate_b_buffer.extend(dst)

        utime_animkf = [
            ngl.AnimKeyFrameFloat(0, 0),
            ngl.AnimKeyFrameFloat(cfg.duration - 1.0, 1)
        ]
        utime = ngl.AnimatedFloat(utime_animkf)

        render = ngl.Render(q, p, nb_instances=dim_cut**2)
        render.update_frag_resources(tex0=random_tex)
        render.update_vert_resources(time=utime)
        render.update_instance_attributes(
            uv_offset=ngl.BufferVec2(data=uv_offset_buffer),
            translate_a=ngl.BufferVec2(data=translate_a_buffer),
            translate_b=ngl.BufferVec2(data=translate_b_buffer),
        )

        return render
示例#4
0
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),
    array_ivec3=lambda data: ngl.BufferIVec3(data=data),
    array_ivec4=lambda data: ngl.BufferIVec4(data=data),
    array_mat4=lambda data: ngl.BufferMat4(data=data),
    array_vec2=lambda data: ngl.BufferVec2(data=data),
    array_vec3=lambda data: ngl.BufferVec3(data=data),
    array_vec4=lambda data: ngl.BufferVec4(data=data),
    single_bool=lambda data: ngl.UniformBool(data),
    single_float=lambda data: ngl.UniformFloat(data),