Exemplo n.º 1
0
 def _on_button_click(self):
     positions = [[random.uniform(-50, 50),
                   random.uniform(-50, 50), 0] for i in range(8)]
     line = gfx.Line(gfx.Geometry(positions=positions),
                     gfx.LineMaterial(thickness=3))
     self._scene.add(line)
     self._canvas.update()
Exemplo n.º 2
0
scene2 = gfx.Scene()

positions = np.array(
    [
        [-1, -1, 0.5],
        [-1, +1, 0.5],
        [+1, +1, 0.5],
        [+1, -1, 0.5],
        [-1, -1, 0.5],
        [+1, +1, 0.5],
    ],
    np.float32,
)
geometry2 = gfx.Geometry(positions=positions * 0.9)
material2 = gfx.LineMaterial(thickness=5.0, color=(0.8, 0.0, 0.2, 1.0))
line2 = gfx.Line(geometry2, material2)
scene2.add(line2)

camera2 = gfx.NDCCamera()


def animate():
    rot = gfx.linalg.Quaternion().set_from_euler(gfx.linalg.Euler(0.005, 0.01))
    cube1.rotation.multiply(rot)

    renderer.render(scene1, camera1, flush=False)
    renderer.render(scene2, camera2)

    canvas.request_draw()
Exemplo n.º 3
0
import numpy as np
from wgpu.gui.auto import WgpuCanvas, run
import pygfx as gfx


canvas = WgpuCanvas()
renderer = gfx.renderers.WgpuRenderer(canvas)
scene = gfx.Scene()

xx = np.linspace(-50, 50, 10)
yy = np.random.uniform(20, 50, 10)
geometry = gfx.Geometry(positions=[(x, y, 0) for x, y in zip(xx, yy)])
if True:  # Set to False to try this for a line
    ob = gfx.Points(geometry, gfx.PointsMaterial(color=(0, 1, 1, 1), size=20))
else:
    ob = gfx.Line(geometry, gfx.LineMaterial(color=(0, 1, 1, 1), thickness=12))
scene.add(ob)

camera = gfx.OrthographicCamera(120, 120)


@canvas.add_event_handler("pointer_down")
def handle_event(event):
    info = renderer.get_pick_info((event["x"], event["y"]))
    wobject = info["world_object"]
    # If a point was clicked ..
    if wobject and "vertex_index" in info:
        i = int(round(info["vertex_index"]))
        geometry.positions.data[i, 1] *= -1
        geometry.positions.update_range(i)
        canvas.request_draw()
Exemplo n.º 4
0
renderer = gfx.WgpuRenderer(canvas)

# A straight line
line1 = [[100, 100], [100, 200], [100, 200], [100, 400]]

# A line with a 180 degree turn (a bit of a special case for the implementation)
line2 = [[200, 100], [200, 400], [200, 100]]

# A swiggly line
line3 = [[300 + random.randint(-10, 10), 100 + i * 3] for i in range(100)]

# A line with other turns
line4 = [[400, 100], [500, 200], [400, 300], [450, 400]]

scene = gfx.Scene()

material = gfx.LineMaterial(thickness=80.0, color=(0.8, 0.7, 0.0, 1.0))

for line in [line1, line2, line3, line4]:
    line = [pos + [0] for pos in line]  # Make the positions vec3
    geometry = gfx.Geometry(positions=line)
    line = gfx.Line(geometry, material)
    scene.add(line)

camera = gfx.ScreenCoordsCamera()


if __name__ == "__main__":
    canvas.request_draw(lambda: renderer.render(scene, camera))
    run()
Exemplo n.º 5
0
scene = gfx.Scene()
positions = [[200 + np.sin(i) * i * 6, 200 + np.cos(i) * i * 6, 0] for i in range(20)]
positions += [[400 - np.sin(i) * i * 6, 200 + np.cos(i) * i * 6, 0] for i in range(20)]
positions += [
    [450, 400, 0],
    [375, 400, 0],
    [300, 400, 0],
    [400, 370, 0],
    [300, 340, 0],
]

# Spiral away in z (to make the depth buffer less boring)
for i in range(len(positions)):
    positions[i][2] = i

line = gfx.Line(
    gfx.Geometry(positions=positions),
    gfx.LineMaterial(thickness=12.0, color=(0.8, 0.7, 0.0)),
)
scene.add(line)

camera = gfx.OrthographicCamera(600, 500)
camera.position.set(300, 250, 0)


if __name__ == "__main__":
    renderer_svg.render(scene, camera)
    canvas.request_draw(lambda: renderer.render(scene, camera))
    run()
Exemplo n.º 6
0
def test_render_registry_of_svg():
    r = gfx.renderers.svg._svgrenderer.registry

    assert None is r.get_render_function(Object1(Material1()))

    assert r.get_render_function(gfx.Line(None, gfx.LineMaterial()))
Exemplo n.º 7
0
use_thin_lines = True

print(nvertices * rows * cols, "vertices in total")

x = np.linspace(0.05, 0.95, nvertices, dtype=np.float32)

for row in range(rows):
    for col in range(cols):
        y = np.sin(x * 25) * 0.45 + np.random.normal(0, 0.02, len(x)).astype(np.float32)
        positions = np.column_stack([x, y, np.zeros_like(x)])
        geometry = gfx.Geometry(positions=positions)
        if use_thin_lines:
            material = gfx.LineThinMaterial(color=(col / cols, row / rows, 0.5, 1.0))
        else:
            material = gfx.LineMaterial(
                thickness=0.2 + 2 * row / rows, color=(col / cols, row / rows, 0.5, 1.0)
            )
        line = gfx.Line(geometry, material)
        line.position.x = col
        line.position.y = row
        scene.add(line)

camera = gfx.OrthographicCamera(cols, rows)
camera.maintain_aspect = False
controls = gfx.PanZoomControls(camera.position.clone())
controls.pan(gfx.linalg.Vector3(cols / 2, rows / 2, 0))
controls.add_default_event_handlers(canvas, camera)


def animate():
    controls.update_camera(camera)
Exemplo n.º 8
0
"""
Display a line depicting a noisy signal consisting of a lot of points.
"""

import numpy as np
from wgpu.gui.auto import WgpuCanvas, run
import pygfx as gfx

canvas = WgpuCanvas()
renderer = gfx.WgpuRenderer(canvas)

scene = gfx.Scene()

# todo: crank this to 1M when wgpu allows it :D
x = np.linspace(0, 100, 10_000, dtype=np.float32)
y = np.sin(x) * 30 + np.random.normal(0, 5, len(x)).astype(np.float32)

positions = np.column_stack([x, y, np.zeros_like(x)])
line = gfx.Line(
    gfx.Geometry(positions=positions),
    gfx.LineMaterial(thickness=2.0, color=(0.0, 0.7, 0.3, 1.0)),
)
scene.add(line)

camera = gfx.OrthographicCamera(110, 110)
camera.position.set(50, 0, 0)

if __name__ == "__main__":
    canvas.request_draw(lambda: renderer.render(scene, camera))
    run()