Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Make a shader to fix affine mapping into uv (quadrilateral interpolation)

Discussion in 'Shaders' started by luigis, Oct 15, 2019.

  1. luigis

    luigis

    Joined:
    Oct 30, 2013
    Posts:
    15
    For my project i need to warp a quad mesh and correct the affine UV mapping to a Projective mapping.
    I've digged the web to find some practical examples but some solution are to difficult to me that i've no experince with shader programming.

    The ready to go solution it's starting from this thread: https://forum.unity.com/threads/correcting-affine-texture-mapping-for-trapezoids.151283/

    But i'd like to do something more complex that a trapezoid like this:


    and transform like this:



    I've tried to follow the explanation from this topic: https://gamedev.stackexchange.com/q...on-a-generated-mesh-that-tapers/176352#176352
    and i wrote my shader that is this:
    Code (CSharp):
    1. Shader "Unlit/ProjectiveShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.        
    7.  
    8.        
    9.     }
    10.     SubShader
    11.     {
    12.         Tags { "RenderType"="Opaque" }
    13.         LOD 100
    14.  
    15.         Pass
    16.         {
    17.             CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.             // make fog work
    21.             #pragma multi_compile_fog
    22.  
    23.             #include "UnityCG.cginc"
    24.  
    25.             struct appdata
    26.             {
    27.                 float4 vertex : POSITION;
    28.                 float3 uv : TEXCOORD0; // Change float2 to float 3.
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float3 uv : TEXCOORD0;
    34.                 UNITY_FOG_COORDS(1)
    35.                 float4 vertex : SV_POSITION;
    36.             };
    37.  
    38.             sampler2D _MainTex;
    39.             float4 _MainTex_ST;
    40.  
    41.  
    42.             v2f vert (appdata v)
    43.             {
    44.                 v2f o;
    45.                 o.vertex = UnityObjectToClipPos(v.vertex);
    46.                 //o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    47.                 o.uv = v.uv;
    48.                 // If you define a float4 with the special name _MainTex_ST,
    49.                 // you can get the same effect the macro had by doing this:
    50.                 o.uv.xy = o.uv.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    51.                 UNITY_TRANSFER_FOG(o,o.vertex);
    52.                 return o;
    53.             }
    54.  
    55.             fixed4 frag (v2f i) : SV_Target
    56.             {
    57.  
    58.                 float2 uv = i.uv.xy / i.uv.z;
    59.                 // sample the texture
    60.                 fixed4 col = tex2D(_MainTex, i.uv);
    61.                 // apply fog
    62.                 UNITY_APPLY_FOG(i.fogCoord, col);
    63.                 return col;
    64.             }
    65.             ENDCG
    66.         }
    67.     }
    68. }
    but seems doesn't work at all.

    The most complete explanation start here http://www.reedbeta.com/blog/quadrilateral-interpolation-part-2/ and it's very well explained but before i start to reinvent the wheel, someone know if there is a ready shader in the asset store ?
    If not it's better to try to convert the last solution to a Cg shader that works in unity? Or there is a better way?

    Thank you a lot
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,652
    I wrote a simplified break down of how to fix it.
    https://forum.unity.com/threads/help-giving-a-productionaly-generated-symmetric-mesh-stretched-the-mesh-in-a-weird-way.545413/#post-3605017


    And a few posts later there’s an example shader implementing it. However it requires special setup of the mesh UVs. But really all solutions do. That’s why the shader you posted doesn’t work, it assumes you’ve done some special work to modify the mesh UVs. The technique I detailed above is similar.