Skip to content

Commit 02985c3

Browse files
authored
ui_material example webgl2 fix (#17852)
# Objective Fixes #17851 ## Solution Align the `slider` uniform to 16 bytes by making it a `vec4`. ## Testing Run the example using: ``` cargo run -p build-wasm-example -- --api webgl2 ui_material basic-http-server examples/wasm/ ```
1 parent 101fcaa commit 02985c3

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

assets/shaders/custom_ui_material.wgsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#import bevy_ui::ui_vertex_output::UiVertexOutput
33

44
@group(1) @binding(0) var<uniform> color: vec4<f32>;
5-
@group(1) @binding(1) var<uniform> slider: f32;
5+
@group(1) @binding(1) var<uniform> slider: vec4<f32>;
66
@group(1) @binding(2) var material_color_texture: texture_2d<f32>;
77
@group(1) @binding(3) var material_color_sampler: sampler;
88
@group(1) @binding(4) var<uniform> border_color: vec4<f32>;
@@ -50,7 +50,7 @@ fn fragment(in: UiVertexOutput) -> @location(0) vec4<f32> {
5050

5151
// sample the texture at this position if it's to the left of the slider value
5252
// otherwise return a fully transparent color
53-
if in.uv.x < slider {
53+
if in.uv.x < slider.x {
5454
let output_color = textureSample(material_color_texture, material_color_sampler, in.uv) * color;
5555
return output_color;
5656
} else {

examples/ui/ui_material.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn setup(
4444
},
4545
MaterialNode(ui_materials.add(CustomUiMaterial {
4646
color: LinearRgba::WHITE.to_f32_array().into(),
47-
slider: 0.5,
47+
slider: Vec4::splat(0.5),
4848
color_texture: asset_server.load("branding/banner.png"),
4949
border_color: LinearRgba::WHITE.to_f32_array().into(),
5050
})),
@@ -66,8 +66,9 @@ struct CustomUiMaterial {
6666
color: Vec4,
6767
/// Represents how much of the image is visible
6868
/// Goes from 0 to 1
69+
/// A `Vec4` is used here because Bevy with webgl2 requires that uniforms are 16-byte aligned but only the first component is read.
6970
#[uniform(1)]
70-
slider: f32,
71+
slider: Vec4,
7172
/// Image used to represent the slider
7273
#[texture(2)]
7374
#[sampler(3)]
@@ -97,7 +98,7 @@ fn animate(
9798
let new_color = Color::hsl((time.elapsed_secs() * 60.0) % 360.0, 1., 0.5);
9899
let border_color = Color::hsl((time.elapsed_secs() * 60.0) % 360.0, 0.75, 0.75);
99100
material.color = new_color.to_linear().to_vec4();
100-
material.slider =
101+
material.slider.x =
101102
((time.elapsed_secs() % (duration * 2.0)) - duration).abs() / duration;
102103
material.border_color = border_color.to_linear().to_vec4();
103104
}

0 commit comments

Comments
 (0)