Skip to content

Commit e318469

Browse files
authored
void_chromakey: Make the starfield scale-independent (#2050)
Previously the starfield was drawn differently in a 1280×720 viewport (the base resolution) compared to a 3840x2160 viewport (300% scale, fullscreen on a 4K monitor). This is because we calculated the position of each vertex in screen pixel coordinates (i.e. ranging from (0, 0) to (1280x720) in the first case, and (0, 0) to (3840, 2160) in the second, and then divided element-wise by a constant (1920, 1080) (not updated for the resolution change) to get a texture coordinate. So the bottom-right pixel of the viewport corresponded to (1, 1) in the first case and (3, 3) in the second. Instead we can use SCREEN_UV to get, as the name suggests, a UV coordinate for the pixel on the screen (or rather, the viewport, but when fullscreened these are the same) ranging from (0, 0) in the top-left corner and (1, 1) in the bottom-right. The other use of the `resolution` uniform was to scale the x coordinate so that stars appear square. This previously gave the wrong effect if the viewport is not 16:9 aspect ratio: the stars would be stretched with the aspect ratio. Happily Godot provides SCREEN_PIXEL_SIZE which is defined to be the inverse of the screen resolution, so we can get the true aspect ratio from this and scale by that instead. (In fact I had left a comment to this effect...) Resolves #1189
1 parent 7f9c1ba commit e318469

2 files changed

Lines changed: 2 additions & 13 deletions

File tree

scenes/game_elements/props/void/components/void_chromakey.gdshader

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
*/
66
shader_type canvas_item;
77

8-
// TODO: Can we get this from SCREEN_PIXEL_SIZE?
9-
/**
10-
* Viewport size.
11-
*/
12-
uniform vec2 resolution = vec2(1920.0, 1080.0);
13-
148
/**
159
* For best results, use Simplex noise.
1610
*/
@@ -36,20 +30,16 @@ uniform vec4 chroma_key: source_color = vec4(1.0, 0.0, 0.0, 1.0);
3630
*/
3731
uniform vec4 background_color: source_color = vec4(0.086, 0.11, 0.18, 1.0);
3832

39-
// The origin is the upper-left corner of the screen and coordinates ranging from (0.0, 0.0) to viewport size.
40-
varying vec2 screen_pos;
41-
4233
varying vec4 modulate;
4334

4435
void vertex() {
45-
screen_pos = (CANVAS_MATRIX * MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
4636
modulate = COLOR;
4737
}
4838

4939
void fragment() {
5040
if (texture(TEXTURE, UV) == chroma_key) {
51-
vec2 uv = screen_pos / resolution;
52-
uv.x *= resolution.x / resolution.y;
41+
vec2 uv = SCREEN_UV;
42+
uv.x *= SCREEN_PIXEL_SIZE.y / SCREEN_PIXEL_SIZE.x;
5343

5444
float stars = 0.0;
5545

scenes/game_elements/props/void/void_chromakey_material.tres

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ noise = SubResource("FastNoiseLite_ebh07")
1111

1212
[resource]
1313
shader = ExtResource("1_nktdb")
14-
shader_parameter/resolution = Vector2(1920, 1080)
1514
shader_parameter/noise_texture = SubResource("NoiseTexture2D_bmckr")
1615
shader_parameter/sparsity = 20.0
1716
shader_parameter/flicker_rate = 7.0

0 commit comments

Comments
 (0)