コード例 #1
0
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))
コード例 #2
0
def data_eval(cfg):
    cfg.aspect_ratio = (1, 1)

    # Entangled dependencies between evals
    t = ngl.Time()
    a = ngl.UniformFloat(0.7)
    b = ngl.UniformFloat(0.3)
    x = ngl.EvalFloat("sin(a - b + t*4)")
    x.update_resources(t=t, a=a, b=b)
    color = ngl.EvalVec4(
        expr0="sat(sin(x + t*4)/2 + wiggle/3)",
        expr1="abs(fract(sin(t + a)))",
        expr2=None,  # re-use expr1
        expr3="1",
    )
    color.update_resources(wiggle=ngl.NoiseFloat(), t=t, a=a, x=x)

    vert = textwrap.dedent("""\
        void main()
        {
            ngl_out_pos = ngl_projection_matrix * ngl_modelview_matrix * vec4(ngl_position, 1.0);
        }
        """)
    frag = textwrap.dedent("""\
        void main()
        {
            ngl_out_color = color;
        }
        """)
    program = ngl.Program(vertex=vert, fragment=frag)
    geometry = ngl.Quad(corner=(-1, -1, 0), width=(2, 0, 0), height=(0, 2, 0))
    render = ngl.Render(geometry, program)
    render.update_frag_resources(color=color)

    return render
コード例 #3
0
def py_bindings_allow_node():
    c = ngl.Camera(eye=(0, 1, 0))
    assert c.set_eye(ngl.EvalVec3()) == 0
    assert c.set_eye(1, 0, 0) == 0

    c = ngl.Camera(eye=ngl.NoiseVec3())
    assert c.set_eye(1, 0, 0) == 0
    assert c.set_eye(ngl.UniformVec3()) == 0

    r = ngl.Rotate(angle=30)
    assert r.set_angle(ngl.NoiseFloat()) == 0
    assert r.set_angle(-45) == 0

    r = ngl.Rotate(angle=ngl.EvalFloat())
    assert r.set_angle(90) == 0
    assert r.set_angle(ngl.UniformFloat()) == 0
コード例 #4
0
def py_bindings_dict():
    foo = ngl.UniformVec3(value=(1, 2, 3), label="foo-node")
    assert foo.set_value(3, 2, 1) == 0
    render = ngl.Render(vert_resources=dict(foo=foo))

    assert render.set_label("r") == 0
    # Delete the previous entry and add another
    ret = render.update_vert_resources(
        foo=None,
        bar=ngl.UniformFloat(value=4, label="bar-node"),
    )
    assert ret == 0
    # Update by using a dict
    ret = render.update_vert_resources(
        dict(
            foo=ngl.UniformVec2(),
            bar=ngl.EvalFloat(),
        ))
    assert ret == 0