Fixed coordinate scaling

master
Mark 2023-12-29 22:56:19 -08:00
parent 0184418394
commit 8be7cdf5a3
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
2 changed files with 17 additions and 11 deletions

View File

@ -221,7 +221,7 @@ impl GPUState {
// Don't divide by 2, we use this later. // Don't divide by 2, we use this later.
let height = s.size / s.pos.z; let height = s.size / s.pos.z;
// Width or height, whichever is larger // Width or height, whichever is larger.
// Accounts for sprite rotation. // Accounts for sprite rotation.
let m = height * texture.aspect.max(1.0); let m = height * texture.aspect.max(1.0);
@ -259,9 +259,14 @@ impl GPUState {
// After finishing all ops, translate. // After finishing all ops, translate.
// This must be done last, all other operations // This must be done last, all other operations
// require us to be at (0, 0). // require us to be at (0, 0).
//
// Note that we divide camera zoom by two.
// THIS IS IMPORTANT!
// The height of the viewport is `zoom` in game units,
// but it's 2 in screen units! (since coordinates range from -1 to 1)
let translate = Matrix4::from_translation(Vector3 { let translate = Matrix4::from_translation(Vector3 {
x: pos.x / game.camera.zoom / self.window_aspect, x: pos.x / (game.camera.zoom / 2.0) / self.window_aspect,
y: pos.y / game.camera.zoom, y: pos.y / (game.camera.zoom / 2.0),
z: 0.0, z: 0.0,
}); });
@ -301,15 +306,15 @@ impl GPUState {
let screen_aspect = Matrix4::from_nonuniform_scale(1.0 / self.window_aspect, 1.0, 1.0); let screen_aspect = Matrix4::from_nonuniform_scale(1.0 / self.window_aspect, 1.0, 1.0);
let ptranslate = Matrix4::from_translation(Vector3 { let ptranslate = Matrix4::from_translation(Vector3 {
x: parent_pos.x / game.camera.zoom / self.window_aspect, x: parent_pos.x / (game.camera.zoom / 2.0) / self.window_aspect,
y: parent_pos.y / game.camera.zoom, y: parent_pos.y / (game.camera.zoom / 2.0),
z: 0.0, z: 0.0,
}); });
let protate = Matrix4::from_angle_z(parent_angle); let protate = Matrix4::from_angle_z(parent_angle);
let translate = Matrix4::from_translation(Vector3 { let translate = Matrix4::from_translation(Vector3 {
x: s.pos.x / game.camera.zoom / self.window_aspect, x: s.pos.x / (game.camera.zoom / 2.0) / self.window_aspect,
y: s.pos.y / game.camera.zoom, y: s.pos.y / (game.camera.zoom / 2.0),
z: 0.0, z: 0.0,
}); });

View File

@ -75,7 +75,6 @@ fn vertex_main(
// but not *too* many. We thus scale linearly. // but not *too* many. We thus scale linearly.
let hide_fraction = 1.0 - 1.0 / (zoom_min_times * 0.8); let hide_fraction = 1.0 - 1.0 / (zoom_min_times * 0.8);
// Hide some stars at large zoom levels.
if ( if (
instance.size < ( instance.size < (
hide_fraction * (global.starfield_size_limits.y - global.starfield_size_limits.x) hide_fraction * (global.starfield_size_limits.y - global.starfield_size_limits.x)
@ -100,9 +99,11 @@ fn vertex_main(
} }
// Divide by two, because viewport height is 2 in screen units
// (coordinates go from -1 to 1)
var pos: vec2<f32> = vec2<f32>( var pos: vec2<f32> = vec2<f32>(
vertex.position.x * scale / global.window_aspect.x, vertex.position.x * (scale/2.0) / global.window_aspect.x,
vertex.position.y * scale vertex.position.y * (scale/2.0)
); );
// World position relative to camera // World position relative to camera
@ -113,7 +114,7 @@ fn vertex_main(
// Translate // Translate
pos = pos + ( pos = pos + (
// Don't forget to correct distance for screen aspect ratio too! // Don't forget to correct distance for screen aspect ratio too!
(camera_pos / (global.camera_zoom.x * (instance.position.z))) (camera_pos / (global.camera_zoom.x * instance.position.z))
/ vec2<f32>(global.window_aspect.x, 1.0) / vec2<f32>(global.window_aspect.x, 1.0)
); );