赞
踩
这个shader 参考了:
unity-frosted-glass-master毛玻璃效果
https://blog.csdn.net/f_957995490/article/details/106616041
上面这个方案使用commandbuff制作效果,但是无法处理透明物体和UI。原因是:CommandBuff无法在UI渲染之后和玻璃模型渲染之前插入。(如果那位兄弟有解决办法,希望告知)
不得已只能使用GrabPass来获取全屏图像。
又因为只在一个Shader中处理,就没有使用迭代和downsize操作。
模糊效果不是特别好,在手机上还凑合。
PS:如果ui canvas 设置了Order in Layer, 那么可以给玻璃添加SortingGroup 来调节渲染顺序。
不多说了,上代码:
Shader "BlurGlass" { Properties { _MaskTex ("Mask", 2D) = "white" {} _BlurSize ("BlurSize", Range(0, 4)) = 1 } SubShader { Tags { "RenderType"="Opaque" "Queue"="Overlay"} GrabPass{"_GrabPassTexture"} Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float4 pos : POSITION; float2 uv : TEXCOORD7; float4 grap_uv : TEXCOORD0; float4 uv01 : TEXCOORD1; float4 uv23 : TEXCOORD2; float4 uv45 : TEXCOORD3; float4 uv10 : TEXCOORD4; float4 uv32 : TEXCOORD5; float4 uv54 : TEXCOORD6; }; float _BlurSize; sampler2D _MaskTex; float4 _MaskTex_ST; sampler2D _GrabPassTexture; float4 _GrabPassTexture_TexelSize; v2f vert (appdata v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MaskTex); o.grap_uv = ComputeGrabScreenPos(o.pos); o.grap_uv = o.grap_uv /o.grap_uv.w; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 _maskColor = tex2D(_MaskTex, i.uv); _BlurSize = _maskColor.r * _BlurSize; i.uv01 = i.grap_uv.xyxy + _GrabPassTexture_TexelSize.xyxy * float4(1,1, -1,-1) * _BlurSize; i.uv23 = i.grap_uv.xyxy + _GrabPassTexture_TexelSize.xyxy * float4(1,1, -1,-1) * 2.0 * _BlurSize; i.uv45 = i.grap_uv.xyxy + _GrabPassTexture_TexelSize.xyxy * float4(1,1, -1,-1) * 3.0 * _BlurSize; i.uv10 = i.grap_uv.xyxy + _GrabPassTexture_TexelSize.xyxy * float4(1,-1, -1,1) * _BlurSize; i.uv32 = i.grap_uv.xyxy + _GrabPassTexture_TexelSize.xyxy * float4(1,-1, -1,1) * 2.0 * _BlurSize; i.uv54 = i.grap_uv.xyxy + _GrabPassTexture_TexelSize.xyxy * float4(1,-1, -1,1) * 3.0 * _BlurSize; half4 color = float4 (0,0,0,0); color += 0.20 * tex2D (_GrabPassTexture, i.grap_uv ); color += 0.1 * tex2D (_GrabPassTexture, i.uv01.xy ); color += 0.1 * tex2D (_GrabPassTexture, i.uv01.zw ); color += 0.075 * tex2D (_GrabPassTexture, i.uv23.xy ); color += 0.075 * tex2D (_GrabPassTexture, i.uv23.zw ); color += 0.025 * tex2D (_GrabPassTexture, i.uv45.xy ); color += 0.025 * tex2D (_GrabPassTexture, i.uv45.zw ); color += 0.1 * tex2D (_GrabPassTexture, i.uv10.xy ); color += 0.1 * tex2D (_GrabPassTexture, i.uv10.zw ); color += 0.075* tex2D (_GrabPassTexture, i.uv32.xy ); color += 0.075* tex2D (_GrabPassTexture, i.uv32.zw ); color += 0.025 * tex2D (_GrabPassTexture, i.uv54.xy ); color += 0.025 * tex2D (_GrabPassTexture, i.uv54.zw ); return color ; } ENDCG } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。