Search Unity

Question Trapezoid Texture Warping (Homography Method)

Discussion in 'General Graphics' started by kweiming, Jun 11, 2020.

  1. kweiming

    kweiming

    Joined:
    Jun 18, 2017
    Posts:
    12
    Hi
    I have some questions about the texture mapping on trapezoid mesh
    to avoid texture warping in trapezoidal mesh, I used homography matrix to generate UV of the meshes one by one.
    But after homography transformation, the texture is not seamless between each meshes.
    is there any way to fix this problem or other way to make the correct mapping??
    if there are any advises I'll be very happy! Thanks beforehand!

    source: https://github.com/kweimingxx/NetMeshQuadWarp
     

    Attached Files:

    Last edited: Jun 12, 2020
  2. kweiming

    kweiming

    Joined:
    Jun 18, 2017
    Posts:
    12
    Screenshot (602).png The uv spacing of polygons will be nonuniform along the edge when using homography method.
    After some research, I know i should interpolate it manually in fragment shader
    But still can't get the right outcome
    My shader:

    Code (CSharp):
    1. Shader "Custom/BilinearInterpolate"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Texture", 2D) = "white" {}
    6.     }
    7.  
    8.     CGINCLUDE
    9.     #include "UnityCG.cginc"
    10.  
    11.     sampler2D _MainTex;
    12.     float4 _MainTex_TexelSize;
    13.     sampler2D _CheckBoardTex;
    14.     sampler2D _SpoutTex;
    15.  
    16.     float _Homography[9];
    17.     float _InvHomography[9];
    18.     float _InvHomographyUV[9];
    19.  
    20.     float4 _VertexValues[4];
    21.     float4 _UVValues[4];
    22.     float4x4 _HomographyMat;
    23.  
    24.     struct v2f
    25.     {
    26.         float4 pos : SV_POSITION;
    27.         float2 uv : TEXCOORD0;
    28.         float2 q  : TEXCOORD1;
    29.         float2 b1  : TEXCOORD2;
    30.         float2 b2  : TEXCOORD3;
    31.         float2 b3  : TEXCOORD4;
    32.     };
    33.  
    34.     v2f vert(appdata_img i)
    35.     {
    36.         v2f o;
    37.         o.pos = UnityObjectToClipPos(i.vertex);
    38.         o.uv = i.texcoord;
    39.  
    40.         o.q = _VertexValues[0] - i.vertex;
    41.         o.b1 = _VertexValues[1] - _VertexValues[0];
    42.         o.b2 = _VertexValues[2] - _VertexValues[0];
    43.         o.b3 = _VertexValues[0] - _VertexValues[1] - _VertexValues[2] + _VertexValues[3];
    44.  
    45.         return o;
    46.     }
    47.  
    48.     float plot(float2 st, float2 pct) {
    49.         return smoothstep(pct - 0.02, pct, st.y) -
    50.             smoothstep(pct, pct + 0.02, st.y);
    51.     }
    52.  
    53.     float Wedge2D(float2 v, float2 w)
    54.     {
    55.         return v.x * w.y - v.y * w.x;
    56.     }
    57.  
    58.     float4 frag0(v2f i) : SV_Target
    59.     {
    60.         // Set up quadratic formula
    61.         float A = Wedge2D(i.b2, i.b3);
    62.         float B = Wedge2D(i.b2, i.b1) + Wedge2D(i.q, i.b3);
    63.         float C = Wedge2D(i.q, i.b1);
    64.  
    65.         float A1 = Wedge2D(i.b1, i.b3);
    66.         float B1 = Wedge2D(i.b1, i.b2) + Wedge2D(i.q, i.b3);
    67.         float C1 = Wedge2D(i.q, i.b2);
    68.  
    69.         // Solve for v
    70.         float2 uv;
    71.         float u1, u2;
    72.         float v1, v2;
    73.         if (abs(A) < 1.e-14) // == 0
    74.         {
    75.             // Linear form
    76.             u1 = -C / B;
    77.             u2 = u1;
    78.         }
    79.         else
    80.         {
    81.             float discrim = B * B - 4 * A * C;
    82.  
    83.             if (discrim >= 0 ){
    84.                 u1 = (-B + sqrt(discrim)) / (2 * A);
    85.                 u2 = (-B - sqrt(discrim)) / (2 * A);
    86.             }
    87.             else {
    88.                 u1 = -1000;
    89.                 u2 = u1;
    90.             }
    91.         }
    92.         float mu = -10000;
    93.         if (u1 >= 0 && u1 <= 1) {
    94.             mu = u1;
    95.         }
    96.  
    97.         if (u2 >= 0 && u2 <= 1) {
    98.             mu = u2;
    99.         }
    100.     //------------------------------------------------------
    101.         if (abs(A1) < 1.e-14) // == 0
    102.         {
    103.             // Linear form
    104.             v1 = -C1 / B1;
    105.             v2 = v1;
    106.         }
    107.         else
    108.         {
    109.             float discrim1 = B1 * B1 - 4 * A1 * C1;
    110.  
    111.             if (discrim1 >= 0 ){          
    112.                 v1 = (-B1 + sqrt(discrim1)) / (2 * A1);
    113.                 v2 = (-B1 - sqrt(discrim1)) / (2 * A1);
    114.             }
    115.             else {
    116.                 v1 = -1000;
    117.                 v2 = v1;
    118.             }
    119.         }
    120.         float lambda = -10000;
    121.         if (v1 >= 0 && v1 <= 1) {
    122.             lambda = v1;
    123.         }
    124.  
    125.         if (v2 >= 0 && v2 <= 1) {
    126.             lambda = v2;
    127.         }
    128.  
    129.         uv = float2(mu, lambda);
    130.  
    131.         float3 p0 = lerp(_UVValues[0], _UVValues[2], uv.x);
    132.         float3 p1 = lerp(_UVValues[1], _UVValues[3], uv.x);
    133.         float3 ps = lerp(p0, p1, uv.y);
    134.    
    135.         //return float4(i.uv, 0, 1);
    136.         //return float4(uv, 0, 1);
    137.        //return tex2D(_SpoutTex, ps) * 1;
    138.         return tex2D(_SpoutTex, uv) * 1;
    139.     }
    140.  
    141.     ENDCG
    142.  
    143.     SubShader
    144.     {
    145.         // No culling or depth
    146.         //Cull Off ZWrite Off ZTest Always
    147.  
    148.         // Pass 0: calculate
    149.         Pass
    150.         {
    151.             //Blend One One
    152.             CGPROGRAM
    153.             #pragma vertex   vert
    154.             #pragma fragment frag0
    155.             ENDCG
    156.         }
    157.     }
    158. }
    159.  
    reference:
    http://reedbeta.com/blog/quadrilateral-interpolation-part-2/#implementation
    https://numfactory.upc.edu/web/FiniteElements/Pract/P4-QuadInterpolation/html/QuadInterpolation.html
     
    Last edited: Jun 15, 2020
  3. kweiming

    kweiming

    Joined:
    Jun 18, 2017
    Posts:
    12
    Screenshot (602).png

    three polygons with weird uv coordinate
    but the rest five polygons seems good
     
    Last edited: Jun 15, 2020