使用噪聲紋理制作消融效果

  1. 噪聲紋理(一張灰度圖)采樣得到其中噪聲值(rgb中其中一個(gè)就行),使用這個(gè)值進(jìn)行alphaTest或者其他用途
  2. 使用了一個(gè)過渡色,即將消融與不消融之間的過渡顏色,具體原理在代碼中有注釋
  3. 陰影的特殊處理,因?yàn)橄诤蟛粦?yīng)該再投射陰影,所以必須要自己定義“LightMode” = “ShadowCaster”陰影投射pass,得到正確的陰影投射圖。
Shader "FFD/15/Dissolve"
{
    Properties
    {
        _BurnAmount("Burn Amount",range(0.0,1.0)) = 0.0
        _LineWidth("Burn Line Width",range(0.0,0.2)) = 0.1
        _BumpScale ("Bump Scale",range(-1,1)) = 0
        _MainTex("Main Tex",2D) = "white"{}
        _BumpTex("Bump Map",2D) = "bump"{}
        _BurnTex("Burn Map",2D) = "white"{}
        _BurnFirstColor("Burn First Color",Color) = (1,1,1,1)
        _BurnSecondColor("Burn Second Color",Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
    

        Pass
        {
            cull off
            Tags{"LightMode" = "ForwardBase"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
        
            #pragma multi_compile_fwardbase
            
            #include "UnityCG.cginc"
            #include "autolight.cginc"
            #include "lighting.cginc"

            struct v2f
            {
                float2 mainUV : TEXCOORD0;
                float2 bumpUV : TEXCOORD1;
                float2 burnUV : TEXCOORD2;
                float3 worldNormal : NORMAL;
                float4 TtoW0 : TEXCOORDD3;
                float4 TtoW1 : TEXCOORD4;
                float4 TtoW2 : TEXCOORD5;
                float4 pos : SV_POSITION;
                SHADOW_COORDS(6)
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _BumpTex;
            float4 _BumpTex_ST;
            sampler2D _BurnTex;
            float4 _BurnTex_ST;
            float _BurnAmount;
            float _LineWidth;
            float _BumpScale;
            fixed4 _BurnFirstColor;
            fixed4 _BurnSecondColor;


            v2f vert (appdata_full v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);

                o.mainUV = TRANSFORM_TEX(v.texcoord, _MainTex);
                o.bumpUV = TRANSFORM_TEX(v.texcoord, _BumpTex);
                o.burnUV = TRANSFORM_TEX(v.texcoord, _BurnTex);

                o.worldNormal = UnityObjectToWorldNormal(v.normal);

                float3 biNormal = cross(v.normal,v.tangent.xyz)*v.tangent.w;
                float3 biNormalWorld = UnityObjectToWorldDir(biNormal);

                float3 tangentWorld = UnityObjectToWorldDir(v.tangent);

                float4 worldPos = mul(unity_ObjectToWorld,v.vertex);

                o.TtoW0 = float4(tangentWorld.x,biNormalWorld.x,o.worldNormal.x,worldPos.x);
                o.TtoW1 = float4(tangentWorld.y,biNormalWorld.y,o.worldNormal.y,worldPos.y);
                o.TtoW2 = float4(tangentWorld.z,biNormalWorld.z,o.worldNormal.z,worldPos.z);


                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.mainUV);

                fixed3 burn = tex2D(_BurnTex,i.burnUV).rgb;

                clip(burn.g-_BurnAmount);

                float3x3 tanToWorld = float3x3(i.TtoW0.xyz,i.TtoW1.xyz,i.TtoW2.xyz);

                float4 worldPos = float4(i.TtoW0.w,i.TtoW1.w,i.TtoW2.w,1);

                float3 norBump = UnpackNormal(tex2D(_BumpTex,i.bumpUV));
                norBump.xy = norBump.xy * _BumpScale;
                norBump.z = sqrt(1-saturate(dot(norBump.xy,norBump.xy)));

                float3 worldNormal = normalize(mul(tanToWorld,norBump));

                float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);

                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * col;

                fixed3 diffuse = _LightColor0.xyz * saturate(dot(worldNormal,worldLight)) * col;

//              UNITY_LIGHT_ATTENUATION(atten,i,worldPos);
                float shadow = SHADOW_ATTENUATION(i);

                float t = 1-smoothstep(0,_LineWidth,(burn.r-_BurnAmount));

                //如果t為1的話說明當(dāng)前像素馬上要被灼燒掉此時(shí)顏色應(yīng)該很紅,如果為0的話說明還要等BurnAmount的值增加到1才灼燒,此時(shí)顏色應(yīng)該很淡
                //如果t為0說明burn.r-_BurnAmount>_LineWidth;也就是說amount還沒怎么增長(zhǎng),此時(shí)burnColor全為第一個(gè)顏色
                fixed4 burnColor = lerp(_BurnFirstColor,_BurnSecondColor,t);

//              burnColor = pow(burnColor,5);
                
                fixed3 finalColor = lerp(ambient+diffuse*shadow,burnColor.xyz,t*step(0.001,_BurnAmount));

                return fixed4(finalColor,1);
//              fixed testSmooth = smoothstep(0.5,0.6,0.655);
//              return fixed4(testSmooth,testSmooth,testSmooth,testSmooth);
//              return fixed4(0.655,0.655,0.655,0.655);
            }
            ENDCG
        }

        Pass
        {
            Tags{"LightMode" = "ShadowCaster"}
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #pragma multi_compile_shadowcaster

            #include "unitycg.cginc"
            #include "autolight.cginc"
            #include "lighting.cginc"

            float _BurnAmount;
            sampler2D _BurnTex;
            float4 _BurnTex_ST;


            struct v2f{
                //系統(tǒng)定義的宏,包含了陰影投射需要的變量值
                V2F_SHADOW_CASTER;
                float2 uv : TEXCOORD0;
            };

            v2f vert(appdata_base v)
            {
                v2f o;
                //計(jì)算得到上面宏定義好的變量值
                TRANSFER_SHADOW_CASTER_NORMALOFFSET(o);
                o.uv = TRANSFORM_TEX(v.texcoord,_BurnTex);
                return o;
            }

            fixed4 frag(v2f i):SV_Target
            {
                fixed3 burn = tex2D(_BurnTex,i.uv).rgb;
                clip(burn.r-_BurnAmount);
                //完成陰影投射
                SHADOW_CASTER_FRAGMENT(i);
            }


            ENDCG
        }

    }
    FallBack "Diffuse"
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、紋理基礎(chǔ) 3D圖形渲染中最基本的操作就是對(duì)一個(gè)表面應(yīng)用紋理。紋理可以表現(xiàn)只從網(wǎng)格的幾何形狀無法得到的附加細(xì)節(jié)。...
    cain_huang閱讀 9,170評(píng)論 0 7
  • 秦失其鹿,天下共逐之。中國(guó)歷史上每一個(gè)戰(zhàn)亂的時(shí)代都是一個(gè)英雄輩出的年代,在那些熱血沸騰的廝殺中總有一些...
    了然此無痕閱讀 428評(píng)論 0 0
  • 鹵菜怎么做才好吃呢? 鹵菜哥是這樣理解的:合理的配料「香辛料、調(diào)味品」、對(duì)火候的掌握、對(duì)食材的了解和選用食材的新鮮...
    廖相暉閱讀 547評(píng)論 0 1
  • 堅(jiān)持早起已經(jīng)有一個(gè)月了,收獲很大,但是總感覺還是達(dá)不到自己的要求,時(shí)間太少想做的事情很多,有一次有事情睡得晚,第二...
    陸小遠(yuǎn)閱讀 263評(píng)論 0 0

友情鏈接更多精彩內(nèi)容