Mesh Blending with Alpha

This shader is used to blend between two meshes or mesh and terrain, it is applied to the mesh. The blending works via the alpha value. The problems of non-convex meshes, are avoided by setting the render_mode to depth_draw_always.

Features:

– works with non-convex meshes

– no triplanar mapping

 

Shader code
/*
	Mesh Blending with Alpha Shader by Firerabbit
	
	MIT License
*/
shader_type spatial;
render_mode depth_draw_always;

uniform float proximity_fade_distance : hint_range(0.0, 1.0) = 0.3;
uniform float falloff : hint_range(0.001, 5.0) = 1.0;
uniform vec4 color : hint_color = vec4(1.0);
uniform sampler2D albedo : hint_albedo;


float saturate(float val) {
	return clamp(val, 0.0, 1.0);
}


void fragment() {
	float depth = texture(DEPTH_TEXTURE,SCREEN_UV).r;
	
	vec2 remaped_uv = UV * vec2(2.0) - vec2(1.0);
	float remaped_depth = depth * 2.0 - 1.0;
	
	vec4 world_pos = INV_PROJECTION_MATRIX * vec4(remaped_uv, remaped_depth, 1.0);
	world_pos.xyz /= world_pos.w;
	
	ALPHA *= saturate(1.0 - smoothstep(world_pos.z + proximity_fade_distance, world_pos.z, VERTEX.z));
	ALPHA = pow(ALPHA, falloff);
	ALBEDO = color.rgb * texture(albedo, UV).rgb;
}
Tags
alpha, blending, terrain
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Firerabbit

Dither opacity with GLES2 Support

Toon Shader

Post Effect Outline Shader for GLES2

Related shaders

Terrain Mesh Blending with Dithering

mesh-terrain blending

Uniplanar mapping with dithered blending

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments