Search Unity

Billboard Shader not working for imported meshes

Discussion in 'Shaders' started by Daoyee, Mar 1, 2019.

  1. Daoyee

    Daoyee

    Joined:
    May 1, 2018
    Posts:
    2
    Hi all!
    Basically, I have a working billboard shader works great on default Quad mesh but not on other meshes imported from Blender. While I think the different coordinate system may be the cause I have no idea about how to fix it. IS that my imported Object space is not as same as UNITY_MATRIX_V?

    Code (CSharp):
    1.  void vert(inout appdata_full v, out Input o)
    2.         {
    3.             UNITY_INITIALIZE_OUTPUT(Input, o);
    4.  
    5.             // get the camera basis vectors
    6.             float3 forward = -normalize(UNITY_MATRIX_V._m20_m21_m22);
    7.             float3 up = float3(0, 1, 0);
    8.             float3 right = normalize(UNITY_MATRIX_V._m00_m01_m02);
    9.            
    10.             // rotate to face camera
    11.             float4x4 rotationMatrix = float4x4(right,   0,
    12.                                                 up,      0,
    13.                                                 forward, 0,
    14.                                                 0, 0, 0, 1);
    15.            
    16.             float offset = 0;
    17.             v.vertex = mul(v.vertex, rotationMatrix);
    18.             // undo o_to_j
    19.             v.vertex.xyz = mul((float3x3)unity_WorldToObject, v.vertex.xyz);
    20.             v.normal = mul(v.normal, rotationMatrix);
    21.         }
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    7,677
    That billboard shader assumes your mesh's pivot is at the center of the mesh, and it's forward facing is along the -Z axis. If your mesh doesn't match those requirements, it won't work.