You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#[derive(Debug,Clone,TypeUuid,AsBindGroup)]#[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"]pubstructCustomMaterial{#[uniform(0)]color:Color,#[texture(1)]#[sampler(2)]color_texture:Option<Handle<Image>>,alpha_mode:AlphaMode,}/// The Material trait is very configurable, but comes with sensible defaults for all methods./// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!implMaterialforCustomMaterial{fnfragment_shader() -> ShaderRef{"shaders/custom_material.wgsl".into()}fnalpha_mode(&self) -> AlphaMode{self.alpha_mode}}
When i want to pass a config of shader asset url into Material, but i dont know how to do it.
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin,LogDiagnosticsPlugin},
log::LogPlugin,
prelude::*,
reflect::TypeUuid,
render::render_resource::{AsBindGroup,ShaderRef},
window::PresentMode,};use wasm_bindgen::prelude::*;#[derive(Resource,Default)]pubstructAppConfig{pubasset_url:String,}implAppConfig{pubfnnew(url:String) -> Self{AppConfig{asset_url: url }}}#[wasm_bindgen]pubstructVWindow{width:f32,height:f32,title:String,canvas:String,}#[wasm_bindgen]implVWindow{pubfnnew(width:f32,height:f32,title:String,canvas:String) -> Self{Self{
width,
height,
title,
canvas,}}}#[wasm_bindgen]pubfninit_bevy(win:VWindow,url:String){init(win,AppConfig::new(url));}pubfninit(win:VWindow,config:AppConfig){letVWindow{
width,
height,
title,
canvas,} = win;App::new().add_plugins(DefaultPlugins.set(LogPlugin{filter:"info,wgpu_core=warn,wgpu_hal=warn,vgr=debug".into(),level: bevy::log::Level::DEBUG,})).add_plugins(DefaultPlugins.set(WindowPlugin{window:WindowDescriptor{
width,
height,
title,canvas:Some(canvas),present_mode:PresentMode::Fifo,
..Default::default()},
..Default::default()})).add_plugins(DefaultPlugins).add_plugin(LogDiagnosticsPlugin::default()).add_plugin(FrameTimeDiagnosticsPlugin::default()).add_plugin(MaterialPlugin::<CustomMaterial>::default()).add_startup_system(move |commands:Commands,meshes:ResMut<Assets<Mesh>>,materials:ResMut<Assets<CustomMaterial>>,asset_server:Res<AssetServer>| {setup(commands, meshes, materials, asset_server,&config)},).run();}/// set up a simple 3D scenefnsetup(mutcommands:Commands,mutmeshes:ResMut<Assets<Mesh>>,mutmaterials:ResMut<Assets<CustomMaterial>>,asset_server:Res<AssetServer>,config:&AppConfig,){// cube
commands.spawn(MaterialMeshBundle{mesh: meshes.add(Mesh::from(shape::Cube{size:1.0})),transform:Transform::from_xyz(0.0,0.5,0.0),material: materials.add(CustomMaterial{color:Color::GREEN,color_texture:Some(asset_server.load("branding/icon.png")),alpha_mode:AlphaMode::Blend,}),
..default()});// camera
commands.spawn(Camera3dBundle{transform:Transform::from_xyz(-2.0,2.5,5.0).looking_at(Vec3::ZERO,Vec3::Y),
..default()});}// This is the struct that will be passed to your shader#[derive(Debug,Clone,TypeUuid,AsBindGroup)]#[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"]pubstructCustomMaterial{#[uniform(0)]color:Color,#[texture(1)]#[sampler(2)]color_texture:Option<Handle<Image>>,alpha_mode:AlphaMode,}/// The Material trait is very configurable, but comes with sensible defaults for all methods./// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!implMaterialforCustomMaterial{fnfragment_shader() -> ShaderRef{"shaders/custom_material.wgsl".into()}fnalpha_mode(&self) -> AlphaMode{self.alpha_mode}}
I want to use config.asset_url instead shaders/custom_material.wgsl, but the fragment_shader of Material has no param.
how to do it?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
This is
Material
example codeWhen i want to pass a config of shader asset url into Material, but i dont know how to do it.
I want to use config.asset_url instead
shaders/custom_material.wgsl
, but the fragment_shader ofMaterial
has no param.how to do it?
Beta Was this translation helpful? Give feedback.
All reactions