Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Billboard and scale

Discussion in 'Shaders' started by Phantom_X, Nov 10, 2016.

  1. Phantom_X

    Phantom_X

    Joined:
    Jul 11, 2013
    Posts:
    305
    Hey,
    Trying to get billboarding and scale work in the vertex shader

    this makes the scale and billboard work, but the position is wrong
    Code (CSharp):
    1.                    
    2. float4 wpos = mul (_Object2World, v.vertex);              
    3. o.pos       = mul(UNITY_MATRIX_P, mul(UNITY_MATRIX_MV, float4(0,0,0,1)) - float4(wpos.x, wpos.y, wpos.z, 0));
    this makes the position and billboard work, but not the scale
    Code (CSharp):
    1.                    
    2. float4 wpos = v.vertex;              
    3. o.pos       = mul(UNITY_MATRIX_P, mul(UNITY_MATRIX_MV, float4(0,0,0,1)) - float4(wpos.x, wpos.y, wpos.z, 0));
    4.  
     
  2. cnlohr

    cnlohr

    Joined:
    Nov 25, 2020
    Posts:
    10
    For anyone else who is searching for this answer, it was answered on this stackoverflow question:

    https://stackoverflow.com/a/54187589/2926815

    Code (csharp):
    1.  
    2.                 float4 BillboardVertex = mul(UNITY_MATRIX_P,
    3.                     mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0))
    4.                     + float4(v.vertex.x, v.vertex.y, 0.0, 0.0)
    5.                     * float4(
    6.                         length(unity_ObjectToWorld._m00_m10_m20),
    7.                         length(unity_ObjectToWorld._m01_m11_m21), 1.0, 1.0));
    8.  
    At last this worked for me.