Scene: Set transform layer before calling user content - #5884
Merged
Merged
Conversation
This provides a simple way for the user to access the `TSTransform` that will be applied to the `Scene` within the user content function, e.g. ```rust ui.ctx().layer_transform_to_global(ui.layer_id()) ``` Previously this still kind of worked, but resulted in the user content lagging behind the actual scene position by a single frame, which looks a bit strange. With this PR, the user content using the transform no longer lags by a frame, and matches the scene's transform perfectly. Accessing the `TSTransform` of the `Scene` within the user content function is useful for the case where the user may want to instantiate new `Ui` sublayers that also track the scene (by default, sublayers do *not* apply the same transform as the scene, likely the cause of emilk#5682). With these changes, the user can have sublayers track the scene like so: ```rust let scene_layer = ui.layer_id(); let sub_layer = egui::LayerId::new(scene_layer.order, self.id); ui.ctx().set_sublayer(scene_layer, sub_layer); let scene_transform = ui.ctx().layer_transform_to_global(scene_layer).unwrap(); ui.ctx().set_transform_layer(sub_layer, scene_transform); ``` Tested with: - `egui_demo_app` scene example. - Local `egui_graph` demo example.
|
Preview available at https://egui-pr-preview.github.io/pr/5884-scene-transform |
Scene - set transform layer before calling user contentScene: Set transform layer before calling user content
lucasmerlin
approved these changes
Apr 8, 2025
lucasmerlin
left a comment
Collaborator
There was a problem hiding this comment.
Thanks! Makes sense to me
This was referenced Apr 8, 2025
Merged
2 tasks
darkwater
pushed a commit
to darkwater/egui
that referenced
this pull request
Aug 24, 2025
This changes the `Scene` behaviour to call `set_transform_layer` prior to calling the user content fn, rather than after. ### Motivation This provides a simple way for the user to access the `TSTransform` that will be applied to the `Scene` within the user content function, e.g. ```rust ui.ctx().layer_transform_to_global(ui.layer_id()) ``` Previously getting the transform like this still kind of worked, but resulted in the user content lagging behind the actual scene position by a single frame, which looks a bit strange. With this PR, the user content using the transform no longer lags by a frame, and matches the scene's transform perfectly. Accessing the `TSTransform` of the `Scene` within the user content function is useful for the case where the user may want to instantiate new `Ui` sublayers that also track the scene (by default, sublayers do *not* apply the same transform as the scene, likely the cause of emilk#5682). With these changes, the user can have sublayers track the scene like so: ```rust let scene_layer = ui.layer_id(); let sub_layer = egui::LayerId::new(scene_layer.order, self.id); ui.ctx().set_sublayer(scene_layer, sub_layer); let scene_transform = ui.ctx().layer_transform_to_global(scene_layer).unwrap(); ui.ctx().set_transform_layer(sub_layer, scene_transform); ``` ### Tested with - `egui_demo_app` scene example. - Local `egui_graph` demo example. --- <!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo for it to `egui_demo_lib`, or a new example. * Do NOT open PR:s from your `master` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! I will review your PR, but my time is limited! --> * Closes <https://github.com/emilk/egui/issues/THE_RELEVANT_ISSUE> * [x] I have followed the instructions in the PR template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This changes the
Scenebehaviour to callset_transform_layerprior to calling the user content fn, rather than after.Motivation
This provides a simple way for the user to access the
TSTransformthat will be applied to theScenewithin the user content function, e.g.Previously getting the transform like this still kind of worked, but resulted in the user content lagging behind the actual scene position by a single frame, which looks a bit strange.
With this PR, the user content using the transform no longer lags by a frame, and matches the scene's transform perfectly.
Accessing the
TSTransformof theScenewithin the user content function is useful for the case where the user may want to instantiate newUisublayers that also track the scene (by default, sublayers do not apply the same transform as the scene, likely the cause of #5682). With these changes, the user can have sublayers track the scene like so:Tested with
egui_demo_appscene example.egui_graphdemo example.