Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
#define_import_path bevy_pbr::pbr_functions
|
|
|
|
|
2022-10-26 20:13:59 +00:00
|
|
|
#ifdef TONEMAP_IN_SHADER
|
|
|
|
#import bevy_core_pipeline::tonemapping
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2022-09-28 05:54:11 +00:00
|
|
|
fn alpha_discard(material: StandardMaterial, output_color: vec4<f32>) -> vec4<f32>{
|
|
|
|
var color = output_color;
|
|
|
|
if ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_OPAQUE) != 0u) {
|
|
|
|
// NOTE: If rendering as opaque, alpha should be ignored so set to 1.0
|
|
|
|
color.a = 1.0;
|
|
|
|
} else if ((material.flags & STANDARD_MATERIAL_FLAGS_ALPHA_MODE_MASK) != 0u) {
|
|
|
|
if (color.a >= material.alpha_cutoff) {
|
|
|
|
// NOTE: If rendering as masked alpha and >= the cutoff, render as fully opaque
|
|
|
|
color.a = 1.0;
|
|
|
|
} else {
|
|
|
|
// NOTE: output_color.a < in.material.alpha_cutoff should not is not rendered
|
|
|
|
// NOTE: This and any other discards mean that early-z testing cannot be done!
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
bevy_pbr: Fix incorrect and unnecessary normal-mapping code (#5766)
# Objective
- Fixes #4019
- Fix lighting of double-sided materials when using a negative scale
- The FlightHelmet.gltf model's hose uses a double-sided material. Loading the model with a uniform scale of -1.0, and comparing against Blender, it was identified that negating the world-space tangent, bitangent, and interpolated normal produces incorrect lighting. Discussion with Morten Mikkelsen clarified that this is both incorrect and unnecessary.
## Solution
- Remove the code that negates the T, B, and N vectors (the interpolated world-space tangent, calculated world-space bitangent, and interpolated world-space normal) when seeing the back face of a double-sided material with negative scale.
- Negate the world normal for a double-sided back face only when not using normal mapping
### Before, on `main`, flipping T, B, and N
<img width="932" alt="Screenshot 2022-08-22 at 15 11 53" src="https://user-images.githubusercontent.com/302146/185965366-f776ff2c-cfa1-46d1-9c84-fdcb399c273c.png">
### After, on this PR
<img width="932" alt="Screenshot 2022-08-22 at 15 12 11" src="https://user-images.githubusercontent.com/302146/185965420-8be493e2-3b1a-4188-bd13-fd6b17a76fe7.png">
### Double-sided material without normal maps
https://user-images.githubusercontent.com/302146/185988113-44a384e7-0b55-4946-9b99-20f8c803ab7e.mp4
---
## Changelog
- Fixed: Lighting of normal-mapped, double-sided materials applied to models with negative scale
- Fixed: Lighting and shadowing of back faces with no normal-mapping and a double-sided material
## Migration Guide
`prepare_normal` from the `bevy_pbr::pbr_functions` shader import has been reworked.
Before:
```rust
pbr_input.world_normal = in.world_normal;
pbr_input.N = prepare_normal(
pbr_input.material.flags,
in.world_normal,
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
in.world_tangent,
#endif
#endif
in.uv,
in.is_front,
);
```
After:
```rust
pbr_input.world_normal = prepare_world_normal(
in.world_normal,
(material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u,
in.is_front,
);
pbr_input.N = apply_normal_mapping(
pbr_input.material.flags,
pbr_input.world_normal,
#ifdef VERTEX_TANGENTS
#ifdef STANDARDMATERIAL_NORMAL_MAP
in.world_tangent,
#endif
#endif
in.uv,
);
```
2022-11-03 20:37:32 +00:00
|
|
|
fn prepare_world_normal(
|
|
|
|
world_normal: vec3<f32>,
|
|
|
|
double_sided: bool,
|
|
|
|
is_front: bool,
|
|
|
|
) -> vec3<f32> {
|
|
|
|
var output: vec3<f32> = world_normal;
|
|
|
|
#ifndef VERTEX_TANGENTS
|
|
|
|
#ifndef STANDARDMATERIAL_NORMAL_MAP
|
|
|
|
// NOTE: When NOT using normal-mapping, if looking at the back face of a double-sided
|
|
|
|
// material, the normal needs to be inverted. This is a branchless version of that.
|
|
|
|
output = (f32(!double_sided || is_front) * 2.0 - 1.0) * output;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_normal_mapping(
|
2022-06-28 00:58:50 +00:00
|
|
|
standard_material_flags: u32,
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
world_normal: vec3<f32>,
|
|
|
|
#ifdef VERTEX_TANGENTS
|
|
|
|
#ifdef STANDARDMATERIAL_NORMAL_MAP
|
|
|
|
world_tangent: vec4<f32>,
|
|
|
|
#endif
|
|
|
|
#endif
|
2022-07-08 20:55:08 +00:00
|
|
|
#ifdef VERTEX_UVS
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
uv: vec2<f32>,
|
2022-07-08 20:55:08 +00:00
|
|
|
#endif
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
) -> vec3<f32> {
|
2022-08-18 21:54:40 +00:00
|
|
|
// NOTE: The mikktspace method of normal mapping explicitly requires that the world normal NOT
|
|
|
|
// be re-normalized in the fragment shader. This is primarily to match the way mikktspace
|
|
|
|
// bakes vertex tangents and normal maps so that this is the exact inverse. Blender, Unity,
|
|
|
|
// Unreal Engine, Godot, and more all use the mikktspace method. Do not change this code
|
|
|
|
// unless you really know what you are doing.
|
|
|
|
// http://www.mikktspace.com/
|
|
|
|
var N: vec3<f32> = world_normal;
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
|
|
|
|
#ifdef VERTEX_TANGENTS
|
|
|
|
#ifdef STANDARDMATERIAL_NORMAL_MAP
|
|
|
|
// NOTE: The mikktspace method of normal mapping explicitly requires that these NOT be
|
|
|
|
// normalized nor any Gram-Schmidt applied to ensure the vertex normal is orthogonal to the
|
|
|
|
// vertex tangent! Do not change this code unless you really know what you are doing.
|
|
|
|
// http://www.mikktspace.com/
|
|
|
|
var T: vec3<f32> = world_tangent.xyz;
|
|
|
|
var B: vec3<f32> = world_tangent.w * cross(N, T);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef VERTEX_TANGENTS
|
2022-07-08 20:55:08 +00:00
|
|
|
#ifdef VERTEX_UVS
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
#ifdef STANDARDMATERIAL_NORMAL_MAP
|
|
|
|
// Nt is the tangent-space normal.
|
2022-06-29 02:48:46 +00:00
|
|
|
var Nt = textureSample(normal_map_texture, normal_map_sampler, uv).rgb;
|
2022-06-28 00:58:50 +00:00
|
|
|
if ((standard_material_flags & STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP) != 0u) {
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
// Only use the xy components and derive z for 2-component normal maps.
|
2022-06-29 02:48:46 +00:00
|
|
|
Nt = vec3<f32>(Nt.rg * 2.0 - 1.0, 0.0);
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
Nt.z = sqrt(1.0 - Nt.x * Nt.x - Nt.y * Nt.y);
|
|
|
|
} else {
|
2022-06-29 02:48:46 +00:00
|
|
|
Nt = Nt * 2.0 - 1.0;
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
}
|
|
|
|
// Normal maps authored for DirectX require flipping the y component
|
2022-06-28 00:58:50 +00:00
|
|
|
if ((standard_material_flags & STANDARD_MATERIAL_FLAGS_FLIP_NORMAL_MAP_Y) != 0u) {
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
Nt.y = -Nt.y;
|
|
|
|
}
|
|
|
|
// NOTE: The mikktspace method of normal mapping applies maps the tangent-space normal from
|
|
|
|
// the normal map texture in this way to be an EXACT inverse of how the normal map baker
|
|
|
|
// calculates the normal maps so there is no error introduced. Do not change this code
|
|
|
|
// unless you really know what you are doing.
|
|
|
|
// http://www.mikktspace.com/
|
|
|
|
N = normalize(Nt.x * T + Nt.y * B + Nt.z * N);
|
|
|
|
#endif
|
2022-07-08 20:55:08 +00:00
|
|
|
#endif
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Correctly calculates the view vector depending on whether
|
|
|
|
// the projection is orthographic or perspective.
|
|
|
|
fn calculate_view(
|
|
|
|
world_position: vec4<f32>,
|
|
|
|
is_orthographic: bool,
|
|
|
|
) -> vec3<f32> {
|
|
|
|
var V: vec3<f32>;
|
|
|
|
if (is_orthographic) {
|
|
|
|
// Orthographic view vector
|
|
|
|
V = normalize(vec3<f32>(view.view_proj[0].z, view.view_proj[1].z, view.view_proj[2].z));
|
|
|
|
} else {
|
|
|
|
// Only valid for a perpective projection
|
|
|
|
V = normalize(view.world_position.xyz - world_position.xyz);
|
|
|
|
}
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PbrInput {
|
2022-07-14 21:17:16 +00:00
|
|
|
material: StandardMaterial,
|
|
|
|
occlusion: f32,
|
|
|
|
frag_coord: vec4<f32>,
|
|
|
|
world_position: vec4<f32>,
|
2022-06-28 00:58:50 +00:00
|
|
|
// Normalized world normal used for shadow mapping as normal-mapping is not used for shadow
|
|
|
|
// mapping
|
2022-07-14 21:17:16 +00:00
|
|
|
world_normal: vec3<f32>,
|
2022-06-28 00:58:50 +00:00
|
|
|
// Normalized normal-mapped world normal used for lighting
|
2022-07-14 21:17:16 +00:00
|
|
|
N: vec3<f32>,
|
2022-06-28 00:58:50 +00:00
|
|
|
// Normalized view vector in world space, pointing from the fragment world position toward the
|
|
|
|
// view world position
|
2022-07-14 21:17:16 +00:00
|
|
|
V: vec3<f32>,
|
|
|
|
is_orthographic: bool,
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
};
|
|
|
|
|
2022-06-28 00:58:50 +00:00
|
|
|
// Creates a PbrInput with default values
|
|
|
|
fn pbr_input_new() -> PbrInput {
|
|
|
|
var pbr_input: PbrInput;
|
|
|
|
|
|
|
|
pbr_input.material = standard_material_new();
|
|
|
|
pbr_input.occlusion = 1.0;
|
|
|
|
|
|
|
|
pbr_input.frag_coord = vec4<f32>(0.0, 0.0, 0.0, 1.0);
|
|
|
|
pbr_input.world_position = vec4<f32>(0.0, 0.0, 0.0, 1.0);
|
|
|
|
pbr_input.world_normal = vec3<f32>(0.0, 0.0, 1.0);
|
|
|
|
|
|
|
|
pbr_input.is_orthographic = false;
|
|
|
|
|
|
|
|
pbr_input.N = vec3<f32>(0.0, 0.0, 1.0);
|
|
|
|
pbr_input.V = vec3<f32>(1.0, 0.0, 0.0);
|
|
|
|
|
|
|
|
return pbr_input;
|
|
|
|
}
|
|
|
|
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
fn pbr(
|
|
|
|
in: PbrInput,
|
|
|
|
) -> vec4<f32> {
|
|
|
|
var output_color: vec4<f32> = in.material.base_color;
|
|
|
|
|
|
|
|
// TODO use .a for exposure compensation in HDR
|
|
|
|
let emissive = in.material.emissive;
|
|
|
|
|
|
|
|
// calculate non-linear roughness from linear perceptualRoughness
|
|
|
|
let metallic = in.material.metallic;
|
|
|
|
let perceptual_roughness = in.material.perceptual_roughness;
|
|
|
|
let roughness = perceptualRoughnessToRoughness(perceptual_roughness);
|
|
|
|
|
|
|
|
let occlusion = in.occlusion;
|
|
|
|
|
2022-09-28 05:54:11 +00:00
|
|
|
output_color = alpha_discard(in.material, output_color);
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
|
|
|
|
// Neubelt and Pettineo 2013, "Crafting a Next-gen Material Pipeline for The Order: 1886"
|
|
|
|
let NdotV = max(dot(in.N, in.V), 0.0001);
|
|
|
|
|
|
|
|
// Remapping [0,1] reflectance to F0
|
|
|
|
// See https://google.github.io/filament/Filament.html#materialsystem/parameterization/remapping
|
|
|
|
let reflectance = in.material.reflectance;
|
|
|
|
let F0 = 0.16 * reflectance * reflectance * (1.0 - metallic) + output_color.rgb * metallic;
|
|
|
|
|
|
|
|
// Diffuse strength inversely related to metallicity
|
|
|
|
let diffuse_color = output_color.rgb * (1.0 - metallic);
|
|
|
|
|
|
|
|
let R = reflect(-in.V, in.N);
|
|
|
|
|
|
|
|
// accumulate color
|
|
|
|
var light_accum: vec3<f32> = vec3<f32>(0.0);
|
|
|
|
|
|
|
|
let view_z = dot(vec4<f32>(
|
|
|
|
view.inverse_view[0].z,
|
|
|
|
view.inverse_view[1].z,
|
|
|
|
view.inverse_view[2].z,
|
|
|
|
view.inverse_view[3].z
|
|
|
|
), in.world_position);
|
|
|
|
let cluster_index = fragment_cluster_index(in.frag_coord.xy, view_z, in.is_orthographic);
|
2022-07-08 19:57:43 +00:00
|
|
|
let offset_and_counts = unpack_offset_and_counts(cluster_index);
|
|
|
|
|
|
|
|
// point lights
|
|
|
|
for (var i: u32 = offset_and_counts[0]; i < offset_and_counts[0] + offset_and_counts[1]; i = i + 1u) {
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
let light_id = get_light_id(i);
|
|
|
|
let light = point_lights.data[light_id];
|
|
|
|
var shadow: f32 = 1.0;
|
|
|
|
if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
|
|
|
|
&& (light.flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) {
|
|
|
|
shadow = fetch_point_shadow(light_id, in.world_position, in.world_normal);
|
|
|
|
}
|
|
|
|
let light_contrib = point_light(in.world_position.xyz, light, roughness, NdotV, in.N, in.V, R, F0, diffuse_color);
|
|
|
|
light_accum = light_accum + light_contrib * shadow;
|
|
|
|
}
|
|
|
|
|
2022-07-08 19:57:43 +00:00
|
|
|
// spot lights
|
|
|
|
for (var i: u32 = offset_and_counts[0] + offset_and_counts[1]; i < offset_and_counts[0] + offset_and_counts[1] + offset_and_counts[2]; i = i + 1u) {
|
|
|
|
let light_id = get_light_id(i);
|
|
|
|
let light = point_lights.data[light_id];
|
|
|
|
var shadow: f32 = 1.0;
|
|
|
|
if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
|
|
|
|
&& (light.flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) {
|
|
|
|
shadow = fetch_spot_shadow(light_id, in.world_position, in.world_normal);
|
|
|
|
}
|
|
|
|
let light_contrib = spot_light(in.world_position.xyz, light, roughness, NdotV, in.N, in.V, R, F0, diffuse_color);
|
|
|
|
light_accum = light_accum + light_contrib * shadow;
|
|
|
|
}
|
|
|
|
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
let n_directional_lights = lights.n_directional_lights;
|
|
|
|
for (var i: u32 = 0u; i < n_directional_lights; i = i + 1u) {
|
|
|
|
let light = lights.directional_lights[i];
|
|
|
|
var shadow: f32 = 1.0;
|
|
|
|
if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
|
|
|
|
&& (light.flags & DIRECTIONAL_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) {
|
|
|
|
shadow = fetch_directional_shadow(i, in.world_position, in.world_normal);
|
|
|
|
}
|
|
|
|
let light_contrib = directional_light(light, roughness, NdotV, in.N, in.V, R, F0, diffuse_color);
|
|
|
|
light_accum = light_accum + light_contrib * shadow;
|
|
|
|
}
|
|
|
|
|
|
|
|
let diffuse_ambient = EnvBRDFApprox(diffuse_color, 1.0, NdotV);
|
|
|
|
let specular_ambient = EnvBRDFApprox(F0, perceptual_roughness, NdotV);
|
|
|
|
|
|
|
|
output_color = vec4<f32>(
|
|
|
|
light_accum +
|
|
|
|
(diffuse_ambient + specular_ambient) * lights.ambient_color.rgb * occlusion +
|
|
|
|
emissive.rgb * output_color.a,
|
|
|
|
output_color.a);
|
|
|
|
|
|
|
|
output_color = cluster_debug_visualization(
|
|
|
|
output_color,
|
|
|
|
view_z,
|
|
|
|
in.is_orthographic,
|
2022-07-08 19:57:43 +00:00
|
|
|
offset_and_counts,
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
cluster_index,
|
|
|
|
);
|
|
|
|
|
2022-06-26 00:00:23 +00:00
|
|
|
return output_color;
|
|
|
|
}
|
|
|
|
|
2022-10-26 20:13:59 +00:00
|
|
|
#ifdef TONEMAP_IN_SHADER
|
2022-06-26 00:00:23 +00:00
|
|
|
fn tone_mapping(in: vec4<f32>) -> vec4<f32> {
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
// tone_mapping
|
2022-06-26 00:00:23 +00:00
|
|
|
return vec4<f32>(reinhard_luminance(in.rgb), in.a);
|
2022-08-18 21:54:40 +00:00
|
|
|
|
Callable PBR functions (#4939)
# Objective
- Builds on top of #4938
- Make clustered-forward PBR lighting/shadows functionality callable
- See #3969 for details
## Solution
- Add `PbrInput` struct type containing a `StandardMaterial`, occlusion, world_position, world_normal, and frag_coord
- Split functionality to calculate the unit view vector, and normal-mapped normal into `bevy_pbr::pbr_functions`
- Split high-level shading flow into `pbr(in: PbrInput, N: vec3<f32>, V: vec3<f32>, is_orthographic: bool)` function in `bevy_pbr::pbr_functions`
- Rework `pbr.wgsl` fragment stage entry point to make use of the new functions
- This has been benchmarked on an M1 Max using `many_cubes -- sphere`. `main` had a median frame time of 15.88ms, this PR 15.99ms, which is a 0.69% frame time increase, which is within noise in my opinion.
---
## Changelog
- Added: PBR shading code is now callable. Import `bevy_pbr::pbr_functions` and its dependencies, create a `PbrInput`, calculate the unit view and normal-mapped normal vectors and whether the projection is orthographic, and call `pbr()`!
2022-06-21 20:50:06 +00:00
|
|
|
// Gamma correction.
|
|
|
|
// Not needed with sRGB buffer
|
|
|
|
// output_color.rgb = pow(output_color.rgb, vec3(1.0 / 2.2));
|
|
|
|
}
|
2022-10-26 20:13:59 +00:00
|
|
|
#endif
|
2022-09-28 05:54:11 +00:00
|
|
|
|