Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Need help correcting affine texture mapping for trapezoids

Discussion in 'Shaders' started by WhiteNattou, Mar 13, 2020.

  1. WhiteNattou

    WhiteNattou

    Joined:
    Jul 19, 2017
    Posts:
    3
    I've looked at multiple threads related to this topic, but somehow I get a weird result when I try to use the codes for correcting affine texture mapping in Unity 2019.2.7f2.
    (https://forum.unity.com/threads/hel...-the-mesh-in-a-weird-way.545413/#post-3605017)

    upload_2020-3-13_18-40-53.png
    The above image is what I want, but the result I get is as follows:

    upload_2020-3-13_18-44-14.png

    The original image appears to be stretched in a wrong way.
    Here's the code I'm using at the moment.
    Code (CSharp):
    1.  
    2. Shader "Custom/Trapezoid" {
    3.     Properties{
    4.         //uniform
    5.         _MainTex("Base (RGB)", 2D) = "white" {}
    6.     }
    7.         SubShader{
    8.         Pass{
    9.         CGPROGRAM
    10.  
    11. #pragma vertex vert
    12. #pragma fragment frag
    13. #include "UnityCG.cginc"
    14.  
    15.         struct vertexInput {
    16.         float4 vertex : POSITION;
    17.         float2 uv     : TEXCOORD0;
    18.         float2 uv2  : TEXCOORD1;
    19.     };
    20.  
    21.     struct fragmentInput {
    22.         float4 position : SV_POSITION;
    23.         float2 uv       : TEXCOORD0;
    24.         float2 uv2  : TEXCOORD1;
    25.     };
    26.  
    27.     //uniform
    28.     uniform sampler2D _MainTex;
    29.     float4 _MainTex_ST;
    30.  
    31.     fragmentInput vert(vertexInput input) {
    32.         fragmentInput output;
    33.         output.position = UnityObjectToClipPos(input.vertex);
    34.  
    35.         output.uv = input.uv;
    36.         output.uv2 = input.uv2;
    37.         return output;
    38.     }
    39.  
    40.     float4 frag(fragmentInput input) : COLOR{
    41.         return tex2D(_MainTex, (float2(input.uv.x / input.uv2.x, input.uv.y)));
    42.     }
    43.  
    44.         ENDCG
    45.     }
    46.     }
    47. }
    48.  
    I've tried other codes too, but they all have similar results.
    Can someone help me, or give me some kind of advice?
    Thanks
     

    Attached Files:

  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256
    WhiteNattou likes this.
  3. WhiteNattou

    WhiteNattou

    Joined:
    Jul 19, 2017
    Posts:
    3
    I think I figured it out, thank you bgolus :D
    Inputting the uv and uv2 values in the C# script made it work properly
    upload_2020-3-16_13-46-57.png
     
  4. WhiteNattou

    WhiteNattou

    Joined:
    Jul 19, 2017
    Posts:
    3
    Hallo
    I created one at the right, but what I actually needed seems to be the one on the left (perspective corrected affine texture mapping)
    does anyone have a good idea on how to do this?(I'm a little stuck)
    thanks
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,256