4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】イメージエフェクトを使用しない簡易GlowShader

Posted at

なるべく軽量にするため、イメージエフェクトを使用せずに光の玉を表現させる一例です。

gif_animation_005_m.gif
一枚の板で、円をぼかし拡大縮小して
光っている風に見せかけてます。

vertex

どこから見ても同じように見せるようにビルボードにします。


Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }

・・・

struct appdata
{
    float4 vertex : POSITION;
    float2 uv : TEXCOORD0;
};
 
struct v2f
{
     float2 uv : TEXCOORD0;
     float4 vertex : SV_POSITION;
};

・・・

v2f vert (appdata v)
{
  v2f o;

  o.vertex = mul(UNITY_MATRIX_P,
      mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0)) 
      + float4(v.vertex.x, v.vertex.y, 0.0, 0.0)
      * float4(0.5, 0.5, 1.0, 1.0)); // XYのスケーリング
}

ビルボードについての詳細はこちらの記事が詳しいです。
常にカメラを向くオブジェクトをGPUで実装

fragment

四角形からはみ出ない程度に円を描いて良い感じにぼかし、アニメーションさせています。
形状によってはテクスチャのほうがきれいに表現できるかと思います。

fixed4 frag (v2f i) : SV_Target
{
    float4 col = _Color;
 
    float dist= length(i.uv - 0.5) * 2;
    // 外に行くほど薄くなるようぼかす
    col *= saturate(1 - dist);
    // 中心の光具合を強調・拡縮アニメーション
    col *= (1 - dist) * lerp(1, 3, sin(_Time.z) * 0.5 + 0.5);

    col = saturate(col);
 
    clip(col.a - 0.0001);
    return col;
}

アルファブレンド

背景と馴染むようこれもよい感じに調整。
サンプルの場合は

Blend SrcAlpha DstAlpha

ちなみに確認時は以下のようにしておくとmaterialで調整ができるので便利です。

Properties
{
    [Enum(UnityEngine.Rendering.BlendMode)]_BlendSrc("Blend Src", Float) = 0
    [Enum(UnityEngine.Rendering.BlendMode)]_BlendDst("Blend Dst", Float) = 0
}
・・・

Blend [_BlendSrc][_BlendDst]

利用できる場面

結局板なので、角度によって他のオブジェクトに板が遮られたりすると
切れ目が不自然に見えたりしまいます。
近距離のオブジェクトとの兼ね合いでどのくらい許容できるか次第かと思います。

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?