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] - Array texture example #5077

Closed
wants to merge 5 commits into from

Conversation

superdump
Copy link
Contributor

@superdump superdump commented Jun 23, 2022

Objective

  • Make the reusable PBR shading functionality a little more reusable
    • Add constructor functions for StandardMaterial and PbrInput structs to populate them with default values
    • Document unclear PbrInput members
  • Demonstrate how to reuse the bevy PBR shading functionality
  • The final important piece from Reusable PBR shader types/bindings/functions #3969 as the initial shot at making the PBR shader code reusable in custom materials

Solution

  • Add back and rework the 'old' array_texture example from pre-0.6.
  • Create a custom shader material
    • Use a single array texture binding and sampler for the material bind group
    • Use a shader that calls pbr() from the bevy_pbr::pbr_functions import
  • Spawn a row of cubes using the custom material
  • In the shader, select the array texture layer to sample by using the world position x coordinate modulo the number of array texture layers

Screenshot 2022-06-23 at 12 28 05

@james7132 james7132 added A-Rendering Drawing game state to the screen C-Examples An addition or correction to our examples labels Jun 23, 2022
@superdump superdump added this to the Bevy 0.8 milestone Jun 23, 2022

// Create a new array texture asset from the loaded texture.
let array_layers = 4;
image.reinterpret_stacked_2d_as_array(array_layers);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a shame this can't be done up-front when loading the image.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I guess asset pipeline and metadata stuff will sort that out.

Comment on lines +91 to +98
for x in -5..=5 {
commands.spawn_bundle(MaterialMeshBundle {
mesh: mesh_handle.clone(),
material: material_handle.clone(),
transform: Transform::from_xyz(x as f32 + 0.5, 0.0, 0.0),
..Default::default()
});
}
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'm spawning these here so that they only exist when the asset has loaded. I think that was because otherwise a non-array-texture fallback image is attempted to be used for the binding due to how mesh_pipeline.get_image_texture() works.

@superdump superdump force-pushed the array-texture-example branch from 3a2d2f2 to 26f8d1d Compare June 25, 2022 22:18

// Prepare a 'processed' StandardMaterial by sampling all textures to resolve
// the material members
var pbr_input: PbrInput = pbr_input_new();
Copy link
Member

@cart cart Jun 28, 2022

Choose a reason for hiding this comment

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

Random thought (not necessary for this pr): maybe we should have a pbr_input_default() function that sets as much as possible "automatically". Hard to do for FragmentInput, given that it is defined per-shader. But we could at least auto-calculate is_orthographic. Maybe we could have a standardized struct for "normal" fragment inputs, which users pass in to pbr_input_default(standard_frag_input_here)? Again, definitely not something to do in this pr, but it feels like theres room for ux improvements here.

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 agree and want to iterate some more on this when I have time.

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.

Looks good to me, barring one comment. Merging this before #5053 will break it, but adapting this to a manual AsBindGroup impl should be straightforward / im happy to pick that up.

}

impl Material for ArrayTextureMaterial {
// fn vertex_shader(asset_server: &AssetServer) -> Option<Handle<Shader>> {
Copy link
Member

Choose a reason for hiding this comment

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

We should remove this comment. I don't like including "unnecessary" commented out code in examples.

@cart
Copy link
Member

cart commented Jun 28, 2022

bors r+

bors bot pushed a commit that referenced this pull request Jun 28, 2022
# Objective

- Make the reusable PBR shading functionality a little more reusable
  - Add constructor functions for `StandardMaterial` and `PbrInput` structs to populate them with default values
  - Document unclear `PbrInput` members
- Demonstrate how to reuse the bevy PBR shading functionality
- The final important piece from #3969 as the initial shot at making the PBR shader code reusable in custom materials

## Solution

- Add back and rework the 'old' `array_texture` example from pre-0.6.
- Create a custom shader material
  - Use a single array texture binding and sampler for the material bind group
  - Use a shader that calls `pbr()` from the `bevy_pbr::pbr_functions` import
- Spawn a row of cubes using the custom material
- In the shader, select the array texture layer to sample by using the world position x coordinate modulo the number of array texture layers

<img width="1392" alt="Screenshot 2022-06-23 at 12 28 05" src="https://user-images.githubusercontent.com/302146/175278593-2296f519-f577-4ece-81c0-d842283784a1.png">

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
@bors bors bot changed the title Array texture example [Merged by Bors] - Array texture example Jun 28, 2022
@bors bors bot closed this Jun 28, 2022
inodentry pushed a commit to IyesGames/bevy that referenced this pull request Aug 8, 2022
# Objective

- Make the reusable PBR shading functionality a little more reusable
  - Add constructor functions for `StandardMaterial` and `PbrInput` structs to populate them with default values
  - Document unclear `PbrInput` members
- Demonstrate how to reuse the bevy PBR shading functionality
- The final important piece from bevyengine#3969 as the initial shot at making the PBR shader code reusable in custom materials

## Solution

- Add back and rework the 'old' `array_texture` example from pre-0.6.
- Create a custom shader material
  - Use a single array texture binding and sampler for the material bind group
  - Use a shader that calls `pbr()` from the `bevy_pbr::pbr_functions` import
- Spawn a row of cubes using the custom material
- In the shader, select the array texture layer to sample by using the world position x coordinate modulo the number of array texture layers

<img width="1392" alt="Screenshot 2022-06-23 at 12 28 05" src="https://user-images.githubusercontent.com/302146/175278593-2296f519-f577-4ece-81c0-d842283784a1.png">

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
james7132 pushed a commit to james7132/bevy that referenced this pull request Oct 28, 2022
# Objective

- Make the reusable PBR shading functionality a little more reusable
  - Add constructor functions for `StandardMaterial` and `PbrInput` structs to populate them with default values
  - Document unclear `PbrInput` members
- Demonstrate how to reuse the bevy PBR shading functionality
- The final important piece from bevyengine#3969 as the initial shot at making the PBR shader code reusable in custom materials

## Solution

- Add back and rework the 'old' `array_texture` example from pre-0.6.
- Create a custom shader material
  - Use a single array texture binding and sampler for the material bind group
  - Use a shader that calls `pbr()` from the `bevy_pbr::pbr_functions` import
- Spawn a row of cubes using the custom material
- In the shader, select the array texture layer to sample by using the world position x coordinate modulo the number of array texture layers

<img width="1392" alt="Screenshot 2022-06-23 at 12 28 05" src="https://user-images.githubusercontent.com/302146/175278593-2296f519-f577-4ece-81c0-d842283784a1.png">

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

- Make the reusable PBR shading functionality a little more reusable
  - Add constructor functions for `StandardMaterial` and `PbrInput` structs to populate them with default values
  - Document unclear `PbrInput` members
- Demonstrate how to reuse the bevy PBR shading functionality
- The final important piece from bevyengine#3969 as the initial shot at making the PBR shader code reusable in custom materials

## Solution

- Add back and rework the 'old' `array_texture` example from pre-0.6.
- Create a custom shader material
  - Use a single array texture binding and sampler for the material bind group
  - Use a shader that calls `pbr()` from the `bevy_pbr::pbr_functions` import
- Spawn a row of cubes using the custom material
- In the shader, select the array texture layer to sample by using the world position x coordinate modulo the number of array texture layers

<img width="1392" alt="Screenshot 2022-06-23 at 12 28 05" src="https://user-images.githubusercontent.com/302146/175278593-2296f519-f577-4ece-81c0-d842283784a1.png">

Co-authored-by: Carter Anderson <mcanders1@gmail.com>
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-Examples An addition or correction to our examples
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants