Search Unity

Question Magnification based on texture.

Discussion in 'Shaders' started by Relo999, Apr 15, 2021.

  1. Relo999

    Relo999

    Joined:
    Dec 22, 2015
    Posts:
    24
    I got this magnification shader. The problem is I wanted to make a shader that will use texture to get magnification value, but I have no clue how to solve that. I imagine using a texture where black would be 0 magnification and and white being full magnification and being able to use a gradient to move between them.


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

    Relo999

    Joined:
    Dec 22, 2015
    Posts:
    24
    I hope this helps explain what I'd like to do. Still haven't figured it out...

    Closest I have gotten is simply using the map as an source for the Alpha of the magnification, which creates a sort of visual echo effect rather than a spatial distortion look.
     
  3. acedam55

    acedam55

    Joined:
    May 7, 2019
    Posts:
    1
    Use a render texture for the magnification and lerp between it and the screen
     
    Last edited: Dec 22, 2023