Search Unity

Magnification shader

Discussion in 'Universal Render Pipeline' started by DebugLogError, Apr 8, 2020.

  1. DebugLogError

    DebugLogError

    Joined:
    Jul 24, 2013
    Posts:
    60
    I'm trying to get this magnification shader to work with the URP but I'm not sure what needs to change.

    I would really appreciate any help!

    Code (CSharp):
    1. Shader "Custom/Magnification"
    2. {
    3.     Properties
    4.     {
    5.         _Magnification("Magnification", Float) = 1
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Tags{ "Queue" = "Transparent" "PreviewType" = "Plane" }
    11.         LOD 100
    12.  
    13.         GrabPass{ "_GrabTexture" }
    14.  
    15.         Pass
    16.             {
    17.                 ZTest On
    18.                 ZWrite Off
    19.                 Blend One Zero
    20.                 Lighting Off
    21.                 Fog{ Mode Off }
    22.  
    23.             CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.  
    27.             #include "UnityCG.cginc"
    28.  
    29.             struct appdata
    30.             {
    31.                 float4 vertex : POSITION;
    32.             };
    33.  
    34.             struct v2f
    35.             {
    36.                 //our UV coordinate on the GrabTexture
    37.                 float4 uv : TEXCOORD0;
    38.                 //our vertex position after projection
    39.                 float4 vertex : SV_POSITION;
    40.             };
    41.  
    42.             sampler2D _GrabTexture;
    43.             half _Magnification;
    44.  
    45.             v2f vert(appdata v)
    46.             {
    47.                 v2f o;
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);
    49.                 //the UV coordinate of our object's center on the GrabTexture
    50.                 float4 uv_center = ComputeGrabScreenPos(UnityObjectToClipPos(float4(0, 0, 0, 1)));
    51.                 //the vector from uv_center to our UV coordinate on the GrabTexture
    52.                 float4 uv_diff = ComputeGrabScreenPos(o.vertex) - uv_center;
    53.                 //apply magnification
    54.                 uv_diff /= _Magnification;
    55.                 //save result
    56.                 o.uv = uv_center + uv_diff;
    57.                 return o;
    58.             }
    59.  
    60.             fixed4 frag(v2f i) : COLOR
    61.             {
    62.                 return tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(i.uv));
    63.             }
    64.             ENDCG
    65.         }
    66.     }
    67. }
    Annotation 2020-04-08 112128.jpg
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    There's no grabpass in URP anymore, I think you would have to use _CameraOpaqueTexture which (afaik) is similar to the grabpass functionality.

    Maybe you could remake this as a Shader Graph? You can access Scene Color in Shader Graph which provides you that same camera image.
     
    Last edited: Apr 17, 2020
    gotiobg and DebugLogError like this.
  3. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    172
    Did you manage to pull this off? Im looking for a magnification shader for URP for 3 years
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Three years? Surely you already tried to sample the Scene Color in the Shader Graph during those three years?

    You could just manipulate the UV coordinates (resize UVs) and then perform the sampling and finally render the sampled image to the magnifying glass surface. This setup could produce something like this (I just did a quick test.):

    20210627_magnifying_glass.PNG
     
    DebugLogError likes this.
  5. swantonb

    swantonb

    Joined:
    Apr 10, 2018
    Posts:
    172
    Very nice! Thank you
     
  6. gotiobg

    gotiobg

    Joined:
    May 10, 2017
    Posts:
    19
    What worked for me was swapping the Screen Position node to the Camera.Position node