mix 2 tilemap to break repetitive
How to Use the Shader:
-
Attach the Shader:
Apply this shader to the top tilemap layer (e.g., grass) using aShaderMaterial
. -
Assign a Noise Texture:
-
In the material properties, set the
noise_texture
uniform to a seamless noise texture. -
Ensure the texture’s Wrap Mode is set to Repeat in the import settings.
-
-
Adjust Transparency:
-
Modify the
threshold
value (0.0 to 1.0) to control how much of the layer becomes transparent. -
Lower values make more areas transparent, revealing the bottom tilemap layer (e.g., dirt).
-
-
Control Noise Scale:
-
Use the
noise_scale
uniform to adjust the size of the noise pattern. -
Increasing the values makes the noise pattern appear smaller and more frequent.
-
-
Ensure Tilemap Alignment:
-
This shader tiles the noise across the entire tilemap, not per individual tile.
-
If the effect moves unexpectedly, ensure your tilemap’s transform is correct.
-
This will create a natural blending effect between the two tilemap layers! 🚀
Shader code
shader_type canvas_item;
varying vec2 world_position;
uniform sampler2D noise_texture; // Global noise texture
uniform float threshold : hint_range(0.0, 1.0) = 0.5; // Transparency threshold
uniform float blur_control : hint_range(0.0, 0.5) = 0.1; // Controls the range of the smooth transition
uniform bool invert_noise = false; // Inverts the noise texture if true
uniform vec2 noise_scale = vec2(0.115, 0.105); // Scale factor for the noise texture
void vertex() {
// Compute the world position using the model matrix.
world_position = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
}
void fragment() {
// Generate a tiling UV for the noise texture.
vec2 world_uv = fract(world_position * noise_scale * 0.01);
// Sample the noise texture.
float noise_value = texture(noise_texture, world_uv).r;
// Optionally invert the noise value based on the boolean control.
if (invert_noise) {
noise_value = 1.0 - noise_value;
}
// Get the original texture color.
vec4 tex_color = texture(TEXTURE, UV);
// Use smoothstep to create a soft transition based on the blur_control value.
float alpha = smoothstep(threshold - blur_control, threshold + blur_control, noise_value);
tex_color.a *= alpha;
COLOR = tex_color;
}