123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
- //Blurs with predefined kernel and weights
- Shader "ZED/ZED Blur"
- {
- Properties
- {
- _MainTex("Texture", 2D) = "white" {}
- }
- SubShader
- {
- Tags { "RenderType" = "Opaque" }
- ZWrite Off
- ZTest Always
- Cull Off
- //0 - Blur based on alpha
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma target 4.0
- #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;
- v2f vert(appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- return o;
- }
- float4 _MainTex_TexelSize;
- uniform int horizontal;
- uniform float weights[5];
- uniform float offset[5];
- float4 frag(v2f vi) : SV_Target
- {
- float2 uv = vi.uv;
- float result = tex2D(_MainTex, uv).a * weights[0]; // current fragment's contribution
- int i = 0;
- if (horizontal == 1)
- {
- result += tex2D(_MainTex, uv + float2(offset[1] * _MainTex_TexelSize.x, 0.0)).a * weights[1];
- result += tex2D(_MainTex, uv - float2(offset[1] * _MainTex_TexelSize.x, 0.0)).a * weights[1];
- result += tex2D(_MainTex, uv + float2(offset[2] * _MainTex_TexelSize.x, 0.0)).a * weights[2];
- result += tex2D(_MainTex, uv - float2(offset[2] * _MainTex_TexelSize.x, 0.0)).a * weights[2];
-
- }
- else
- {
- result += tex2D(_MainTex, uv + float2(0.0, offset[1] * _MainTex_TexelSize.y)).a * weights[1];
- result += tex2D(_MainTex, uv - float2(0.0, offset[1] * _MainTex_TexelSize.y)).a * weights[1];
- result += tex2D(_MainTex, uv + float2(0.0, offset[2] * _MainTex_TexelSize.y)).a * weights[2];
- result += tex2D(_MainTex, uv - float2(0.0, offset[2] * _MainTex_TexelSize.y)).a * weights[2];
- }
- return float4(tex2D(_MainTex, uv).rgb, result);
- }
- ENDCG
- }
- // 1 - Blur based on RGB, R unchanged, GB blured
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma target 4.0
- #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;
- v2f vert(appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- return o;
- }
- float4 _MainTex_TexelSize;
- uniform int horizontal;
- uniform float weights2[5];
- uniform float offset2[5];
- float4 frag(v2f vi) : SV_Target
- {
- float2 uv = vi.uv;
- float3 result = tex2D(_MainTex, uv).rgb * weights2[0]; // current fragment's contribution
- result.r = tex2D(_MainTex, uv).r;
- int i = 0;
- if (horizontal == 1)
- {
- result.gb += tex2D(_MainTex, uv + float2(offset2[1] * _MainTex_TexelSize.x, 0.0)).gb * weights2[1];
- result.gb += tex2D(_MainTex, uv - float2(offset2[1] * _MainTex_TexelSize.x, 0.0)).gb * weights2[1];
- result.gb += tex2D(_MainTex, uv + float2(offset2[2] * _MainTex_TexelSize.x, 0.0)).gb * weights2[2];
- result.gb += tex2D(_MainTex, uv - float2(offset2[2] * _MainTex_TexelSize.x, 0.0)).gb * weights2[2];
-
- }
- else
- {
- result.gb += tex2D(_MainTex, uv + float2(0.0, offset2[1] * _MainTex_TexelSize.y)).gb * weights2[1];
- result.gb += tex2D(_MainTex, uv - float2(0.0, offset2[1] * _MainTex_TexelSize.y)).gb * weights2[1];
- result.gb += tex2D(_MainTex, uv + float2(0.0, offset2[2] * _MainTex_TexelSize.y)).gb * weights2[2];
- result.gb += tex2D(_MainTex, uv - float2(0.0, offset2[2] * _MainTex_TexelSize.y)).gb * weights2[2];
-
- }
- return float4(result.x, result.y, result.z, 1);
- }
- ENDCG
- }
- // 2 - Blur based with mask
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma target 4.0
- #include "UnityCG.cginc"
- struct appdata
- {
- float4 vertex : POSITION;
- float2 uv : TEXCOORD0;
- };
- struct v2f
- {
- float2 uv : TEXCOORD0;
- float4 vertex : SV_POSITION;
- };
- sampler2D _MainTex;
- sampler2D _Mask;
- float4 _MainTex_ST;
- v2f vert(appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- return o;
- }
- float4 _MainTex_TexelSize;
- uniform int horizontal;
- uniform float weights[5];
- uniform float offset[5];
- float4 frag(v2f vi) : SV_Target
- {
- //TODO issue : The blur will shrink the virtual due to the mask
- float2 uv = vi.uv;
- float3 result = tex2D(_MainTex, uv).rgb;
- if (tex2D(_Mask, uv).r != 0) {
- result *= weights[0]; // current fragment's contribution
- int i = 0;
- if (horizontal == 1)
- {
- result += tex2D(_MainTex, uv + float2(offset[1] * _MainTex_TexelSize.x, 0.0)).rgb * weights[1];
- result += tex2D(_MainTex, uv - float2(offset[1] * _MainTex_TexelSize.x, 0.0)).rgb * weights[1];
- result += tex2D(_MainTex, uv + float2(offset[2] * _MainTex_TexelSize.x, 0.0)).rgb * weights[2];
- result += tex2D(_MainTex, uv - float2(offset[2] * _MainTex_TexelSize.x, 0.0)).rgb * weights[2];
-
- }
- else
- {
- result += tex2D(_MainTex, uv + float2(0.0, offset[1] * _MainTex_TexelSize.y)).rgb * weights[1];
- result += tex2D(_MainTex, uv - float2(0.0, offset[1] * _MainTex_TexelSize.y)).rgb * weights[1];
- result += tex2D(_MainTex, uv + float2(0.0, offset[2] * _MainTex_TexelSize.y)).rgb * weights[2];
- result += tex2D(_MainTex, uv - float2(0.0, offset[2] * _MainTex_TexelSize.y)).rgb * weights[2];
-
- }
- }
- return float4(result, 1);
- }
- ENDCG
- }
- //3 - Blur based on R
- Pass
- {
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma target 4.0
- #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;
- v2f vert(appdata v)
- {
- v2f o;
- o.vertex = UnityObjectToClipPos(v.vertex);
- o.uv = TRANSFORM_TEX(v.uv, _MainTex);
- return o;
- }
- float4 _MainTex_TexelSize;
- uniform int horizontal;
- uniform float weights[5];
- uniform float offset[5];
- float4 frag(v2f vi) : SV_Target
- {
- float2 uv = vi.uv;
- float result = tex2D(_MainTex, uv).r * weights[0]; // current fragment's contribution
- int i = 0;
- if (horizontal == 1)
- {
- result += tex2D(_MainTex, uv + float2(offset[1] * _MainTex_TexelSize.x, 0.0)).r * weights[1];
- result += tex2D(_MainTex, uv - float2(offset[1] * _MainTex_TexelSize.x, 0.0)).r * weights[1];
- result += tex2D(_MainTex, uv + float2(offset[2] * _MainTex_TexelSize.x, 0.0)).r * weights[2];
- result += tex2D(_MainTex, uv - float2(offset[2] * _MainTex_TexelSize.x, 0.0)).r * weights[2];
-
- }
- else
- {
- result += tex2D(_MainTex, uv + float2(0.0, offset[1] * _MainTex_TexelSize.y)).r * weights[1];
- result += tex2D(_MainTex, uv - float2(0.0, offset[1] * _MainTex_TexelSize.y)).r * weights[1];
- result += tex2D(_MainTex, uv + float2(0.0, offset[2] * _MainTex_TexelSize.y)).r * weights[2];
- result += tex2D(_MainTex, uv - float2(0.0, offset[2] * _MainTex_TexelSize.y)).r * weights[2];
-
- }
- return float4(result, result, result, result);
- }
- ENDCG
- }
- }
- }
|