【Defold】modelコンポーネント用「Inner Outline」シェーダー
内側を縁取りする。
目次
シェーダーコード
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;
uniform mediump vec4 light;
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 _InnerOutlineColor;
uniform lowp vec4 _InnerOutlineThickness;
uniform lowp vec4 _InnerOutlineAlpha;
uniform lowp vec4 _InnerOutlineGlow;
vec3 GetPixel(float offsetX, float offsetY, vec2 uv, sampler2D tex)
{
// 決め打ちの400はtex0のテクスチャサイズ。とりあえず決め打ち。
vec2 texelSize = vec2(1.0 / 400.0, 1.0 / 400.0);
return texture2D(tex, (uv + vec2(offsetX * texelSize.x, offsetY * texelSize.y))).xyz;
}
void main()
{
vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
vec4 col = texture2D(tex0, var_texcoord0.xy) * tint_pm;
vec3 innerT = abs(GetPixel(0.0, _InnerOutlineThickness.w, var_texcoord0.xy, tex0) - GetPixel(0.0, -_InnerOutlineThickness.w, var_texcoord0.xy, tex0));
innerT += abs(GetPixel(_InnerOutlineThickness.w, 0.0, var_texcoord0.xy, tex0) - GetPixel(-_InnerOutlineThickness.w, 0.0, var_texcoord0.xy, tex0));
innerT = (innerT / 2.0) * col.w * _InnerOutlineAlpha.w;
col.xyz += length(innerT) * _InnerOutlineColor.xyz * _InnerOutlineGlow.w;
/*
innerT *= col.w * _InnerOutlineAlpha.w;
col.xyz = length(innerT) * _InnerOutlineColor.xyz * _InnerOutlineGlow.w;
col.w = step(0.3, col.x + col.y + col.z);
*/
gl_FragColor = col;
}
マテリアルの設定はこちらです。