def __init__(self, noise=1): super().__init__() self.uniform_buffer = gfx.Buffer( gfx.utils.array_from_shadertype(self.uniform_type)) self.uniform_buffer.data["time"] = 0 self.uniform_buffer.data["noise"] = noise
def create_object(texcoords, tex, xpos): geometry = get_geometry() geometry.texcoords = gfx.Buffer(texcoords) material = gfx.MeshPhongMaterial(map=tex, clim=(-0.05, 1)) obj = gfx.Mesh(geometry, material) obj.position.x = xpos scene.add(obj)
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)) geometry.texcoords.data[:, 2] = index / nslices geometry.texcoords.update_range(0, geometry.texcoords.nitems)
""" Example demonstrating colormaps in 4 modes: grayscale, gray+alpha, RGB, RGBA. """ import numpy as np from wgpu.gui.auto import WgpuCanvas, run import pygfx as gfx canvas = WgpuCanvas(size=(900, 400)) renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() geometry = gfx.torus_knot_geometry(1, 0.3, 128, 32) geometry.texcoords = gfx.Buffer(geometry.texcoords.data[:, 0]) camera = gfx.OrthographicCamera(16, 3) def create_object(tex, xpos): material = gfx.MeshPhongMaterial(map=tex, clim=(-0.05, 1)) obj = gfx.Mesh(geometry, material) obj.position.x = xpos scene.add(obj) # === 1-channel colormap: grayscale cmap1 = np.array([(1,), (0,), (0,), (1,)], np.float32) tex1 = gfx.Texture(cmap1, dim=1).get_view(filter="linear") create_object(tex1, -6)
# 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) camera.look_at(gfx.linalg.Vector3())