【Defold】Sprite用 Outline シェーダー

2020/10/12Defold, DEVELOP

Defold 用の Outlineシェーダーです。

uniform highp mat4 view_proj;
 
attribute highp vec4 position;
attribute mediump vec2 texcoord0;
 
varying mediump vec2 var_texcoord0;
 
void main()
{
    gl_Position = view_proj * vec4(position.xyz, 1.0);
    var_texcoord0 = texcoord0;
}
varying mediump vec2 var_texcoord0;
uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;
uniform lowp vec4 _TextureSize;
uniform lowp vec4 _OutlineColor;
uniform lowp vec4 _OutlineWidth;
void main()
{
    lowp vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    lowp vec4 col = texture2D(texture_sampler, var_texcoord0.xy) * tint_pm;
    vec2 texelSize = vec2(1.0 / _TextureSize.x, 1.0 / _TextureSize.y);
    texelSize *= _OutlineWidth.w;
        
    float leftPixel = texture2D(texture_sampler, var_texcoord0.xy + vec2(-texelSize.x, 0)).w;
    float upPixel = texture2D(texture_sampler, var_texcoord0.xy + vec2(0, texelSize.y)).w;
    float rightPixel = texture2D(texture_sampler, var_texcoord0.xy + vec2(texelSize.x, 0)).w;
    float bottomPixel = texture2D(texture_sampler, var_texcoord0.xy + vec2(0, -texelSize.y)).w;
    float outline = (1.0 - leftPixel * upPixel * rightPixel * bottomPixel) * col.w;
    gl_FragColor = mix(col, _OutlineColor, outline);
}

マテリアルの設定はこちらです。

目次

結果

お知らせ

Posted by kazupon