PyGfx points and lines exampleΒΆ

Note that this example depends on pygfx (``pip install -U pygfx``).

An example demonstrating points and lines, in two separate canvases.

[1]:
import numpy as np
import pygfx as gfx
from wgpu.gui.jupyter import WgpuCanvas
[2]:
canvas1 = WgpuCanvas()
renderer1 = gfx.WgpuRenderer(canvas1)

scene1 = gfx.Scene()

x = np.linspace(20, 620, 200, dtype=np.float32)
y = np.sin(x / 10) * 100 + 200

positions = np.column_stack([x, y, np.zeros_like(x)])
colors = np.random.uniform(0, 1, (x.size, 4)).astype(np.float32)
geometry = gfx.Geometry(positions=positions, colors=colors)


# Also see LineSegmentMaterial and LineThinSegmentMaterial
material = gfx.LineSegmentMaterial(
    thickness=6.0, color=(0.0, 0.7, 0.3, 0.5), vertex_colors=True
)
line = gfx.Line(geometry, material)
scene1.add(line)

camera1 = gfx.ScreenCoordsCamera()

canvas1.request_draw(lambda: renderer1.render(scene1, camera1))
canvas1
Forcing backend: Vulkan (4)
initial snapshot
[2]:
[3]:
canvas2 = WgpuCanvas()
renderer2 = gfx.WgpuRenderer(canvas2)

scene2 = gfx.Scene()

positions = np.random.normal(0, 0.5, (100, 3)).astype(np.float32)
geometry = gfx.Geometry(positions=positions)

material = gfx.PointsMaterial(size=10, color=(0, 1, 0.5, 0.7))
points = gfx.Points(geometry, material)
scene2.add(points)
scene2.add(gfx.Background(gfx.BackgroundMaterial((0.2, 0.0, 0, 1), (0, 0.0, 0.2, 1))))

camera2 = gfx.NDCCamera()

canvas2.request_draw(lambda: renderer2.render(scene2, camera2))
canvas2
Forcing backend: Vulkan (4)
initial snapshot
[3]:
[ ]: