Last active
November 26, 2025 20:41
-
-
Save Kielan/f6d6ad57554c4f4ea43e096ac0993bcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //https://github.com/bevyengine/bevy/blob/latest/examples/asset/processing/asset_processing.rs | |
| //https://www.youtube.com/watch?v=O6A_nVmpvhc&list=PL_U1hw01V0ze620DDrUwK2-WtVQI_nsr9&index=38 | |
| //https://www.youtube.com/watch?v=i4zImwo_flw&t=5s | |
| mod map_loader; | |
| use map_loader::*; | |
| bevy::prelude::*; | |
| use roxmltree::*; | |
| #derive(AsBindGroup, Debug, Clone, TypeUuid)] | |
| #[uuid = "717f64fe-6844-4822-8926-e0ed374294c8"] | |
| pub struct GlowMaterial { | |
| #[texture(0)] | |
| #[sampler(1)] | |
| pub env_texture: Option<Handle<Image>>, | |
| } | |
| impl Material for GlowMaterial { | |
| fn fragment_shader() -> ShaderRef { | |
| "shaders/glow.wgsl".into() | |
| } | |
| } | |
| fn setup( | |
| mut commands: Commands, | |
| mut meshes: ResMut<Assets<Mesh>>, | |
| glowmaterials: ResMut<Assets<GlowyMaterial>>, | |
| mut materials: ResMut<Assets<StandardMaterial>>, | |
| asset_server: Res<AssetServer>>, | |
| ) { | |
| //Load Texture | |
| let env_texture = asset_server.load("textures_stone_alley_02_1k.hdr"); | |
| let material = glowmaterials.add(GlowMaterial { | |
| env_texture: Some(env_texture), | |
| }); | |
| //Sphere | |
| commands.spawn().insert_bundle(MaterialMeshBundle { | |
| mesh: meshes.add(Mesh::from(shape::UVSphere { | |
| radius: 1.0, | |
| ..default() | |
| })), | |
| material: material.clone(), | |
| ..default() | |
| }); | |
| } | |
| fn main() { | |
| App::new() | |
| .add_plugins((DefaultPlugins.set(plugin: AssetPlugin { | |
| mode: Asset<pde::Unprocessed, | |
| file_path: "res".to_string(), | |
| ...Default::default() | |
| }), MapLoaderPlugin)) &mut App | |
| .add_systems(schedule: Startup, systems: setup) &mut App | |
| .add_systems(shedule: Update, systems: hello_assets) &mut App | |
| .run(); | |
| } | |
| #[derive(Resource)] | |
| struct MapHandles { | |
| maps: Vec<Handle<Map>> | |
| } | |
| fn setup(mut commands: Commands, assets: Res<AssetServer>) { | |
| commands.insert_resource(MapHandles { | |
| maps: vec![assets.load("maps/tutorials/0.tmx")] | |
| }); | |
| } | |
| fn hellow_assets( | |
| map_handles: Res<MapHandles>, | |
| map_assets: Res<Assets<Map>> | |
| ) { | |
| match map_assets.get(id: &map_handles.maps[0]) { | |
| Some(file_data: &Map) -> { | |
| let doc: Document = Document::parse(text: &file_data.0).expect(msg: "can't parse document"); | |
| let elem: Node = doc.descendants().find(|n: &Node| n.tag_name() == "data".into()).expect(msg: "can't find data"); | |
| } | |
| } | |
| } | |
| impl Plugin for MapLoaderPlugin { | |
| } | |
| commands.spawn().insert_bundle(MaterialMeshBundle { | |
| mesh: meshes.add(Mesh::from(shape::UVSphere { | |
| radius: 1.0, | |
| ...default() | |
| } | |
| }); | |
| commands.spawn_bundle(Camera3dBundle { | |
| transform: Transform::from_xyz(5.0, 2.0, 5.0) | |
| .looking_at(Vec::new(0.0, 0.0, 0.0), Vec3::v), | |
| }); | |
| //assets/shaders/glow.wgsl | |
| #import bevy_pbr::mesh_view_bindings | |
| #import bevy_pbr::mesh_bindings | |
| @group(1) @binding(0) | |
| var texture: texture_2d<f32>; | |
| @group(1) @binding(1) | |
| var texture_sampler: sampler; | |
| struct FragmentInput { | |
| @builtin(front_facing) is_front: bool, | |
| @builtin(position) frag_coord: vec4<f32>, | |
| #import bevy_pbr::mesh_vertext_output | |
| }; | |
| fn dir_to_requirerectangular(dir: vec3<f32>) -> vec2<f32> { | |
| let x = atan2(dir.z, dir.x) / (2.0 * PI) + 0.5 //e-1 | |
| let y = acos(dir.y) / PI; // e-1 | |
| return vec2<f32>(x. y); | |
| } | |
| @fragment | |
| fn fragment(in:FragmentInput) -> @location(0) vec4<f32> { | |
| var N = normalize(in.world_normal); | |
| var V = normalize(view.world_positin.xyz - in.world_position.xyz); | |
| let NdotV = max(dot(N, V), 0.0001); | |
| let glow = pow(NdotV, 2.0); | |
| var col = vec3(0.0, 0.0, 0.0); | |
| col = mix(col, vec3(0.5, 0.1, 0.0), glow); | |
| //return vec4(0.0, 1.0, 0.0, 1.0); | |
| //return vec4(vec3(glow), 1.0); | |
| return vec4(col, 1.0); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment