Sprite Billboard

https://www.reddit.com/r/Unity3D/comments/ahqbod/a_billboard_sprite_shader_in_only_one_axis/

To make the sprite look at the camera all the time (billboard)_

  • View space is a rotated version of world space with the xy plane parallel to the view plane. It’s intuitive to make the billboard in here. First we transform the origin in the view coordinates and then
float4 newVertex = mul(UNITY_MATRIX_P, mul(UNITY_MATRIX_MV, float4(0.0, 0.0, 0.0, 1.0)) + float4(input.vertex.x, input.vertex.y, 0.0, 1.0));
  • We can also cancel out the rotation before the model matrix and then apply the view matrix.
float4x4 v = UNITY_MATRIX_V;
float3 right = normalize(v._m00_m01_m02);
float3 up = normalize(v._m10_m11_m12);
float3 forward = normalize(v._m20_m21_m22);
//get the rotation parts of the matrix
float4x4 rotationMatrix = 
float4x4(right, 0,
    	up, 0,
    	forward, 0,
    	0, 0, 0, 1);

float4x4 rotationInv = transpose(rotationMatrix);

float4 pos = input.vertex;
float4 clipPos = mul(rotatiionInv, pos);
output.vertex = TransformObjectToHClip(clipPos.xyz);

To add billboard effect on certain axis is to not cancel the rotation on that axis. float3 right = normalize(v._m00_m01_m02); becomes float3 right = float3(0,1,0);