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. Dismiss Notice

codding UV for get best resault in pinching

Discussion in 'General Graphics' started by ehsan_wwe1, Apr 3, 2016.

  1. ehsan_wwe1

    ehsan_wwe1

    Joined:
    Apr 16, 2011
    Posts:
    22
    hello
    for first , i'm sorry for My bad thread title ( i don't know what i should say if you can change it )

    i have a 2D project but im my project we need change the vertex position

    i have problem because my Plane made from 4 vertex and if i want change the position of both vertex in right
    i get bad resault

    like up-right plane in the uploaded image

    anybody have any way to get resault like down-right plane in the image?

    my used code to generate up-right plane is a normaly way

    Code (csharp):
    1.  
    2. Mesh1.vertices = new Vector3[] { sphere1.position, sphere2.position,  sphere3.position, sphere4.position};
    3.             Mesh1.triangles = new int[] { 0, 1, 3, 0, 3, 2 };
    4.             Mesh1.uv = new Vector2[] { new Vector2(0,0), new Vector2(0,1)
    5.                                     , new Vector2(1, 1), new Vector2(1, 0)
    6.             };
    7.  
     

    Attached Files:

  2. ehsan_wwe1

    ehsan_wwe1

    Joined:
    Apr 16, 2011
    Posts:
    22
  3. ehsan_wwe1

    ehsan_wwe1

    Joined:
    Apr 16, 2011
    Posts:
    22
    after many try and search i found a good way to get my resault
    i use this C# code


    thanx to Jessy for good thread


    Code (csharp):
    1.  
    2.  
    3.             Mesh1.vertices = new Vector3[] { sphere1.position, sphere2.position, sphere3.position, sphere4.position };
    4.             Mesh1.triangles = new int[] { 0, 1, 2, 0, 2, 3 };  
    5.  
    6.             Vector3[] vertices = Mesh1.vertices;
    7.  
    8.             var shiftedPositions = new Vector2[] {
    9.                new Vector2(0 ,0),
    10.                new Vector2(0, vertices[1].y - vertices[0].y),
    11.                new Vector2(Mathf.Abs(vertices[0].x - vertices[3].x) , vertices[2].y - vertices[3].y),
    12.                new Vector2(Mathf.Abs(vertices[0].x - vertices[3].x) , 0)
    13.            };
    14.  
    15.             var keshesh = Mathf.Abs(vertices[3].x - vertices[0].x)  ;
    16.  
    17.             Debug.Log(keshesh);
    18.  
    19.             Mesh1.uv = shiftedPositions;
    20.  
    21.             var widths_heights = new Vector2[4];
    22.             widths_heights[0].x = widths_heights[3].x = shiftedPositions[3].x / keshesh;
    23.             widths_heights[1].x = widths_heights[2].x = shiftedPositions[3].x / keshesh;
    24.             widths_heights[0].y = widths_heights[1].y = shiftedPositions[1].y;
    25.             widths_heights[2].y = widths_heights[3].y = shiftedPositions[2].y;
    26.            
    27.             Mesh1.uv2 = widths_heights;
    28.  
    and this shader

    Code (csharp):
    1.  
    2. Shader "Custom/UVfixing" {
    3.    
    4.     Properties {
    5.    
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.     }
    8.    
    9.     SubShader {
    10.        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    11.         LOD 400
    12.         ZWrite Off
    13.         ColorMask RGB
    14.         pass{
    15.             CGPROGRAM
    16.  
    17.             uniform sampler2D _MainTex;
    18.            
    19.             #pragma vertex vert            
    20.             #pragma fragment frag
    21.            
    22.  
    23.         struct vertexInput {
    24.             float4 vertex : POSITION;          
    25.             float4 texcoord  : TEXCOORD0;
    26.             float4 texcoord1  : TEXCOORD1;        
    27.         };
    28.  
    29.         struct vertexOutput {
    30.             float4 pos : SV_POSITION;
    31.             float2 uv  : TEXCOORD0;
    32.             float2 uv2  : TEXCOORD1;          
    33.         };
    34.  
    35.         vertexOutput vert(vertexInput input)
    36.         {
    37.             vertexOutput output;
    38.             output.pos = mul(UNITY_MATRIX_MVP,  input.vertex);
    39.            
    40.             output.uv = input.texcoord;
    41.             output.uv2  = input.texcoord1 ;
    42.            
    43.             return output;
    44.         }
    45.  
    46.         float4 frag(vertexOutput input) : COLOR
    47.         {      
    48.             return  tex2D(_MainTex, float2(input.uv)/float2(input.uv2 ));      
    49.         }
    50.        
    51.          ENDCG
    52.     }
    53.     }
    54.    
    55. }
    56.  
    and get this resault


    thanx allot all of masters
    result2.jpg