Search Unity

Help with corner pin vertex shader

Discussion in 'Shaders' started by Spree_User, Oct 24, 2017.

  1. Spree_User

    Spree_User

    Joined:
    Aug 24, 2017
    Posts:
    6
    I'm new to shader programming and am having some problems. What I want to do is to be able to reposition all corners of a mesh one by one.

    I have a shader that is working to some degree, but I'm having some problems. This might be more of a math question than a shader programming question, but perhaps someone knows a better way to achieve this.

    Here is the shader that I am using right now, based on another thread found on this forum:
    Code (CSharp):
    1. Shader "Distort/Experiment"
    2. {
    3.     Properties{
    4.         _MainTex("Base (RGB)", 2D) = "white" {}
    5.     _DLx("Down Left x", Range(-20.0,20.0)) = 0
    6.         _DLy("Down Left y", Range(-20.0,20.0)) = 0
    7.         _DRx("Down Right x", Range(-2.0,2.0)) = 0
    8.         _DRy("Down Right y", Range(-2.0,2.0)) = 0
    9.         _TLx("Top Left x", Range(-2.0,2.0)) = 0
    10.         _TLy("Top Left y", Range(-2.0,2.0)) = 0
    11.         _TRx("Top Right x", Range(-2.0,2.0)) = 0
    12.         _TRy("Top Right y", Range(-2.0,2.0)) = 0
    13.     }
    14.         SubShader{
    15.         Pass{
    16.         //ZTest Always Cull Off ZWrite Off
    17.         Fog{ Mode off }
    18.         CGPROGRAM
    19. #pragma vertex vert
    20. #pragma fragment frag
    21. #pragma fragmentoption ARB_precision_hint_fastest
    22. #include "UnityCG.cginc"
    23.         uniform sampler2D _MainTex;
    24.     struct v2f {
    25.         float4 pos : SV_POSITION;
    26.         float2 uv : TEXCOORD0;
    27.     };
    28.     float _DRx;
    29.     float _DRy;
    30.     float _DLx;
    31.     float _DLy;
    32.     float _TLx;
    33.     float _TLy;
    34.     float _TRx;
    35.     float _TRy;
    36.     v2f vert(appdata_img v)
    37.     {
    38.         v2f o;
    39.         o.pos = UnityObjectToClipPos(v.vertex);
    40.         o.uv = v.texcoord;
    41.  
    42.         o.pos.x += _DLx*o.uv.x*o.uv.y;
    43.         o.pos.y += _DLy*o.uv.x*o.uv.y;
    44.         o.pos.x += _DRx*(1 - o.uv.x)*o.uv.y;
    45.         o.pos.y += _DRy*(1 - o.uv.x)*o.uv.y;
    46.         o.pos.x += _TLx*o.uv.x*(1 - o.uv.y);
    47.         o.pos.y += _TLy*o.uv.x*(1 - o.uv.y);
    48.         o.pos.x += _TRx*(1 - o.uv.x)*(1 - o.uv.y);
    49.         o.pos.y += _TRy*(1 - o.uv.x)*(1 - o.uv.y);
    50.         return o;
    51.     }
    52.     float4 frag(v2f i) : SV_Target
    53.     {
    54.         return tex2D(_MainTex, i.uv);
    55.     }
    56.         ENDCG
    57.     }
    58.     }
    59.         Fallback off
    60. }

    Here's an image that highlights the main problem:


    I didn't notice this at first, but as you can see, this gives me a curved line in the texture, but I need the offset to be linear.

    When all corners are in play, this is not as obvious, but the problem is still there:


    I've been trying to figure out another approach to this, but I'm feeling a bit stuck. Any ideas?
    Thanks,
    Hannes
     
  2. wio2016

    wio2016

    Joined:
    Jan 11, 2019
    Posts:
    1
    Hi @Spree_User
    Did you ever find a solution to this?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348