add png image loading and enable transparency
This commit is contained in:
		
							parent
							
								
									9d71d0d3fc
								
							
						
					
					
						commit
						baac7ff296
					
				| @ -22,6 +22,7 @@ erased-serde = "0.3" | ||||
| type-uuid = "0.1" | ||||
| shaderc = "0.6" | ||||
| libloading = "0.5.2" | ||||
| png = "0.16.0" | ||||
| # rspirv = { git = "https://github.com/gfx-rs/rspirv.git", rev = "baa469eae2932271174593eb066894d7a7a38439" } | ||||
| spirv-reflect = "0.2.3" | ||||
| bevy_derive = { path = "bevy_derive" } | ||||
|  | ||||
| @ -12,7 +12,7 @@ fn setup(world: &mut World) { | ||||
| 
 | ||||
|     let texture_handle = { | ||||
|         let mut texture_storage = world.resources.get_mut::<AssetStorage<Texture>>().unwrap(); | ||||
|         let texture = Texture::load(TextureType::Data(asset::create_texels(256), 256, 256)); | ||||
|         let texture = Texture::load(TextureType::Png(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/temp_bevy_logo.png").to_string())); | ||||
|         texture_storage.add(texture) | ||||
|     }; | ||||
| 
 | ||||
| @ -29,7 +29,7 @@ fn setup(world: &mut World) { | ||||
|         }) | ||||
|         // light
 | ||||
|         .add_archetype(LightEntity { | ||||
|             translation: Translation::new(4.0, -4.0, 5.0), | ||||
|             translation: Translation::new(4.0, 4.0, 5.0), | ||||
|             ..Default::default() | ||||
|         }) | ||||
|         // camera
 | ||||
|  | ||||
| @ -1,7 +1,9 @@ | ||||
| use crate::{render::render_graph::{TextureDimension, TextureDescriptor}, asset::Asset}; | ||||
| use std::fs::File; | ||||
| 
 | ||||
| pub enum TextureType { | ||||
|     Data(Vec<u8>, usize, usize), | ||||
|     Png(String) // TODO: please rethink this
 | ||||
| } | ||||
| 
 | ||||
| pub struct Texture { | ||||
| @ -14,6 +16,13 @@ impl Asset<TextureType> for Texture { | ||||
|     fn load(descriptor: TextureType) -> Self { | ||||
|         let (data, width, height) = match descriptor { | ||||
|             TextureType::Data(data, width, height) => (data.clone(), width, height), | ||||
|             TextureType::Png(path) => { | ||||
|                 let decoder = png::Decoder::new(File::open(&path).unwrap()); | ||||
|                 let (info, mut reader) = decoder.read_info().unwrap(); | ||||
|                 let mut buf = vec![0; info.buffer_size()]; | ||||
|                 reader.next_frame(&mut buf).unwrap(); | ||||
|                 (buf, info.width as usize, info.height as usize) | ||||
|             } | ||||
|         }; | ||||
| 
 | ||||
|         Texture { data, width, height } | ||||
|  | ||||
| @ -48,8 +48,16 @@ impl ForwardPipelineBuilder for RenderGraphBuilder { | ||||
|             }) | ||||
|             .add_color_state(wgpu::ColorStateDescriptor { | ||||
|                 format: wgpu::TextureFormat::Bgra8UnormSrgb, | ||||
|                 color_blend: wgpu::BlendDescriptor::REPLACE, | ||||
|                 alpha_blend: wgpu::BlendDescriptor::REPLACE, | ||||
|                 color_blend: wgpu::BlendDescriptor { | ||||
|                     src_factor: wgpu::BlendFactor::SrcAlpha, | ||||
|                     dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, | ||||
|                     operation: wgpu::BlendOperation::Add, | ||||
|                 }, | ||||
|                 alpha_blend: wgpu::BlendDescriptor { | ||||
|                     src_factor: wgpu::BlendFactor::One, | ||||
|                     dst_factor: wgpu::BlendFactor::One, | ||||
|                     operation: wgpu::BlendOperation::Add, | ||||
|                 }, | ||||
|                 write_mask: wgpu::ColorWrite::ALL, | ||||
|             }) | ||||
|             .add_vertex_buffer_descriptor(Vertex::get_vertex_buffer_descriptor()) | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Carter Anderson
						Carter Anderson