Skip to content

Ability to scale camera-axii individually #388

@kushalkolar

Description

@kushalkolar

It would be useful to have a way to automatically scale the camera to fill a world object on the canvas, for example if we plot a sine wave with small amplitudes it barely shows up unless the camera is scaled appropriately. Doing this automatically would be useful for dynamic scientific plots! I think I can add this functionality to camera.show_object() and controller.show_object(), but I couldn't figure out how to map from canvas coordinates to world coordinates w.r.t. the camera.

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


canvas = WgpuCanvas()
renderer = gfx.WgpuRenderer(canvas)

scene = gfx.Scene()
xs = np.linspace(0, 30, 100)
ys = np.sin(xs) / 10
zs = np.ones(100)

positions = np.dstack([xs, ys, zs])[0].astype(np.float32)

line = gfx.Line(
    gfx.Geometry(positions=positions),
    gfx.LineMaterial(thickness=2.0, color=(1, 1, 1, 1)),
)
scene.add(line)

camera = gfx.OrthographicCamera()

controller = gfx.PanZoomController()
controller.add_default_event_handlers(renderer, camera)

controller.show_object(camera, line)

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


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

image

if you set camear.scale.y = 0.1:

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions