Shader Screen-Reading behind an Object

Godot Version

4.3

Question

I got this black hole shader working, but the screen texture reads the screen infront the black hole aswell. (You can see the Fish in the blackhole)


Is there a way to only read the screen behind the black hole

The shader is on a Quad mesh instance

shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_disabled, diffuse_burley, specular_disabled, unshaded, shadows_disabled;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;

uniform sampler2D curve;

uniform vec3 uv1_scale;
uniform vec3 uv1_offset;
uniform vec3 uv2_scale;
uniform vec3 uv2_offset;

void vertex() {
	UV = UV * uv1_scale.xy + uv1_offset.xy;

	// Billboard Mode: Enabled
	MODELVIEW_MATRIX = VIEW_MATRIX * mat4(
			MAIN_CAM_INV_VIEW_MATRIX[0],
			MAIN_CAM_INV_VIEW_MATRIX[1],
			MAIN_CAM_INV_VIEW_MATRIX[2],
			MODEL_MATRIX[3]);
	MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);
}

void fragment() {
	
	vec2 vec_to_center = vec2(0.5, 0.5) - UV;
	float center_dist = length(vec_to_center);
	float curve_value = texture(curve, vec2(center_dist)).r;
	vec2 diff = normalize(vec_to_center) * 0.6 * curve_value;
	
	ALBEDO = texture(SCREEN_TEXTURE, SCREEN_UV - diff).rgb;
}

Hey, did you ever solved this? I’m working on a black hole shader too and I was wondering this

Check Screen-reading shaders — Godot Engine (stable) documentation in English

This should be doable with some Viewport trickery.

An alternative approach could be to check the depth map and try to use some kind of mock value (~black since space) for pixels that are in front of the black hole.

A third approach is to make the object you don’t want to show up in the screen texture transparent since those aren’t included in the hint_screen_texture :slightly_smiling_face: But this of course means that they aren’t included even if behind the black hole