3D border distance

Used to detect the distance from the player to the border and realize the range luminous display.

The default is to use a noise map, but it can be modified to a specified texture.

 

How to use:

Add to the object material (if you need to keep it transparent, please add it to next_pass)

Dynamically update player_position in the code

Shader code
shader_type spatial;
render_mode blend_mix, unshaded; // 启用透明混合

// 控制参数
uniform float fade_radius : hint_range(0.5, 10.0) = 2.0;
uniform float max_intensity : hint_range(0.0, 5.0) = 1.0;
uniform vec3 player_position;
uniform sampler2D noise_tex : source_color; // 噪声纹理
uniform float noise_scale : hint_range(0.1, 5.0) = 1.0;
uniform float pulse_speed : hint_range(0.0, 5.0) = 1.0;

varying vec3 world_pos; // 传递世界坐标

void vertex() {
    // 计算顶点世界坐标(关键修正)
    world_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
}

void fragment() {
    // 动态半径(呼吸效果)
    float animated_radius = fade_radius * (0.9 + 0.1 * sin(TIME * pulse_speed));
    
    // 计算玩家距离(世界坐标系)
    float distance_to_player = distance(world_pos, player_position);
    
    // 基础渐变强度
    float intensity = 1.0 - smoothstep(0.0, animated_radius, distance_to_player);
    
    // 添加噪声扰动
    vec2 noise_uv = UV * noise_scale;
    float noise = texture(noise_tex, noise_uv).r;
    intensity *= mix(0.5, 1.5, noise); // 噪声影响强度
    
    // 颜色混合
    vec4 base_color = vec4(0.0, 0.0, 0.0, 0.0); // 完全透明
    vec4 glow_color = vec4(0.5, 0.8, 1.0, 0.8); // 半透明光晕
    
    // 最终效果
    ALBEDO = glow_color.rgb * intensity * max_intensity;
    EMISSION = ALBEDO;
    ALPHA = glow_color.a * intensity; // 根据强度控制透明度
}
Tags
3d, border, boundary
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.

Related shaders

Hex Mask/Border/Outline

Animated TV-Static Border Shader

Diagonal Mask / Border / Edge

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments