unity 不使用GrabPass实现ui毛玻璃

由于有些手机不支持GrabPass,所以ui上面的毛玻璃不能用这种方式实现

解决方案:用camera渲染出来的rendertexture代替GrabPass

基本思路就这样,实现不麻烦,我直接拿别人写过用grabpass实现的shader,改成用rendertexture

局限性:只能用于背景是场景的

先看效果

unity 不使用GrabPass实现ui毛玻璃_第1张图片

unity 不使用GrabPass实现ui毛玻璃_第2张图片

1.shader,只是把用到grabpass的改成用rendertexture

Shader "Custom/UIBackBlur"
{
	Properties
	{
		[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
		_Color("Main Color", Color) = (1,1,1,1)
		_Size("Size", Range(0, 500)) = 1
		_BG("BG", 2D) = "white" {}
		_Factor("_Factor",Range(0, 1)) = 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{

		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 _BG;
	float4 _BG_TexelSize;
	float4 _MainTex_TexelSize;
	float _Size;
	uniform float4 _Color;
	fixed _Factor;

	half4 GrabPixel(v2f i, float weight, float kernelx) {
		if (i.uvgrab.x == 0 && i.uvgrab.y == 0) {
			kernelx = 0;
		}
		return tex2Dproj(_BG, UNITY_PROJ_COORD(float4(i.uvgrab.x + _BG_TexelSize.x*kernelx*_Size, i.uvgrab.y, i.uvgrab.z, i.uvgrab.w))) * weight;
	}

	half4 GrabPixel2(v2f i, float weight, float kernely) {
		if (i.uvgrab.x == 0 && i.uvgrab.y == 0) {
			kernely = 0;
		}
		return tex2Dproj(_BG, UNITY_PROJ_COORD(float4(i.uvgrab.x, i.uvgrab.y + _BG_TexelSize.y*kernely*_Size, i.uvgrab.z, i.uvgrab.w))) * weight;
	}

	half4 frag(v2f i) : COLOR{
		half4 sum = half4(0,0,0,0);
		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);

		half4 sum2 = half4(0, 0, 0, 0);
		sum2 += GrabPixel2(i, 0.05, -4.0);
		sum2 += GrabPixel2(i, 0.09, -3.0);
		sum2 += GrabPixel2(i, 0.12, -2.0);
		sum2 += GrabPixel2(i, 0.15, -1.0);
		sum2 += GrabPixel2(i, 0.18, 0.0);
		sum2 += GrabPixel2(i, 0.15, +1.0);
		sum2 += GrabPixel2(i, 0.12, +2.0);
		sum2 += GrabPixel2(i, 0.09, +3.0);
		sum2 += GrabPixel2(i, 0.05, +4.0);

		sum = sum * 0.5 + sum2 * 0.5;
		float4 col5 = tex2Dproj(_BG, UNITY_PROJ_COORD(i.uvgrab));
		float decayFactor = 1.0f;
		if (i.uvgrab.x == 0 && i.uvgrab.y == 0) {
			decayFactor = 0;
		}
		sum = lerp(col5, sum, _Factor) * i.color * _Color;

		return sum;
	}
		ENDCG
	}
	}
	}
}

2.保存摄像机的rendertexture

代码比较简单就是保存的rendertexture赋值给上面shader对应的材质球,然后ui挂这个材质球就ok

为了性能OnPreRender和OnPostRender,而且后面可能还要用后处理

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraTex : MonoBehaviour {

    public Material m;

    public RenderTexture cameraRenderTex;

    public Camera mainCamera;

    private int bg = Shader.PropertyToID("_BG");

    private void Awake()
    {
        mainCamera.allowHDR = false;
        mainCamera.allowMSAA = false;
    }


    private void OnPreRender()
    {
        cameraRenderTex = RenderTexture.GetTemporary(Screen.width, Screen.height, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
        mainCamera.targetTexture = cameraRenderTex;
        m.SetTexture(bg, cameraRenderTex);
    }

    private void OnPostRender()
    {
        mainCamera.targetTexture = null;
        Graphics.Blit(cameraRenderTex, null as RenderTexture);
        RenderTexture.ReleaseTemporary(cameraRenderTex);
    }
}

 

下面是工程地址

链接:https://pan.baidu.com/s/1ZnlAitgCUEVHTJkS-aIrOw 
提取码:3im2 

你可能感兴趣的:(Shader)