power_play/res/shaders/grid.hlsl
2025-05-16 12:05:37 -05:00

55 lines
1.4 KiB
HLSL

struct vs_input {
float4 pos : POSITION;
float4 col : COLOR;
float line_thickness : THICKNESS;
float line_spacing : SPACING;
float2 offset : OFFSET;
};
struct ps_input {
float4 screen_pos : SV_POSITION;
float4 col : COLOR;
float line_thickness : THICKNESS;
float line_spacing : SPACING;
float2 offset : OFFSET;
};
cbuffer vs_constants : register(b0)
{
float4x4 projection;
};
ps_input vs_main(vs_input input)
{
ps_input output;
output.screen_pos = mul(projection, float4(input.pos.xy, 0.f, 1.f));
output.col = input.col;
output.line_thickness = input.line_thickness;
output.line_spacing = input.line_spacing;
output.offset = input.offset;
return output;
}
float4 ps_main(ps_input input) : SV_TARGET
{
float2 screen_pos = input.screen_pos.xy + input.offset;
float half_thickness = input.line_thickness / 2;
float spacing = input.line_spacing;
float4 color = 0;
float2 v = abs(round(screen_pos / spacing) * spacing - screen_pos);
float dist = min(v.x, v.y);
color = input.col * step(dist, half_thickness);
if (screen_pos.y <= half_thickness && screen_pos.y >= -half_thickness) {
color.r = 1;
}
if (screen_pos.x <= half_thickness && screen_pos.x >= -half_thickness) {
color.g = 1;
}
return color;
}