Skip to content

Commit

Permalink
- add checkerboard shader for primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
polymonster committed Feb 17, 2023
1 parent f7ba4c1 commit 6631302
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
10 changes: 5 additions & 5 deletions plugins/ecs_basic/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn setup_primitives(
mut commands: bevy_ecs::system::Commands) {

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

let size = 10.0;
let half_size = size * 0.5;
let step = size * 2.5;
let half_size = size * 0.5;
let step = size * half_size;
let half_extent = rc * half_size;
let start_pos = vec3f(half_extent, size, half_extent);

Expand All @@ -62,8 +62,8 @@ pub fn setup_primitives(
if i < meshes.len() {
let iter_pos = start_pos + vec3f(x as f32 * step, 0.0, y as f32 * step);
commands.spawn((
MeshComponent {0: meshes[i].clone()},
WorldMatrix { 0: Mat4f::from_translation(iter_pos) * Mat4f::from_scale(splat3f(10.0))},
MeshComponent(meshes[i].clone()),
WorldMatrix(Mat4f::from_translation(iter_pos) * Mat4f::from_scale(splat3f(10.0))),
));
}
i = i + 1;
Expand Down
30 changes: 24 additions & 6 deletions src/shaders/basic.hlsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
struct vs_input_3d {
float3 position : POSITION;
float4 colour: TEXCOORD;
};

struct vs_output {
float4 position : SV_POSITION0;
float4 colour: TEXCOORD;
float4 colour: TEXCOORD0;
float2 texcoord: TEXCOORD1;
};

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

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

return output;
}
Expand Down Expand Up @@ -132,8 +129,29 @@ ps_output ps_main(vs_output input) {
return output;
}



ps_output ps_checkerboard(vs_output input) {
ps_output output;
output.colour = input.colour;

float size = 5.0;

float x = (input.texcoord.x * 0.5 + 0.5) * size;
float y = (input.texcoord.y * 0.5 + 0.5) * size;

float ix;
modf(x, ix);
float rx = fmod(ix, 2.0) < 0.1 ? 0.0 : 1.0;

float iy;
modf(y, iy);
float ry = fmod(iy, 2.0) < 0.1 ? 0.0 : 1.0;

float rxy = rx + ry > 1.0 ? 0.0 : rx + ry;

output.colour.rgb *= rxy < 0.001 ? 0.5 : 1.0;

output.colour.a = 1.0;
return output;
}

0 comments on commit 6631302

Please sign in to comment.