Fixes for WESL on Windows (#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.
This commit is contained in:
charlotte 2025-03-17 15:29:29 -07:00 committed by GitHub
parent 4b457cc2ce
commit 35bf9753e8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 63 additions and 33 deletions

View File

@ -1,4 +1,4 @@
import super::shaders::util::make_polka_dots;
import super::util::make_polka_dots;
struct VertexOutput {
@builtin(position) position: vec4<f32>,

View File

@ -1,29 +1,44 @@
fn make_polka_dots(pos: vec2<f32>, time: f32) -> vec4<f32> {
// Create repeating circles
let scaled_pos = pos * 6.0;
let cell = vec2<f32>(fract(scaled_pos.x), fract(scaled_pos.y));
let dist_from_center = distance(cell, vec2<f32>(0.5));
// Make dots alternate between pink and purple
var dist_from_center = distance(cell, vec2<f32>(0.5));
let is_even = (floor(scaled_pos.x) + floor(scaled_pos.y)) % 2.0;
var dot_color = vec3<f32>(0.0);
var is_dot = 0.0;
@if(!PARTY_MODE) {
let color1 = vec3<f32>(1.0, 0.4, 0.8); // pink
let color2 = vec3<f32>(0.6, 0.2, 1.0); // purple
dot_color = mix(color1, color2, is_even);
}
// Animate the colors in party mode
@if(PARTY_MODE) {
let color1 = vec3<f32>(1.0, 0.2, 0.2); // red
let color2 = vec3<f32>(0.2, 0.2, 1.0); // blue
let oscillation = (sin(time * 10.0) + 1.0) * 0.5;
let animated_color1 = mix(color1, color2, oscillation);
let animated_color2 = mix(color2, color1, oscillation);
is_dot = step(dist_from_center, 0.3);
} @else {
let grid_x = floor(scaled_pos.x);
let grid_y = floor(scaled_pos.y);
let wave_speed = 3.0;
let wave_phase = time * wave_speed;
let diagonal_pos = (grid_x + grid_y) * 0.5;
let wave_value = sin(diagonal_pos + wave_phase);
let wave_normalized = (wave_value + 1.0) * 0.5;
let color1 = vec3<f32>(1.0, 0.3, 0.7);
let color2 = vec3<f32>(0.5, 0.1, 1.0);
let intense_color1 = vec3<f32>(1.0, 0.1, 0.9);
let intense_color2 = vec3<f32>(0.8, 0.0, 1.0);
let animated_color1 = mix(color1, intense_color1, wave_normalized);
let animated_color2 = mix(color2, intense_color2, wave_normalized);
dot_color = mix(animated_color1, animated_color2, is_even);
let size_mod = 0.15 * wave_value;
dist_from_center = dist_from_center * (1.0 - size_mod);
// Animate whether something is a dot by position but also time
is_dot = step(dist_from_center, 0.3 + wave_normalized * 0.2);
}
// Draw the dot
let is_dot = step(dist_from_center, 0.3);
return vec4<f32>(dot_color * is_dot, is_dot);
}

View File

@ -113,7 +113,7 @@ tracing = { version = "0.1", default-features = false, features = ["std"] }
indexmap = { version = "2" }
fixedbitset = { version = "0.5" }
bitflags = "2"
wesl = { version = "0.1.0", optional = true }
wesl = { version = "0.1.2", optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# Omit the `glsl` feature in non-WebAssembly by default.

View File

@ -164,23 +164,34 @@ impl Shader {
match import_path {
ShaderImport::AssetPath(asset_path) => {
let asset_path = std::path::PathBuf::from(&asset_path);
// Resolve and normalize the path
let asset_path = asset_path.canonicalize().unwrap_or(asset_path);
// Strip the asset root
// Get the base path and canonicalize it to match the format of the asset path
let mut base_path = bevy_asset::io::file::FileAssetReader::get_base_path();
// TODO: integrate better with the asset system rather than hard coding this
base_path.push("assets");
let asset_path = asset_path
.strip_prefix(&base_path)
.unwrap_or_else(|_| &asset_path);
// Wesl paths are provided as absolute relative to the asset root
let asset_path = std::path::Path::new("/").join(asset_path);
// And with a striped file name
let asset_path = asset_path.with_extension("");
let asset_path = asset_path.to_str().unwrap_or_else(|| {
panic!("Failed to convert path to string: {:?}", asset_path)
});
let import_path = ShaderImport::AssetPath(asset_path.to_string());
let base_path = base_path.canonicalize().unwrap_or(base_path);
// Try to make the path relative to the base path
let relative_path = match asset_path.canonicalize() {
Ok(canonical_asset_path) => {
match canonical_asset_path.strip_prefix(&base_path) {
Ok(rel_path) => rel_path.to_path_buf(),
Err(_) => canonical_asset_path,
}
}
Err(_) => asset_path,
};
// Create the shader import path - always starting with "/"
let shader_path = std::path::Path::new("/").join(&relative_path);
// Convert to a string with forward slashes and without extension
let import_path_str = shader_path
.with_extension("")
.to_string_lossy()
.replace('\\', "/");
let import_path = ShaderImport::AssetPath(import_path_str.to_string());
Shader {
path,
imports,

View File

@ -70,16 +70,20 @@ fn setup(
fn update(
time: Res<Time>,
mut query: Query<&MeshMaterial3d<CustomMaterial>>,
mut query: Query<(&MeshMaterial3d<CustomMaterial>, &mut Transform)>,
mut materials: ResMut<Assets<CustomMaterial>>,
keys: Res<ButtonInput<KeyCode>>,
) {
for material in query.iter_mut() {
for (material, mut transform) in query.iter_mut() {
let material = materials.get_mut(material).unwrap();
material.time = time.elapsed_secs();
if keys.just_pressed(KeyCode::Space) {
material.party_mode = !material.party_mode;
}
if material.party_mode {
transform.rotate(Quat::from_rotation_y(0.005));
}
}
}