2
1

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 1 year has passed since last update.

【Unity】DissolveShader

Posted at

概要

じわーっと消えてくエフェクトです。
Dissolve.gif

消したい画像にノイズ画像を組み合わせると実現できます。
今回はPerlinノイズを使用しました。

実装

Shader "Custom/Dissolve"
{
    Properties
    {
        [HDR]
        _BaseColor ("Color", Color) = (1, 1, 1)
        [HDR]
        _EdgeColor ("Dissolve Color", Color) = (0, 0, 0)
        _MainTex ("Texture", 2D) = "white" {}
        _DissolveTex ("Dissolve Texture", 2D) = "white" {}
        _AlphaClipThreshold ("Alpha Clip Threshold", Range(0, 1)) = 1
        _EdgeWidth ("Dissolve Margin Width", Range(0, 1)) = 0.01
    }
    SubShader
    {
        Tags {
            "RenderType"="Transparent"
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderPipeline"="UniversalPipeline"
        }
        LOD 100

        Pass
        {
            Name "ForwardLit"
            Tags { "LightMode"="UniversalForward" }

            Cull Off
            Lighting Off
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha

            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            struct Attributes
            {
                float4 positionOS : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct Varyings
            {
                float2 uv : TEXCOORD0;
                float4 positionHCS : SV_POSITION;
            };

            half4 _BaseColor;
            half4 _EdgeColor;
            sampler2D _MainTex;
            sampler2D _DissolveTex;
            half _AlphaClipThreshold;
            half _EdgeWidth;

            CBUFFER_START(UnityPerMaterial)
            float4 _MainTex_ST;
            float4 _DissolveTex_ST;
            CBUFFER_END

            Varyings vert (Attributes v)
            {
                Varyings o;
                o.positionHCS = TransformObjectToHClip(v.positionOS.xyz);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            half4 frag (Varyings i) : SV_Target
            {
                half4 edgeColor = half4(1, 1, 1, 1);

                half4 dissolve = tex2D(_DissolveTex, i.uv);
                float alpha = (dissolve.r * 0.2 + dissolve.g * 0.7 + dissolve.b * 0.1) * 0.999;
                
                if (alpha < _AlphaClipThreshold + _EdgeWidth && _AlphaClipThreshold > 0)
                {
                    edgeColor = _EdgeColor;
                }
                if (alpha < _AlphaClipThreshold)
                {
                    discard;
                }
                return tex2D(_MainTex, i.uv) * _BaseColor * edgeColor;
            }
            ENDHLSL
        }
    }
}

あとがき

ノイズ画像によってエフェクトががらっと変わるので色々試してみるのがいいかもしれません。
細かいノイズより大雑把なものの方が見た目良さそう:thinking:

参考にしたリンク
https://qiita.com/JunNishimura/items/14b3ae3f80c9098aeb5a
https://zenn.dev/r_ngtm/books/shadergraph-cookbook/viewer/recipe-dissolve

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?