Search Unity

Billboard Shader doesnt work when pass translate

Discussion in 'Shaders' started by psomgeorg, Aug 26, 2019.

  1. psomgeorg

    psomgeorg

    Joined:
    Mar 16, 2019
    Posts:
    99
    Hi i am trying to render smoke particles and do the animation in shaders(the movement). For billboarding i have used this formula :
    Code (CSharp):
    1.  
    2. o.vertex = mul(UNITY_MATRIX_P,
    3.                     mul(mul(UNITY_MATRIX_V,unity_ObjectToWorld), float4(0.0, 0.0, 0.0, 1.0))
    4.                     + float4(v.vertex.x, v.vertex.y,0.0, 0.0)*float4(2.0f,2.0f,1.0f,1.0f));
    5.  
    It works without any problems. I want to pass via property ( which is an instanced property) the new location for each particle so that i change it with linear interpolation over time. However when i pass any other matrix instead of unity_ObjectToWorld it messes up. When i move around some particles just disappear and some others appear. And before you say that the problem may be the data i pass, let me tell you that even when i have a translation for instance to this position (1.0,1.0,1.0,1.0) it has the same problem.

    I will show you the whole vertex shader:
    Code (CSharp):
    1.  
    2. v2f vert (appdata v)
    3.             {
    4.                 v2f o;
    5.                 //instancing initialization, not anything non-trivial
    6.                 UNITY_SETUP_INSTANCE_ID(v);
    7.                 UNITY_TRANSFER_INSTANCE_ID(v, o);
    8.                 int id = UNITY_ACCESS_INSTANCED_PROP(InstanceProperties, _ID);
    9.                 float4x4 worldMatrix = unity_ObjectToWorld;
    10.                //As an example lets set the translation of all particles to (1.0,1.0,1.0)
    11.                 worldMatrix[0][3] = 1.0;
    12.                 worldMatrix[1][3] = 1.0;
    13.                 worldMatrix[2][3] = 1.0;
    14.                 worldMatrix[3][3] = 1.0;
    15.  
    16.                     o.vertex = mul(UNITY_MATRIX_P,
    17.                     mul(mul(UNITY_MATRIX_V,worldMatrix), float4(0.0, 0.0, 0.0, 1.0))
    18.                     + float4(v.vertex.x, v.vertex.y,0.0, 0.0)*float4(2.0f,2.0f,1.0f,1.0f));
    19.  
    20.                //Flipbook texture animation, dont bother
    21.              
    22.                 float speed = 12;
    23.              
    24.                 float2 size = float2(1.0f / 8, 1.0f / 8);
    25.                 uint totalFrames = 64;
    26.                 uint index = _Time.y*speed + id;
    27.                 uint indexX = (index)%8;
    28.                 uint indexX2 = (index  + 1 ) % 8;
    29.                 uint indexY = floor(((index ) % 64) / 8);
    30.                 uint indexY2 = floor(((index  +1) % 64) / 8);
    31.                 float2 offset = float2(size.x * indexX, -size.y * indexY);
    32.                 float2 offset2 = float2(size.x * indexX2, -size.y * indexY2);
    33.                 float2 newUV = v.uv * size;
    34.                 newUV.y = newUV.y + size.y * (8 - 1);
    35.                 o.uv = newUV + offset;
    36.                 o.uv2 = newUV + offset2;
    37.                  
    38.               return o;
    39. }
    40.  
    This is if we place everything at 1.0,1.0,1.0 it seems fine, but if i change my camera's rotation a bit,it will disappear
    upload_2019-8-26_13-33-12.png
    upload_2019-8-26_13-34-29.png
    And lets see particles at their correct position(position is programmed via C# when we instantiate them)
    upload_2019-8-26_13-36-4.png
    If we place their position via shaders though, we have this result
    upload_2019-8-26_13-41-13.png

    In pictures it doesnt have a big difference but if i move the mouse a little bit some particles (for instance in the right) willl disappear. And when i move around i see particles disappearing
    upload_2019-8-26_13-42-26.png