Skip to content

Scene: Set transform layer before calling user content - #5884

Merged
emilk merged 1 commit into
emilk:masterfrom
mitchmindtree:scene-transform
Apr 8, 2025
Merged

emilk merged 1 commit into
emilk:masterfrom
mitchmindtree:scene-transform

Conversation

@mitchmindtree

@mitchmindtree mitchmindtree commented Apr 7, 2025

Copy link
Copy Markdown
Contributor

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.

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 #5682). With these changes, the user can have sublayers track the scene like so:

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.

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.
@github-actions

github-actions Bot commented Apr 7, 2025

Copy link
Copy Markdown

Preview available at https://egui-pr-preview.github.io/pr/5884-scene-transform
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

@mitchmindtree mitchmindtree changed the title refactor: Scene - set transform layer before calling user content Scene: Set transform layer before calling user content Apr 7, 2025
@lucasmerlin lucasmerlin added bug Something is broken egui labels Apr 8, 2025

@lucasmerlin lucasmerlin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Makes sense to me

@emilk
emilk merged commit 4445497 into emilk:master Apr 8, 2025
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something is broken egui

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants