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 a repeating texture example #11161

Closed
wants to merge 2 commits into from
Closed

Add a repeating texture example #11161

wants to merge 2 commits into from

Conversation

IQuick143
Copy link
Contributor

@IQuick143 IQuick143 commented Dec 31, 2023

Objective

Changelog

Started with the commits from the PR #11109
Added another commit that

  • Re-orders functions (main -> setup -> other)
  • Generalises the function quad_1_2 to quad_with_custom_uv to make the sample code more useful and reusable
  • Added a comment explaining why we are manipulating UVs and how texture sampler tie into that

Further details

Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@IQuick143
Copy link
Contributor Author

@alice-i-cecile @IceSentry would you mind reviewing this?

@MrGVSV MrGVSV added C-Examples An addition or correction to our examples A-Assets Load files from disk to use for things like images, models, and sounds labels Dec 31, 2023
Comment on lines +72 to +76
// Instead of using a standard quad with UV coordinates in the range `0..=1` (spanning the entire texture),
// we create a quad with UV coordinates in the range `-1..=2`, going beyond the texture by one entire unit in each direction.
// Texture behaviour then depends on the selected sampler mode.
// By default (`ImageAddressMode::ClampToEdge`) the out-of-bounds UV coordinates will map to the edge of the texture.
// When `ImageAddressMode::Repeat` is specified the out-of-bounds UV coordinates will repeat the texture.
Copy link
Contributor

Choose a reason for hiding this comment

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

My opinion, which has very little weight in this context would be this:

Less text is better, because those who read the example, would have to spend less time and energy reading it.

standard quad with UV coordinates

There's no such thing as "standard quad with UV coordinates". For example, if you have a mesh for a sphere, it usually contains hundreds quads, and none of them use coordinates 0..1. Quads with XY and UV coordinates 0..1 are used mostly in examples (and probably in Minecraft).

Also the comments below "usual UV range 0..=1", "custom UV coordinates" are not exactly correct for the same reason.

Texture behaviour then depends on the selected sampler mode

Texture behavior always depends on the sampler more, not "then". Perhaps you meant "Texture behaviour then depends on the selected address mode", which indeed then depends, because when UV is in range 0..=1, it does not depend on address mode.

By default (ImageAddressMode::ClampToEdge) the out-of-bounds UV coordinates will map to the edge of the texture.
When ImageAddressMode::Repeat is specified the out-of-bounds UV coordinates will repeat the texture

These would be the redundant comments because they

  • repeat the mostly obvious enum variant name
  • repeat enum documentation
  • basically repeat what is said several lines above where the image is loaded (where this comments belongs)

Again, feel free to ignore this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You make a good point in regards to the sampler vs address mode, that should be reworded.

As per what a standard quad UV means, you're right that when you're talking about quad as a graphics primitive (part of a mesh), it could have any UVs for any reason, but when one talks about a quad as a mesh itself (a geometric quadrilateral) it does occur to me only as natural to use UV coordinates as usual.
I said usual UV range 0..=1, because in that range lie UVs of most meshes people come into contact with (3D designers don't typically make their UVs sample outside of a texture from my experience).

You're right the comment does seem to give a bunch of redundant information that could be found in the docstrings for ImageAddressMode, though the purpose of the comment is mainly to explain why it takes user-handled in UV coordinates + a change in address mode to achieve the desired repeating effect, because just changing the adress mode would not actually produce any noticeable effects.
I'll consider shortening this part to a refference to the appropriate docs.

Anyway if someone gives me another opinion on this, I'll rewrite it.

@stepancheg
Copy link
Contributor

Generalises the function quad_1_2 to quad_with_custom_uv to make the sample code more useful and reusable

In my very subjective opinion, examples are not meant to optimized for reading, not for code reuse.

@mockersf mockersf added the S-Blocked This cannot move forward until something else changes label Dec 31, 2023
@rparrett
Copy link
Contributor

Could you edit your PR description to include that this also fixes #399?

Also, I think we can simplify and unblock this by just removing the non-repeating texture from the example. Discussion: #11136 (comment)

let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![[0., 0., 0.], [1., 0., 0.], [1., 1., 0.], [0., 1., 0.]],
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the vertex positions should match those of Bevy's "default quad." Users putting together code from various Bevy examples might be surprised that this quad isn't "anchored" on its center like the default one.

I personally feel like we should just modify the default Quad here, and that the example would be a bit more self-documenting with a function more like repeating_quad(times: f32). I have an example using that method here: #11136 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Modifying the default Quad might be better, I'll at least fix the mesh coordinates.

A better function name is welcome, although repeating_quad(times: f32) feels a little suggestive, because it suggest using this mesh would automatically cause repeating, whereas it only sets up a mesh that can be repeated over, but maybe this is not such a problem given the comments explaining the mechanisms.

@IQuick143
Copy link
Contributor Author

Could you edit your PR description to include that this also fixes #399?

Also, I think we can simplify and unblock this by just removing the non-repeating texture from the example. Discussion: #11136 (comment)

Issue #399 added!
I think the example is better when it shows directly the effects of different ImageAdressModes, but that's maybe just my opinion. The argument that it would unblock this is somewhat compelling but I think that since #11111 needs to be fixed anyway, this can probably wait (perhaps reconsider if this block would decide if this PR makes it into 0.13 or not).

@janhohenheim
Copy link
Member

janhohenheim commented Mar 6, 2024

One point against this fixing #399:
On 3D, most users will want to repeat textures on GLTFs, e.g. a texture on a house that repeats its facade, or a texture on a tree that repeats the bark, etc.
As far as I know, it is still not possible to set these things to repeat after the texture has already been loaded from a GLTF. Please correct me if I'm wrong.
Right now, the best way to do this is to scale the UVs through the uv_transform GLTF extension to have enough repetitions, which does not work when a model is rescaled (10 repeats on a small house might be good enough, but a big house might need 30)
Notes that is distinct from #11111 as we are not loading a texture again, but want to change the settings after it was loaded.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I don't have any blocking concerns: I think that the Quad swap would be nice, but should be done seperately.

@bugsweeper
Copy link
Contributor

bugsweeper commented May 1, 2024

Please correct me if I'm wrong.

@janhohenheim StandardMaterial also has uv_transform since #11904 , which is done exactly for GLTF

@janhohenheim
Copy link
Member

janhohenheim commented May 1, 2024

@bugsweeper yep, I wrote that PR 😉
Thanks for noting this though, so that others may know as well :)

github-merge-queue bot pushed a commit that referenced this pull request May 5, 2024
# Objective

Fixes #11136 .
Fixes #11161.

## Solution

- Set image sampler with repeated mode for u and v
- set uv_transform of StandardMaterial to resizing params

## Testing

Got this view on example run

![image](https://github.com/bevyengine/bevy/assets/17225606/a5f7c414-7966-4c31-97e1-320241ddc75b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Examples An addition or correction to our examples S-Blocked This cannot move forward until something else changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an example demonstrating how to repeat textures
8 participants