Geometry Plane

Use a plane geometry to show a texture, which is continuously updated to show video.

geometry plane
Imageio: 'cockatoo.mp4' was not found on your computer; downloading it now.
Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/images/cockatoo.mp4 (712 kB)
Downloading: 8192/728751 bytes (1.1%)728751/728751 bytes (100.0%)
  Done
File saved as /home/docs/.imageio/images/cockatoo.mp4.

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


def loop_video(video):
    while True:
        for frame in iio.imiter(video):
            yield frame[:, :, 0]


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

frame_generator = loop_video("imageio:cockatoo.mp4")
tex = gfx.Texture(next(frame_generator), dim=2)

geometry = gfx.plane_geometry(200, 200, 12, 12)
material = gfx.MeshBasicMaterial(map=tex)
plane = gfx.Mesh(geometry, material)
scene.add(plane)

camera = gfx.PerspectiveCamera(70)
camera.local.z = 200

scene.add(gfx.AmbientLight(), gfx.DirectionalLight())


def animate():
    # Read next frame, rewind if we reach the end
    tex.data[:] = next(frame_generator)
    tex.update_range((0, 0, 0), tex.size)

    renderer.render(scene, camera)
    canvas.request_draw()


if __name__ == "__main__":
    renderer.request_draw(animate)
    run()

Total running time of the script: (0 minutes 0.575 seconds)

Gallery generated by Sphinx-Gallery