【Defold】Shader:ガウス処理

Defold, DEVELOP

3×3のシンプルなぼかしシェーダーです。

Material プロパティ

Vertex Constants

NameTypeValue
view_projViewProj0, 0, 0, 0
u_texelOffsetUser0, 0, 0, 0

Fragment Constants

NameTypeValue
tintUser1, 1, 1, 1

Tags

tile

シェーダーコード

uniform highp mat4 view_proj;
// positions are in world space
attribute highp vec4 position;
attribute mediump vec2 texcoord0;
varying mediump vec2 var_texcoord0;
varying vec2 blurCoordinates[9];
uniform vec4 u_texelOffset;
void main()
{
	gl_Position = view_proj * vec4(position.xyz, 1.0);
	var_texcoord0 = texcoord0;
	blurCoordinates[0] = var_texcoord0.st + vec2( 0.0,  0.0) * u_texelOffset.st;
	blurCoordinates[1] = var_texcoord0.st + vec2(+1.0,  0.0) * u_texelOffset.st;
	blurCoordinates[2] = var_texcoord0.st + vec2(-1.0,  0.0) * u_texelOffset.st;
	blurCoordinates[3] = var_texcoord0.st + vec2( 0.0, +1.0) * u_texelOffset.st;
	blurCoordinates[4] = var_texcoord0.st + vec2( 0.0, -1.0) * u_texelOffset.st;
	blurCoordinates[5] = var_texcoord0.st + vec2(-1.0, -1.0) * u_texelOffset.st;
	blurCoordinates[6] = var_texcoord0.st + vec2(+1.0, -1.0) * u_texelOffset.st;
	blurCoordinates[7] = var_texcoord0.st + vec2(-1.0, +1.0) * u_texelOffset.st;
	blurCoordinates[8] = var_texcoord0.st + vec2(+1.0, +1.0) * u_texelOffset.st;	
}
varying mediump vec2 var_texcoord0;
uniform lowp sampler2D texture_sampler;
uniform lowp vec4 tint;
varying vec2 blurCoordinates[9];
void main()
{
	vec4 sum = vec4(0.0);
	sum += texture2D(texture_sampler, blurCoordinates[0]) * 4.0;
	sum += texture2D(texture_sampler, blurCoordinates[1]) * 2.0;
	sum += texture2D(texture_sampler, blurCoordinates[2]) * 2.0;
	sum += texture2D(texture_sampler, blurCoordinates[3]) * 2.0;
	sum += texture2D(texture_sampler, blurCoordinates[4]) * 2.0;
	sum += texture2D(texture_sampler, blurCoordinates[5]) * 1.0;
	sum += texture2D(texture_sampler, blurCoordinates[6]) * 1.0;
	sum += texture2D(texture_sampler, blurCoordinates[7]) * 1.0;
	sum += texture2D(texture_sampler, blurCoordinates[8]) * 1.0;
	sum /= 16.0; 
	gl_FragColor = sum;	
}

スクリプト

function init(self)
    local width = 128;
    local height = 128;
    model.set_constant("gameobject の url", "u_texelOffset", vmath.vector4(0.5/width , 0.5/height, 0, 0))
end

結果

Posted by kazupon