Skip to content

Instantly share code, notes, and snippets.

@wiseConst
Forked from shakesoda/cofactor.glsl
Created April 13, 2025 19:26
Show Gist options
  • Save wiseConst/ac68457266768844af707ec5d334aec2 to your computer and use it in GitHub Desktop.
Save wiseConst/ac68457266768844af707ec5d334aec2 to your computer and use it in GitHub Desktop.
cofactor matrix, as described by https://github.com/graphitemaster/normals_revisited
// cofactor matrix. drop-in replacement for the traditional transpose(inverse(m)) normal matrix calculation.
// this is a substantial performance improvement.
// short form found via shadertoy: https://www.shadertoy.com/view/3s33z
mat3 cofactor(mat4 m) {
return mat3(
cross(m[1].xyz, m[2].xyz),
cross(m[2].xyz, m[0].xyz),
cross(m[0].xyz, m[1].xyz)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment