Drawing vertices at given position

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.

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:

Shader "Squish" 
{ 
   SubShader 
   { 
      Pass 
      {          
          
CGPROGRAM 
  
#pragma vertex vert 
#include "UnityCG.cginc"

struct a2v 
{ 
   float4 vertex : POSITION; 

}; 

struct v2f { 
    float4 pos : POSITION;
}; 


v2f vert(a2v IN) 
{ 
    v2f OUT; 

	float4 wpos = mul( _Object2World, IN.vertex);
	
	if(wpos.y < 0)
		wpos.y = 0;
	
	wpos = mul(_World2Object, wpos); 
    
	OUT.pos = mul(UNITY_MATRIX_MVP, wpos); 
    return OUT;

} 

ENDCG 
      } // Pass 
   } // SubShader 
} // Shader