Search Unity

Correcting affine texture mapping for trapezoids

Discussion in 'Shaders' started by Jessy, Sep 15, 2012.

  1. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325

    Attached Files:

  2. b45h05h4

    b45h05h4

    Joined:
    Dec 22, 2011
    Posts:
    6
    Hello I got that error when importing your package
    would u please share again? I am using Unity 3.5 yet
    maybe this is the problem?

    $fatalerrata.jpg
     
  3. karam

    karam

    Joined:
    Feb 20, 2013
    Posts:
    1
    I have another problem that I can't slove

    $Untitled.png
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    No, that's not the problem, but I don't know what is. Works fine for me.


    Your problem is that you're on Windows. You'll have to force Unity into OpenGL mode.
     
  5. b45h05h4

    b45h05h4

    Joined:
    Dec 22, 2011
    Posts:
    6
    Would you please explain more about what you have done in the shader or give me a reference to understand it

    specifically the calculations you did

    for exmple this statement

    Code (csharp):
    1. gl_FragColor = texture2D(_MainTex, shiftedPosition / width_height);
     
  6. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    You'd need to look at AffineUVFix.cs, which feeds the necessary data to the shader. The key is that at every pixel, the texture coordinate is the a fraction of the way to both dimensions of the quad.
     
  7. b45h05h4

    b45h05h4

    Joined:
    Dec 22, 2011
    Posts:
    6
    hmm, ya

    I have written the Shader in Cg for windows users it could be helpful for some people

    here it is

    Code (csharp):
    1. Shader "Custom/Affine UV fix Cg" {
    2.    
    3.     Properties {
    4.    
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.     }
    7.    
    8.     SubShader {
    9.        
    10.         pass{
    11.             CGPROGRAM
    12.  
    13.             uniform sampler2D _MainTex;
    14.            
    15.             #pragma vertex vert            
    16.             #pragma fragment frag
    17.            
    18.  
    19.         struct vertexInput {
    20.             float4 vertex : POSITION;          
    21.             float4 texcoord  : TEXCOORD0;
    22.             float4 texcoord1  : TEXCOORD1;         
    23.         };
    24.  
    25.         struct vertexOutput {
    26.             float4 pos : SV_POSITION;
    27.             float4 uv  : TEXCOORD0;
    28.             float4 uv2  : TEXCOORD1;           
    29.         };
    30.  
    31.         vertexOutput vert(vertexInput input)
    32.         {
    33.             vertexOutput output;
    34.             output.pos = mul(UNITY_MATRIX_MVP,  input.vertex);
    35.            
    36.             output.uv = input.texcoord;
    37.             output.uv2  = input.texcoord1 ;
    38.            
    39.             return output;
    40.         }
    41.  
    42.         float4 frag(vertexOutput input) : COLOR
    43.         {      
    44.             return  tex2D(_MainTex, float2(input.uv)/float2(input.uv2 ));      
    45.         }
    46.        
    47.          ENDCG // here ends the part in Cg
    48.     }
    49.     }
    50.    
    51. }
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Why bother to use float4s for the UVs?
     
  9. b45h05h4

    b45h05h4

    Joined:
    Dec 22, 2011
    Posts:
    6
    because I am very new to shaders, I change it to float2 and it worked

    thank you :)
     
  10. b45h05h4

    b45h05h4

    Joined:
    Dec 22, 2011
    Posts:
    6
    Would you please replay my pm sir,

    thanks in advance
     
  11. leonida

    leonida

    Joined:
    Jul 3, 2012
    Posts:
    2
    someone try to use this package on unity 5?
    I tried to import it but does not work.
     
  12. ehsan_wwe1

    ehsan_wwe1

    Joined:
    Apr 16, 2011
    Posts:
    22
    use it
    Code (csharp):
    1.  
    2. Shader "Custom/Affine UV fix Cg" {
    3.  
    4.     Properties {
    5.  
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader {
    10.      
    11.         pass{
    12.             CGPROGRAM
    13.  
    14.             uniform sampler2D _MainTex;
    15.          
    16.             #pragma vertex vert          
    17.             #pragma fragment frag
    18.          
    19.  
    20.         struct vertexInput {
    21.             float4 vertex : POSITION;        
    22.             float4 texcoord  : TEXCOORD0;
    23.             float4 texcoord1  : TEXCOORD1;      
    24.         };
    25.  
    26.         struct vertexOutput {
    27.             float4 pos : SV_POSITION;
    28.             float2 uv  : TEXCOORD0;
    29.             float2 uv2  : TEXCOORD1;        
    30.         };
    31.  
    32.         vertexOutput vert(vertexInput input)
    33.         {
    34.             vertexOutput output;
    35.             output.pos = mul(UNITY_MATRIX_MVP,  input.vertex);
    36.          
    37.             output.uv = input.texcoord;
    38.             output.uv2  = input.texcoord1 ;
    39.          
    40.             return output;
    41.         }
    42.  
    43.         float4 frag(vertexOutput input) : COLOR
    44.         {    
    45.             return  tex2D(_MainTex, float2(input.uv)/float2(input.uv2 ));    
    46.         }
    47.      
    48.          ENDCG // here ends the part in Cg
    49.     }
    50.     }
    51.  
    52. }
    53.