-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi pass rendering and updated sampling
- Loading branch information
1 parent
03e9f23
commit 7e72d6c
Showing
3 changed files
with
146 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
[gd_resource type="Shader" format=2] | ||
|
||
[resource] | ||
code = "// [Godot Super Scaling] | ||
// created by Andres Hernandez | ||
shader_type canvas_item; | ||
render_mode unshaded; | ||
|
||
// viewport to sample and its resolution | ||
uniform sampler2D viewport; | ||
uniform vec2 view_resolution; | ||
varying vec2 view_pixel_size; | ||
|
||
// offset coordinates used for super sampling | ||
const vec2 offset_uv = vec2(0.28, 0.56); | ||
|
||
// partial derivative on x-axis | ||
vec2 deriv_x(vec2 pos, vec4 frag, vec2 pixel) { | ||
vec2 offset = vec2(pixel.x, 0.0); | ||
vec2 pos_plus = pos + offset; | ||
vec2 pos_minus = pos - offset; | ||
int coord = int(frag.x) / 2; | ||
bool even = int(coord * 2) == int(frag.x); | ||
return even ? (pos_plus - pos) : (pos - pos_minus); | ||
} | ||
|
||
// partial derivative on y-axis | ||
vec2 deriv_y(vec2 pos, vec4 frag, vec2 pixel) { | ||
vec2 offset = vec2(0.0, pixel.y); | ||
vec2 pos_plus = pos + offset; | ||
vec2 pos_minus = pos - offset; | ||
int coord = int(frag.y) / 2; | ||
bool even = int(coord * 2) == int(frag.y); | ||
return even ? (pos_plus - pos) : (pos - pos_minus); | ||
} | ||
|
||
// take 4 samples in a rotated grid for super sampling | ||
vec4 super_sample(vec2 base_uv, vec4 frag, vec2 pixel) { | ||
vec2 dx = deriv_x(base_uv, frag, pixel); | ||
vec2 dy = deriv_y(base_uv, frag, pixel); | ||
vec2 uv = vec2(0.0); | ||
vec4 color = vec4(0.0); | ||
uv = base_uv + offset_uv.x * dx + offset_uv.y * dy; | ||
color += textureLod(viewport, uv, 0.0); | ||
uv = base_uv - offset_uv.x * dx - offset_uv.y * dy; | ||
color += textureLod(viewport, uv, 0.0); | ||
uv = base_uv + offset_uv.y * dx - offset_uv.x * dy; | ||
color += textureLod(viewport, uv, 0.0); | ||
uv = base_uv - offset_uv.y * dx + offset_uv.x * dy; | ||
color += textureLod(viewport, uv, 0.0); | ||
color *= 0.25; | ||
return color; | ||
} | ||
|
||
// precompute the sample direction texture coordinates | ||
void vertex() { | ||
view_pixel_size = 1.0 / view_resolution; | ||
} | ||
|
||
// draw the new color on the screen | ||
void fragment() { | ||
vec4 result = super_sample(UV, FRAGCOORD, view_pixel_size); | ||
result.a = 1.0; | ||
COLOR = result; | ||
}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters