Godot Shader描边

Posted 张学徒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Godot Shader描边相关的知识,希望对你有一定的参考价值。

Godot 3.4.2

让图片内容描边

shader_type canvas_item;

uniform vec4 color : hint_color = vec4(1.0);
uniform float width : hint_range(0, 50) = 1.0;
uniform int pattern : hint_range(0, 2) = 0;
uniform bool inside = false;
uniform bool add_margins = true;
uniform bool visible = true;

void vertex()
	if (visible)
	    if (add_margins)
	        VERTEX += (UV * 2.0 - 1.0) * width;
	    
	


bool hasContraryNeighbour(vec2 uv, vec2 texture_pixel_size, sampler2D texture)
    for (float i = -ceil(width); i <= ceil(width); i++)
        float x = abs(i) > width ? width * sign(i) : i;
        float offset;

        if (pattern == 0)
            offset = width - abs(x);
         else if (pattern == 1)
            offset = floor(sqrt(pow(width + 0.5, 2) - x * x));
         else if (pattern == 2)
            offset = width;
        

        for (float j = -ceil(offset); j <= ceil(offset); j++)
            float y = abs(j) > offset ? offset * sign(j) : j;
            vec2 xy = uv + texture_pixel_size * vec2(x, y);
            if ((xy != clamp(xy, vec2(0.0), vec2(1.0)) || texture(texture, xy).a == 0.0) == inside)
                return true;
            
        

    
    return false;


void fragment()
	if (visible)
		vec2 uv = UV;
		 if (add_margins) 
	        vec2 texture_pixel_size = vec2(1.0) / (vec2(1.0) / TEXTURE_PIXEL_SIZE + vec2(width * 2.0));
	        uv = (uv - texture_pixel_size * width) * TEXTURE_PIXEL_SIZE / texture_pixel_size;
	        if (uv != clamp(uv, vec2(0.0), vec2(1.0)))
	            COLOR.a = 0.0;
	         else 
	            COLOR = texture(TEXTURE, uv);
	        
	     else 
	        COLOR = texture(TEXTURE, uv);
	    

	    if ((COLOR.a > 0.0) == inside && hasContraryNeighbour(uv, TEXTURE_PIXEL_SIZE, TEXTURE))
	        COLOR.rgb = inside ? mix(COLOR.rgb, color.rgb, color.a): color.rgb;
	        COLOR.a += (1.0 - COLOR.a) * color.a;
	    
	 else 
		COLOR = texture(TEXTURE, UV);
	

以上是关于Godot Shader描边的主要内容,如果未能解决你的问题,请参考以下文章

Godot Shader替换颜色

Godot Shader替换颜色

Godot Shader替换颜色

关于Unity中的模型描边与Shader切换(专题二)

描边shader(法线外拓)

Unity Shader实现描边效果