【Defold】modelコンポーネント用「Blur」シェーダー

Defold, DEVELOP

ぼかし。

目次

シェーダーコード

attribute highp vec4 position;
attribute mediump vec2 texcoord0;
attribute mediump vec3 normal;
 
uniform mediump mat4 mtx_worldview;
uniform mediump mat4 mtx_view;
uniform mediump mat4 mtx_proj;
uniform mediump mat4 mtx_normal;
 
varying highp vec4 var_position;
varying mediump vec3 var_normal;
varying mediump vec2 var_texcoord0;
 
void main()
{
    vec4 p = mtx_worldview * vec4(position.xyz, 1.0);
    var_position = p;
    var_texcoord0 = texcoord0;
    var_normal = normalize((mtx_normal * vec4(normal, 0.0)).xyz);
    gl_Position = mtx_proj * p;
}
varying highp vec4 var_position;
varying mediump vec3 var_normal;
varying mediump vec2 var_texcoord0;
 
uniform lowp sampler2D tex0;
uniform lowp vec4 tint;
uniform lowp vec4 _BlurIntensity;
 
float Blur_G(float bhqp, float x)
{
    return exp(-(x * x) / (2.0 * bhqp * bhqp));
}
 
vec4 Blur(vec2 uv, sampler2D source, float Intensity)
{
    int iterations = 16;
    int halfIterations = iterations / 2;
    float sigmaX = 0.1 + Intensity * 0.5;
    float sigmaY = sigmaX;
    float total = 0.0;
    vec4 ret = vec4(0, 0, 0, 0);
     
    for (int iy = 0; iy < iterations; ++iy)
    {
        float fy = Blur_G(sigmaY, float(iy) -float(halfIterations));
        float offsety = float(iy - halfIterations) * 0.00390625;
        for (int ix = 0; ix < iterations; ++ix)
        {
            float fx = Blur_G(sigmaX, float(ix) - float(halfIterations));
            float offsetx = float(ix - halfIterations) * 0.00390625;
            total += fx * fy;
            ret += texture2D(source, uv + vec2(offsetx, offsety)) * fx * fy;
        }
    }
    return ret / total;
}
 
void main()
{
    vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
    vec4 col = Blur(var_texcoord0.xy, tex0, _BlurIntensity.w) * tint_pm;
 
    gl_FragColor = col;
}

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

テスト用スクリプト

function init(self)
    go.set(msg.url("#model"), "_BlurIntensity.w", 0.0)
    go.animate(msg.url("#model"), "_BlurIntensity.w", go.PLAYBACK_LOOP_PINGPONG, 10.0, go.EASING_LINEAR, 2.0)
end

結果

お知らせ

Posted by kazupon