Skip to content

Instantly share code, notes, and snippets.

@mattatz
Last active July 28, 2022 05:31
Show Gist options
  • Save mattatz/76d59f96c9eebeb319819d0a504ecf25 to your computer and use it in GitHub Desktop.
Save mattatz/76d59f96c9eebeb319819d0a504ecf25 to your computer and use it in GitHub Desktop.
Build a 4x4 look at matrix in cginc
#ifndef _LOOK_AT_MATRIX_
#define _LOOK_AT_MATRIX_
float4x4 look_at_matrix(float3 at, float3 eye, float3 up) {
float3 zaxis = normalize(at - eye);
float3 xaxis = normalize(cross(up, zaxis));
float3 yaxis = cross(zaxis, xaxis);
return float4x4(
xaxis.x, yaxis.x, zaxis.x, 0,
xaxis.y, yaxis.y, zaxis.y, 0,
xaxis.z, yaxis.z, zaxis.z, 0,
0, 0, 0, 1
);
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment