Dissolve效果代码理解

Shader "ShaderBook/15/Dissolve"{
    Properties{
        //表示消融程度
        _BurnAmount("Burn Amount",Range(0.0,1.0)) = 0.0
        //消融效果时的线宽
        _LineWidth("Burn Line Width",Range(0.0,0.2)) = 0.1
        _MainTex("Base (RGB)",2D) = "white" {}
        _BumpMap("Bump Map",2D) = "bump" {}
        //火焰边缘颜色
        //两种颜色,可以用渐变纹使颜色更丰富
        _BurnFirstColor("Burn First Color", Color) = (1, 0, 0, 1)
		_BurnSecondColor("Burn Second Color", Color) = (1, 0, 0, 1)
        //噪声纹理
		_BurnMap("Burn Map", 2D) = "white"{}
        //渐变纹理
        _RampTex("Ramp Tex",2D) = "white"{}
    }
    SubShader{
		Tags { "RenderType"="Opaque" "Queue"="Geometry"}
        
		Pass {
			Tags { "LightMode"="ForwardBase" }

			Cull Off

            CGPROGRAM

            #include "Lighting.cginc"
            #include "AutoLight.cginc"

			#pragma multi_compile_fwdbase

            #pragma vertex vert
			#pragma fragment frag

            fixed      _BurnAmount;
            fixed      _LineWidth;
            sampler2D  _MainTex;
            sampler2D  _BumpMap;
            fixed4     _BurnFirstColor;
			fixed4     _BurnSecondColor;
            sampler2D  _BurnMap;
            sampler2D  _RampTex;

            float4 _MainTex_ST;
			float4 _BumpMap_ST;
			float4 _BurnMap_ST;

            struct v2f {
                float4 pos       :SV_POSITION;
                float2 uvMainTex :TEXCOORD0;
                float2 uvBumpMap :TEXCOORD1;
                float2 uvBurnMap :TEXCOORD2;
                float3 lightDir  :TEXCOORD3;
				float3 worldPos : TEXCOORD4;
                SHADOW_COORDS(5)
            };
            v2f vert(appdata_tan v){
                v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
                
                o.uvMainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
				o.uvBumpMap = TRANSFORM_TEX(v.texcoord, _BumpMap);
				o.uvBurnMap = TRANSFORM_TEX(v.texcoord, _BurnMap);

                TANGENT_SPACE_ROTATION;
                //计算局部光照,并将其转化到切线空间
                //我们也可以不改变光照,而改变法线,将发现变换到世界空间
                o.lightDir = mul(rotation,ObjSpaceLightDir(v.vertex));

                o.worldPos = mul(unity_ObjectToWorld,v.vertex).xyz;

  				TRANSFER_SHADOW(o);
                
                return o;
            }
            fixed4 frag(v2f i):SV_TARGET{
                //采样噪声纹理
				fixed3 burn = tex2D(_BurnMap, i.uvBurnMap).rgb;
                clip(burn.r - _BurnAmount);//参数小于0时候,像素被清除,AlphaTest

                float3 tangentLightDir = normalize(i.lightDir);
				fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uvBumpMap));//获取切线空间法线
                
				fixed3 albedo = tex2D(_MainTex, i.uvMainTex).rgb;
				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

				fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir));

                //_LineWidth宽度范围模拟渐变。t为0是正常颜色,为1时位于消融的边界
				fixed  t = 1 - smoothstep(0.0, _LineWidth, burn.r - _BurnAmount);                                        
				//fixed3 burnColor = lerp(_BurnFirstColor, _BurnSecondColor, t);
				fixed3 burnColor = tex2D(_RampTex,float2(1-t,0));
                //加强颜色模拟烧焦痕迹
				burnColor = pow(burnColor, 2);

                UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);                    //小于0.001时候为0,其余为1,为了完全消除燃烧痕迹
				//混合烧焦的颜色和原本的颜色
                fixed3 finalColor = lerp(ambient + diffuse * atten, burnColor, t * step(0.0001, _BurnAmount));

                return fixed4(finalColor,1);
            }
        ENDCG
        }
    }
    //绘制阴影的shader,如果需要显示cutoff的阴影
    //需要我们手写shadowCaster
    Fallback "Diffuse"
}

找了一天也没有找到可以运行的demo,不过原理我大概懂了,明天尝试手写出来!!!
在这里插入图片描述
燃烧效果.我想要白到发光的瞬间燃烧,所以没有让他更加真实(其实懒得用ps).

在这里插入图片描述
我仔细一看发现火焰燃烧的太整齐了,可以加一点噪声(白噪声?)使火焰更自然一点?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值