Skip to content

Commit 9f6380c

Browse files
committed
feat: Texture blending (transparency)
FIXES #56
1 parent c3c5cb3 commit 9f6380c

File tree

4 files changed

+31
-33
lines changed

4 files changed

+31
-33
lines changed

examples/tetris/main.rs

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,20 @@
1+
use scion::game_layer::{GameLayer, GameLayerController, SimpleGameLayer};
2+
use scion::legion::{Resources, system, World};
3+
use scion::rendering::bidimensional::{Camera2D, Material2D, Position2D, Transform2D};
4+
use scion::rendering::bidimensional::components::Square;
15
use scion::Scion;
2-
use scion::legion::{system, World, Resources};
3-
use scion::game_layer::{SimpleGameLayer, GameLayer, GameLayerController};
46

57
#[system]
6-
fn test(){
8+
fn test() {
79
log::info!("Hello all");
810
}
911

1012
#[derive(Default)]
11-
struct LayerA{
12-
tmp: usize
13-
}
13+
struct LayerA;
1414

15-
impl SimpleGameLayer for LayerA{
16-
fn update(&mut self, _world: &mut World, resource: &mut Resources) {
17-
log::info!("HeullohA...{}", self.tmp);
18-
self.tmp += 1;
19-
if self.tmp >= 301 {
20-
resource.get_mut::<GameLayerController>().unwrap().pop_layer();
21-
resource.get_mut::<GameLayerController>().unwrap().push_layer(GameLayer::strong::<LayerB>());
22-
}
23-
}
24-
}
25-
26-
#[derive(Default)]
27-
struct LayerB;
15+
impl SimpleGameLayer for LayerA {
16+
fn on_start(&mut self, world: &mut World, resource: &mut Resources) {
2817

29-
impl SimpleGameLayer for LayerB{
30-
fn update(&mut self, _world: &mut World, _resource: &mut Resources) {
31-
log::info!("HeullohB...");
3218
}
3319
}
3420

src/rendering/bidimensional/components/square.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::ops::Range;
22

3-
use wgpu::{
4-
util::BufferInitDescriptor, BindGroupLayout, Device, RenderPipeline, SwapChainDescriptor,
5-
};
3+
use wgpu::{util::BufferInitDescriptor, BindGroupLayout, Device, RenderPipeline, SwapChainDescriptor, BlendFactor, BlendOperation};
64

75
use crate::rendering::bidimensional::{
86
gl_representations::TexturedGlVertex, scion2d::Renderable2D, transform::Position2D,
@@ -98,8 +96,16 @@ impl Renderable2D for Square {
9896
entry_point: "main",
9997
targets: &[wgpu::ColorTargetState {
10098
format: sc_desc.format,
101-
alpha_blend: wgpu::BlendState::REPLACE,
102-
color_blend: wgpu::BlendState::REPLACE,
99+
alpha_blend: wgpu::BlendState {
100+
src_factor: BlendFactor::One,
101+
dst_factor: BlendFactor::One,
102+
operation: BlendOperation::Add,
103+
},
104+
color_blend: wgpu::BlendState {
105+
src_factor: BlendFactor::SrcAlpha,
106+
dst_factor: BlendFactor::OneMinusSrcAlpha,
107+
operation: BlendOperation::Add,
108+
},
103109
write_mask: wgpu::ColorWrite::ALL,
104110
}],
105111
}),

src/rendering/bidimensional/components/triangle.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::ops::Range;
22

3-
use wgpu::{
4-
util::BufferInitDescriptor, BindGroupLayout, Device, RenderPipeline, SwapChainDescriptor,
5-
};
3+
use wgpu::{util::BufferInitDescriptor, BindGroupLayout, Device, RenderPipeline, SwapChainDescriptor, BlendFactor, BlendOperation};
64

75
use crate::rendering::bidimensional::{
86
gl_representations::TexturedGlVertex, scion2d::Renderable2D, transform::Position2D,
@@ -84,8 +82,16 @@ impl Renderable2D for Triangle {
8482
entry_point: "main",
8583
targets: &[wgpu::ColorTargetState {
8684
format: sc_desc.format,
87-
alpha_blend: wgpu::BlendState::REPLACE,
88-
color_blend: wgpu::BlendState::REPLACE,
85+
alpha_blend: wgpu::BlendState {
86+
src_factor: BlendFactor::One,
87+
dst_factor: BlendFactor::One,
88+
operation: BlendOperation::Add,
89+
},
90+
color_blend: wgpu::BlendState {
91+
src_factor: BlendFactor::SrcAlpha,
92+
dst_factor: BlendFactor::OneMinusSrcAlpha,
93+
operation: BlendOperation::Add,
94+
},
8995
write_mask: wgpu::ColorWrite::ALL,
9096
}],
9197
}),

src/rendering/bidimensional/scion2d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ fn get_default_color_attachment(frame: &SwapChainTexture) -> RenderPassColorAtta
216216
resolve_target: None,
217217
ops: wgpu::Operations {
218218
load: wgpu::LoadOp::Clear(wgpu::Color {
219-
r: 0.,
219+
r: 1.,
220220
g: 0.,
221221
b: 0.,
222222
a: 1.0,

0 commit comments

Comments
 (0)