Search Unity

Flickering mesh

Discussion in 'Shaders' started by ardo314, Sep 12, 2016.

  1. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    Shader noob here.

    I wrote myself a billboard shader:

    Code (CSharp):
    1. float4 vertex = i.vertex;
    2. float2 camera = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1)).xz;
    3.  
    4. float cosa = dot(float2(0, -1), camera) / length(camera);
    5.  
    6. float direction = dot(float2(-1, 0), camera);
    7. float angle = sign(direction) * acos(cosa);
    8.  
    9. vertex = rotate(vertex, angle);
    10.  
    11. o.vertex = mul(UNITY_MATRIX_MVP, vertex);
    This works very good except for the flickering.


    I also applied a script to the meshobject which uses
    Code (CSharp):
    1. transform.LookAt(camera)
    so i can reliably reproduce the error i saw occasionally which appeared when the objects normal was directly pointing at the camera. That's when the mesh starts flickering. It's probably because of how i calculate the delta angle but I'm kind of lost.

    The error:
    https://giphy.com/gifs/l3vRmutGsDjSFqeLC

    What bugs me is that the flickering also appears when just rotating the camera, although i never use the camera rotation in my calculations.
     
    Last edited: Sep 12, 2016