91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Unity3D UGUI特效之Image高斯模糊效果

發布時間:2020-10-11 13:58:42 來源:腳本之家 閱讀:437 作者:蘇小敗在路上 欄目:編程語言

這幾天研究了下模糊特效,看了很多文章,其原理就是拿取圖片或屏幕數據,然后將周圍的元素和目標位置的顏色值進行一個融合計算,然后自己寫了一個小小的測試程序。

這個模糊也可以分成兩種,一個是自身模糊,一個是從屏幕上取值進行模糊。第一個用于一些小的列表展示,比如未解鎖時,是模糊的。第二個是凸顯彈框效果的,將背景都模糊掉,自己將這個稍微加強了些可以指定模糊一個位置。

針對移動平臺,使用高斯模糊,其實效率不是很高,如果要很好的效果,那么速度卡;如果要速度快,那么效果達不到要求。但是還是在這里記錄下,興許以后能用上。

先說第一個,掛在Image下的模糊特效。

Shader "Custom/FrontBlur" {
 Properties
 {
 [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
 _Color ("Tint", Color) = (1,1,1,1)
 
 [HideInInspector]_StencilComp ("Stencil Comparison", Float) = 8
 [HideInInspector]_Stencil ("Stencil ID", Float) = 0
 [HideInInspector]_StencilOp ("Stencil Operation", Float) = 0
 [HideInInspector]_StencilWriteMask ("Stencil Write Mask", Float) = 255
 [HideInInspector]_StencilReadMask ("Stencil Read Mask", Float) = 255

 [HideInInspector]_ColorMask ("Color Mask", Float) = 15

 [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
 
 _Size ("Size", Range(0, 50)) = 5
 }

 SubShader
 {
 Tags
 { 
 "Queue"="Transparent" 
 "IgnoreProjector"="True" 
 "RenderType"="Transparent" 
 "PreviewType"="Plane"
 "CanUseSpriteAtlas"="True"
 }
 
 Stencil
 {
 Ref [_Stencil]
 Comp [_StencilComp]
 Pass [_StencilOp] 
 ReadMask [_StencilReadMask]
 WriteMask [_StencilWriteMask]
 }

 Cull Off
 Lighting Off
 ZWrite Off
 ZTest [unity_GUIZTestMode]
 Blend SrcAlpha OneMinusSrcAlpha
 ColorMask [_ColorMask]

 Pass
 {
 Name "FrontBlurHor"
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #pragma target 2.0

 #include "UnityCG.cginc"
 #include "UnityUI.cginc"

 #pragma multi_compile __ UNITY_UI_ALPHACLIP
 
 struct appdata_t
 {
 float4 vertex : POSITION;
 float4 color : COLOR;
 float2 texcoord : TEXCOORD0;
 UNITY_VERTEX_INPUT_INSTANCE_ID
 };

 struct v2f
 {
 float4 vertex : SV_POSITION;
 fixed4 color : COLOR;
 float2 texcoord : TEXCOORD0;
 float4 worldPosition : TEXCOORD1;
 UNITY_VERTEX_OUTPUT_STEREO
 };
 
 fixed4 _Color;
 fixed4 _TextureSampleAdd;
 float4 _ClipRect;

 v2f vert(appdata_t IN)
 {
 v2f OUT;
 UNITY_SETUP_INSTANCE_ID(IN);
 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
 OUT.worldPosition = IN.vertex;
 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

 OUT.texcoord = IN.texcoord;
 
 OUT.color = IN.color * _Color;
 return OUT;
 }

 sampler2D _MainTex;
 float4 _MainTex_TexelSize;
 float _Size;

 half4 GrabPixel(v2f i, float weight, float kernelx){
 if (_Size <= 1 || weight == 0){
  return tex2D(_MainTex, half2(i.texcoord.x + _MainTex_TexelSize.x*kernelx*_Size, i.texcoord.y)) * weight;
 }else{
  half4 sum = half4(0,0,0,0);
  sum += tex2D(_MainTex, half2(i.texcoord.x + _MainTex_TexelSize.x*kernelx*_Size*0.2, i.texcoord.y))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x + _MainTex_TexelSize.x*kernelx*_Size*0.4, i.texcoord.y))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x + _MainTex_TexelSize.x*kernelx*_Size*0.6, i.texcoord.y))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x + _MainTex_TexelSize.x*kernelx*_Size*0.8, i.texcoord.y))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x + _MainTex_TexelSize.x*kernelx*_Size*1.0, i.texcoord.y))*0.2;
  return (sum + _TextureSampleAdd) * weight;
 }
 }

 half4 GrabPixely(v2f i, float weight, float kernely){
 if (_Size <= 1 || weight == 0){
  return tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size)) * weight;
 }else{
  half4 sum = half4(0,0,0,0);
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*0.2))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*0.4))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*0.6))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*0.8))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*1.0))*0.2;
  return (sum + _TextureSampleAdd) * weight;
 }
 }

 fixed4 frag(v2f IN) : SV_Target
 {
 half4 sum = half4(0,0,0,0);
 // #define GRABPIXEL(weight, kernelx) (tex2D(_MainTex, half2(IN.texcoord.x + _MainTex_TexelSize.x * kernelx*_Size, IN.texcoord.y)) + _TextureSampleAdd) * weight
 
 // sum += GrabPixel(IN, 0.05, -4.0);
 // sum += GrabPixel(IN, 0.09, -3.0);
 // sum += GrabPixel(IN, 0.12, -2.0);
 // sum += GrabPixel(IN, 0.15, -1.0);
 // sum += GrabPixel(IN, 0.18, 0.0);
 // sum += GrabPixel(IN, 0.15, +1.0);
 // sum += GrabPixel(IN, 0.12, +2.0);
 // sum += GrabPixel(IN, 0.09, +3.0);
 // sum += GrabPixel(IN, 0.05, +4.0);

 for(int i=0;i<9;i++){
  sum += GrabPixel(IN, 1.0/9, i-4.0);
 }

 // half4 sumy = half4(0,0,0,0);
 // for(int i=0;i<15;i++){
 // sumy += GrabPixely(IN, 1.0/15, i-7.0);
 // }
 // half4 sum = (sumx + sumy) * 0.5;

 // sum += GrabPixel(IN, 0.01, -9.0);
 // sum += GrabPixel(IN, 0.02, -8.0);
 // sum += GrabPixel(IN, 0.03, -7.0);
 // sum += GrabPixel(IN, 0.04, -6.0);
 // sum += GrabPixel(IN, 0.05, -5.0);
 // sum += GrabPixel(IN, 0.06, -4.0);
 // sum += GrabPixel(IN, 0.07, -3.0);
 // sum += GrabPixel(IN, 0.08, -2.0);
 // sum += GrabPixel(IN, 0.09, -1.0);
 // sum += GrabPixel(IN, 0.10, 0.0);
 // sum += GrabPixel(IN, 0.09, +1.0);
 // sum += GrabPixel(IN, 0.08, +2.0);
 // sum += GrabPixel(IN, 0.07, +3.0);
 // sum += GrabPixel(IN, 0.06, +4.0);
 // sum += GrabPixel(IN, 0.05, +5.0);
 // sum += GrabPixel(IN, 0.04, +6.0);
 // sum += GrabPixel(IN, 0.03, +7.0);
 // sum += GrabPixel(IN, 0.02, +8.0);
 // sum += GrabPixel(IN, 0.01, +9.0);

 sum = sum * IN.color;
 sum.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
 #ifdef UNITY_UI_ALPHACLIP
 clip (sum.a - 0.001);
 #endif
 return sum;

 // float distance = _Distance;

 // fixed4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;

 // color += (tex2D(_MainTex, half2(IN.texcoord.x + distance, IN.texcoord.y + distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x + distance, IN.texcoord.y)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x + distance, IN.texcoord.y - distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x, IN.texcoord.y - distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x - distance, IN.texcoord.y - distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x - distance, IN.texcoord.y)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x - distance, IN.texcoord.y + distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x, IN.texcoord.y + distance)) + _TextureSampleAdd) * IN.color;
 // color /= 9;
 
 // color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
 
 // #ifdef UNITY_UI_ALPHACLIP
 // clip (color.a - 0.001);
 // #endif

 // return color;
 }
 ENDCG
 }
 Pass
 {
 Name "FrontBlurVer"
 CGPROGRAM
 #pragma vertex vert
 #pragma fragment frag
 #pragma target 2.0

 #include "UnityCG.cginc"
 #include "UnityUI.cginc"

 #pragma multi_compile __ UNITY_UI_ALPHACLIP
 
 struct appdata_t
 {
 float4 vertex : POSITION;
 float4 color : COLOR;
 float2 texcoord : TEXCOORD0;
 UNITY_VERTEX_INPUT_INSTANCE_ID
 };

 struct v2f
 {
 float4 vertex : SV_POSITION;
 fixed4 color : COLOR;
 float2 texcoord : TEXCOORD0;
 float4 worldPosition : TEXCOORD1;
 UNITY_VERTEX_OUTPUT_STEREO
 };
 
 fixed4 _Color;
 fixed4 _TextureSampleAdd;
 float4 _ClipRect;

 v2f vert(appdata_t IN)
 {
 v2f OUT;
 UNITY_SETUP_INSTANCE_ID(IN);
 UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
 OUT.worldPosition = IN.vertex;
 OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

 OUT.texcoord = IN.texcoord;
 
 OUT.color = IN.color * _Color;
 return OUT;
 }

 sampler2D _MainTex;
 float4 _MainTex_TexelSize;
 float _Size;

 half4 GrabPixel(v2f i, float weight, float kernely){
 if (_Size <= 1 || weight == 0){
  return tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size)) * weight;
 }else{
  half4 sum = half4(0,0,0,0);
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*0.2))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*0.4))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*0.6))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*0.8))*0.2;
  sum += tex2D(_MainTex, half2(i.texcoord.x, i.texcoord.y + _MainTex_TexelSize.y*kernely*_Size*1.0))*0.2;
  return (sum + _TextureSampleAdd) * weight;
 }
 }

 fixed4 frag(v2f IN) : SV_Target
 {
 half4 sum = half4(0,0,0,0); 
 // #define GRABPIXEL(weight, kernely) (tex2D(_MainTex, half2(IN.texcoord.x, IN.texcoord.y + _MainTex_TexelSize.y * kernely*_Size)) + _TextureSampleAdd) * weight
 
 // sum += GrabPixel(IN, 0.05, -4.0);
 // sum += GrabPixel(IN, 0.09, -3.0);
 // sum += GrabPixel(IN, 0.12, -2.0);
 // sum += GrabPixel(IN, 0.15, -1.0);
 // sum += GrabPixel(IN, 0.18, 0.0);
 // sum += GrabPixel(IN, 0.15, +1.0);
 // sum += GrabPixel(IN, 0.12, +2.0);
 // sum += GrabPixel(IN, 0.09, +3.0);
 // sum += GrabPixel(IN, 0.05, +4.0);

 for(int i=0;i<9;i++){
  sum += GrabPixel(IN, 1.0/9, i-4.0);
 }

 // sum += GrabPixel(IN, 0.01, -9.0);
 // sum += GrabPixel(IN, 0.02, -8.0);
 // sum += GrabPixel(IN, 0.03, -7.0);
 // sum += GrabPixel(IN, 0.04, -6.0);
 // sum += GrabPixel(IN, 0.05, -5.0);
 // sum += GrabPixel(IN, 0.06, -4.0);
 // sum += GrabPixel(IN, 0.07, -3.0);
 // sum += GrabPixel(IN, 0.08, -2.0);
 // sum += GrabPixel(IN, 0.09, -1.0);
 // sum += GrabPixel(IN, 0.10, 0.0);
 // sum += GrabPixel(IN, 0.09, +1.0);
 // sum += GrabPixel(IN, 0.08, +2.0);
 // sum += GrabPixel(IN, 0.07, +3.0);
 // sum += GrabPixel(IN, 0.06, +4.0);
 // sum += GrabPixel(IN, 0.05, +5.0);
 // sum += GrabPixel(IN, 0.04, +6.0);
 // sum += GrabPixel(IN, 0.03, +7.0);
 // sum += GrabPixel(IN, 0.02, +8.0);
 // sum += GrabPixel(IN, 0.01, +9.0);

 sum = sum * IN.color;
 sum.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
 #ifdef UNITY_UI_ALPHACLIP
 clip (sum.a - 0.001);
 #endif
 return sum;

 // float distance = _Distance;

 // fixed4 color = (tex2D(_MainTex, IN.texcoord) + _TextureSampleAdd) * IN.color;

 // color += (tex2D(_MainTex, half2(IN.texcoord.x + distance, IN.texcoord.y + distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x + distance, IN.texcoord.y)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x + distance, IN.texcoord.y - distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x, IN.texcoord.y - distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x - distance, IN.texcoord.y - distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x - distance, IN.texcoord.y)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x - distance, IN.texcoord.y + distance)) + _TextureSampleAdd) * IN.color;
 // color += (tex2D(_MainTex, half2(IN.texcoord.x, IN.texcoord.y + distance)) + _TextureSampleAdd) * IN.color;
 // color /= 9;
 
 // color.a *= UnityGet2DClipping(IN.worldPosition.xy, _ClipRect);
 
 // #ifdef UNITY_UI_ALPHACLIP
 // clip (color.a - 0.001);
 // #endif

 // return color;
 }
 ENDCG
 }
 }
}

里面分了兩個Pass來計算,這樣算出的效果會好很多,然后里面試了跟多的計算,越多的分層,那么得到的效果也越高,也意味著更加的不流暢。將shader放到一個新建的材質球上,然后把材質拖到Image組件的Material屬性欄上就可以了。

Unity3D UGUI特效之Image高斯模糊效果

看看效果:

Unity3D UGUI特效之Image高斯模糊效果

Unity3D UGUI特效之Image高斯模糊效果

然后說第二個方式,它是將背景都給模糊的效果。

Shader "Custom/BackBlur"
{
 Properties
 {
  [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
 _Color ("Main Color", Color) = (1,1,1,1)
  _Size ("Size", Range(0, 20)) = 1
 }
 Category { 
 
  // We must be transparent, so other objects are drawn before this one. 
  Tags { 
 "Queue"="Transparent" 
 "IgnoreProjector"="True" 
 "RenderType"="Transparent"
 "PreviewType" = "Plane"
 "CanUseSpriteAtlas" = "True"
 } 
 
 
  SubShader { 
 
   // Horizontal blur 
   GrabPass {      
    Tags { "LightMode" = "Always" } 
   } 
   Pass {
    Tags { "LightMode" = "Always" } 

    Name "BackBlurHor"
    CGPROGRAM 
    #pragma vertex vert 
    #pragma fragment frag 
    #pragma fragmentoption ARB_precision_hint_fastest 
    #include "UnityCG.cginc" 
 
    struct appdata_t { 
     float4 vertex : POSITION; 
     float2 texcoord : TEXCOORD0;
  float4 color : COLOR;
    }; 
 
    struct v2f { 
     float4 vertex : POSITION; 
     float4 uvgrab : TEXCOORD0;
  float4 color : COLOR;
    };
 
    v2f vert (appdata_t v) { 
     v2f o; 
     o.vertex = UnityObjectToClipPos(v.vertex); 
     #if UNITY_UV_STARTS_AT_TOP 
     float scale = -1.0; 
     #else 
     float scale = 1.0; 
     #endif 
     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5; 
     o.uvgrab.zw = o.vertex.zw; 

  o.color = v.color;
     return o; 
    } 
 
    sampler2D _GrabTexture; 
    float4 _GrabTexture_TexelSize;
  float4 _MainTex_TexelSize;
    float _Size;
    uniform float4 _Color;

    // static float GaussianKernel[9] = {
    //  0.05, 0.09, 0.12,
    //  0.15, 0.18, 0.15,
    //  0.12, 0.09, 0.05
    // };

    // static float GaussianKernel[19] = {
    //  0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09,
    //  0.1,
    //  0.09, 0.08, 0.07, 0.06, 0.05, 0.04, 0.03, 0.02, 0.01,
    // };
    // static float GaussianKernelD[19] = {
    //  -9.0, -8.0, -7.0, -6.0, -5.0, -4.0, -3.0, -2.0, -1.0,
    //  0.0,
    //  +1.0, +2.0, +3.0, +4.0, +5.0, +6.0, +7.0, +8.0, +9.0,
    // };

    half4 GrabPixel(v2f i, float weight, float kernelx){
     if (i.uvgrab.x == 0 && i.uvgrab.y == 0){
      kernelx = 0;
     }
     return tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x*kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight;
    }
    half4 frag( v2f i ) : COLOR { 
     half4 sum = half4(0,0,0,0);
     // #define GRABPIXEL(weight, kernelx) tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x + _GrabTexture_TexelSize.x * kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight
     
     sum += GrabPixel(i, 0.05, -4.0);
     sum += GrabPixel(i, 0.09, -3.0);
     sum += GrabPixel(i, 0.12, -2.0);
     sum += GrabPixel(i, 0.15, -1.0);
     sum += GrabPixel(i, 0.18, 0.0);
     sum += GrabPixel(i, 0.15, +1.0);
     sum += GrabPixel(i, 0.12, +2.0);
     sum += GrabPixel(i, 0.09, +3.0);
     sum += GrabPixel(i, 0.05, +4.0);

     // sum += GrabPixel(i, 0.01, -9.0);
     // sum += GrabPixel(i, 0.02, -8.0);
     // sum += GrabPixel(i, 0.03, -7.0);
     // sum += GrabPixel(i, 0.04, -6.0);
     // sum += GrabPixel(i, 0.05, -5.0);
     // sum += GrabPixel(i, 0.06, -4.0);
     // sum += GrabPixel(i, 0.07, -3.0);
     // sum += GrabPixel(i, 0.08, -2.0);
     // sum += GrabPixel(i, 0.09, -1.0);
     // sum += GrabPixel(i, 0.10, 0.0);
     // sum += GrabPixel(i, 0.09, +1.0);
     // sum += GrabPixel(i, 0.08, +2.0);
     // sum += GrabPixel(i, 0.07, +3.0);
     // sum += GrabPixel(i, 0.06, +4.0);
     // sum += GrabPixel(i, 0.05, +5.0);
     // sum += GrabPixel(i, 0.04, +6.0);
     // sum += GrabPixel(i, 0.03, +7.0);
     // sum += GrabPixel(i, 0.02, +8.0);
     // sum += GrabPixel(i, 0.01, +9.0);

     float4 col5 = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  float decayFactor = 1.0f;
  if (i.uvgrab.x == 0 && i.uvgrab.y == 0){
  decayFactor = 0;
  }
  sum = lerp(col5, sum, decayFactor) * i.color * _Color;
 
     return sum; 
    } 
    ENDCG 
   } 
   // Vertical blur 
   GrabPass {       
    Tags { "LightMode" = "Always" } 
   } 
   Pass { 
    Tags { "LightMode" = "Always" }

    Name "BackBlurVer"
    CGPROGRAM 
    #pragma vertex vert 
    #pragma fragment frag 
    #pragma fragmentoption ARB_precision_hint_fastest 
    #include "UnityCG.cginc" 
 
    struct appdata_t { 
     float4 vertex : POSITION; 
     float2 texcoord: TEXCOORD0; 
  float4 color : COLOR;
    }; 
 
    struct v2f { 
     float4 vertex : POSITION; 
     float4 uvgrab : TEXCOORD0; 
  float4 color : COLOR;
    }; 
 
    v2f vert (appdata_t v) { 
     v2f o; 
     o.vertex = UnityObjectToClipPos(v.vertex); 
     #if UNITY_UV_STARTS_AT_TOP 
     float scale = -1.0; 
     #else 
     float scale = 1.0; 
     #endif 
     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5; 
     o.uvgrab.zw = o.vertex.zw; 

  o.color = v.color;
     return o; 
    } 
 
    sampler2D _GrabTexture; 
    float4 _GrabTexture_TexelSize; 
    float _Size;
    uniform float4 _Color;

    half4 GrabPixel(v2f i, float weight, float kernely){
     if (i.uvgrab.x == 0 && i.uvgrab.y == 0){
      kernely = 0;
     }
     return tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y*kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight;
    }
 
    half4 frag( v2f i ) : COLOR {
     half4 sum = half4(0,0,0,0); 
     // #define GRABPIXEL(weight,kernely) tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _GrabTexture_TexelSize.y * kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight 
 
     sum += GrabPixel(i, 0.05, -4.0);
     sum += GrabPixel(i, 0.09, -3.0);
     sum += GrabPixel(i, 0.12, -2.0);
     sum += GrabPixel(i, 0.15, -1.0);
     sum += GrabPixel(i, 0.18, 0.0);
     sum += GrabPixel(i, 0.15, +1.0);
     sum += GrabPixel(i, 0.12, +2.0);
     sum += GrabPixel(i, 0.09, +3.0);
     sum += GrabPixel(i, 0.05, +4.0);

     // sum += GrabPixel(i, 0.01, -9.0);
     // sum += GrabPixel(i, 0.02, -8.0);
     // sum += GrabPixel(i, 0.03, -7.0);
     // sum += GrabPixel(i, 0.04, -6.0);
     // sum += GrabPixel(i, 0.05, -5.0);
     // sum += GrabPixel(i, 0.06, -4.0);
     // sum += GrabPixel(i, 0.07, -3.0);
     // sum += GrabPixel(i, 0.08, -2.0);
     // sum += GrabPixel(i, 0.09, -1.0);
     // sum += GrabPixel(i, 0.10, 0.0);
     // sum += GrabPixel(i, 0.09, +1.0);
     // sum += GrabPixel(i, 0.08, +2.0);
     // sum += GrabPixel(i, 0.07, +3.0);
     // sum += GrabPixel(i, 0.06, +4.0);
     // sum += GrabPixel(i, 0.05, +5.0);
     // sum += GrabPixel(i, 0.04, +6.0);
     // sum += GrabPixel(i, 0.03, +7.0);
     // sum += GrabPixel(i, 0.02, +8.0);
     // sum += GrabPixel(i, 0.01, +9.0);

  float4 col5 = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
  float decayFactor = 1.0f;
  if (i.uvgrab.x == 0 && i.uvgrab.y == 0){
  decayFactor = 0;
  }
  sum = lerp(col5, sum, decayFactor) * i.color * _Color;
 
     return sum; 
    } 
    ENDCG 
   }
  } 
 } 
}


計算是一樣的,不過這個相對來說要慢,畢竟是將整個屏幕那來計算的,肯定會慢很多。用法也一樣,shader放到一個新建的材質球上,然后將材質球拖到一個Image組件的Material屬性欄中即可,這里你可以調整Image的寬高,這樣可以指定模糊那一塊區域了。

看效果:

Unity3D UGUI特效之Image高斯模糊效果

然后我移動和調整寬高之后:

Unity3D UGUI特效之Image高斯模糊效果

當然也可以調成整屏模糊:

Unity3D UGUI特效之Image高斯模糊效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

普定县| 探索| 黄冈市| 昭苏县| 历史| 白沙| 汶川县| 上虞市| 交口县| 乐山市| 兖州市| 龙川县| 屏东市| 安泽县| 上饶市| 株洲县| 开阳县| 千阳县| 前郭尔| 恭城| 巴里| 沾化县| 清远市| 凌云县| 南丹县| 夏邑县| 虎林市| 岫岩| 会理县| 平果县| 东安县| 淮北市| 平原县| 航空| 襄樊市| 福安市| 车险| 正阳县| 九龙城区| 山阴县| 曲靖市|