【Defold】modelコンポーネント用「Hologram」シェーダー
目次
シェーダーコード
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 _HologramStripesAmount;
uniform lowp vec4 _HologramUnmodAmount;
uniform lowp vec4 _HologramStripesSpeed;
uniform lowp vec4 _HologramMinAlpha;
uniform lowp vec4 _HologramMaxAlpha;
uniform lowp vec4 _Time;
uniform lowp vec4 _RandomSeed;
float saturate(float f)
{
return clamp(f, 0.0, 1.0);
}
float RemapFloat(float inValue, float inMin, float inMax, float outMin, float outMax)
{
return outMin + (inValue - inMin) * (outMax - outMin) / (inMax - inMin);
}
void main()
{
vec4 tint_pm = vec4(tint.xyz * tint.w, tint.w);
vec4 col = texture2D(tex0, var_texcoord0.xy) * tint_pm;
float totalHologram = _HologramStripesAmount.w + _HologramUnmodAmount.w;
float hologramYCoord = mod((var_texcoord0.y + (mod((_Time.w + _RandomSeed.w),1) * _HologramStripesSpeed.w)),totalHologram) / totalHologram;
float alpha = RemapFloat(saturate(hologramYCoord - (_HologramUnmodAmount.w / totalHologram)), 0.0, 1.0, _HologramMinAlpha.w, saturate(_HologramMaxAlpha.w));
col.w *= mix(alpha, 1.0, max(sign((_HologramUnmodAmount.w / totalHologram) - hologramYCoord), 0.0));
col.xyz *= max(1, _HologramMaxAlpha.w * max(sign(hologramYCoord - (_HologramUnmodAmount.w / totalHologram)), 0.0));
gl_FragColor = col;
}
マテリアルの設定はこちらです。
テスト用スクリプト
function init(self)
go.set(msg.url("#model"), "_RandomSeed.w", math.random(0, 1000.0))
go.set(msg.url("#model"), "_RandomSeed.w", 0.0)
go.animate(msg.url("#model"), "_Time.w", go.PLAYBACK_LOOP_FORWARD, 1.0, go.EASING_LINEAR, 20.0)
end