Skip to content

Instantly share code, notes, and snippets.

@renaudbedard
Created September 11, 2014 18:39
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save renaudbedard/7a90ec4a5a7359712202 to your computer and use it in GitHub Desktop.
Save renaudbedard/7a90ec4a5a7359712202 to your computer and use it in GitHub Desktop.
Billboarding for Unity surface shaders
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
// get the camera basis vectors
float3 forward = -normalize(UNITY_MATRIX_V._m20_m21_m22);
float3 up = float3(0, 1, 0); //normalize(UNITY_MATRIX_V._m10_m11_m12);
float3 right = normalize(UNITY_MATRIX_V._m00_m01_m02);
// rotate to face camera
float4x4 rotationMatrix = float4x4(right, 0,
up, 0,
forward, 0,
0, 0, 0, 1);
//float offset = _Object2World._m22 / 2;
float offset = 0;
v.vertex = mul(v.vertex + float4(0, offset, 0, 0), rotationMatrix) + float4(0, -offset, 0, 0);
v.normal = mul(v.normal, rotationMatrix);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment