Skip to content

Commit

Permalink
feat: transparency in textures now working
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamAryanpur committed Aug 30, 2023
1 parent 71ce4a6 commit 1dafadf
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl crate::header::Renderer {
targets: &[Some(wgpu::ColorTargetState {
format: self.config.format,
write_mask: wgpu::ColorWrites::ALL,
blend: Some(wgpu::BlendState::REPLACE),
blend: Some(wgpu::BlendState::ALPHA_BLENDING),
})],
}),
primitive: wgpu::PrimitiveState {
Expand Down Expand Up @@ -152,7 +152,10 @@ impl crate::header::Renderer {
sample_count: 1,
dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb,
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
usage: wgpu::TextureUsages::TEXTURE_BINDING
| wgpu::TextureUsages::COPY_DST
| wgpu::TextureUsages::COPY_SRC
| wgpu::TextureUsages::RENDER_ATTACHMENT,
view_formats: &[wgpu::TextureFormat::Rgba8Unorm],
});

Expand Down
19 changes: 18 additions & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ unsafe impl Sync for Pipeline {}
/// Container for pipeline data. Allows for sharing resources with other objects
#[derive(Debug)]
pub enum PipelineData<T> {
/// No data, just a reference to a buffer
Copy(String),
Data(T),
}
Expand Down Expand Up @@ -292,7 +293,7 @@ pub struct Camera {
unsafe impl Send for Camera {}
unsafe impl Sync for Camera {}

// ? These definitions are taken from wgpu API docs
// These definitions are taken from wgpu API docs
#[derive(Debug, Clone, Copy)]
pub struct ShaderSettings {
// ===== PRIMITIVE ===== //
Expand Down Expand Up @@ -364,6 +365,21 @@ impl Default for ShaderSettings {
unsafe impl Send for ShaderSettings {}
unsafe impl Sync for ShaderSettings {}

/// Instance buffer data that is sent to GPU
#[repr(C)]
#[derive(Debug, Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct InstanceRaw {
pub model: uniform_type::Matrix,
}

/// Instance buffer data storage
#[derive(Debug, Clone, Copy)]
pub struct Instance {
pub position: nalgebra_glm::Vec3,
pub rotation: nalgebra_glm::Vec3,
pub scale: nalgebra_glm::Vec3,
}

/// Allows all events to be fetched directly, making it easier to add custom additions to the engine.
pub trait EnginePlugin: Any {
/// This is ran before any of the render events, it's generally used to capture raw input.
Expand Down Expand Up @@ -438,6 +454,7 @@ pub fn pixel_to_cartesian(value: f32, max: u32) -> f32 {
}
}

/// A unified way to handle strings
pub trait StringBuffer: StringBufferTrait + Clone {}
pub trait StringBufferTrait {
fn as_str(&self) -> &str;
Expand Down
50 changes: 48 additions & 2 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/

use crate::header::{
glm, pixel_to_cartesian, uniform_type, Object, ObjectSettings, Pipeline, PipelineData,
Renderer, RotateAxis, TextureData, Textures, Vertex,
glm, pixel_to_cartesian, uniform_type, Instance, InstanceRaw, Object, ObjectSettings, Pipeline,
PipelineData, Renderer, RotateAxis, TextureData, Textures, Vertex,
};
use crate::uniform_type::{Array4, Matrix};
use crate::utils::default_resources::{DEFAULT_MATRIX_4, DEFAULT_SHADER, DEFAULT_TEXTURE};
Expand Down Expand Up @@ -538,3 +538,49 @@ fn fs_main(input: VertexOutput) -> @location(0) vec4<f32> {
}
}
}

impl Instance {
/// Creates a new instance
pub fn new(position: glm::Vec3, rotation: glm::Vec3, scale: glm::Vec3) -> Self {
Self {
position,
rotation,
scale,
}
}

/// Gathers all information and builds a Raw Instance to be sent to GPU
pub fn to_raw(&self) -> InstanceRaw {
let position_matrix = glm::Mat4::from_vec(self.position.as_slice().to_vec());
let rotation_matrix = glm::Mat4::from_vec(self.rotation.as_slice().to_vec());
let scale_matrix = glm::Mat4::from_vec(self.scale.as_slice().to_vec());
InstanceRaw {
model: Matrix::from_im(position_matrix * rotation_matrix * scale_matrix),
}
}

/// Sets the position
pub fn set_position(&mut self, position: glm::Vec3) {
self.position = position;
}

/// Sets the rotation
pub fn set_rotation(&mut self, rotation: glm::Vec3) {
self.rotation = rotation;
}

/// Sets the scale
pub fn set_scale(&mut self, scale: glm::Vec3) {
self.scale = scale;
}
}

impl Default for Instance {
fn default() -> Self {
Self {
position: glm::Vec3::new(0.0, 0.0, 0.0),
rotation: glm::Vec3::new(0.0, 0.0, 0.0),
scale: glm::Vec3::new(1.0, 1.0, 1.0),
}
}
}

0 comments on commit 1dafadf

Please sign in to comment.