def create_scene(clipping_planes, clipping_mode): maxsize = 221 scene = gfx.Scene() for n in range(20, maxsize, 50): material = gfx.MeshPhongMaterial( color=(n / maxsize, 1, 0, 1), clipping_planes=clipping_planes, clipping_mode=clipping_mode, ) geometry = gfx.box_geometry(n, n, n) cube = gfx.Mesh(geometry, material) scene.add(cube) return scene
Example showing multiple rotating cubes. This also tests the depth buffer. """ import imageio from wgpu.gui.auto import WgpuCanvas, run import pygfx as gfx canvas = WgpuCanvas() renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() im = imageio.imread("imageio:chelsea.png") tex = gfx.Texture(im, dim=2).get_view(filter="linear") material = gfx.MeshBasicMaterial(map=tex) geometry = gfx.box_geometry(100, 100, 100) cubes = [gfx.Mesh(geometry, material) for i in range(8)] for i, cube in enumerate(cubes): cube.position.set(350 - i * 100, 0, 0) scene.add(cube) background = gfx.Background(None, gfx.BackgroundMaterial((0, 1, 0, 1), (0, 1, 1, 1))) scene.add(background) camera = gfx.PerspectiveCamera(70, 16 / 9) camera.position.z = 500 def animate(): for i, cube in enumerate(cubes):
# todo: if we have per-vertex coloring, we can paint on the mesh instead :D import numpy as np import imageio from wgpu.gui.auto import WgpuCanvas, run import pygfx as gfx canvas = WgpuCanvas() renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() im = imageio.imread("imageio:astronaut.png").astype(np.float32) / 255 tex = gfx.Texture(im, dim=2).get_view(filter="linear", address_mode="repeat") geometry = gfx.box_geometry(200, 200, 200) material = gfx.MeshBasicMaterial(map=tex) cube = gfx.Mesh(geometry, material) cube.position.x += 150 scene.add(cube) torus = gfx.Mesh(gfx.torus_knot_geometry(100, 20, 128, 32), gfx.MeshPhongMaterial()) torus.position.x -= 150 scene.add(torus) camera = gfx.PerspectiveCamera(70, 16 / 9) camera.position.z = 400 @canvas.add_event_handler("pointer_down")
scene = gfx.Scene() # Create cubemap texture tex_size = width, height, 6 tex = gfx.Texture(im, dim=2, size=tex_size) view = tex.get_view(view_dim="cube", layer_range=range(6)) # And the background image with the cube texture background = gfx.Background(None, gfx.BackgroundImageMaterial(map=view)) scene.add(background) # Let's add some cubes to make the scene less boring cubes = [] for pos in (-600, 0, -600), (-600, 0, +600), (+600, 0, -600), (+600, 0, +600): clr = (0.5, 0.6, 0.0, 1.0) cube = gfx.Mesh(gfx.box_geometry(200, 200, 200), gfx.MeshPhongMaterial(color=clr)) cube.position.from_array(pos) cubes.append(cube) scene.add(cube) camera = gfx.PerspectiveCamera(70) camera.scale.y = -1 camera.position.z = 0 def animate(): rot = gfx.linalg.Quaternion().set_from_euler(gfx.linalg.Euler(0.01, 0.02)) for cube in cubes: cube.rotation.multiply(rot)
import pygfx as gfx # First create the subscene, that reders into a texture texture1 = gfx.Texture(dim=2, size=(200, 200, 1), format="rgba8unorm") renderer1 = gfx.renderers.WgpuRenderer(texture1) scene1 = gfx.Scene() background1 = gfx.Background(None, gfx.BackgroundMaterial((0, 0.5, 0, 1))) scene1.add(background1) im = imageio.imread("imageio:bricks.jpg").astype(np.float32) / 255 tex = gfx.Texture(im, dim=2).get_view(filter="linear", address_mode="repeat") geometry1 = gfx.box_geometry(200, 200, 200) material1 = gfx.MeshPhongMaterial(map=tex, color=(1, 1, 0, 1.0)) cube1 = gfx.Mesh(geometry1, material1) scene1.add(cube1) camera1 = gfx.PerspectiveCamera(70, 16 / 9) camera1.position.z = 300 # Then create the actual scene, in the visible canvas canvas2 = WgpuCanvas() renderer2 = gfx.renderers.WgpuRenderer(canvas2) scene2 = gfx.Scene()
renderer = gfx.renderers.WgpuRenderer(canvas) # Prepare a very small data volume. The data is integer and not uint8, # so its not interpolated (a wgpu restriction). In this case this is intended. voldata = np.ones((3, 3, 3), np.int16) * 200 voldata[1:-1, :, :] = 600 voldata[:, 1:-1, :] = 600 voldata[:, :, 1:-1] = 600 voldata[1, 1, 1] = 800 # Create a texture, (wrapped in a geometry) for it geo = gfx.Geometry(grid=gfx.Texture(voldata, dim=3)) # Prepare two 3x3x3 boxes to indicate the proper position box1 = gfx.Mesh( gfx.box_geometry(3.1, 3.1, 3.1), gfx.MeshBasicMaterial(color=(1, 0, 0, 1), wireframe=True, wireframe_thickness=2), ) box2 = gfx.Mesh( gfx.box_geometry(3.1, 3.1, 3.1), gfx.MeshBasicMaterial(color=(0, 1, 0, 1), wireframe=True, wireframe_thickness=2), ) # In scene1 we show a raycasted volume scene1 = gfx.Scene() vol = gfx.Volume(geo, gfx.VolumeRayMaterial(clim=(0, 2000))) vol.position.set(-1, -1, -1) scene1.add(vol, box1) # In scene2 we show volume slices scene2 = gfx.Scene()