Depth Buffer Object Edge Dissolve

To apply:

Create a MeshInstance

Set the mesh to QuadMesh

Create a new ShaderMaterial

Paste the shader and tweak the settings

Shader code
shader_type spatial;
render_mode unshaded;

uniform float zNear = 1.0;
uniform float zFar = 5.0;

uniform vec4 effectColor : hint_color = vec4(1);
uniform bool flatColor = false;

void vertex() {
	POSITION = vec4(VERTEX, 1.0);
}

float _linearizeScale(vec4 view){
	float linearDepth = vec3((view.xyz / view.w)).z;
	float depthScaled =  2.0 * (zNear * zFar) / (zFar + (linearDepth * zNear));
	return depthScaled;
}

void fragment() {
	//depth sample at screen_uv coords;
	float depthSample = texture(DEPTH_TEXTURE, SCREEN_UV).x;
	
	vec3 ndc = vec3(SCREEN_UV, depthSample);
	vec4 view = (INV_PROJECTION_MATRIX * vec4(ndc, 1.0));
	float realDepth = _linearizeScale(view);
	
	if (flatColor) {
		if (realDepth > 0.0) {
			realDepth = 1.0;
		}
	}
	
	ALBEDO = vec3(realDepth) * effectColor.rgb;
	
//	debug;
//	vec4 originalColor = texture(SCREEN_TEXTURE, SCREEN_UV);
//	ALBEDO = originalColor.rgb;
}
Tags
Color, Depth buffer, edge highlight, post process
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from absentSpaghetti

Dotted Circle

Sobel Edge Detection Post Process

Linear Depth/Depth Fog

Related shaders

Sobel Edge Outline shader per-object

Move object’s DEPTH value relative to camera

Per object depth-based outline

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments