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] - Add PBR textures #1632

Closed
wants to merge 14 commits into from
Closed

Conversation

mtsr
Copy link
Contributor

@mtsr mtsr commented Mar 12, 2021

This PR adds normal maps on top of PBR #1554. Once that PR lands, the changes should look simpler.

Edit: Turned out to be so little extra work, I added metallic/roughness texture too. And occlusion and emissive.

Comment on lines 33 to 39
for input_variable in module.enumerate_input_variables(None).unwrap() {
if input_variable.name == GL_VERTEX_INDEX
|| input_variable.name == GL_INSTANCE_INDEX
|| input_variable.name == GL_FRONT_FACING
{
continue;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I might misunderstand the point of this condition, if so I'd love to learn. But wouldn't a more long-term solution be (snipped some use changes for clarity):

diff --git a/crates/bevy_render/src/shader/shader_reflect.rs b/crates/bevy_render/src/shader/shader_reflect.rs
index 238da1ef..95761fed 100644
--- a/crates/bevy_render/src/shader/shader_reflect.rs
+++ b/crates/bevy_render/src/shader/shader_reflect.rs
@@ -31,8 +32,9 @@ impl ShaderLayout {
                 // obtain attribute descriptors from reflection
                 let mut vertex_attributes = Vec::new();
                 for input_variable in module.enumerate_input_variables(None).unwrap() {
-                    if input_variable.name == GL_VERTEX_INDEX
-                        || input_variable.name == GL_INSTANCE_INDEX
+                    if input_variable
+                        .decoration_flags
+                        .contains(ReflectDecorationFlags::BUILT_IN)
                     {
                         continue;
                     }

Copy link
Member

Choose a reason for hiding this comment

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

Oooh that does seem like a win / appears to do what we want it to.

@mtsr mtsr changed the title Add normal maps Add normal maps + metallic/roughness texture Mar 12, 2021
@mtsr
Copy link
Contributor Author

mtsr commented Mar 12, 2021

2021-03-12_11-37-48.mp4

@Ratysz Ratysz added C-Feature A new feature, making something new possible A-Rendering Drawing game state to the screen labels Mar 12, 2021
@mtsr
Copy link
Contributor Author

mtsr commented Mar 13, 2021

Rebased on pbr.

@mtsr
Copy link
Contributor Author

mtsr commented Mar 19, 2021

Rebased on the latest pbr.

@mtsr
Copy link
Contributor Author

mtsr commented Mar 20, 2021

And rebased onto main now that #1554 has been merged.

@jakobhellermann
Copy link
Contributor

Should the standard shapes like cube or sphere provide the tangent vertex attribute?

@@ -310,7 +380,9 @@ void main() {
vec3 diffuse_ambient = EnvBRDFApprox(diffuseColor, 1.0, NdotV);
vec3 specular_ambient = EnvBRDFApprox(F0, perceptual_roughness, NdotV);

output_color.rgb = light_accum + (diffuse_ambient + specular_ambient) * AmbientColor;
output_color.rgb = light_accum;
Copy link
Member

Choose a reason for hiding this comment

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

Tiny comments here would make it easier for folks new to rendering to figure out exactly what's happening here.

See the // tone_mapping comment just below.

examples/README.md Outdated Show resolved Hide resolved
examples/README.md Outdated Show resolved Hide resolved
@pkupper
Copy link
Contributor

pkupper commented Mar 24, 2021

Looking at the image below, there is clearly something wrong with the normal mapping in the example.
bevy_normalmapping
One possible mistake I found is that in the fragment shader the sample taken from the normal map is not converted into the -1.0 to 1.0 range before being transformed to world space. But that alone doesn't seem to fix the issue.

@alice-i-cecile
Copy link
Member

Transferring @pkupper's comment from Discord for discoverability:

Another thing that's missing is calculating normals and tangents if they are not present in the gltf. Tried a normal map sample gltf for easier debugging (https://github.com/KhronosGroup/glTF-Sample-Models) and it didn't load due to missing tangents. The gltf spec defines how both should be calculated (https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#meshes). But that is probably something for another PR.

@mtsr mtsr changed the title Add normal maps + metallic/roughness texture Add PBR textures Mar 25, 2021
@mtsr
Copy link
Contributor Author

mtsr commented Mar 25, 2021

Should the standard shapes like cube or sphere provide the tangent vertex attribute?

Possibly. Do we want to pull that into this PR? (neutral question) It has both pros and cons, I think, but I guess it does make sense to make this more fit for general use, rather than having to add that in a separate PR.

Transferring @pkupper's comment from Discord for discoverability:

Another thing that's missing is calculating normals and tangents if they are not present in the gltf.

This is actually pretty similar as above, except for externally loaded models. I guess it makes sense too.

Tried a normal map sample gltf for easier debugging (https://github.com/KhronosGroup/glTF-Sample-Models) and it didn't load due to missing tangents. The gltf spec defines how both should be calculated (https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#meshes). But that is probably something for another PR.

After @pkupper pointed out the other samples, I found there's actually a really good sample for testing this PR, which is NormalTangentMirror. I considered adding an example with these models, but it would add quite a bit of extra size, which I'm not sure is good. Maybe in a separate repo? Later, anyway.

@mtsr
Copy link
Contributor Author

mtsr commented Mar 25, 2021

We're hitting a minor snag, where GltfLoader passes off the textures to be loaded as a Vec<AssetPath>, without any texture information. Specifically, we cannot tell AssetServer to create Linear textures from these, rather than Srgb.

The effect of this is that Normal, Metallic, Roughness etc textures get gamma-incorrected during sampling, basically doing a Srgb->linear conversion on already linear textures.

I don't really know where to start fixing this. If somebody is able to help out, so I don't have to also dig into the Asset system, please do. Otherwise I may look into it this weekend or next week.

@jakobhellermann
Copy link
Contributor

I think a second PR is fine for adding the missing tangent vertex attributes.

@mtsr
Copy link
Contributor Author

mtsr commented Mar 26, 2021

Rebase on #1762 in progress...

bors bot pushed a commit that referenced this pull request Mar 26, 2021
Load textures from gltf as linear when needed.

This is for #1632, but can be done independently and won't have any visible impact before.

* during iteration over materials, register textures that need to be loaded as linear
* during iteration over textures
  * directly load bytes from external files instead of adding them as dependencies in the load context
  * configure the texture the same way for buffered and external textures
  * if the texture is linear rgb, set as linear rgb
@cart
Copy link
Member

cart commented Mar 26, 2021

bors r+

bors bot pushed a commit that referenced this pull request Mar 26, 2021
This PR adds normal maps on top of PBR #1554. Once that PR lands, the changes should look simpler.

Edit: Turned out to be so little extra work, I added metallic/roughness texture too. And occlusion and emissive.

Co-authored-by: Carter Anderson <[email protected]>
@bors bors bot changed the title Add PBR textures [Merged by Bors] - Add PBR textures Mar 26, 2021
@bors bors bot closed this Mar 26, 2021
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-Feature A new feature, making something new possible
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants