From 49513bfbe96f375c7c752397e177369c85ee1ba7 Mon Sep 17 00:00:00 2001 From: Arkitu <85173315+Arkitu@users.noreply.github.com> Date: Sat, 28 Sep 2024 11:54:20 +0200 Subject: [PATCH] transparency for textures --- src/graphics.rs | 49 +++++++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index 515669c..ec748b7 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -2,9 +2,9 @@ use std::sync::Arc; use bytemuck::{Pod, Zeroable}; use wgpu::{include_wgsl, util::DeviceExt, BindGroup, Buffer, Device, Queue, RenderPipeline, Surface, SurfaceConfiguration, VertexBufferLayout}; -use winit::{event::{Event, WindowEvent}, window::Window}; +use winit::{event::WindowEvent, window::Window}; -use crate::{state::State, App}; +use crate::state::State; mod texture; use texture::{Texture, TEXTURE_DIMENSIONS}; @@ -14,14 +14,17 @@ use texture::{Texture, TEXTURE_DIMENSIONS}; pub struct Vertex { pub pos: [f32; 2], /// Rgba color by default but if texture flag is not set. - /// Else the first 2 f32 are texture coordinates and the 2 last are not important + /// Else the first 2 f32 are texture coordinates and the 2 last are not used pub color: [f32; 4], /// Each bit is used as a flag : + /// /// 1: Scaled and moved according to camera + /// /// 2: Grayscale + /// /// 4: Texture instead of color /// - /// For exemple 0b01 corresponds to Scaled and 0b11 to Scaled and Grayscale + /// For example 0b001 corresponds to Scaled and 0b011 to Scaled and Grayscale pub effect: u32, } impl Vertex { @@ -205,7 +208,22 @@ impl<'a> Graphics<'a> { module: &shader, entry_point: "fs_main", compilation_options: Default::default(), - targets: &[Some(swapchain_format.into())], + targets: &[Some(wgpu::ColorTargetState { + format: swapchain_format, + blend: Some(wgpu::BlendState { + color: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::SrcAlpha, + dst_factor: wgpu::BlendFactor::OneMinusSrcAlpha, + operation: wgpu::BlendOperation::Add + }, + alpha: wgpu::BlendComponent { + src_factor: wgpu::BlendFactor::One, + dst_factor: wgpu::BlendFactor::One, + operation: wgpu::BlendOperation::Add + } + }), + write_mask: wgpu::ColorWrites::ALL, + })], }), primitive: wgpu::PrimitiveState::default(), depth_stencil: None, @@ -265,27 +283,6 @@ impl<'a> Graphics<'a> { _ => {} } } - pub fn event(&mut self, event: &Event<()>, window: &Window) { - match event { - Event::WindowEvent { - event: WindowEvent::Resized(new_size), - .. - } => { - // Reconfigure the surface with the new size - self.surface_config.width = new_size.width; - self.surface_config.height = new_size.height; - self.surface.configure(&self.device, &self.surface_config); - // On macos the window needs to be redrawn manually after resizing - window.request_redraw(); - }, - Event::AboutToWait => { - // RedrawRequested will only trigger once unless we manually - // request it. - window.request_redraw(); - }, - _ => {} - } - } pub fn render(&self, state: &State) { let frame = self.surface .get_current_texture()