Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Drawing vertices at given position

Discussion in 'Shaders' started by illogikaStudios, Mar 21, 2011.

  1. illogikaStudios

    illogikaStudios

    Joined:
    Nov 19, 2008
    Posts:
    66
    Hi,

    I have a skinned mesh character whose animation sometimes brings some clothing vertices under the floor. I would like to draw those vertices at y=0. I understand there is no way to get the vertices position after skinning is applied. So I was wondering if it would be possible to do this via CG code?

    This is an iOS/Android project.

    Thanks.
     
  2. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    It should be possible. In the vertex shader you would compte the world position by multiplying with _Object2World matrix. Then if y is < 0 you would set y to 0. Then you would multiply by the view matrix and by the projection matrix. Now, AFAIK, the view matrix is not available, so you would have to supply it through script. The projection matrix should be UNITY_MATRIX_P

    EDIT: I guess a slightly better alternative would be to go back to object space by using _World2Object and then multiply by UNITY_MATRIX_MVP. This way you don't have to supply a view matrix from script.

    for example:
    Code (csharp):
    1.  
    2. Shader "Squish"
    3. {
    4.    SubShader
    5.    {
    6.       Pass
    7.       {          
    8.          
    9. CGPROGRAM
    10.  
    11. #pragma vertex vert
    12. #include "UnityCG.cginc"
    13.  
    14. struct a2v
    15. {
    16.    float4 vertex : POSITION;
    17.  
    18. };
    19.  
    20. struct v2f {
    21.     float4 pos : POSITION;
    22. };
    23.  
    24.  
    25. v2f vert(a2v IN)
    26. {
    27.     v2f OUT;
    28.  
    29.     float4 wpos = mul( _Object2World, IN.vertex);
    30.    
    31.     if(wpos.y < 0)
    32.         wpos.y = 0;
    33.    
    34.     wpos = mul(_World2Object, wpos);
    35.    
    36.     OUT.pos = mul(UNITY_MATRIX_MVP, wpos);
    37.     return OUT;
    38.  
    39. }
    40.  
    41. ENDCG
    42.       } // Pass
    43.    } // SubShader
    44. } // Shader
    45.  
     
    Last edited: Mar 22, 2011