Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Last active June 29, 2024 05:23
Show Gist options
  • Save partybusiness/a2426f551ef58de38a616db8e927cef4 to your computer and use it in GitHub Desktop.
Save partybusiness/a2426f551ef58de38a616db8e927cef4 to your computer and use it in GitHub Desktop.
Godot Depth Cube
shader_type spatial;
render_mode unshaded, blend_mix, shadows_disabled;
uniform vec4 main_colour:source_color;
//calculates depth to the back of a cube behind each 0 to 1 UV face.bool
//If you have a cube where each face is 0 to 1 UV then it calculates the distance to each side and the back
//based on my earlier depth cube in Unity: https://gist.github.com/partybusiness/b7ec339d16d3f3b7e2b202ac4e5d8686
void fragment() {
vec3 calc_bi = cross(TANGENT, NORMAL);
mat3 view_to_surface = inverse(mat3(TANGENT, calc_bi, NORMAL));
vec3 viewDir = view_to_surface * (-VERTEX + EYE_OFFSET);//viewDir_pass
float distToGround = max(length((UV.y / viewDir.y)* viewDir), (viewDir.y<0.0)?3.0:0.0);
float distToRoof = max(length((-(1.0 - UV.y) / viewDir.y)* viewDir), (viewDir.y>0.0)?3.0:0.0);
float distToLeft = max(length(((UV.x ) / viewDir.x) * viewDir), (viewDir.x<0.0)?3.0:0.0);
float distToRight = max(length((-(1.0 - UV.x ) / viewDir.x) * viewDir), (viewDir.x>0.0)?3.0:0.0);
float distToBack = length(1.0 / viewDir.z * viewDir);
float minDist = min(distToBack, min( min(distToGround, distToRoof), min(distToLeft, distToRight)));
ALBEDO = main_colour.rgb;
ALPHA = (main_colour.a * minDist);
}
shader_type spatial;
render_mode unshaded, blend_mix, shadows_disabled;
uniform vec4 main_colour:source_color;
uniform float icon_depth = 0.5;
uniform sampler2D icon_texture:repeat_disable, filter_nearest;
//calculates depth to the back of a cube behind each 0 to 1 UV face.bool
//If you have a cube where each face is 0 to 1 UV then it calculates the distance to each side and the back
//this one adds the ability to include an icon texture inset at a given depth
//based on my earlier depth cube in Unity: https://gist.github.com/partybusiness/b7ec339d16d3f3b7e2b202ac4e5d8686
void fragment() {
vec3 calc_bi = cross(TANGENT, NORMAL);
mat3 view_to_surface = inverse(mat3(TANGENT, calc_bi, NORMAL));
vec3 viewDir = view_to_surface * (-VERTEX + EYE_OFFSET);//viewDir_pass
float distToGround = max(length((UV.y / viewDir.y)* viewDir), (viewDir.y<0.0)?3.0:0.0);
float distToRoof = max(length((-(1.0 - UV.y) / viewDir.y)* viewDir), (viewDir.y>0.0)?3.0:0.0);
float distToLeft = max(length(((UV.x ) / viewDir.x) * viewDir), (viewDir.x<0.0)?3.0:0.0);
float distToRight = max(length((-(1.0 - UV.x ) / viewDir.x) * viewDir), (viewDir.x>0.0)?3.0:0.0);
float distToBack = length(1.0 / viewDir.z * viewDir);
float minDist = min(distToBack, min( min(distToGround, distToRoof), min(distToLeft, distToRight)));
vec2 iconUV = clamp(UV - viewDir.xy/viewDir.z * icon_depth, 0.0,1.0);
vec4 icon_sample = texture(icon_texture, iconUV);
ALBEDO = main_colour.rgb * icon_sample.rgb;
ALPHA = (main_colour.a * minDist) * icon_sample.a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment