Unity3d 技巧(11) -VR 瞬移睜眼閉眼 能減少眩暈感

 using UnityEngine;
    [ExecuteInEditMode]
    [RequireComponent(typeof(Camera))]


    public class ScreenFade : MonoBehaviour
    {
        public static ScreenFade instance;


        #region Variables
        public bool BeginFade = false;
        public Shader RadialBlurShader = null;
        private Material RadialBlurMaterial = null;


        [Range(0.0f, 2.0f)]
        public float SampleDist = 0f;


        [Range(0.0f, 5.0f)]
        public float SampleStrength = 0f;


        public Texture2D _maskTexture;
        public Texture2D maskTexture
        {
            get
            {
                if (_maskTexture == null)

                {

//這是一張黑色的Png 圖片就可以了,想做其他效果可以改變圖片的透明度等

                    string texture = "EyeFade";
                     _maskTexture = (Texture2D)Resources.Load<Texture2D>(texture);
                }
                return _maskTexture;
            }
        }


        #endregion


        public static void Start(Color newColor, float duration)
        {
            if (instance)
            {
                instance.StartFade(newColor, duration);
            }
        }


        public void StartFade(Color newColor, float duration)
        {
            BeginFade = true;
        }


        void Start()
        {
            FindShaders();
            CreateMaterials();


            instance = this;
        }


        void FindShaders()
        {
            if (!RadialBlurShader)
            {
                RadialBlurShader = Shader.Find("PengLu/ImageEffect/Unlit/RadialBlur");
               //RadialBlurShader = Shader.Find("Particles/Additive");
            }
        }


        void CreateMaterials()
        {
            if (!RadialBlurMaterial)
            {
                RadialBlurMaterial = new Material(RadialBlurShader);
                RadialBlurMaterial.hideFlags = HideFlags.HideAndDontSave;
            }
        }
        public bool DeBug = false;
        float TempTime = 0.2f;
        void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (!enabled)
            {
                Graphics.Blit(source, destination);
                return;
            }


            #region  Fade
            if (!DeBug)
            {
                if (BeginFade)
                {
                    if (SampleStrength < 3.0F)
                    {
                        SampleDist = 1F;
                        SampleStrength += 0.5f;
                    }
                    else
                    {
                        BeginFade = false;
                        TempTime = 0.2f;
                    }
                }
                if (!BeginFade)
                {
                    if (TempTime > 0)
                    {
                        TempTime -= Time.deltaTime;


                    }
                    else
                    {
                        TempTime = 0;
                    }
                    if (TempTime == 0)
                    {
                        if (SampleStrength > 0)
                        {
                            SampleDist = 1F;
                            SampleStrength -= 0.2f;
                        }
                        else
                        {
                            if (SampleDist > 0)
                                SampleDist -= Time.deltaTime;
                            else
                            {
                                SampleDist = 0;
                                SampleStrength = 0;
                            }
                        }
                    }
                }
            }
            #endregion


            if (SampleDist != 0 && SampleStrength != 0)
            {
                int rtW = source.width / 8;
                int rtH = source.height / 8;
                RadialBlurMaterial.SetFloat("_SampleDist", SampleDist);
                RadialBlurMaterial.SetFloat("_SampleStrength", SampleStrength);
                RadialBlurMaterial.SetTexture("_BlurTex", maskTexture);
                Graphics.Blit(source, destination, RadialBlurMaterial);               
            }
            else
            {
                Graphics.Blit(source, destination);


            }
        }

    }



Shader "PengLu/ImageEffect/Unlit/RadialBlur" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
}




CGINCLUDE


#include "UnityCG.cginc"


struct appdata_t {
float4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
};


struct v2f {
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
};


sampler2D _MainTex;
sampler2D _BlurTex;
uniform float _SampleDist;
uniform float _SampleStrength;

v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
return o;
}

fixed4 fragRadialBlur (v2f i) : COLOR
{
fixed2 dir = 0.5-i.texcoord;
fixed dist = length(dir);
dir /= dist;
dir *= _SampleDist;


fixed4 sum = tex2D(_MainTex, i.texcoord - dir*0.01);
sum += tex2D(_MainTex, i.texcoord - dir*0.02);
sum += tex2D(_MainTex, i.texcoord - dir*0.03);
sum += tex2D(_MainTex, i.texcoord - dir*0.05);
sum += tex2D(_MainTex, i.texcoord - dir*0.08);
sum += tex2D(_MainTex, i.texcoord + dir*0.01);
sum += tex2D(_MainTex, i.texcoord + dir*0.02);
sum += tex2D(_MainTex, i.texcoord + dir*0.03);
sum += tex2D(_MainTex, i.texcoord + dir*0.05);
sum += tex2D(_MainTex, i.texcoord + dir*0.08);
sum *= 0.1;

return sum;
}


fixed4 fragCombine (v2f i) : COLOR
{
// fixed2 dir = 0.5-i.texcoord;
fixed dist = length(0.5-i.texcoord);
fixed4  col = tex2D(_MainTex, i.texcoord);
fixed4  blur = tex2D(_BlurTex, i.texcoord);
col=lerp(col, blur,saturate(_SampleStrength*dist));
return col;
}
ENDCG


SubShader {
 ZTest Always  ZWrite Off Cull Off Blend Off


 Fog { Mode off } 
//0  
Pass { 
CGPROGRAM

#pragma vertex vert
#pragma fragment fragRadialBlur
#pragma fragmentoption ARB_precision_hint_fastest 

ENDCG  
}
//1
Pass { 
CGPROGRAM

#pragma vertex vert
#pragma fragment fragCombine
#pragma fragmentoption ARB_precision_hint_fastest 

ENDCG  
}

}




}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章