天天看點

【godot shader】圓形裁剪,圓形頭像

1.建立Material

【godot shader】圓形裁剪,圓形頭像

2.建立Shader

【godot shader】圓形裁剪,圓形頭像

3.輸入以下代碼

shader_type canvas_item;

// 紋理的尺寸
uniform vec2 texture_size;
// 圓半徑
uniform float radius;

// 判斷是否在圓内
bool check(vec2 texCoord) {
	vec2 pos = vec2(texCoord.x*texture_size.x, (1.0 - texCoord.y)*texture_size.y);
	if (pow(pos.x - texture_size.x*0.5, 2.0) + pow(pos.y - texture_size.y*0.5, 2.0) <= pow(radius, 2.0)){
	    return true;
	} else {
	    return false;
	}
}

void fragment(){
	vec4 o = texture(TEXTURE, UV);
    if(!check(UV)) {
		//不在圓内則透明
      o *= vec4(0, 0, 0, 0);
    }
    COLOR = o;
}
           

繼續閱讀