55 lines
1.6 KiB
Plaintext
55 lines
1.6 KiB
Plaintext
shader_type spatial;
|
|
render_mode blend_add, cull_disabled;
|
|
|
|
uniform vec4 intersection_color : source_color = vec4(1.0, 0.5, 0.0, 1.0);
|
|
uniform float intersection_thickness = 0.5;
|
|
uniform float intensity = 1.0f;
|
|
uniform float dots_per_radian : hint_range(1.0, 50.0, 1.0) = 10.0f;
|
|
uniform sampler2D DEPTH_TEXTURE : hint_depth_texture, filter_linear_mipmap;
|
|
varying vec3 v_normal;
|
|
|
|
void vertex() {
|
|
v_normal = NORMAL;
|
|
}
|
|
|
|
|
|
void fragment() {
|
|
// 1. Get linear depth of the scene (opaque objects)
|
|
float depth = texture(DEPTH_TEXTURE, SCREEN_UV).x;
|
|
vec3 ndc = vec3(SCREEN_UV * 2.0 - 1.0, depth);
|
|
vec4 view = INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
|
|
view.xyz /= view.w;
|
|
float linear_depth = -view.z;
|
|
|
|
// 2. Get linear depth of the current object
|
|
float linear_frag_coord = -VERTEX.z;
|
|
|
|
// 3. Calculate difference
|
|
float diff = linear_depth - linear_frag_coord;
|
|
|
|
// 4. Create intersection mask
|
|
float intersect = clamp(diff / intersection_thickness, 0.0, 1.0);
|
|
|
|
// Invert so the intersection is 1.0
|
|
intersect = 1.0 - intersect;
|
|
|
|
// Output color
|
|
float strength = .25 + .75 * (1.0 + sin(TIME*3.0))/2.0;
|
|
|
|
ALPHA = max(.05, intersect * intersection_color.a * strength);
|
|
float angle = atan(v_normal.z, v_normal.x);
|
|
float dot_step = PI / dots_per_radian;
|
|
angle = round(angle / dot_step + 0.5f);
|
|
|
|
if(ALPHA == .05){
|
|
ALBEDO = intersection_color.rgb;
|
|
ALPHA = 0.01;
|
|
}else{
|
|
ALBEDO = intersection_color.rgb * strength;
|
|
EMISSION = intersection_color.rgb * intensity * strength; // Glow effect
|
|
if(mod(angle,2.0) == 0.0){
|
|
ALPHA = 0.0;
|
|
}
|
|
}
|
|
|
|
} |