Understanding lag in renderer event handlers #607
-
I'm basically trying to create an infinite line by creating a import numpy as np
import pygfx
from wgpu.gui.auto import WgpuCanvas, run
from wgpu.gui.glfw import GlfwWgpuCanvas
from pylinalg import vec_transform, vec_unproject
canvas = GlfwWgpuCanvas()
renderer = pygfx.WgpuRenderer(canvas)
scene = pygfx.Scene()
camera = pygfx.OrthographicCamera(100, 100)
controller = pygfx.PanZoomController(camera, register_events=renderer)
line = pygfx.Line(
geometry=pygfx.Geometry(positions=np.array([[0, 100, 0], [0, 0, 0]]).astype(np.float32)),
material=pygfx.LineMaterial(color="w", thickness=10)
)
scene.add(line)
camera.show_object(line)
def map_screen_to_world(pos) -> np.ndarray:
vs = renderer.logical_size
# get position relative to viewport
pos_rel = (
pos[0] - renderer.rect[0],
pos[1] - renderer.rect[1],
)
# convert screen position to NDC
pos_ndc = (pos_rel[0] / vs[0] * 2 - 1, -(pos_rel[1] / vs[1] * 2 - 1), 0)
# get world position
pos_ndc += vec_transform(camera.world.position, camera.camera_matrix)
pos_world = vec_unproject(pos_ndc[:2], camera.camera_matrix)
# default z is zero for now
return np.array([*pos_world[:2], 0])
def camera_edges_to_world():
"""Convert current viewport bbox to world coordinates"""
xymin = renderer.rect[:2]
xymax = renderer.rect[2:]
xmin, ymin = map_screen_to_world(xymin)[:2]
xmax, ymax = map_screen_to_world(xymax)[:2]
return xmin, ymin, xmax, ymax
def set_linear_selector_endpoints(*args):
"""set the endpoints of the line"""
xmin, ymin, xmax, ymax = camera_edges_to_world()
line.geometry.positions.data[:, 1] = ymin, ymax
line.geometry.positions.update_range()
canvas.request_draw(lambda: renderer.render(scene, camera))
run() Updating the line endpoints after pointer events creates obvious lag. It takes a fraction of a second to update the endpoints when zooming out (wheel) or moving past the current edge with the pointer. renderer.add_event_handler(set_linear_selector_endpoints, "pointer_move", "wheel", "resize") pointer_events.mp4Using There is no lag with the pointer events (when moving past the edge), but there is still lag with zoom (mouse wheel) events renderer.add_event_handler(set_linear_selector_endpoints, "before_render") before_render.mp4 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I just realized, I think this is best achieved by making a custom shader to create the behavior of an infinitely long line? |
Beta Was this translation helpful? Give feedback.
That would be the ideal solution.
You could also render a Line in screen space (using NDC camera) in a second render pass, that would not require any dynamic update logic.