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): rot = gfx.linalg.Quaternion().set_from_euler( gfx.linalg.Euler(0.01 * i, 0.02 * i)) cube.rotation.multiply(rot) renderer.render(scene, camera) canvas.request_draw()
""" Example showing subdivided polyhedrons. """ import pygfx as gfx scene = gfx.Scene() material = gfx.MeshBasicMaterial(wireframe=True, side="FRONT") geometries = [gfx.tetrahedron_geometry(subdivisions=i) for i in range(4)] for i, geometry in enumerate(geometries): polyhedron = gfx.Mesh(geometry, material) polyhedron.position.set(6 - i * 3, 0, 0) scene.add(polyhedron) background = gfx.Background(None, gfx.BackgroundMaterial((0, 1, 0, 1), (0, 1, 1, 1))) scene.add(background) if __name__ == "__main__": gfx.show(scene, up=gfx.linalg.Vector3(0, 0, 1))
import numpy as np from wgpu.gui.auto import WgpuCanvas, run import pygfx as gfx canvas = WgpuCanvas() renderer = gfx.WgpuRenderer(canvas) scene = gfx.Scene() positions = np.random.normal(0, 0.5, (100, 3)).astype(np.float32) sizes = np.random.rand(100).astype(np.float32) * 50 colors = np.random.rand(100, 4).astype(np.float32) geometry = gfx.Geometry(positions=positions, sizes=sizes, colors=colors) material = gfx.PointsMaterial(vertex_colors=True, vertex_sizes=True) points = gfx.Points(geometry, material) scene.add(points) scene.add( gfx.Background(None, gfx.BackgroundMaterial((0.2, 0.0, 0, 1), (0, 0.0, 0.2, 1))) ) camera = gfx.NDCCamera() if __name__ == "__main__": canvas.request_draw(lambda: renderer.render(scene, camera)) run()
""" import numpy as np import imageio from wgpu.gui.auto import WgpuCanvas, run 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
Example showing the axes and grid helpers with a perspective camera. * The grid spans the x-z plane (orange and blue axis). * The yellow axis (y) stick up from the plane. * The red box fits snugly around the grid. """ from wgpu.gui.auto import WgpuCanvas, run import pygfx as gfx canvas = WgpuCanvas() renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() background = gfx.Background( None, gfx.BackgroundMaterial((0, 0.1, 0, 1), (0, 0.1, 0.1, 1))) scene.add(background) axes = gfx.AxesHelper(length=40) scene.add(axes) grid = gfx.GridHelper(size=100) scene.add(grid) box = gfx.BoxHelper(size=100) scene.add(box) camera = gfx.PerspectiveCamera(70, 16 / 9) camera.position.set(75, 75, 75) camera.look_at(gfx.linalg.Vector3())