-
-
Save belzecue/8503e1ab97c4e5b184a5b698830bdc1d to your computer and use it in GitHub Desktop.
Mode 7 shader for Godot
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
shader_type canvas_item; | |
uniform mat4 TRANSFORM; | |
uniform vec2 DEPTH; | |
uniform bool REPEAT_X; | |
uniform bool REPEAT_Y; | |
uniform bool FLIP; | |
void fragment() { | |
// Create the matrix. A workaround is used to modify the matrix's W column | |
// because Godot's transforms are 3x4, not 4x4. | |
mat4 mat = mat4(1.0); | |
mat[0].w = DEPTH.x; | |
mat[1].w = DEPTH.y; | |
// Transform UV into [-1, 1] range | |
vec2 uv = UV * 2.0 - vec2(1, 1); | |
// Turn position into 4d vector | |
vec4 pos = vec4(uv, 1.0, 1.0); | |
pos = mat * pos; | |
pos.xy = pos.xy / pos.w; | |
// Apply transformation to position | |
float w = pos.w; | |
pos.z = 0.0; | |
pos.w = 1.0; | |
// Apply depth to position | |
pos = TRANSFORM * pos; | |
// divide position by w coordinate; this applies perspective | |
// Set UV to position; transform its range to [0, 1] | |
uv = (pos.xy + vec2(1, 1)) * 0.5; | |
// Determine if uv is in range or repeating pattern | |
if (((uv.x > 0.0 && uv.x < 1.0) || REPEAT_X) && ((uv.y > 0.0 && uv.y < 1.0) || REPEAT_Y) && (w > 0.0 || FLIP)) { | |
// Apply texture | |
uv = mod(uv, 1.0); | |
COLOR = texture(TEXTURE, uv); | |
} else { | |
COLOR = vec4(0, 0, 0, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment