Search Unity

Magnifying Shader with alpha clip from texture.

Discussion in 'Shaders' started by duducorvao, Jan 9, 2020.

  1. duducorvao

    duducorvao

    Joined:
    Feb 21, 2016
    Posts:
    3
    Hello, I'm trying to use a magnifying shader to apply a zoom in a certain area of my game and clip some part of the magnified area with a simple white circle texture with a black bezel. I want to show the magnified part only in the white area of the circle.

    I found some magnifying shader here: https://www.reddit.com/r/Unity3D/comments/5n081a/magnifying_glass_shader_i_need_a_shader_that/ and made some modifications to try to clip the image.

    The results i'm getting are : the image created go full black, or show the magnified image in the entire RectTransform.

    Can you guys tell me what i'm doing wrong in my shader? Thanks

    Shader:
    Code (CSharp):
    1. Shader "Unlit/ZoomShader"
    2. {
    3.     Properties
    4.     {
    5.         _MaskColor("Mask Color", Color) = (1,1,1,1)
    6.         _Mask("Mask", 2D) = "white" {}
    7.         _Magnification("Magnification", Float) = 1
    8.         _UVCenterOffset("UVCenterOffset", Vector) = (0,0,0,1)
    9.     }
    10.  
    11.         SubShader
    12.     {
    13.         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
    14.         LOD 100
    15.  
    16.         GrabPass{ "_GrabTexture" }
    17.  
    18.         Pass
    19.             {
    20.                 //ZTest On
    21.                 //ZWrite Off
    22.                 //Blend One Zero
    23.                 //Lighting Off
    24.                 //Fog{ Mode Off }
    25.  
    26.             CGPROGRAM
    27.             #pragma vertex vert
    28.             #pragma fragment frag
    29.  
    30.             #include "UnityCG.cginc"
    31.  
    32.             struct appdata
    33.             {
    34.                 float4 vertex : POSITION;
    35.                 float4 uv : TEXCOORD0;
    36.                 float4 uv2 : TEXCOORD1;
    37.             };
    38.  
    39.             struct v2f
    40.             {
    41.                 //our vertex position after projection
    42.                 float4 vertex : SV_POSITION;
    43.  
    44.                 //our UV coordinate on the GrabTexture
    45.                 float4 uv : TEXCOORD0;
    46.                 float2 uv2 : TEXCOORD1;
    47.             };
    48.  
    49.             float4 _Mask_ST;
    50.             sampler2D _GrabTexture;
    51.             float4 _MaskColor;
    52.             sampler2D _Mask;
    53.             half _Magnification;
    54.             float4 _UVCenterOffset;
    55.  
    56.             v2f vert(appdata v)
    57.             {
    58.                 v2f o;
    59.                 o.vertex = UnityObjectToClipPos(v.vertex);
    60.  
    61.                 //the UV coordinate of our object's center on the GrabTexture
    62.                 float4 uv_center = ComputeGrabScreenPos(UnityObjectToClipPos(float4(0, 0, 0, 1)));
    63.  
    64.                 uv_center += _UVCenterOffset;
    65.                 //the vector from uv_center to our UV coordinate on the GrabTexture
    66.                 float4 uv_diff = ComputeGrabScreenPos(o.vertex) - uv_center;
    67.                 //apply magnification
    68.                 uv_diff /= _Magnification;
    69.                 //save result
    70.                 o.uv = uv_center + uv_diff;
    71.                 o.uv2 = TRANSFORM_TEX(v.uv2, _Mask);
    72.                 return o;
    73.             }
    74.  
    75.             fixed4 frag(v2f i) : COLOR
    76.             {
    77.                 fixed4 albedo = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uv));
    78.                 fixed4 mask = tex2D(_Mask, UNITY_PROJ_COORD(i.uv2));
    79.                 bool isMask = mask != _MaskColor;
    80.                 albedo = (1 - isMask)*albedo + (isMask * mask);
    81.                 return albedo;
    82.             }
    83.             ENDCG
    84.         }
    85.     }
    86. }
     
  2. duducorvao

    duducorvao

    Joined:
    Feb 21, 2016
    Posts:
    3
    Well. I fixed on my own.
    Here is the fixed shader if anyone want it:


    Code (CSharp):
    1. Shader "Unlit/ZoomShader"
    2. {
    3.     Properties
    4.     {
    5.         _Mask("Mask", 2D) = "white" {}
    6.         _Magnification("Magnification", Float) = 1
    7.         _UVCenterOffset("UVCenterOffset", Vector) = (0,0,0,1)
    8.     }
    9.  
    10.         SubShader
    11.     {
    12.         Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
    13.         Cull Off ZWrite Off ZTest Always
    14.         Blend SrcAlpha OneMinusSrcAlpha
    15.  
    16.         GrabPass{ "_GrabTexture" }
    17.  
    18.         Pass
    19.             {
    20.  
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.  
    25.             #include "UnityCG.cginc"
    26.  
    27.             struct appdata
    28.             {
    29.                 float4 vertex : POSITION;
    30.                 float4 uv : TEXCOORD0;
    31.                 float4 uv2 : TEXCOORD1;
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 //our vertex position after projection
    37.                 float4 vertex : SV_POSITION;
    38.  
    39.                 //our UV coordinate on the GrabTexture
    40.                 float4 uv : TEXCOORD0;
    41.                 float2 uv2 : TEXCOORD1;
    42.             };
    43.  
    44.             float4 _Mask_ST;
    45.             sampler2D _GrabTexture;
    46.             sampler2D _Mask;
    47.             half _Magnification;
    48.             float4 _UVCenterOffset;
    49.  
    50.             v2f vert(appdata v)
    51.             {
    52.                 v2f o;
    53.                 o.vertex = UnityObjectToClipPos(v.vertex);
    54.  
    55.                 //the UV coordinate of our object's center on the GrabTexture
    56.                 float4 uv_center = ComputeGrabScreenPos(UnityObjectToClipPos(float4(0, 0, 0, 1)));
    57.  
    58.                 uv_center += _UVCenterOffset;
    59.                 //the vector from uv_center to our UV coordinate on the GrabTexture
    60.                 float4 uv_diff = ComputeGrabScreenPos(o.vertex) - uv_center;
    61.                 //apply magnification
    62.                 uv_diff /= _Magnification;
    63.                 //save result
    64.                 o.uv = uv_center + uv_diff;
    65.                 o.uv2 = v.uv;//TRANSFORM_TEX(v.uv2, _Mask);
    66.                 return o;
    67.             }
    68.  
    69.             fixed4 frag(v2f i) : COLOR
    70.             {
    71.                 fixed4 albedo = tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uv));
    72.                 fixed4 mask = tex2D(_Mask, i.uv2);
    73.                 return albedo *= mask;
    74.             }
    75.             ENDCG
    76.         }
    77.     }
    78. }
     
    sivabalan93 and Nikitty like this.