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

Revert "Support calculating normals for indexed meshes" (#12716) and add support for calculating smooth normals #13333

Merged
merged 1 commit into from May 16, 2024

Conversation

adithramachandran
Copy link
Contributor

Objective

Solution

  • Partially revert the changes in Support calculating normals for indexed meshes #11654 to compute flat normals for both indexed and unindexed meshes in compute_flat_normals
  • Create a new method, compute_smooth_normals, that computes smooth normals for indexed meshes
  • Create a new method, compute_normals, that computes smooth normals for indexed meshes and flat normals for unindexed meshes by default. Use this new method instead of compute_flat_normals.

Testing

  • Run the example with and without the changes to ensure that the results are identical.

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 ✨

@alice-i-cecile alice-i-cecile added this to the 0.14 milestone May 12, 2024
@alice-i-cecile alice-i-cecile added C-Bug An unexpected or incorrect behavior C-Enhancement A new feature A-Rendering Drawing game state to the screen C-Needs-Release-Note Work that should be called out in the blog due to impact labels May 12, 2024
@alice-i-cecile
Copy link
Member

@ManevilleF could I get your review here?

Copy link
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

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

Like I said in my comment I'd prefer if the gltf loader wasn't changed and always use compute_flat_normals.

Right now it would lead to some meshes having flat normals and some smooth normals and it won't be obvious why unless you know about this.

Other than that, LGTM, thanks for picking this up.

Copy link
Contributor

@ManevilleF ManevilleF left a comment

Choose a reason for hiding this comment

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

Some thoughts, there are some comments unrelated to this PR but to the one it's reverting.

  • I don't know if a unified compute_normals that makes a choice between smooth and flat is useful
  • The unindexed version of compute_flat_normals makes the assumption we duplicated vertices, and this is no longer mentioned, and hard to validate

crates/bevy_render/src/mesh/mesh/mod.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/mesh/mesh/mod.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/mesh/mesh/mod.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/mesh/mesh/mod.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/mesh/mesh/mod.rs Outdated Show resolved Hide resolved
@IceSentry
Copy link
Contributor

I don't know if a unified compute_normals that makes a choice between smooth and flat is useful

Yeah, I think I originally mentioned a function named like that because people were trying to add smooth normals to a function called compute_flat_normals so it made more sense to have a function that didn't explicitly mention flat/smooth in the name. But if we have both functions, just let users pick I guess. Although, there might still be an argument to have something like that for users that don't care and just want something to be computed.

@IceSentry
Copy link
Contributor

The unindexed version of compute_flat_normals makes the assumption we duplicated vertices, and this is no longer mentioned, and hard to validate

Right, good catch. I think the solution I would prefer is to keep the previous behaviour of panicking and suggesting users to either use compute_smooth_normals or duplicate_vertices/compute_flat_normals.

@IceSentry
Copy link
Contributor

Actually, if we go back to the 0.13 behaviour of panicking then a compute_normals unified function makes more sense because it will try to pick something without panicking. The doc should definitely explain this though.

@IceSentry
Copy link
Contributor

IceSentry commented May 12, 2024

@adithramachandran here's the code of the currently released version https://github.com/bevyengine/bevy/blob/latest/crates/bevy_render/src/mesh/mesh/mod.rs#L534

I think compute_flat_normals should be identical after your PR. Except, pointing users in the assert message to consider compute_smooth_normals when indices are used, but keep the duplicate_vertices suggestion that is already present

@robtfm
Copy link
Contributor

robtfm commented May 12, 2024

I’m not sure I fully understood the discussion but I would prefer the gltf loader not to panic in circumstances where it’s possible to find a viable strategy (thinking about user generated content).

Would the proposed changes mean indexed meshes with no normals would panic? If so are we really confident that panicking is the best answer?

If so I guess we can catch the panic to avoid crashing (I have a pr for that) or perhaps add a loader setting (which could be a separate pr as well). But I just want to make sure that’s really necessary.

@IceSentry
Copy link
Contributor

I’m not sure I fully understood the discussion but I would prefer the gltf loader not to panic in circumstances where it’s possible to find a viable strategy (thinking about user generated content).

For me the main issue here is to make sure compute_flat_normals actually computes flat normals always. The currently released version does panic, so while I would also like an approach that doesn't panic I think it's more important to resolve the case of compute_flat_normals not computing flat normals. We can use compute_normals that picks flat/smooth normals based on the presence of indices, that part I don't mind much.

@ManevilleF
Copy link
Contributor

For me the main issue here is to make sure compute_flat_normals actually computes flat normals always. The currently released version does panic, so while I would also like an approach that doesn't panic I think it's more important to resolve the case of compute_flat_normals not computing flat normals. We can use compute_normals that picks flat/smooth normals based on the presence of indices, that part I don't mind much.

Makes sense, this means that this PR approach to allows both:

  • compute_normals enables to have normals in a non panicking way, no matter the presence of indices
  • compute_flat_normals allows both indexed and non indexed meshes, which is an improvement as we don't necessarily need to duplicate vertices for flat normals

We could add a doc comment explaining that a certain layout of vertices are expected for non indexed geometry, similar to the output of duplicate_vertices

@adithramachandran
Copy link
Contributor Author

Thanks for the comments! I'm trying to synthesize the conversation here into a set of changes that I need to make to unblock the merge:

  • Implement the hygiene changes suggested by @ManevilleF (chunk_exact instead of the roundabout iteration using pointers, unifying match branches, etc.)
  • In compute_flat_normals:
    • Add a doc comment explaining the constraints around unindexed meshes
    • In the assert message, tell users to consider using compute_smooth_normals with indexed meshes (keep the existing recommendation to call duplicate_vertices
    • In the unindexed branch, assert that we have duplicated vertices ("keep the previous behaviour of panicking")

Things that won't be changed:

  • compute_normals, since it allows users to compute normals regardless of whether the mesh is indexed
  • compute_flat_normals should continue to handle both indexed and non-indexed meshes, which allows users to not necessarily have to call duplicate_vertices for indexed meshes.

Does that sound complete?

@IceSentry
Copy link
Contributor

IceSentry commented May 12, 2024

Does that sound complete?

Yes, that seems good to me.

You didn't explicitly mentioned it but also you should definitely use the Vec3 thing @ManevilleF mentioned.

@adithramachandran
Copy link
Contributor Author

The two points about updating the assert in the unindexed branch of compute_flat_normals don't seem to hold true, since that assert doesn't exist anymore (if indices are defined, we use them). I've tried to encapsulate the information about the layout of vertices in an unindexed mesh in the doc comment instead.

I also noticed that the Mesh::with_computed[_/_smooth_/_flat_]normals need to be created/updated to reflect the underlying implementations, so I've included those in the updated commit as well.

@IceSentry
Copy link
Contributor

IceSentry commented May 12, 2024

The two points about updating the assert in the unindexed branch of compute_flat_normals don't seem to hold true, since that assert doesn't exist anymore (if indices are defined, we use them). I've tried to encapsulate the information about the layout of vertices in an unindexed mesh in the doc comment instead.

Kinda, for me the issue is that the indexed path just assumes that duplicate vertices was called and if it wasn't called then it just generates wrong normals without warnings. That's why I mentioned just going back to the previous behaviour of not trying to do it for indexed meshes, but I guess it doesn't matter too much since it is documented in the doc comment.

I don't want to block this PR though, so I'll just approve it anyway and we'll figure something out in the future if it becomes an issue.

…2716)

# Objective

- Refactor the changes merged in bevyengine#11654 to compute flat normals for
indexed meshes instead of smooth normals.

## Solution

- Partially revert the changes in bevyengine#11654 to compute flat normals for
both indexed and unindexed meshes in `compute_flat_normals`
- Create a new method, `compute_smooth_normals`, that computes smooth
normals for indexed meshes
- Create a new method, `compute_normals`, that computes smooth normals
for indexed meshes and flat normals for unindexed meshes by default. Use
this new method instead of `compute_flat_normals`.

## Testing

- Run the example with and without the changes to ensure that the
results are identical.
@IceSentry IceSentry self-requested a review May 14, 2024 02:25
Copy link
Contributor

@IceSentry IceSentry left a comment

Choose a reason for hiding this comment

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

Everything LGTM now.

@IceSentry IceSentry requested a review from ManevilleF May 14, 2024 02:31
@superdump superdump added the X-Uncontroversial This work is generally agreed upon label May 14, 2024
@alice-i-cecile alice-i-cecile changed the title Revert "Support calculating normals for indexed meshes" (#12716) Revert "Support calculating normals for indexed meshes" (#12716) and add support for calculating smooth normals May 16, 2024
@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed C-Needs-Release-Note Work that should be called out in the blog due to impact labels May 16, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue May 16, 2024
Merged via the queue into bevyengine:main with commit 65e62ba May 16, 2024
29 checks passed
github-merge-queue bot pushed a commit that referenced this pull request May 18, 2024
# Objective

- some gltf files are broken since #13333 

```
thread 'IO Task Pool (2)' panicked at crates/bevy_render/src/mesh/mesh/mod.rs:581:9:
`compute_flat_normals` can't work on indexed geometry. Consider calling either `Mesh::compute_smooth_normals` or `Mesh::duplicate_vertices` followed by `Mesh::compute_flat_normals`.
```

- test with example `custom_gltf_vertex_attribute` or
`gltf_skinned_mesh`


## Solution

- Call the wrapper function for normals that will either call
`compute_flat_normals` or `compute_smooth_normals` as appropriate

## Testing

- Ran the two examples mentioned above
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 C-Enhancement A new feature S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it X-Uncontroversial This work is generally agreed upon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Revert "Support calculating normals for indexed meshes"
6 participants