-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move wgsl color operations from bevy_pbr to bevy_render (#13209)
# Objective `bevy_pbr/utils.wgsl` shader file contains mathematical constants and color conversion functions. Both of those should be accessible without enabling `bevy_pbr` feature. For example, tonemapping can be done in non pbr scenario, and it uses color conversion functions. Fixes #13207 ## Solution * Move mathematical constants (such as PI, E) from `bevy_pbr/src/render/utils.wgsl` into `bevy_render/src/maths.wgsl` * Move color conversion functions from `bevy_pbr/src/render/utils.wgsl` into new file `bevy_render/src/color_operations.wgsl` ## Testing Ran multiple examples, checked they are working: * tonemapping * color_grading * 3d_scene * animated_material * deferred_rendering * 3d_shapes * fog * irradiance_volumes * meshlet * parallax_mapping * pbr * reflection_probes * shadow_biases * 2d_gizmos * light_gizmos --- ## Changelog * Moved mathematical constants (such as PI, E) from `bevy_pbr/src/render/utils.wgsl` into `bevy_render/src/maths.wgsl` * Moved color conversion functions from `bevy_pbr/src/render/utils.wgsl` into new file `bevy_render/src/color_operations.wgsl` ## Migration Guide In user's shader code replace usage of mathematical constants from `bevy_pbr::utils` to the usage of the same constants from `bevy_render::maths`.
- Loading branch information
Showing
14 changed files
with
92 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
crates/bevy_core_pipeline/src/tonemapping/tonemapping_shared.wgsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#define_import_path bevy_render::color_operations | ||
|
||
#import bevy_render::maths::FRAC_PI_3 | ||
|
||
// Converts HSV to RGB. | ||
// | ||
// Input: H ∈ [0, 2π), S ∈ [0, 1], V ∈ [0, 1]. | ||
// Output: R ∈ [0, 1], G ∈ [0, 1], B ∈ [0, 1]. | ||
// | ||
// <https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative> | ||
fn hsv_to_rgb(hsv: vec3<f32>) -> vec3<f32> { | ||
let n = vec3(5.0, 3.0, 1.0); | ||
let k = (n + hsv.x / FRAC_PI_3) % 6.0; | ||
return hsv.z - hsv.z * hsv.y * max(vec3(0.0), min(k, min(4.0 - k, vec3(1.0)))); | ||
} | ||
|
||
// Converts RGB to HSV. | ||
// | ||
// Input: R ∈ [0, 1], G ∈ [0, 1], B ∈ [0, 1]. | ||
// Output: H ∈ [0, 2π), S ∈ [0, 1], V ∈ [0, 1]. | ||
// | ||
// <https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB> | ||
fn rgb_to_hsv(rgb: vec3<f32>) -> vec3<f32> { | ||
let x_max = max(rgb.r, max(rgb.g, rgb.b)); // i.e. V | ||
let x_min = min(rgb.r, min(rgb.g, rgb.b)); | ||
let c = x_max - x_min; // chroma | ||
|
||
var swizzle = vec3<f32>(0.0); | ||
if (x_max == rgb.r) { | ||
swizzle = vec3(rgb.gb, 0.0); | ||
} else if (x_max == rgb.g) { | ||
swizzle = vec3(rgb.br, 2.0); | ||
} else { | ||
swizzle = vec3(rgb.rg, 4.0); | ||
} | ||
|
||
let h = FRAC_PI_3 * (((swizzle.x - swizzle.y) / c + swizzle.z) % 6.0); | ||
|
||
// Avoid division by zero. | ||
var s = 0.0; | ||
if (x_max > 0.0) { | ||
s = c / x_max; | ||
} | ||
|
||
return vec3(h, s, x_max); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters