Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for KHR_texture_transform #11904

Merged
merged 18 commits into from
Feb 21, 2024

Conversation

janhohenheim
Copy link
Contributor

@janhohenheim janhohenheim commented Feb 16, 2024

Adopted #8266, so copy-pasting the description from there:

Objective

Support the KHR_texture_transform extension for the glTF loader.

Solution

As is, this only supports a single transform. Looking at Godot's source, they support one transform with an optional second one for detail, AO, and emission. glTF specifies one per texture. The public domain materials I looked at seem to share the same transform. So maybe having just one is acceptable for now. I tried to include a warning if multiple different transforms exist for the same material.

Note the gltf crate doesn't expose the texture transform for the normal and occlusion textures, which it should, so I just ignored those for now. (note by @janhohenheim: this is still the case)

Via cargo run --release --example scene_viewer ~/src/clone/glTF-Sample-Models/2.0/TextureTransformTest/glTF/TextureTransformTest.gltf:

texture_transform

Changelog

  • Support for the KHR_texture_transform extension added. Texture UVs that were scaled, rotated, or offset in a GLTF are now properly handled.
  • Added a new method affine2_to_square to the WGSL API. An Affine2 can be efficiently packed for the GPU as a mat3x2. This new methods unpacks that into a mat3x3 that can be used to transform coordinates.
  • For consistency with the new affine2_to_square, we renamed affine_to_square to affine3_to_square

Migration guide

Rename affine_to_square to affine3_to_square

@janhohenheim
Copy link
Contributor Author

@mockersf you wrote the following on the original PR:

A Affine2 seems better (smaller and faster) for the transform than a Mat3?

Could you explain to me why? Unless I understood something wrong, we'll need the Mat3 on the WGSL side anyways, so we might as well have a matrix on the StandardMaterial as well in order to not have to convert them around on every frame.

@janhohenheim
Copy link
Contributor Author

Note that the current code recalculates the uv in both pbr_prepass_functions.wgsl and pbr_types.wgsl. Could someone with more knowledge of the shaders tell me how you usually cache these sorts of values?

@janhohenheim
Copy link
Contributor Author

janhohenheim commented Feb 16, 2024

Not sure what I'm supposed to do about the duplicated dependencies check, could someone help me out there as well? The feature I added shouldn't pull in any additional transient dependencies according to gltf and gltf-json's Cargo.toml

@alice-i-cecile alice-i-cecile added C-Enhancement A new feature A-Rendering Drawing game state to the screen labels Feb 16, 2024
@alice-i-cecile alice-i-cecile added this to the 0.14 milestone Feb 16, 2024
@janhohenheim janhohenheim changed the title Add support for KHR_texture_transform (Adopted) Add support for KHR_texture_transform Feb 17, 2024
@Kanabenki
Copy link
Contributor

Note that the current code recalculates the uv in both pbr_prepass_functions.wgsl and pbr_types.wgsl. Could someone with more knowledge of the shaders tell me how you usually cache these sorts of values?

Since these happen in separate passes and the result vary depending on the input vertex I don't think you can share the transformed uvs easily? Besides I don't think that's a major perf cost.

Not sure what I'm supposed to do about the duplicated dependencies check, could someone help me out there as well? The feature I added shouldn't pull in any additional transient dependencies according to gltf and gltf-json's Cargo.toml

I think the current CI failure for that isn't caused by your changes, it is present in other PRs and isn't a blocker for merging.

@janhohenheim
Copy link
Contributor Author

Alright, thanks! Then this PR is ready for merging from my point of view :)

Copy link
Contributor

@Kanabenki Kanabenki left a comment

Choose a reason for hiding this comment

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

A couple of suggestions 🙂

crates/bevy_pbr/src/pbr_material.rs Outdated Show resolved Hide resolved
crates/bevy_pbr/src/pbr_material.rs Outdated Show resolved Hide resolved
crates/bevy_gltf/src/loader.rs Outdated Show resolved Hide resolved
@janhohenheim
Copy link
Contributor Author

Good improvement suggestions, thanks! I'll implement them later today :)

@janhohenheim
Copy link
Contributor Author

@Kanabenki ready for a re-review and I've got a question for you in one of the comments.

@janhohenheim
Copy link
Contributor Author

@Kanabenki I think I addressed all your comments now :) Thanks for the review!

@superdump
Copy link
Contributor

@janhohenheim - the renaming of affine_to_square and the changes to StandardMaterial(Uniform) are breaking changes so should be documented in the PR description as we use tooling to extract those parts to make the migration guides so please add that information (and any I missed :)) to the description.

@alice-i-cecile alice-i-cecile added the C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide label Feb 20, 2024
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Could you add some context on what users should update when this change get released in a new version of Bevy?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 21, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 21, 2024
Merged via the queue into bevyengine:main with commit 8531033 Feb 21, 2024
28 of 29 checks passed
@alice-i-cecile alice-i-cecile added the C-Needs-Release-Note Work that should be called out in the blog due to impact label Feb 23, 2024
@mockersf
Copy link
Member

this PR broke WebGL2 rendering: #12081

@janhohenheim
Copy link
Contributor Author

janhohenheim commented Feb 24, 2024

@mockersf ah heck! I'll look into it later today. Sorry for not testing that!

@janhohenheim janhohenheim deleted the khr_texture_transform branch February 24, 2024 09:45
msvbg pushed a commit to msvbg/bevy that referenced this pull request Feb 26, 2024
Adopted bevyengine#8266, so copy-pasting the description from there:

# Objective

Support the KHR_texture_transform extension for the glTF loader.

- Fixes bevyengine#6335
- Fixes bevyengine#11869 
- Implements part of bevyengine#11350
- Implements the GLTF part of bevyengine#399 

## Solution

As is, this only supports a single transform. Looking at Godot's source,
they support one transform with an optional second one for detail, AO,
and emission. glTF specifies one per texture. The public domain
materials I looked at seem to share the same transform. So maybe having
just one is acceptable for now. I tried to include a warning if multiple
different transforms exist for the same material.

Note the gltf crate doesn't expose the texture transform for the normal
and occlusion textures, which it should, so I just ignored those for
now. (note by @janhohenheim: this is still the case)

Via `cargo run --release --example scene_viewer
~/src/clone/glTF-Sample-Models/2.0/TextureTransformTest/glTF/TextureTransformTest.gltf`:


![texture_transform](https://user-images.githubusercontent.com/283864/228938298-aa2ef524-555b-411d-9637-fd0dac226fb0.png)

## Changelog

Support for the
[KHR_texture_transform](https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform)
extension added. Texture UVs that were scaled, rotated, or offset in a
GLTF are now properly handled.

---------

Co-authored-by: Al McElrath <hello@yrns.org>
Co-authored-by: Kanabenki <lucien.menassol@gmail.com>
msvbg pushed a commit to msvbg/bevy that referenced this pull request Feb 26, 2024
Adopted bevyengine#8266, so copy-pasting the description from there:

# Objective

Support the KHR_texture_transform extension for the glTF loader.

- Fixes bevyengine#6335
- Fixes bevyengine#11869 
- Implements part of bevyengine#11350
- Implements the GLTF part of bevyengine#399 

## Solution

As is, this only supports a single transform. Looking at Godot's source,
they support one transform with an optional second one for detail, AO,
and emission. glTF specifies one per texture. The public domain
materials I looked at seem to share the same transform. So maybe having
just one is acceptable for now. I tried to include a warning if multiple
different transforms exist for the same material.

Note the gltf crate doesn't expose the texture transform for the normal
and occlusion textures, which it should, so I just ignored those for
now. (note by @janhohenheim: this is still the case)

Via `cargo run --release --example scene_viewer
~/src/clone/glTF-Sample-Models/2.0/TextureTransformTest/glTF/TextureTransformTest.gltf`:


![texture_transform](https://user-images.githubusercontent.com/283864/228938298-aa2ef524-555b-411d-9637-fd0dac226fb0.png)

## Changelog

Support for the
[KHR_texture_transform](https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_texture_transform)
extension added. Texture UVs that were scaled, rotated, or offset in a
GLTF are now properly handled.

---------

Co-authored-by: Al McElrath <hello@yrns.org>
Co-authored-by: Kanabenki <lucien.menassol@gmail.com>
@alice-i-cecile alice-i-cecile added C-Needs-Release-Note Work that should be called out in the blog due to impact and removed C-Needs-Release-Note Work that should be called out in the blog due to impact labels May 27, 2024
@alice-i-cecile
Copy link
Member

Thank you to everyone involved with the authoring or reviewing of this PR! This work is relatively important and needs release notes! Head over to bevyengine/bevy-website#1287 if you'd like to help out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Breaking-Change A breaking change to Bevy's public API that needs to be noted in a migration guide C-Enhancement A new feature C-Needs-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support the KHR_texture_transform GLTF extension glTF not being rendered correctly. UV texture issue?
5 participants