コード例 #1
0
ファイル: show.py プロジェクト: pygfx/pygfx
def show(object: WorldObject, up=None):
    """Visualize a given WorldObject in a new window with an interactive camera.

    Parameters:
        object (WorldObject): The object to show.
        up (Vector3): Optional. Configure the up vector for the camera controls.
    """
    from wgpu.gui.auto import WgpuCanvas, run

    if isinstance(object, Scene):
        scene = object
    else:
        scene = Scene()
        scene.add(object)

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

    camera = PerspectiveCamera(70, 16 / 9)
    look_at = camera.show_object(object)

    canvas = WgpuCanvas()
    renderer = WgpuRenderer(canvas)

    controls = OrbitControls(camera.position.clone(), look_at, up=up)
    controls.add_default_event_handlers(canvas, camera)

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

    canvas.request_draw(animate)
    run()
コード例 #2
0
ファイル: mesh_slice.py プロジェクト: pygfx/pygfx
"""
Example showing off the mesh slice material.
"""

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

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

geometry = gfx.torus_knot_geometry(1, 0.3, 128, 16)
material1 = gfx.MeshPhongMaterial(color=(0.5, 0.5, 0.5, 1.0))
material2 = gfx.MeshSliceMaterial(thickness=8,
                                  color=(1, 1, 0, 1),
                                  plane=(0, 0, 1, 0))
obj1 = gfx.Mesh(geometry, material1)
obj2 = gfx.Mesh(geometry, material2)
scene.add(obj1)
scene.add(obj2)

camera = gfx.PerspectiveCamera(70, 2)
camera.position.z = 4


def animate():

    dist = material2.plane[3]
    dist += 0.02
    if dist > 1:
        dist = -1.5
コード例 #3
0
ファイル: post_processing1.py プロジェクト: pygfx/pygfx
            },
            "bindings1": {
                0:
                Binding("r_sampler", "sampler/filtering",
                        wobject.texture.get_view()),
                1:
                Binding("r_tex", "texture/auto", wobject.texture.get_view()),
            },
        },
    ]


# %% The application

# The canvas for eventual display
canvas = WgpuCanvas(size=(640, 480))

# The texture to render the scene into
texture = gfx.Texture(dim=2, size=(640, 480, 1), format="rgba8unorm")

# The regular scene

renderer1 = gfx.renderers.WgpuRenderer(texture)
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)
コード例 #4
0
ファイル: colormap_mesh.py プロジェクト: pygfx/pygfx
"""
Example demonstrating different colormap dimensions on a mesh.
"""

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


canvas = WgpuCanvas(size=(900, 400))
renderer = gfx.renderers.WgpuRenderer(canvas)
scene = gfx.Scene()


def get_geometry():
    return gfx.cylinder_geometry(height=2, radial_segments=32, open_ended=True)


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)


geometry = get_geometry()

camera = gfx.OrthographicCamera(16, 3)
コード例 #5
0
ファイル: scene_overlay.py プロジェクト: pygfx/pygfx
"""
Example showing a 3D scene with a 2D overlay.

The idea is to render both scenes, but clear the depth before rendering
the overlay, so that it's always on top.
"""

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

# Create a canvas and renderer

canvas = WgpuCanvas(size=(500, 300))
renderer = gfx.renderers.WgpuRenderer(canvas)

# Compose a 3D scene

scene1 = gfx.Scene()

geometry1 = gfx.box_geometry(200, 200, 200)
material1 = gfx.MeshPhongMaterial(color=(1, 1, 0, 1.0))
cube1 = gfx.Mesh(geometry1, material1)
scene1.add(cube1)

camera1 = gfx.OrthographicCamera(300, 300)

# Compose another scene, a 2D overlay

scene2 = gfx.Scene()
コード例 #6
0
ファイル: scene_in_a_scene.py プロジェクト: pygfx/pygfx
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()

geometry2 = gfx.box_geometry(200, 200, 200)
material2 = gfx.MeshPhongMaterial(map=texture1.get_view(filter="linear"))
cube2 = gfx.Mesh(geometry2, material2)
scene2.add(cube2)

camera2 = gfx.PerspectiveCamera(70, 16 / 9)
camera2.position.z = 400


def animate():
    rot = gfx.linalg.Quaternion().set_from_euler(gfx.linalg.Euler(0.005, 0.01))
コード例 #7
0
"""
Example demonstrating rendering the same scene into two different canvases.
"""

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

# Create two canvases and two renderers

canvas_a = WgpuCanvas(size=(500, 300))
canvas_b = WgpuCanvas(size=(300, 500))

renderer_b = gfx.renderers.WgpuRenderer(canvas_b)
renderer_a = gfx.renderers.WgpuRenderer(canvas_a)

# Compose a 3D scene with 2 objects

scene = gfx.Scene()

geometry1 = gfx.box_geometry(200, 200, 200)
material1 = gfx.MeshPhongMaterial(color=(1, 1, 0, 1.0))
cube = gfx.Mesh(geometry1, material1)
scene.add(cube)

positions = np.array(
    [[-1, -1, 0], [-1, +1, 0], [+1, +1, 0], [+1, -1, 0], [-1, -1, 0],
     [+1, +1, 0]],
    np.float32,
)
geometry2 = gfx.Geometry(positions=positions * 250)
コード例 #8
0
ファイル: validate_culling.py プロジェクト: pygfx/pygfx
"""
Example test to validate winding and culling.

* The top red knot should look normal and well lit.
* The top green know should show the backfaces, well lit.
* The bottom row shows the same, but the camera looks backwards.

"""

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

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

# geometry = gfx.BoxGeometry(1, 1, 1)
geometry = gfx.torus_knot_geometry(1, 0.3, 64, 10)

# Create red know shown normally
material1 = gfx.MeshPhongMaterial(color=(1, 0, 0, 1))
obj1 = gfx.Mesh(geometry, material1)
obj1.position.set(-2, 0, 0)
obj1.material.side = "FRONT"

# Create a green knot for which we show the back
material2 = gfx.MeshPhongMaterial(color=(0, 1, 0, 1))
obj2 = gfx.Mesh(geometry, material2)
obj2.position.set(+2, 0, 0)
obj2.material.side = "BACK"
コード例 #9
0
"""
Display a lot of line objects. Because of the architecture of wgpu,
this is still performant.
"""

import time  # noqa

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


canvas = WgpuCanvas(max_fps=999)
renderer = gfx.WgpuRenderer(canvas, show_fps=True)

scene = gfx.Scene()

# Define number of vertices
cols = 20
rows = 50
nvertices = 30000
use_thin_lines = True

print(nvertices * rows * cols, "vertices in total")

x = np.linspace(0.05, 0.95, nvertices, dtype=np.float32)

for row in range(rows):
    for col in range(cols):
        y = np.sin(x * 25) * 0.45 + np.random.normal(0, 0.02, len(x)).astype(np.float32)
        positions = np.column_stack([x, y, np.zeros_like(x)])