Skip to content

Instantly share code, notes, and snippets.

@ArieLeo
Forked from mattatz/LookAtMatrix.cginc
Created December 4, 2019 06:38
Show Gist options
  • Save ArieLeo/fa3afb76a6253c1e91f2ce543784bf23 to your computer and use it in GitHub Desktop.
Save ArieLeo/fa3afb76a6253c1e91f2ce543784bf23 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