2024-01-04 17:18:31 -08:00
|
|
|
// INCLUDE: global uniform header
|
|
|
|
|
2023-12-23 11:01:27 -08:00
|
|
|
struct InstanceInput {
|
2023-12-25 15:56:27 -08:00
|
|
|
@location(2) position: vec3<f32>,
|
|
|
|
@location(3) size: f32,
|
|
|
|
@location(4) tint: vec2<f32>,
|
2023-12-23 11:01:27 -08:00
|
|
|
};
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
struct VertexInput {
|
|
|
|
@location(0) position: vec3<f32>,
|
|
|
|
@location(1) texture_coords: vec2<f32>,
|
|
|
|
}
|
|
|
|
|
2023-12-23 11:01:27 -08:00
|
|
|
struct VertexOutput {
|
2023-12-23 23:24:04 -08:00
|
|
|
@builtin(position) position: vec4<f32>,
|
|
|
|
@location(0) texture_coords: vec2<f32>,
|
2024-01-04 17:18:31 -08:00
|
|
|
@location(1) texture_index: u32,
|
|
|
|
@location(2) tint: vec2<f32>,
|
2023-12-23 23:24:04 -08:00
|
|
|
}
|
|
|
|
|
2023-12-25 11:17:08 -08:00
|
|
|
@group(0) @binding(0)
|
|
|
|
var texture_array: binding_array<texture_2d<f32>>;
|
|
|
|
@group(0) @binding(1)
|
|
|
|
var sampler_array: binding_array<sampler>;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
fn fmod(x: vec2<f32>, m: f32) -> vec2<f32> {
|
2023-12-24 09:34:39 -08:00
|
|
|
return x - floor(x / m) * m;
|
2023-12-23 11:01:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@vertex
|
2023-12-25 11:17:08 -08:00
|
|
|
fn vertex_main(
|
2023-12-23 23:24:04 -08:00
|
|
|
vertex: VertexInput,
|
2023-12-23 11:01:27 -08:00
|
|
|
instance: InstanceInput,
|
|
|
|
) -> VertexOutput {
|
2023-12-23 23:24:04 -08:00
|
|
|
|
2023-12-24 11:59:39 -08:00
|
|
|
var out: VertexOutput;
|
|
|
|
out.tint = instance.tint;
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
// Center of the tile the camera is currently in, in game coordinates.
|
|
|
|
// x div y = x - (x mod y)
|
|
|
|
let tile_center = (
|
2023-12-24 11:08:44 -08:00
|
|
|
global.camera_position.xy
|
2023-12-23 23:24:04 -08:00
|
|
|
- (
|
|
|
|
fmod(
|
2023-12-24 11:08:44 -08:00
|
|
|
global.camera_position.xy + global.starfield_tile_size.x / 2.0,
|
|
|
|
global.starfield_tile_size.x
|
|
|
|
) - global.starfield_tile_size.x / 2.0
|
2023-12-23 23:24:04 -08:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-12-25 15:56:27 -08:00
|
|
|
|
|
|
|
let zoom_min_times = (
|
|
|
|
global.camera_zoom.x / global.camera_zoom_limits.x
|
|
|
|
);
|
|
|
|
|
|
|
|
// Hide n% of the smallest stars
|
|
|
|
// If we wanted a constant number of stars on the screen, we would do
|
|
|
|
// `let hide_fraction = 1.0 - 1.0 / (zoom_min_times * zoom_min_times);`
|
|
|
|
// We, however, don't want this: a bigger screen should have more stars,
|
|
|
|
// but not *too* many. We thus scale linearly.
|
|
|
|
let hide_fraction = 1.0 - 1.0 / (zoom_min_times * 0.8);
|
|
|
|
|
|
|
|
if (
|
|
|
|
instance.size < (
|
|
|
|
hide_fraction * (global.starfield_size_limits.y - global.starfield_size_limits.x)
|
|
|
|
+ (global.starfield_size_limits.x)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
out.position = vec4<f32>(2.0, 2.0, 0.0, 1.0);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
// Apply sprite aspect ratio & scale factor
|
|
|
|
// also applies screen aspect ratio
|
2023-12-25 15:56:27 -08:00
|
|
|
// Note that we do NOT scale for distance here---this is intentional.
|
2023-12-25 08:46:10 -08:00
|
|
|
var scale: f32 = instance.size / global.camera_zoom.x;
|
2023-12-24 11:08:44 -08:00
|
|
|
|
|
|
|
// Minimum scale to prevent flicker at large zoom levels
|
|
|
|
var real_size = scale * global.window_size.xy;
|
|
|
|
if (real_size.x < 2.0 || real_size.y < 2.0) {
|
2023-12-24 12:06:11 -08:00
|
|
|
// Otherwise, clamp to a minimum scale
|
2023-12-24 11:59:39 -08:00
|
|
|
scale = 2.0 / max(global.window_size.x, global.window_size.y);
|
2023-12-24 11:08:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-29 22:56:19 -08:00
|
|
|
// Divide by two, because viewport height is 2 in screen units
|
|
|
|
// (coordinates go from -1 to 1)
|
2023-12-23 23:24:04 -08:00
|
|
|
var pos: vec2<f32> = vec2<f32>(
|
2023-12-29 22:56:19 -08:00
|
|
|
vertex.position.x * (scale/2.0) / global.window_aspect.x,
|
|
|
|
vertex.position.y * (scale/2.0)
|
2023-12-23 23:24:04 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
// World position relative to camera
|
|
|
|
// (Note that instance position is in a different
|
|
|
|
// coordinate system than usual)
|
2023-12-25 15:56:27 -08:00
|
|
|
let camera_pos = (instance.position.xy + tile_center) - global.camera_position.xy;
|
2023-12-23 23:24:04 -08:00
|
|
|
|
|
|
|
// Translate
|
|
|
|
pos = pos + (
|
|
|
|
// Don't forget to correct distance for screen aspect ratio too!
|
2023-12-29 22:56:19 -08:00
|
|
|
(camera_pos / (global.camera_zoom.x * instance.position.z))
|
2023-12-24 11:08:44 -08:00
|
|
|
/ vec2<f32>(global.window_aspect.x, 1.0)
|
2023-12-23 23:24:04 -08:00
|
|
|
);
|
|
|
|
|
2023-12-25 15:56:27 -08:00
|
|
|
out.position = vec4<f32>(pos, 0.0, 1.0) * instance.position.z;
|
2024-01-04 17:18:31 -08:00
|
|
|
|
2024-01-04 18:15:30 -08:00
|
|
|
let i = sprites.data[global.starfield_sprite.x].first_frame;
|
|
|
|
let t = atlas.data[i];
|
2024-01-04 17:18:31 -08:00
|
|
|
out.texture_index = u32(0);
|
|
|
|
out.texture_coords = vec2(t.xpos, t.ypos);
|
|
|
|
if vertex.texture_coords.x == 1.0 {
|
|
|
|
out.texture_coords = vec2(out.texture_coords.x + t.width, out.texture_coords.y);
|
|
|
|
}
|
|
|
|
if vertex.texture_coords.y == 1.0 {
|
|
|
|
out.texture_coords = vec2(out.texture_coords.x, out.texture_coords.y + t.height);
|
|
|
|
}
|
|
|
|
|
2023-12-23 11:01:27 -08:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
@fragment
|
2023-12-25 11:17:08 -08:00
|
|
|
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
2023-12-24 12:12:02 -08:00
|
|
|
let b = 0.8 - (in.tint.x / 1.5); // brightness
|
2023-12-24 11:59:39 -08:00
|
|
|
let c = in.tint.y; // color
|
2023-12-24 12:12:02 -08:00
|
|
|
// TODO: saturation
|
|
|
|
// TODO: more subtle starfield
|
|
|
|
// TODO: blur stars more?
|
2023-12-24 11:59:39 -08:00
|
|
|
|
|
|
|
let c_top = vec3<f32>(0.369, 0.819, 0.796);
|
|
|
|
let c_bot = vec3<f32>(0.979, 0.556, 0.556);
|
|
|
|
let c_del = c_bot - c_top;
|
|
|
|
|
2023-12-23 23:24:04 -08:00
|
|
|
return textureSampleLevel(
|
2024-01-04 17:18:31 -08:00
|
|
|
texture_array[in.texture_index],
|
2023-12-25 18:24:55 -08:00
|
|
|
sampler_array[0],
|
2023-12-23 23:24:04 -08:00
|
|
|
in.texture_coords,
|
|
|
|
0.0
|
2023-12-24 12:12:02 -08:00
|
|
|
).rgba * vec4<f32>(
|
|
|
|
(c_top + (c_del * c)) * b,
|
|
|
|
1.0
|
|
|
|
);
|
2023-12-23 11:01:27 -08:00
|
|
|
}
|