oden/src/util.wgsl
John Doty 1cb30034f8 [oden] Move scaling entirely into JavaScript
Now the game controls its own resolution. We might want to further
copy Love2D and generate resize events, I don't know.
2023-09-02 09:58:58 -07:00

67 lines
No EOL
2.2 KiB
WebGPU Shading Language

// This file contains utility functions to be used by the other shaders.
// Sorry that WGSL has no direct linking/reference/include function,
// you just kinda have to know it.
// ----------------------------------------------------------------------------
// Resolution Handling
// ----------------------------------------------------------------------------
struct ScreenUniform {
resolution : vec2f,
};
@group(0) @binding(0) // 1.
var<uniform> screen : ScreenUniform;
fn adjust_for_resolution(in_pos: vec2<f32>) -> vec2<f32> {
// The incoming positions are all in pixel space on the screen, we need to
// convert them into clip space, from (-1,-1) to (1,1). (-1,-1) is in the
// bottom-left corner.
//
// Put result in the range [0-2], where (2,2) is the bottom-right corner
// of the screen.
var result = (in_pos * 2.0) / screen.resolution;
// Put result in the range [-1,1] where [1,1] is the bottom-right corner
// of the screen.
result -= 1.0;
// Put result in the range [-1,1] where [1,1] is the top-right corner of
// the screen.
result.y = -result.y;
return result;
}
/**
const RES = vec2f(320.0, 240.0); // The logical resolution of the screen.
fn old_adjust_for_resolution(in_pos: vec2<f32>) -> vec2<f32> {
// Adjust in_pos for the "resolution" of the screen.
let RES_AR = RES.x / RES.y; // The aspect ratio of the logical screen.
// the actual resolution of the screen.
let screen_ar = screen.resolution.x / screen.resolution.y;
// Compute the difference in resolution ... correctly?
//
// nudge is the amount to add to the logical resolution so that the pixels
// stay the same size but we respect the aspect ratio of the screen. (So
// there's more of them in either the x or y direction.)
var nudge = vec2f(0.0);
if (screen_ar > RES_AR) {
nudge.x = (RES.y * screen_ar) - RES.x;
} else {
nudge.y = (RES.x / screen_ar) - RES.y;
}
var new_logical_resolution = RES + nudge;
// Now we can convert the incoming position to clip space, in the new screen.
let centered = in_pos + (nudge / 2.0);
var position = (2.0 * centered / new_logical_resolution) - 1.0;
position.y = -position.y;
return position;
}
*/