Unity——火烧+水波纹效果(噪音图)

2022年01月14日 阅读数:2
这篇文章主要向大家介绍Unity——火烧+水波纹效果(噪音图),主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

使用噪声图实现火烧和水波纹效果;c#

1.溶解

关闭裁剪,根据noise纹理取样,r通道和_BurnAmount比较,裁剪掉小于_BurnAmount的片元;app

经过菲尼尔获得裁剪边缘,添加火焰燃烧的颜色进行混合;动画

阴影pass须要单独写,一样作裁剪;code

image-d129

Pass
{
    Tags {"LightMode" = "ForwardBase"}

    Cull Off

    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag

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

    #pragma multi_compile_fwdbase

    struct appdata
    {
        float4 vertex : POSITION;
        float2 uv : TEXCOORD0;
        float3 normal : NORMAL;
        float4 tangent :TANGENT;
    };

    struct v2f
    {
        float4 uv : TEXCOORD0;
        float2 uvBurnMap : TEXCOORD1;
        float4 pos : SV_POSITION;
        float3 lightDir : TEXCOORD2;
        float3 worldPos : TEXCOORD3;
        SHADOW_COORDS(5)
    };

    float _BurnAmount;
    float _LineWidth;
    sampler2D _MainTex;
    float4 _MainTex_ST;
    sampler2D _BumpMap;
    float4 _BumpMap_ST;
    fixed4 _BurnFirstColor;
    fixed4 _BurnSecondColor;
    sampler2D _BurnMap;
    float4 _BurnMap_ST;

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

        o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex);
        o.uv.zw = TRANSFORM_TEX(v.uv, _BumpMap);
        o.uvBurnMap = TRANSFORM_TEX(v.uv,_BurnMap);

      	TANGENT_SPACE_ROTATION;
        o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;
        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);

        float3 tangentLightDir = normalize(i.lightDir);
        fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uv.zw));

        fixed3 albedo = tex2D(_MainTex,i.uv.xy).rgb;
        fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

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

        //离裁剪边缘的距离,在0-边宽中的百分比,返回0-1中的一个对应值,1-得到剩余半段;
        fixed t = 1 - smoothstep(0.0, _LineWidth, burn.r - _BurnAmount);
        //菲尼尔混合颜色
        fixed3 burnColor = lerp(_BurnFirstColor, _BurnSecondColor, t);
        burnColor = pow(burnColor, 5);

        UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);

        //step,_BurnAmount<=0.00001返回1,不然返回0;插值混合颜色,
        fixed3 finalColor = lerp(ambient + diffuse * atten, burnColor, t * step(0.0001,_BurnAmount));
        
        return fixed4(finalColor, 1);
    }
    ENDCG
}

2.水波纹

设置速度来扰动法线;扰动的偏移乘以屏幕坐标的z值模拟深度越大折射越大的效果;orm

GrabPass抓取屏幕画面作纹理,供折射取样;blog

MainTex是一张噪音纹理图,取样作速度偏移,模拟水波纹;ip

fresnel混合折射反射,获得最终颜色;资源

jluhvfghjkl

fixed4 frag (v2f i) : SV_Target
{
    float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);
	fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
	float2 speed = _Time.y * float2(_WaveXSpeed, _WaveYSpeed);

    //速度来扰动法线,速度上下偏移解包法线两次,取中间
	fixed3 bump1 = UnpackNormal(tex2D(_WaveMap, i.uv.zw + speed)).rgb;
	fixed3 bump2 = UnpackNormal(tex2D(_WaveMap, i.uv.zw - speed)).rgb;
	fixed3 bump = normalize(bump1 + bump2);

    //根据法线计算偏移
    float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;
    
    //乘以z模拟深度越大折射程度越大
	i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;
    
    //透视除法取折射纹素
	fixed3 refrCol = tex2D( _RefractionTex, i.scrPos.xy/i.scrPos.w).rgb;

    //求世界空间法线
	bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));
   
    //主纹理动画,模拟水波纹
	fixed4 texColor = tex2D(_MainTex, i.uv.xy + speed);
    
    //计算反射混合主纹理颜色
	fixed3 reflDir = reflect(-viewDir, bump);
	fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor.rgb * _Color.rgb;

    //fresnel混合反射和折射
    fixed fresnel = pow(1 - saturate(dot(viewDir,bump)),4);
    fixed3 finalColor = reflCol * fresnel + refrCol *(1-fresnel);

    return fixed4(finalColor,1.0); 
}

此外,噪音图还用在全局雾和生成地形,以及动态生成资源上;get