示例#1
0
canvas = WgpuCanvas()
renderer = gfx.renderers.WgpuRenderer(canvas)
scene = gfx.Scene()

vol = imageio.volread("imageio:stent.npz")
nslices = vol.shape[0]
index = nslices // 2

tex_size = tuple(reversed(vol.shape))
tex = gfx.Texture(vol, dim=2, size=tex_size)
view = tex.get_view(filter="linear",
                    view_dim="2d",
                    layer_range=range(index, index + 1))

geometry = gfx.plane_geometry(200, 200, 12, 12)
material = gfx.MeshBasicMaterial(map=view, clim=(0, 2000))
plane = gfx.Mesh(geometry, material)
scene.add(plane)

camera = gfx.OrthographicCamera(200, 200)


@canvas.add_event_handler("wheel")
def handle_event(event):
    global index
    index = index + int(event["dy"] / 90)
    index = max(0, min(nslices - 1, index))
    view = tex.get_view(filter="linear",
                        view_dim="2d",
                        layer_range=range(index, index + 1))
示例#2
0
"""
Example showing transparency using three overlapping planes.
Press space to toggle the order of the planes.
Press 1-6 to select the blend mode.
"""

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

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

geometry = gfx.plane_geometry(50, 50)
plane1 = gfx.Mesh(geometry, gfx.MeshBasicMaterial(color=(1, 0, 0, 0.4)))
plane2 = gfx.Mesh(geometry, gfx.MeshBasicMaterial(color=(0, 1, 0, 0.4)))
plane3 = gfx.Mesh(geometry, gfx.MeshBasicMaterial(color=(0, 0, 1, 0.4)))

plane1.position.set(-10, -10, 1)
plane2.position.set(0, 0, 2)
plane3.position.set(10, 10, 3)

scene.add(plane1, plane2, plane3)

camera = gfx.OrthographicCamera(100, 100)


@canvas.add_event_handler("key_down")
def handle_event(event):
    if event["key"] == " ":
        print("Rotating scene element order")
示例#3
0
import pygfx as gfx

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

axes = gfx.AxesHelper(length=250)
scene.add(axes)

background = gfx.Background(None,
                            gfx.BackgroundMaterial((0, 1, 0, 1), (0, 1, 1, 1)))
scene.add(background)

im = imageio.imread("imageio:astronaut.png")
tex = gfx.Texture(im, dim=2)
geometry = gfx.plane_geometry(512, 512)
material = gfx.MeshBasicMaterial(map=tex.get_view(filter="linear"))
plane = gfx.Mesh(geometry, material)
scene.add(plane)

camera = gfx.OrthographicCamera(512, 512)
camera.position.set(0, 0, 500)
controls = gfx.PanZoomControls(camera.position.clone())
controls.add_default_event_handlers(canvas, camera)


def animate():
    controls.update_camera(camera)
    renderer.render(scene, camera)

示例#4
0
scene = gfx.Scene()

im = np.array(
    [
        [0, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 2],
    ],
    np.float32,
)

tex = gfx.Texture(im, dim=2)

plane = gfx.Mesh(
    gfx.plane_geometry(4, 4),
    gfx.MeshBasicMaterial(map=tex.get_view(filter="nearest"), clim=(0, 2)),
)
plane.position = gfx.linalg.Vector3(2, 2)  # put corner at 0, 0
scene.add(plane)

points = gfx.Points(
    gfx.Geometry(positions=[[0, 0, 1], [4, 4, 1]]),
    gfx.PointsMaterial(color=(0, 1, 0, 1), size=20),
)
scene.add(points)

camera = gfx.OrthographicCamera(10, 10)


if __name__ == "__main__":
示例#5
0
from wgpu.gui.auto import WgpuCanvas, run
import pygfx as gfx


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

vol = imageio.volread("imageio:stent.npz")
nslices = vol.shape[0]
index = nslices // 2

tex = gfx.Texture(vol, dim=3)
view = tex.get_view(filter="linear")

geometry = gfx.plane_geometry(200, 200, 1, 1)
texcoords = np.hstack([geometry.texcoords.data, np.ones((4, 1), np.float32) * 0.5])
geometry.texcoords = gfx.Buffer(texcoords)

material = gfx.MeshBasicMaterial(map=view, clim=(0, 2000))
plane = gfx.Mesh(geometry, material)
scene.add(plane)

camera = gfx.OrthographicCamera(200, 200)


@canvas.add_event_handler("wheel")
def handle_event(event):
    global index
    index = index + event["dy"] / 90
    index = max(0, min(nslices - 1, index))
示例#6
0
# TODO: also add a mesh slice for each plane

planes = []
texcoords = {
    0: [[0.5, 0, 0], [0.5, 1, 0], [0.5, 0, 1], [0.5, 1, 1]],
    1: [[0, 0.5, 0], [1, 0.5, 0], [0, 0.5, 1], [1, 0.5, 1]],
    2: [[0, 0, 0.5], [1, 0, 0.5], [0, 1, 0.5], [1, 1, 0.5]],
}
sizes = {
    0: (vol.shape[1], vol.shape[0]),  # YZ plane
    1: (vol.shape[2], vol.shape[0]),  # XZ plane
    2: (vol.shape[2], vol.shape[1]),  # XY plane (default)
}
for axis in [0, 1, 2]:
    geometry = gfx.plane_geometry(*sizes[axis], 1, 1)
    geometry.texcoords = gfx.Buffer(np.array(texcoords[axis], dtype="f4"))
    plane = gfx.Mesh(geometry, material)
    planes.append(plane)
    scene.add(plane)

    if axis == 0:  # YZ plane
        plane.rotation.set_from_euler(
            gfx.linalg.Euler(0.5 * np.pi, 0.5 * np.pi))
    elif axis == 1:  # XZ plane
        plane.rotation.set_from_euler(gfx.linalg.Euler(0.5 * np.pi))
    # else: XY plane

# camera = gfx.PerspectiveCamera(70, 16 / 9)
camera = gfx.OrthographicCamera(200, 200)
camera.position.set(125, 125, 125)
示例#7
0
# With the ortho camera, you can pick any values you like, also negative
# near values. With the perspective camera you'll want to pick a small
# positive near-value, and a relatively small value for the far-value
# as well, otherwise the distant squares become smaller than 1 pixel ;)

# Select camera and matchingclipping planes
if True:
    near, far = -40, 300
    camera = gfx.OrthographicCamera(2.2, 2.2, near, far)
else:
    near, far = 5, 10
    camera = gfx.PerspectiveCamera(50, 1, near, far)

# %% Create four planes near the z-clipping planes

geometry = gfx.plane_geometry(1, 1)
green_material = gfx.MeshBasicMaterial(color=(0, 0.8, 0.2, 1))
greener_material = gfx.MeshBasicMaterial(color=(0, 1, 0, 1))
red_material = gfx.MeshBasicMaterial(color=(1, 0, 0, 1))

plane1 = gfx.Mesh(geometry, green_material)
plane2 = gfx.Mesh(geometry, red_material)
plane3 = gfx.Mesh(geometry, greener_material)
plane4 = gfx.Mesh(geometry, red_material)

# Note the negation of near and far in the plane's position. This is
# because the camera looks down the z-axis: more negative means moving
# away from the camera, positive values are behind the camera.
plane1.position = gfx.linalg.Vector3(-0.51, -0.51, -(near + 0.01))  # in range
plane2.position = gfx.linalg.Vector3(+0.51, -0.51, -(near - 0.01))  # out range
plane3.position = gfx.linalg.Vector3(-0.51, +0.51, -(far - 0.01))  # in range