Separate out PBR lighting, shadows, clustered forward, and utils from pbr.wgsl (#4938)
# Objective
- Builds on top of #4901
- Separate out PBR lighting, shadows, clustered forward, and utils from `pbr.wgsl` as part of making the PBR code more reusable and extensible.
- See #3969 for details.
## Solution
- Add `bevy_pbr::utils`, `bevy_pbr::clustered_forward`, `bevy_pbr::lighting`, `bevy_pbr::shadows` shader imports exposing many shader functions for external use
- Split `PI`, `saturate()`, `hsv2rgb()`, and `random1D()` into `bevy_pbr::utils`
- Split clustered-forward-specific functions into `bevy_pbr::clustered_forward`, including moving the debug visualization code into a `cluster_debug_visualization()` function in that import
- Split PBR lighting functions into `bevy_pbr::lighting`
- Split shadow functions into `bevy_pbr::shadows`
---
## Changelog
- Added: `bevy_pbr::utils`, `bevy_pbr::clustered_forward`, `bevy_pbr::lighting`, `bevy_pbr::shadows` shader imports exposing many shader functions for external use
- Split `PI`, `saturate()`, `hsv2rgb()`, and `random1D()` into `bevy_pbr::utils`
- Split clustered-forward-specific functions into `bevy_pbr::clustered_forward`, including moving the debug visualization code into a `cluster_debug_visualization()` function in that import
- Split PBR lighting functions into `bevy_pbr::lighting`
- Split shadow functions into `bevy_pbr::shadows`
2022-06-14 00:58:30 +00:00
|
|
|
#define_import_path bevy_pbr::utils
|
|
|
|
|
|
|
|
let PI: f32 = 3.141592653589793;
|
|
|
|
|
|
|
|
fn hsv2rgb(hue: f32, saturation: f32, value: f32) -> vec3<f32> {
|
|
|
|
let rgb = clamp(
|
|
|
|
abs(
|
|
|
|
((hue * 6.0 + vec3<f32>(0.0, 4.0, 2.0)) % 6.0) - 3.0
|
|
|
|
) - 1.0,
|
|
|
|
vec3<f32>(0.0),
|
|
|
|
vec3<f32>(1.0)
|
|
|
|
);
|
|
|
|
|
|
|
|
return value * mix( vec3<f32>(1.0), rgb, vec3<f32>(saturation));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn random1D(s: f32) -> f32 {
|
|
|
|
return fract(sin(s * 12.9898) * 43758.5453123);
|
|
|
|
}
|
2022-09-15 21:58:14 +00:00
|
|
|
|
|
|
|
// returns the (0-1, 0-1) position within the given viewport for the current buffer coords .
|
|
|
|
// buffer coords can be obtained from `@builtin(position).xy`.
|
|
|
|
// the view uniform struct contains the current camera viewport in `view.viewport`.
|
|
|
|
// topleft = 0,0
|
|
|
|
fn coords_to_viewport_uv(position: vec2<f32>, viewport: vec4<f32>) -> vec2<f32> {
|
|
|
|
return (position - viewport.xy) / viewport.zw;
|
|
|
|
}
|