溶解效果

来源:互联网 发布:韩国美颜软件 编辑:程序博客网 时间:2024/04/28 02:53

原理

实现图像溶解效果,实现机制是用一张纹理图作为溶解的过程。因为每个像素存储的数据不一样就像一个不平的地面,数值比较小的就想隐藏掉,数值比较大的就后隐藏。这个就完成了一个消融的过程。
这里写图片描述
如图按照数值比较小的数先一个一个隐藏,这里就产生了消融的过程。
这里写图片描述
第一个是像素的纹理,第二个我用的一个噪声纹理由他来控制消融过程

效果图

这里写图片描述

关键代码

fixed4 frag (v2f i) : SV_Target{    // sample the texture    fixed4 col = tex2D(_MainTex, i.uv);    fixed4 noise = tex2D(_NoiseTex,i.uv);//获取消融控制的纹理    float noiseA = noise.r-_NoiseTime;//减去一随着时间变化的变量得到消融的动画过程    clip(noiseA);    return col;}

完整代码

Shader "Unlit/dissolution"{    Properties    {        _MainTex ("Texture", 2D) = "white" {}        _NoiseTex("NoiseTexture",2D) = "white"{}        _NoiseTime("NoiseTime",Range(0,1)) = 0    }    SubShader    {        Tags { "RenderType"="Opaque" }        LOD 100        Cull Off        Pass        {            CGPROGRAM            #pragma vertex vert            #pragma fragment frag            #include "UnityCG.cginc"            struct appdata            {                float4 vertex : POSITION;                float2 uv : TEXCOORD0;            };            struct v2f            {                float2 uv : TEXCOORD0;                float4 vertex : SV_POSITION;            };            sampler2D _MainTex;            float4 _MainTex_ST;            sampler2D _NoiseTex;            float _NoiseTime;            v2f vert (appdata v)            {                v2f o;                o.vertex = UnityObjectToClipPos(v.vertex);                o.uv = TRANSFORM_TEX(v.uv, _MainTex);                return o;            }            fixed4 frag (v2f i) : SV_Target            {                // sample the texture                fixed4 col = tex2D(_MainTex, i.uv);                fixed4 noise = tex2D(_NoiseTex,i.uv);//获取消融控制的纹理                float noiseA = noise.r-_NoiseTime;//减去一随着时间变化的变量得到消融的动画过程                clip(noiseA);                return col;            }            ENDCG        }    }}

  • 上面的那个要调_NoiseTime变量才能出效果。这里让他支持unity粒子组件,让Color over Lifetime的alpha控制消融过程
  • 还要为溶解的边缘加上一个颜色值来凸显消融过程
    这里写图片描述

效果图

这里写图片描述

关键代码

v2f vert (appdata v){    v2f o;    o.vertex = UnityObjectToClipPos(v.vertex);    o.uv = TRANSFORM_TEX(v.uv, _MainTex);    o.color = v.color;//这个就是Color over Lifetime控制的顶点变量的颜色值    return o;}fixed4 frag (v2f i) : SV_Target{    // sample the texture    fixed4 col = tex2D(_MainTex, i.uv);    fixed4 noise = tex2D(_NoiseTex,i.uv);    float noiseA = noise.r- i.color.a;    clip(noiseA);    //这里是控制消融的边缘颜色代码    col.rgb = lerp(col.rgb,_EdgeColor,(1-noiseA)*step(noiseA,_LineWidth));    return col;}

完整代码

Shader "Particles/Dissolution"{    Properties    {        _MainTex ("Texture", 2D) = "white" {}        _NoiseTex("NoiseTexture",2D) = "white"{}        _EdgeColor ("EdgeColor", Color) = (0.5,0.5,0.5,1)        _LineWidth ("LineWidth",Range(0,0.1)) = 0.02    }    SubShader    {        Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" }        LOD 100        Cull Off Lighting Off ZWrite Off        Pass        {            CGPROGRAM            #pragma vertex vert            #pragma fragment frag            #include "UnityCG.cginc"            struct appdata            {                float4 vertex : POSITION;                fixed4 color : COLOR;                float2 uv : TEXCOORD0;            };            struct v2f            {                float2 uv : TEXCOORD0;                fixed4 color : COLOR;                float4 vertex : SV_POSITION;            };            sampler2D _MainTex;            float4 _MainTex_ST;            sampler2D _NoiseTex;            float4 _EdgeColor;            fixed _LineWidth;            v2f vert (appdata v)            {                v2f o;                o.vertex = UnityObjectToClipPos(v.vertex);                o.uv = TRANSFORM_TEX(v.uv, _MainTex);                o.color = v.color;                return o;            }            fixed4 frag (v2f i) : SV_Target            {                // sample the texture                fixed4 col = tex2D(_MainTex, i.uv);                fixed4 noise = tex2D(_NoiseTex,i.uv);                float noiseA = noise.r- i.color.a;                clip(noiseA);                col.rgb = lerp(col.rgb,_EdgeColor,(1-noiseA)*step(noiseA,_LineWidth));                return col;            }            ENDCG        }    }}

相关阅读

这里使用了lerp,step等内置函数,msdn上面有完整的内置函数列表及说明
msdn HLSL 内置函数列表

原创粉丝点击