Galactica/crates/render/shaders/object.wgsl

221 lines
5.6 KiB
Plaintext
Raw Normal View History

// INCLUDE: global uniform header
2023-12-22 16:51:21 -08:00
struct InstanceInput {
@location(2) texture_index: vec2<u32>,
@location(3) texture_fade: f32,
@location(4) object_index: u32,
2024-01-23 18:44:32 -08:00
@location(5) color: vec4<f32>,
2023-12-22 16:51:21 -08:00
};
struct VertexInput {
@location(0) position: vec3<f32>,
2023-12-23 11:01:27 -08:00
@location(1) texture_coords: vec2<f32>,
};
2023-12-22 16:51:21 -08:00
struct VertexOutput {
2023-12-23 23:24:04 -08:00
@builtin(position) position: vec4<f32>,
2024-01-06 14:02:50 -08:00
@location(0) tween: f32,
@location(1) texture_index_a: u32,
@location(2) texture_coords_a: vec2<f32>,
@location(3) texture_index_b: u32,
@location(4) texture_coords_b: vec2<f32>,
2024-01-23 18:44:32 -08:00
@location(5) color: vec4<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>;
fn transform_vertex(obj: ObjectData, vertex_position: vec2<f32>, texture_index: u32) -> vec4<f32> {
// Object scale
2024-01-13 18:57:02 -08:00
var scale: f32 = obj.size / (global_data.camera_zoom.x * obj.zpos);
if obj.is_child == 1u {
scale /= objects[obj.parent].zpos;
}
let texture = global_atlas[texture_index];
// Apply scale and sprite aspect
// Note that our mesh starts centered at (0, 0). This is important!
var pos: vec2<f32> = vec2(
vertex_position.x * scale * (texture.width / texture.height),
2024-01-06 14:02:50 -08:00
vertex_position.y * scale
);
2024-01-06 14:02:50 -08:00
// Apply rotation
pos = mat2x2(
2024-01-12 22:47:40 -08:00
vec2(cos(obj.angle - 1.5708), sin(obj.angle - 1.5708)),
vec2(-sin(obj.angle - 1.5708), cos(obj.angle - 1.5708))
) * pos;
2024-01-06 14:02:50 -08:00
// Correct for screen aspect, preserving height
// This must be done AFTER rotation.
2024-01-06 14:02:50 -08:00
// (It's thus done later if this is a child)
if obj.is_child == u32(0) {
pos = vec2(
pos.x / global_data.window_aspect.x,
pos.y
);
}
2024-01-06 14:02:50 -08:00
// Translate
//
// Note that we divide camera zoom by two.
// The height of the viewport is `zoom` in game units,
// but it's 2 in screen units (since coordinates range from -1 to 1)
2024-01-06 14:02:50 -08:00
if obj.is_child == u32(0) {
let trans = (vec2(obj.xpos, obj.ypos) - global_data.camera_position) / obj.zpos;
pos = pos + vec2(
trans.x / (global_data.camera_zoom.x / 2.0) / global_data.window_aspect.x,
trans.y / (global_data.camera_zoom.x / 2.0)
);
}
if obj.is_child == u32(1) {
2024-01-13 18:57:02 -08:00
let parent = objects[obj.parent];
2024-01-06 14:02:50 -08:00
// Apply translation relative to parent
// Note that obj.zpos is ignored
pos = pos + vec2(
obj.xpos / (global_data.camera_zoom.x / 2.0) / global_data.window_aspect.x,
obj.ypos / (global_data.camera_zoom.x / 2.0)
2024-01-13 18:57:02 -08:00
) / parent.zpos;
2024-01-06 14:02:50 -08:00
// Apply parent's rotation
pos = mat2x2(
2024-01-12 22:47:40 -08:00
vec2(cos(parent.angle - 1.5708), sin(parent.angle - 1.5708)),
vec2(-sin(parent.angle - 1.5708), cos(parent.angle - 1.5708))
2024-01-06 14:02:50 -08:00
) * pos;
// Correct for screen aspect, preserving height
pos = vec2(
pos.x / global_data.window_aspect.x,
pos.y
);
// Apply parent's translation
let ptrans = (vec2(parent.xpos, parent.ypos) - global_data.camera_position) / parent.zpos;
pos = pos + vec2(
ptrans.x / (global_data.camera_zoom.x / 2.0) / global_data.window_aspect.x,
ptrans.y / (global_data.camera_zoom.x / 2.0)
);
}
return vec4<f32>(pos, 0.0, 1.0);
}
2023-12-22 16:51:21 -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-22 16:51:21 -08:00
instance: InstanceInput,
) -> VertexOutput {
var out: VertexOutput;
// Pick texture size by the size of the visible texture
// (texture index 0 is special, it's the "hidden" texture)
if instance.texture_index.x == 0u && instance.texture_index.y == 0u {
out.position = vec4<f32>(0.0, 0.0, 0.0, 1.0);
} else if instance.texture_index.x == 0u {
out.position = transform_vertex(
objects[instance.object_index],
vertex.position.xy,
instance.texture_index.y
);
} else if instance.texture_index.y == 0u {
out.position = transform_vertex(
objects[instance.object_index],
vertex.position.xy,
instance.texture_index.x
);
} else {
out.position = transform_vertex(
objects[instance.object_index],
vertex.position.xy,
instance.texture_index.x
);
}
2024-01-23 18:44:32 -08:00
out.color = instance.color;
out.tween = instance.texture_fade;
2024-01-06 14:02:50 -08:00
// Texture 0 is special, it's the empty texture
if instance.texture_index.x == 0u {
out.texture_index_a = 0u;
out.texture_coords_a = vec2(0.0, 0.0);
} else {
let t = global_atlas[instance.texture_index.x];
out.texture_index_a = t.atlas_texture;
out.texture_coords_a = vec2(t.xpos, t.ypos);
if vertex.texture_coords.x == 1.0 {
out.texture_coords_a = out.texture_coords_a + vec2(t.width, 0.0);
}
if vertex.texture_coords.y == 1.0 {
out.texture_coords_a = out.texture_coords_a + vec2(0.0, t.height);
}
2024-01-06 14:02:50 -08:00
}
if instance.texture_index.y == 0u {
out.texture_index_b = u32(0u);
out.texture_coords_b = vec2(0.0, 0.0);
} else {
let b = global_atlas[instance.texture_index.y];
out.texture_index_b = u32(b.atlas_texture);
out.texture_coords_b = vec2(b.xpos, b.ypos);
if vertex.texture_coords.x == 1.0 {
out.texture_coords_b = out.texture_coords_b + vec2(b.width, 0.0);
}
if vertex.texture_coords.y == 1.0 {
out.texture_coords_b = out.texture_coords_b + vec2(0.0, b.height);
}
}
2023-12-22 16:51:21 -08:00
return out;
}
@fragment
2023-12-25 11:17:08 -08:00
fn fragment_main(in: VertexOutput) -> @location(0) vec4<f32> {
var texture_a: vec4<f32> = vec4(0.0, 0.0, 0.0, 0.0);
if !(
(in.texture_index_a == 0u) &&
(in.texture_coords_a.x == 0.0) &&
(in.texture_coords_a.y == 0.0)
) {
texture_a = textureSampleLevel(
2024-01-06 14:02:50 -08:00
texture_array[in.texture_index_a],
sampler_array[0],
in.texture_coords_a,
0.0
).rgba;
}
var texture_b: vec4<f32> = vec4(0.0, 0.0, 0.0, 0.0);
if !(
(in.texture_index_b == 0u) &&
(in.texture_coords_b.x == 0.0) &&
(in.texture_coords_b.y == 0.0)
) {
texture_b = textureSampleLevel(
2024-01-06 14:02:50 -08:00
texture_array[in.texture_index_b],
sampler_array[0],
in.texture_coords_b,
0.0
).rgba;
}
let color = mix(
texture_a,
texture_b,
2024-01-06 14:02:50 -08:00
in.tween
2024-01-23 18:44:32 -08:00
) * in.color;
return color;
}