unity shader 玻璃 扭曲效果

作者:雨辰 发布于:2017-3-8 10:41 Wednesday 分类:Unity3D

//玻璃镜面扭曲(双面镜子(试用于unity5.0以上版本))
Shader "hidden/ImageGlass"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _NoiseTex("Noise Texture",2D) = "white"{}
        _MaskTex("Mask Texture",2D) = "white"{}
        _HeatTime ("Heat Time",range(0,1.5)) = 1
        _HeatForce ("Heat Force",range(0,1.0)) = 0.1
    }
    SubShader
    {
        Tags {"Queue" = "Transparent"}

        Blend SrcAlpha OneMinusSrcAlpha
        AlphaTest Greater 0.1
        Cull Off
        ZWrite Off
        Lighting Off

       GrabPass{"_RefractionTex"}    

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            sampler2D _NoiseTex;
            float4 _NoiseTex_ST;
            float  _HeatTime;
            float  _HeatForce;
            sampler2D _RefractionTex;
            sampler2D _MaskTex;


            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 uvgrab : TEXCOORD1;
                float4 vertex : SV_POSITION;
            };

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = TRANSFORM_TEX(v.uv,_MainTex);
                o.uvgrab = ComputeGrabScreenPos(o.vertex);
                return o;
            }


            fixed4 frag (v2f i) : SV_Target
            {
                half4 offestcol1 = tex2D(_NoiseTex,i.uv + _Time.xz * _HeatTime);
                half4 offestcol2 = tex2D(_NoiseTex,i.uv - _Time.yx * _HeatTime);
                i.uvgrab.x += ((offestcol1.r + offestcol2.r) - 1) * _HeatForce;
                i.uvgrab.y += ((offestcol1.g + offestcol2.g) - 1) * _HeatForce;
                half4 col = tex2Dproj(_RefractionTex,UNITY_PROJ_COORD(i.uvgrab));

                half4 maskcol = tex2D(_MaskTex, i.uv);

                col.a = maskcol.a;

                half4 tint = tex2D(_MainTex,i.uv);

                return col * tint;
            }
            ENDCG
        }
    }
}
//试用在手机移动端。扭曲效果 (单面镜,试用与unity5.0)
Shader "hidden/ImageGlass_mobile"
{
     Properties   
    {  
        _MainTex ("Base (RGB)", 2D) = "white" {}  
        _NoiseTex ("Noise Texture (RG)", 2D) = "white" {}  
        _MaskTex ("Mask Texture", 2D) = "white" {}  
        _HeatTime  ("Heat Time", range (0,1.5)) = 1  
        _HeatForce  ("Heat Force", range (0,0.1)) = 0.1  
    }  

    SubShader   
    {  
        Pass  
        {  
            CGPROGRAM  
            #pragma vertex vert_img  
            #pragma fragment frag  
            #pragma fragmentoption ARB_precision_hint_fastest  
            #include "UnityCG.cginc"  

            float _HeatForce;  
            float _HeatTime;  

            uniform sampler2D _MainTex;  
            uniform sampler2D _NoiseTex;  
            uniform sampler2D _MaskTex;  

            fixed4 frag(v2f_img i) : COLOR  
            {  
                fixed mask = tex2D(_MaskTex, i.uv).a;  

                // 扭曲效果  
                half4 offsetColor1 = tex2D(_NoiseTex, i.uv + _Time.xz*_HeatTime);  
                half4 offsetColor2 = tex2D(_NoiseTex, i.uv - _Time.yx*_HeatTime);  
                i.uv.x += ((offsetColor1.r + offsetColor2.r) - 1) * _HeatForce * mask;  
                i.uv.y += ((offsetColor1.g + offsetColor2.g) - 1) * _HeatForce * mask;  

                fixed4 renderTex = tex2D(_MainTex, i.uv);  

                return renderTex;  
            }  

            ENDCG  
        }  
    }   
    FallBack off  
}

	

标签: Unity3D-Shader Unity3D

发表评论:

雨辰 joyimp|@2011-2017 京ICP备16030765号