Skip to content

Commit 35bf975

Browse files
authored
Fixes for WESL on Windows (bevyengine#18373)
# Objective WESL was broken on windows. ## Solution - Upgrade to `wesl_rs` 1.2. - Fix path handling on windows. - Improve example for khronos demo this week.
1 parent 4b457cc commit 35bf975

File tree

5 files changed

+63
-33
lines changed

5 files changed

+63
-33
lines changed

assets/shaders/custom_material.wesl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import super::shaders::util::make_polka_dots;
1+
import super::util::make_polka_dots;
22

33
struct VertexOutput {
44
@builtin(position) position: vec4<f32>,

assets/shaders/util.wesl

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
fn make_polka_dots(pos: vec2<f32>, time: f32) -> vec4<f32> {
2-
// Create repeating circles
32
let scaled_pos = pos * 6.0;
43
let cell = vec2<f32>(fract(scaled_pos.x), fract(scaled_pos.y));
5-
let dist_from_center = distance(cell, vec2<f32>(0.5));
6-
7-
// Make dots alternate between pink and purple
4+
var dist_from_center = distance(cell, vec2<f32>(0.5));
5+
86
let is_even = (floor(scaled_pos.x) + floor(scaled_pos.y)) % 2.0;
97

108
var dot_color = vec3<f32>(0.0);
9+
var is_dot = 0.0;
10+
1111
@if(!PARTY_MODE) {
1212
let color1 = vec3<f32>(1.0, 0.4, 0.8); // pink
1313
let color2 = vec3<f32>(0.6, 0.2, 1.0); // purple
1414
dot_color = mix(color1, color2, is_even);
15-
}
16-
// Animate the colors in party mode
17-
@if(PARTY_MODE) {
18-
let color1 = vec3<f32>(1.0, 0.2, 0.2); // red
19-
let color2 = vec3<f32>(0.2, 0.2, 1.0); // blue
20-
let oscillation = (sin(time * 10.0) + 1.0) * 0.5;
21-
let animated_color1 = mix(color1, color2, oscillation);
22-
let animated_color2 = mix(color2, color1, oscillation);
15+
is_dot = step(dist_from_center, 0.3);
16+
} @else {
17+
let grid_x = floor(scaled_pos.x);
18+
let grid_y = floor(scaled_pos.y);
19+
let wave_speed = 3.0;
20+
let wave_phase = time * wave_speed;
21+
22+
let diagonal_pos = (grid_x + grid_y) * 0.5;
23+
let wave_value = sin(diagonal_pos + wave_phase);
24+
25+
let wave_normalized = (wave_value + 1.0) * 0.5;
26+
27+
let color1 = vec3<f32>(1.0, 0.3, 0.7);
28+
let color2 = vec3<f32>(0.5, 0.1, 1.0);
29+
let intense_color1 = vec3<f32>(1.0, 0.1, 0.9);
30+
let intense_color2 = vec3<f32>(0.8, 0.0, 1.0);
31+
32+
let animated_color1 = mix(color1, intense_color1, wave_normalized);
33+
let animated_color2 = mix(color2, intense_color2, wave_normalized);
34+
2335
dot_color = mix(animated_color1, animated_color2, is_even);
36+
37+
let size_mod = 0.15 * wave_value;
38+
dist_from_center = dist_from_center * (1.0 - size_mod);
39+
// Animate whether something is a dot by position but also time
40+
is_dot = step(dist_from_center, 0.3 + wave_normalized * 0.2);
2441
}
2542

26-
// Draw the dot
27-
let is_dot = step(dist_from_center, 0.3);
2843
return vec4<f32>(dot_color * is_dot, is_dot);
2944
}

crates/bevy_render/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ tracing = { version = "0.1", default-features = false, features = ["std"] }
113113
indexmap = { version = "2" }
114114
fixedbitset = { version = "0.5" }
115115
bitflags = "2"
116-
wesl = { version = "0.1.0", optional = true }
116+
wesl = { version = "0.1.2", optional = true }
117117

118118
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
119119
# Omit the `glsl` feature in non-WebAssembly by default.

crates/bevy_render/src/render_resource/shader.rs

+26-15
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,34 @@ impl Shader {
164164
match import_path {
165165
ShaderImport::AssetPath(asset_path) => {
166166
let asset_path = std::path::PathBuf::from(&asset_path);
167-
// Resolve and normalize the path
168-
let asset_path = asset_path.canonicalize().unwrap_or(asset_path);
169-
// Strip the asset root
167+
168+
// Get the base path and canonicalize it to match the format of the asset path
170169
let mut base_path = bevy_asset::io::file::FileAssetReader::get_base_path();
171-
// TODO: integrate better with the asset system rather than hard coding this
172170
base_path.push("assets");
173-
let asset_path = asset_path
174-
.strip_prefix(&base_path)
175-
.unwrap_or_else(|_| &asset_path);
176-
// Wesl paths are provided as absolute relative to the asset root
177-
let asset_path = std::path::Path::new("/").join(asset_path);
178-
// And with a striped file name
179-
let asset_path = asset_path.with_extension("");
180-
let asset_path = asset_path.to_str().unwrap_or_else(|| {
181-
panic!("Failed to convert path to string: {:?}", asset_path)
182-
});
183-
let import_path = ShaderImport::AssetPath(asset_path.to_string());
171+
let base_path = base_path.canonicalize().unwrap_or(base_path);
172+
173+
// Try to make the path relative to the base path
174+
let relative_path = match asset_path.canonicalize() {
175+
Ok(canonical_asset_path) => {
176+
match canonical_asset_path.strip_prefix(&base_path) {
177+
Ok(rel_path) => rel_path.to_path_buf(),
178+
Err(_) => canonical_asset_path,
179+
}
180+
}
181+
Err(_) => asset_path,
182+
};
183+
184+
// Create the shader import path - always starting with "/"
185+
let shader_path = std::path::Path::new("/").join(&relative_path);
186+
187+
// Convert to a string with forward slashes and without extension
188+
let import_path_str = shader_path
189+
.with_extension("")
190+
.to_string_lossy()
191+
.replace('\\', "/");
192+
193+
let import_path = ShaderImport::AssetPath(import_path_str.to_string());
194+
184195
Shader {
185196
path,
186197
imports,

examples/shader/shader_material_wesl.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,20 @@ fn setup(
7070

7171
fn update(
7272
time: Res<Time>,
73-
mut query: Query<&MeshMaterial3d<CustomMaterial>>,
73+
mut query: Query<(&MeshMaterial3d<CustomMaterial>, &mut Transform)>,
7474
mut materials: ResMut<Assets<CustomMaterial>>,
7575
keys: Res<ButtonInput<KeyCode>>,
7676
) {
77-
for material in query.iter_mut() {
77+
for (material, mut transform) in query.iter_mut() {
7878
let material = materials.get_mut(material).unwrap();
7979
material.time = time.elapsed_secs();
8080
if keys.just_pressed(KeyCode::Space) {
8181
material.party_mode = !material.party_mode;
8282
}
83+
84+
if material.party_mode {
85+
transform.rotate(Quat::from_rotation_y(0.005));
86+
}
8387
}
8488
}
8589

0 commit comments

Comments
 (0)