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

issue with shape::plane uv or indices order #1018

Closed
trashuj opened this issue Dec 6, 2020 · 9 comments · Fixed by #8878
Closed

issue with shape::plane uv or indices order #1018

trashuj opened this issue Dec 6, 2020 · 9 comments · Fixed by #8878
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior

Comments

@trashuj
Copy link

trashuj commented Dec 6, 2020

Bevy version
commit 1398d78 (HEAD -> master, origin/master, origin/HEAD)
Author: Corey Farwell [email protected]
Date: Fri Dec 4 17:31:17 2020 -0500

Allow windows to maximized. (#1004)

Operating system & version
Windows 10

What you did
Display a texture on a shape::Plane.
I modified the 3d_scene.rs to create a simple example (in zip file):
shape.zip

What you expected to happen
Looking at the original png ("textures/rpg/mobs/boss_bee.png"), we can see it's mirrored, ie it looks like we would look at it from the bottom except we look at it from the top (hope that makes sense)

What actually happened
texture is flipped

Additional information
I tried playing with the uv and indices order in shape.rs / struct Plane, but i'm not sure what is the global logic (are the indices wrong or UV?).
Also note that shape::Quad version is correct.

@Moxinilian Moxinilian added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen labels Dec 7, 2020
@FrankenApps
Copy link

Could you maybe upload the whole project?
I tried running your sample but it doesn't even include a main function.

@trashuj
Copy link
Author

trashuj commented Jan 4, 2021

Hi, sorry, for some reason the modified 3d_scene was not attached.
Here it is. Notice how the png is mirrored (without using the modified shape.rs, just copy this over the existing 3d_scene.rs example)

3d_scene.zip

@cart
Copy link
Member

cart commented Jan 4, 2021

Yup that definitely looks flipped to me!

@FrankenApps
Copy link

I have looked into that, because I have experienced weird UV-Mapping for Boxes and Cubes. I would be willing to fix this, however I would need to know as @trashuj noted if the UVs or the indices are wrong.
I have also compared the setup to Threejs here for reference:
Bildschirmfoto 2021-01-05 um 11 55 22
Bildschirmfoto 2021-01-05 um 10 53 28
The bevy scene implementation I used for testing can be found here.

@wmbest2
Copy link

wmbest2 commented May 5, 2021

Running into this as well. I'm playing with building out quads by hand for voxel chunks and wanting to use a generated texture atlas, so having the UVs be intuitive would be very helpful.

Curious if you think this will be addressed as part of the v0.6 graphics pipeline changes @cart or if the best solution here is to just invert the UVs (I believe subtracting them from 1 should do the trick but haven't tested).

@cart
Copy link
Member

cart commented May 5, 2021

This won't be directly addressed by graphics pipeline changes. But this definitely seems like something that should be fixed. We should do a quick comparison to other applications like godot + blender just to make sure we're aligning to the ecosystem. Whoever wants to pick this up is welcome to do it!

@wmbest2
Copy link

wmbest2 commented May 6, 2021

I'm relatively new to rendering, but I found several supporting posts with what I had assumed might be the issue. I assume that when we load the images we are loading them the standard 0,0 at the top left position. However OpenGL (and I assume SPIRV) assumes UV originates at the bottom left of the image.

The options seem to be:

  • Flip the image during load so that the texture itself is flipped in memory
  • Flip the UVs (or really V I suppose) in the shader so that the image remains the same but the UVs are correct

I wanted to toss this out here as rust's image library does have a ::flip_vertical() method which could be used when loading the texture. But I was almost certain this would have implications on the 2D render side.

Again, I want to caveat that this is pretty new to me, so I didn't want to go proposing changes collecting concerns first.

Posts
https://gamedev.stackexchange.com/questions/140789/texture-coordinates-seem-to-have-flipped-or-incorrect-position
https://learnopengl.com/Getting-started/Textures
image
https://www.reddit.com/r/opengl/comments/6mon2j/how_do_you_deal_with_images_being_inverted/

@cart
Copy link
Member

cart commented May 7, 2021

wgpu is "top left y down" for images afaik (same as vulkan and metal). I don't think we should try to change that (or flip things on load). Images are also generally defined this way. I think we need to account for this solely in the uvs we generate for our meshes.

@wmbest2
Copy link

wmbest2 commented May 7, 2021

Ah, indeed that's where my limited knowledge was likely getting in the way. Understanding that UV coordinates in this renderer are top left helps immensely.

I'm assuming for model loading, e.g. .obj files, we would need to correct that during asset loading.

In either case I think this should be enough to correct my understanding. I may add a note to the bevy cheatbook regarding UVs.

@cart cart closed this as completed in 6c86545 Jun 18, 2023
NoahShomette pushed a commit to NoahShomette/bevy that referenced this issue Jun 19, 2023
# Objective

Fix bevyengine#1018 (Textures on the
`Plane` shape appear flipped).

This bug have been around for a very long time apparently, I tested it
was still there (see test code bellow) and sure enough, this image:


![test](https://github.com/bevyengine/bevy/assets/134181069/4cda7cf8-57d9-4677-91f5-02240d1e79b1)

... is flipped vertically when used as a texture on a plane (in main,
0.10.1 and 0.9):

![image](https://github.com/bevyengine/bevy/assets/134181069/0db4f52a-51af-4041-9c45-7bfe1f08b0cc)

I'm pretty confused because this bug is so easy to fix, it has been
around for so long, it is easy to encounter, and PRs touching this code
still didn't fix it: bevyengine#7546 To the
point where I'm wondering if it's actually intended. If it is, please
explain why and this PR can be changed to "mention that in the doc".

## Solution

Fix the UV mapping on the Plane shape

Here is how it looks after the PR

![image](https://github.com/bevyengine/bevy/assets/134181069/e07ce641-3de8-4da3-a4f3-95a6054c86d7)

## Test code

```rust
use bevy::{
    prelude::*,
};

fn main () {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .run();
}

fn setup(
    mut commands: Commands,
    assets: ResMut<AssetServer>,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    commands.spawn(Camera3dBundle {
        transform: Transform::from_xyz(0., 3., 0.).looking_at(Vec3::ZERO, Vec3::NEG_Z),
        ..default()
    });

    let mesh = meshes.add(Mesh::from(shape::Plane::default()));
    let texture_image = assets.load("test.png");
    let material = materials.add(StandardMaterial { 
        base_color_texture: Some(texture_image),
        ..default()
    });
    commands.spawn(PbrBundle {
        mesh,
        material,
        ..default()
    });
}
```

## Changelog

Fix textures on `Plane` shapes being flipped vertically.

## Migration Guide

Flip the textures you use on `Plane` shapes.
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-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants