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

[Merged by Bors] - bevy_pbr: Fix tangent and normal normalization #5666

Closed

Conversation

superdump
Copy link
Contributor

@superdump superdump commented Aug 13, 2022

Objective

  • Morten Mikkelsen clarified that the world normal and tangent must be normalized in the vertex stage and the interpolated values must not be normalized in the fragment stage. This is in order to match the mikktspace approach exactly.
  • Fixes Lighting issue on scaled objects after upgrading to 0.8 #5514 by ensuring the tangent basis matrix (TBN) is orthonormal

Solution

  • Normalize the world normal in the vertex stage and not the fragment stage
  • Normalize the world tangent xyz in the vertex stage
  • Take into account the sign of the determinant of the local to world matrix when calculating the bitangent

Changelog

  • Fixed - scaling a model that uses normal mapping now has correct lighting again

Morten Mikkelsen clarified that the world normal and tangent must be normalized
in the vertex stage and the interpolated values must not be normalized in the
fragment stage.
@superdump superdump added this to the Bevy 0.8.1 milestone Aug 13, 2022
@superdump superdump added C-Bug An unexpected or incorrect behavior A-Rendering Drawing game state to the screen C-Regression Functionality that used to work but no longer does. Add a test for this! labels Aug 13, 2022
@superdump
Copy link
Contributor Author

@superdump
Copy link
Contributor Author

Before merging this I want to add comments about why things are done this way.

@superdump
Copy link
Contributor Author

I hacked the load_gltf example to disable shadows and apply scaling to the scene parent transform, and to the camera position and target and then oscillate the scale based on a cosine curve with period 1 second. On main it looks like this:

Kapture.2022-08-13.at.12.41.38.mp4

On this PR it looks like this:

Kapture.2022-08-13.at.12.39.15.mp4

@superdump
Copy link
Contributor Author

Before merging this I want to add comments about why things are done this way.

Done.

@superdump
Copy link
Contributor Author

bors try

bors bot added a commit that referenced this pull request Aug 13, 2022
@superdump
Copy link
Contributor Author

It seems there is possibly one more detail. Morten Mikkelsen says that if the determinant of the local to world matrix can be negative (I think it definitely can be) then something needs scaling by the sign of the determinant. Just finding out exactly what, then I’ll update the PR.

If the sign of the determinant of the 3x3 model matrix is negative, this must
be applied when calculating the bitangent from the cross-product of the
interpolated world space normal and interpolated world space tangent.

This information came from Morten Mikkelsen and can be understood from section
2 of the following paper, though I do not understand it well enough to be able
to explain it:
https://jcgt.org/published/0009/03/04/
Copy link
Member

@cart cart left a comment

Choose a reason for hiding this comment

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

Seems to work with both uniform and non-uniform scales!

@cart
Copy link
Member

cart commented Aug 18, 2022

bors r+

bors bot pushed a commit that referenced this pull request Aug 18, 2022
# Objective

- Morten Mikkelsen clarified that the world normal and tangent must be normalized in the vertex stage and the interpolated values must not be normalized in the fragment stage. This is in order to match the mikktspace approach exactly.
- Fixes #5514 by ensuring the tangent basis matrix (TBN) is orthonormal

## Solution

- Normalize the world normal in the vertex stage and not the fragment stage
- Normalize the world tangent xyz in the vertex stage
- Take into account the sign of the determinant of the local to world matrix when calculating the bitangent

---

## Changelog

- Fixed - scaling a model that uses normal mapping now has correct lighting again
@bors bors bot changed the title bevy_pbr: Fix tangent and normal normalization [Merged by Bors] - bevy_pbr: Fix tangent and normal normalization Aug 18, 2022
@bors bors bot closed this Aug 18, 2022
cart pushed a commit that referenced this pull request Aug 19, 2022
# Objective

- Morten Mikkelsen clarified that the world normal and tangent must be normalized in the vertex stage and the interpolated values must not be normalized in the fragment stage. This is in order to match the mikktspace approach exactly.
- Fixes #5514 by ensuring the tangent basis matrix (TBN) is orthonormal

## Solution

- Normalize the world normal in the vertex stage and not the fragment stage
- Normalize the world tangent xyz in the vertex stage
- Take into account the sign of the determinant of the local to world matrix when calculating the bitangent

---

## Changelog

- Fixed - scaling a model that uses normal mapping now has correct lighting again
mockersf added a commit to mockersf/bevy that referenced this pull request Aug 26, 2022
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Sep 6, 2022
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Sep 8, 2022
maccesch pushed a commit to Synphonyte/bevy that referenced this pull request Sep 28, 2022
# Objective

- Morten Mikkelsen clarified that the world normal and tangent must be normalized in the vertex stage and the interpolated values must not be normalized in the fragment stage. This is in order to match the mikktspace approach exactly.
- Fixes bevyengine#5514 by ensuring the tangent basis matrix (TBN) is orthonormal

## Solution

- Normalize the world normal in the vertex stage and not the fragment stage
- Normalize the world tangent xyz in the vertex stage
- Take into account the sign of the determinant of the local to world matrix when calculating the bitangent

---

## Changelog

- Fixed - scaling a model that uses normal mapping now has correct lighting again
james7132 pushed a commit to james7132/bevy that referenced this pull request Oct 28, 2022
# Objective

- Morten Mikkelsen clarified that the world normal and tangent must be normalized in the vertex stage and the interpolated values must not be normalized in the fragment stage. This is in order to match the mikktspace approach exactly.
- Fixes bevyengine#5514 by ensuring the tangent basis matrix (TBN) is orthonormal

## Solution

- Normalize the world normal in the vertex stage and not the fragment stage
- Normalize the world tangent xyz in the vertex stage
- Take into account the sign of the determinant of the local to world matrix when calculating the bitangent

---

## Changelog

- Fixed - scaling a model that uses normal mapping now has correct lighting again
superdump added a commit to superdump/bevy that referenced this pull request Nov 10, 2022
This was forgotten in bevyengine#5666 when normalization of normals was moved from the
fragment to vertex stage.
bors bot pushed a commit that referenced this pull request Nov 11, 2022
# Objective

- Make the many foxes not unnecessarily bright. Broken since #5666.
- Fixes #6528 

## Solution

- In #5666 normalisation of normals was moved from the fragment stage to the vertex stage. However, it was not added to the vertex stage for skinned normals. The many foxes are skinned and their skinned normals were not unit normals. which made them brighter. Normalising the skinned normals fixes this.

---

## Changelog

- Fixed: Non-unit length skinned normals are now normalized.
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Dec 21, 2022
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Dec 21, 2022
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Dec 21, 2022
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Jan 6, 2023
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Jan 22, 2023
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

- Morten Mikkelsen clarified that the world normal and tangent must be normalized in the vertex stage and the interpolated values must not be normalized in the fragment stage. This is in order to match the mikktspace approach exactly.
- Fixes bevyengine#5514 by ensuring the tangent basis matrix (TBN) is orthonormal

## Solution

- Normalize the world normal in the vertex stage and not the fragment stage
- Normalize the world tangent xyz in the vertex stage
- Take into account the sign of the determinant of the local to world matrix when calculating the bitangent

---

## Changelog

- Fixed - scaling a model that uses normal mapping now has correct lighting again
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

- Make the many foxes not unnecessarily bright. Broken since bevyengine#5666.
- Fixes bevyengine#6528 

## Solution

- In bevyengine#5666 normalisation of normals was moved from the fragment stage to the vertex stage. However, it was not added to the vertex stage for skinned normals. The many foxes are skinned and their skinned normals were not unit normals. which made them brighter. Normalising the skinned normals fixes this.

---

## Changelog

- Fixed: Non-unit length skinned normals are now normalized.
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Mar 4, 2023
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Mar 5, 2023
HackerFoo added a commit to HackerFoo/bevy that referenced this pull request Apr 10, 2023
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-Regression Functionality that used to work but no longer does. Add a test for this!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lighting issue on scaled objects after upgrading to 0.8
4 participants