Search Unity

Max Screen Size

Discussion in 'Shaders' started by Simeon, Jan 27, 2015.

  1. Simeon

    Simeon

    Joined:
    Sep 26, 2012
    Posts:
    50
    I want so see if you have any thoughts on how to limit the size of a particle in a Geometry Shader, just like in the Shuriken particle system. How to express the particle in viewport size and limit it.

    Code (CSharp):
    1.  
    2. // Vertex Shader ------------------------------------------------
    3.     GS_INPUT VS_Main(appdata v)
    4.     {
    5.         GS_INPUT output = (GS_INPUT)0;
    6.         output.color = v.color;
    7.         output.pos =  mul(_Object2World, v.pos);
    8.         output.info =  float4(v.info,0,0);
    9.         return output;
    10.     }
    11.    
    12.    
    13.     // Geometry Shader -----------------------------------------------------
    14.     [maxvertexcount(4)]
    15.     void GS_Main(point GS_INPUT p[1], inout TriangleStream<FS_INPUT> triStream)
    16.     {
    17.         //inverse view matrix
    18.         float4x4 i_v = inverse(UNITY_MATRIX_V);
    19.         float4 pos = p[0].pos;
    20.         float distance = length(_WorldSpaceCameraPos - pos);
    21.         float3 look = normalize(_WorldSpaceCameraPos - pos);
    22.         float3 up = float3(0,1,0);
    23.                        
    24.         //the rotation matrix
    25.         float4x4 rotateM = rotate(float3(0,0,sin(p[0].pos.x) * _Time.y),float4(0,0,0,0));
    26.         up = mul(rotateM,up);
    27.         //rotate the up vector to match the camera up
    28.         up =  mul(i_v,up);
    29.         float3 right = cross(up, look);
    30.         //individual size of particle
    31.         float size = p[0].info.x;
    32.                        
    33.         //quad vertices centered on 0,0
    34.         float4 v[4];
    35.         v[0] = float4(right * 0.5f  - up * 0.5f,0);
    36.         v[1] = float4(right * 0.5f + up * 0.5f, 0);
    37.         v[2] = float4(- right * 0.5f - up * 0.5f, 0);
    38.         v[3] = float4(- right * 0.5f + up * 0.5f, 0);
    39.                        
    40.         float2 u[4];
    41.         u[0] = float2(1,0);
    42.         u[1] = float2(1,1);
    43.         u[2] = float2(0,0);
    44.         u[3] = float2(0,1);
    45.    
    46.         FS_INPUT pIn;
    47.         //use the look direction as the normal
    48.         pIn.normal = look;
    49.         pIn.color = p[0].color;
    50.         //store the distance of each particle
    51.         pIn.info = float4(distance,0,0,0);
    52.                        
    53.         for(int i = 0;i < 4;i++)
    54.         {
    55.             pIn.pos = mul(UNITY_MATRIX_VP,pos + v[i] * size);
    56.             pIn.tex0 = u[i];
    57.             triStream.Append(pIn);
    58.         }
    59.     }
    60.