Skip to content

Commit ca4272b

Browse files
committed
Fix loading.
1 parent a36d008 commit ca4272b

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

crates/processing_pyo3/assets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/Users/char/src/github.com/processing/libprocessing/assets/

crates/processing_pyo3/examples/custom_material.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def setup():
77
size(800, 600)
88
mode_3d()
99

10-
frag = Shader.load("assets/shaders/custom_material.wesl")
10+
frag = Shader.load("shaders/custom_material.wesl")
1111
mat = Material(fragment=frag, color=[1.0, 0.2, 0.4, 1.0])
1212

1313
def draw():

crates/processing_render/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,10 +1317,12 @@ pub fn shader_create(source: &str) -> error::Result<Entity> {
13171317

13181318
/// Load a shader from a file path.
13191319
pub fn shader_load(path: &str) -> error::Result<Entity> {
1320-
let source = std::fs::read_to_string(path).map_err(|e| {
1321-
error::ProcessingError::ShaderCompilationError(format!("Failed to read {path}: {e}"))
1322-
})?;
1323-
shader_create(&source)
1320+
let path = std::path::PathBuf::from(path);
1321+
app_mut(|app| {
1322+
app.world_mut()
1323+
.run_system_cached_with(material::custom::load_shader, path)
1324+
.unwrap()
1325+
})
13241326
}
13251327

13261328
pub fn shader_destroy(entity: Entity) -> error::Result<()> {

crates/processing_render/src/material/custom.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use bevy_naga_reflect::dynamic_shader::DynamicShader;
4242

4343
use bevy::shader::Shader as ShaderAsset;
4444

45+
use crate::config::{Config, ConfigKey};
4546
use crate::error::{ProcessingError, Result};
4647
use crate::material::MaterialValue;
4748
use crate::render::material::UntypedMaterial;
@@ -140,6 +141,56 @@ pub fn create_shader(
140141
.id())
141142
}
142143

144+
pub fn load_shader(In(path): In<std::path::PathBuf>, world: &mut World) -> Result<Entity> {
145+
use bevy::asset::{
146+
AssetPath, LoadState, handle_internal_asset_events,
147+
io::{AssetSourceId, embedded::GetAssetServer},
148+
};
149+
use bevy::ecs::system::RunSystemOnce;
150+
151+
let config = world.resource::<Config>();
152+
let asset_path: AssetPath = match config.get(ConfigKey::AssetRootPath) {
153+
Some(_) => {
154+
AssetPath::from_path_buf(path).with_source(AssetSourceId::from("assets_directory"))
155+
}
156+
None => AssetPath::from_path_buf(path),
157+
};
158+
159+
let handle: Handle<ShaderAsset> = world.get_asset_server().load(asset_path);
160+
161+
while let LoadState::Loading = world.get_asset_server().load_state(&handle) {
162+
world.run_system_once(handle_internal_asset_events).unwrap();
163+
}
164+
165+
let source = {
166+
let shader_assets = world.resource::<Assets<ShaderAsset>>();
167+
let shader = shader_assets
168+
.get(&handle)
169+
.ok_or(ProcessingError::ShaderNotFound)?;
170+
match &shader.source {
171+
bevy::shader::Source::Wesl(s) | bevy::shader::Source::Wgsl(s) => s.to_string(),
172+
_ => {
173+
return Err(ProcessingError::ShaderCompilationError(
174+
"Unsupported shader source format".to_string(),
175+
));
176+
}
177+
}
178+
};
179+
180+
let (compiled_wgsl, module) = compile_shader(&source)?;
181+
182+
let shader_handle = world
183+
.resource_mut::<Assets<ShaderAsset>>()
184+
.add(ShaderAsset::from_wgsl(compiled_wgsl, "custom_material"));
185+
186+
Ok(world
187+
.spawn(Shader {
188+
module,
189+
shader_handle,
190+
})
191+
.id())
192+
}
193+
143194
pub fn destroy_shader(In(entity): In<Entity>, mut commands: Commands) -> Result<()> {
144195
commands.entity(entity).despawn();
145196
Ok(())

examples/custom_material.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ fn sketch() -> error::Result<()> {
3131
transform_set_position(graphics, 100.0, 100.0, 300.0)?;
3232
transform_look_at(graphics, 0.0, 0.0, 0.0)?;
3333

34-
let frag_source = std::fs::read_to_string("assets/shaders/custom_material.wesl")
35-
.map_err(|e| error::ProcessingError::ShaderCompilationError(e.to_string()))?;
36-
let frag = shader_create(&frag_source)?;
34+
let frag = shader_load("shaders/custom_material.wesl")?;
3735
let mat = material_create_custom(None, Some(frag))?;
3836
material_set(
3937
mat,

0 commit comments

Comments
 (0)