vuforia 背景毛玻璃

素材准备

  • MacOS 10.13.6
  • Unity2018.3.6f1
  • 高斯模糊背景知识
  • 包含vuforia SDK的Unity工程

详细步骤

  • 在工程中创建一个新的shader,直接上shader代码
Shader "Custom/ARBackgroundBlur"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
        _Size ("Size", Range(0, 20)) = 2
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "UnityCG.cginc"

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

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

			sampler2D _MainTex;
			float4 _MainTex_ST;
            float _Size;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
                float depth= _Size*0.001;
                half4 sum = half4(0,0,0,0);
                #define GRABPIXEL(weight,kernelx) tex2D( _MainTex, float2(i.uv.x + kernelx*depth*2, i.uv.y+ kernelx*depth*2)) * weight

                sum += GRABPIXEL(0.025, -5.0);
                sum += GRABPIXEL(0.05, -4.0);
                sum += GRABPIXEL(0.09, -3.0);
                sum += GRABPIXEL(0.12, -2.0);
                sum += GRABPIXEL(0.15, -1.0);
                sum += GRABPIXEL(0.16,  0.0);
                sum += GRABPIXEL(0.15, +1.0);
                sum += GRABPIXEL(0.12, +2.0);
                sum += GRABPIXEL(0.09, +3.0);
                sum += GRABPIXEL(0.05, +4.0);
                sum += GRABPIXEL(0.025, +5.0);

                return sum;
			}
			ENDCG
		}
	}
}
  • 替换VuforiaConfiguration中的VideoBackgroundshader,如图
    在这里插入图片描述
  • 启动Vuforia场景,AR相机下的BackgroundPlaneshader如下,调节size即可改变模糊程度
    vuforia 背景毛玻璃_第1张图片
    *效果如图
  • 对比图

如果帮到你了,帮忙点个赞,祝大家学习愉快!

你可能感兴趣的:(Unity,Vuforia,AR,毛玻璃)