Skip to content

Commit 6631302

Browse files
committed
- add checkerboard shader for primitives
1 parent f7ba4c1 commit 6631302

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

plugins/ecs_basic/src/primitives.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn setup_primitives(
4141
mut commands: bevy_ecs::system::Commands) {
4242

4343
let meshes = vec![
44-
hotline_rs::primitives::create_plane_mesh(&mut device.0, 64),
44+
hotline_rs::primitives::create_plane_mesh(&mut device.0, 1),
4545
hotline_rs::primitives::create_tetrahedron_mesh(&mut device.0),
4646
hotline_rs::primitives::create_cube_mesh(&mut device.0),
4747
];
@@ -51,8 +51,8 @@ pub fn setup_primitives(
5151
let irc = rc as i32;
5252

5353
let size = 10.0;
54-
let half_size = size * 0.5;
55-
let step = size * 2.5;
54+
let half_size = size * 0.5;
55+
let step = size * half_size;
5656
let half_extent = rc * half_size;
5757
let start_pos = vec3f(half_extent, size, half_extent);
5858

@@ -62,8 +62,8 @@ pub fn setup_primitives(
6262
if i < meshes.len() {
6363
let iter_pos = start_pos + vec3f(x as f32 * step, 0.0, y as f32 * step);
6464
commands.spawn((
65-
MeshComponent {0: meshes[i].clone()},
66-
WorldMatrix { 0: Mat4f::from_translation(iter_pos) * Mat4f::from_scale(splat3f(10.0))},
65+
MeshComponent(meshes[i].clone()),
66+
WorldMatrix(Mat4f::from_translation(iter_pos) * Mat4f::from_scale(splat3f(10.0))),
6767
));
6868
}
6969
i = i + 1;

src/shaders/basic.hlsl

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
struct vs_input_3d {
2-
float3 position : POSITION;
3-
float4 colour: TEXCOORD;
4-
};
5-
61
struct vs_output {
72
float4 position : SV_POSITION0;
8-
float4 colour: TEXCOORD;
3+
float4 colour: TEXCOORD0;
4+
float2 texcoord: TEXCOORD1;
95
};
106

117
struct ps_output {
@@ -74,6 +70,7 @@ vs_output vs_mesh(vs_input_mesh input) {
7470
output.position = mul(pos, projection_matrix);
7571

7672
output.colour = float4(input.normal.xyz * 0.5 + 0.5, 1.0);
73+
output.texcoord = input.texcoord;
7774

7875
return output;
7976
}
@@ -132,8 +129,29 @@ ps_output ps_main(vs_output input) {
132129
return output;
133130
}
134131

132+
133+
135134
ps_output ps_checkerboard(vs_output input) {
136135
ps_output output;
137136
output.colour = input.colour;
137+
138+
float size = 5.0;
139+
140+
float x = (input.texcoord.x * 0.5 + 0.5) * size;
141+
float y = (input.texcoord.y * 0.5 + 0.5) * size;
142+
143+
float ix;
144+
modf(x, ix);
145+
float rx = fmod(ix, 2.0) < 0.1 ? 0.0 : 1.0;
146+
147+
float iy;
148+
modf(y, iy);
149+
float ry = fmod(iy, 2.0) < 0.1 ? 0.0 : 1.0;
150+
151+
float rxy = rx + ry > 1.0 ? 0.0 : rx + ry;
152+
153+
output.colour.rgb *= rxy < 0.001 ? 0.5 : 1.0;
154+
155+
output.colour.a = 1.0;
138156
return output;
139157
}

0 commit comments

Comments
 (0)