Last active
May 23, 2020 13:12
-
-
Save h3r/798ddc5ca567f3db51d6f6479cd532b0 to your computer and use it in GitHub Desktop.
GLSL Matrix operations polyfill
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
None of this code has been made by me and just collected those snippets from many sources | |
across the internet. I haven't saved any of the original author names and all the credits | |
should go to them. | |
HPlass ([email protected]) - 2020 | |
*/ | |
/* | |
* ████████╗██████╗ █████╗ ███╗ ██╗███████╗██████╗ ██████╗ ███████╗███████╗ | |
* ╚══██╔══╝██╔══██╗██╔══██╗████╗ ██║██╔════╝██╔══██╗██╔═══██╗██╔════╝██╔════╝ | |
* ██║ ██████╔╝███████║██╔██╗ ██║███████╗██████╔╝██║ ██║███████╗█████╗ | |
* ██║ ██╔══██╗██╔══██║██║╚██╗██║╚════██║██╔═══╝ ██║ ██║╚════██║██╔══╝ | |
* ██║ ██║ ██║██║ ██║██║ ╚████║███████║██║ ╚██████╔╝███████║███████╗ | |
* ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═════╝ ╚══════╝╚══════╝ | |
*/ | |
mat2 transpose(mat2 inMatrix) { | |
vec2 i0 = inMatrix[0]; | |
vec2 i1 = inMatrix[1]; | |
mat2 outMatrix; | |
outMatrix[0] = vec2(i0.x, i1.x); | |
outMatrix[1] = vec2(i0.y, i1.y); | |
return outMatrix; | |
} | |
mat3 transpose(mat3 inMatrix) { | |
vec3 i0 = inMatrix[0]; | |
vec3 i1 = inMatrix[1]; | |
vec3 i2 = inMatrix[2]; | |
mat3 outMatrix; | |
outMatrix[0] = vec3(i0.x, i1.x, i2.x); | |
outMatrix[1] = vec3(i0.y, i1.y, i2.y); | |
outMatrix[2] = vec3(i0.z, i1.z, i2.z); | |
return outMatrix; | |
} | |
mat4 transpose(mat4 inMatrix) { | |
highp vec4 i0 = inMatrix[0]; | |
highp vec4 i1 = inMatrix[1]; | |
highp vec4 i2 = inMatrix[2]; | |
highp vec4 i3 = inMatrix[3]; | |
mat4 outMatrix; | |
outMatrix[0] = vec4(i0.x, i1.x, i2.x, i3.x); | |
outMatrix[1] = vec4(i0.y, i1.y, i2.y, i3.y); | |
outMatrix[2] = vec4(i0.z, i1.z, i2.z, i3.z); | |
outMatrix[3] = vec4(i0.w, i1.w, i2.w, i3.w); | |
return outMatrix; | |
} | |
/* | |
* ██╗███╗ ██╗██╗ ██╗███████╗██████╗ ███████╗███████╗ | |
* ██║████╗ ██║██║ ██║██╔════╝██╔══██╗██╔════╝██╔════╝ | |
* ██║██╔██╗ ██║██║ ██║█████╗ ██████╔╝███████╗█████╗ | |
* ██║██║╚██╗██║╚██╗ ██╔╝██╔══╝ ██╔══██╗╚════██║██╔══╝ | |
* ██║██║ ╚████║ ╚████╔╝ ███████╗██║ ██║███████║███████╗ | |
* ╚═╝╚═╝ ╚═══╝ ╚═══╝ ╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝ | |
*/ | |
float inverse(float m) { | |
return 1.0 / m; | |
} | |
mat2 inverse(mat2 m) { | |
float det = (m[0][0]*m[1][1] - m[0][1]*m[1][0]); | |
return mat2(m[1][1],-m[0][1], | |
-m[1][0], m[0][0]) / det; | |
} | |
mat3 inverse(mat3 m) { | |
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2]; | |
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2]; | |
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2]; | |
float b01 = a22 * a11 - a12 * a21; | |
float b11 = -a22 * a10 + a12 * a20; | |
float b21 = a21 * a10 - a11 * a20; | |
float det = a00 * b01 + a01 * b11 + a02 * b21; | |
return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11), | |
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10), | |
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det; | |
} | |
mat4 inverse(mat4 m) { | |
float | |
a00 = m[0][0], a01 = m[0][1], a02 = m[0][2], a03 = m[0][3], | |
a10 = m[1][0], a11 = m[1][1], a12 = m[1][2], a13 = m[1][3], | |
a20 = m[2][0], a21 = m[2][1], a22 = m[2][2], a23 = m[2][3], | |
a30 = m[3][0], a31 = m[3][1], a32 = m[3][2], a33 = m[3][3], | |
b00 = a00 * a11 - a01 * a10, | |
b01 = a00 * a12 - a02 * a10, | |
b02 = a00 * a13 - a03 * a10, | |
b03 = a01 * a12 - a02 * a11, | |
b04 = a01 * a13 - a03 * a11, | |
b05 = a02 * a13 - a03 * a12, | |
b06 = a20 * a31 - a21 * a30, | |
b07 = a20 * a32 - a22 * a30, | |
b08 = a20 * a33 - a23 * a30, | |
b09 = a21 * a32 - a22 * a31, | |
b10 = a21 * a33 - a23 * a31, | |
b11 = a22 * a33 - a23 * a32, | |
det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; | |
return mat4( | |
a11 * b11 - a12 * b10 + a13 * b09, | |
a02 * b10 - a01 * b11 - a03 * b09, | |
a31 * b05 - a32 * b04 + a33 * b03, | |
a22 * b04 - a21 * b05 - a23 * b03, | |
a12 * b08 - a10 * b11 - a13 * b07, | |
a00 * b11 - a02 * b08 + a03 * b07, | |
a32 * b02 - a30 * b05 - a33 * b01, | |
a20 * b05 - a22 * b02 + a23 * b01, | |
a10 * b10 - a11 * b08 + a13 * b06, | |
a01 * b08 - a00 * b10 - a03 * b06, | |
a30 * b04 - a31 * b02 + a33 * b00, | |
a21 * b02 - a20 * b04 - a23 * b00, | |
a11 * b07 - a10 * b09 - a12 * b06, | |
a00 * b09 - a01 * b07 + a02 * b06, | |
a31 * b01 - a30 * b03 - a32 * b00, | |
a20 * b03 - a21 * b01 + a22 * b00) / det; | |
} | |
/* | |
* ██████╗ ██████╗ ████████╗ █████╗ ████████╗███████╗ | |
* ██╔══██╗██╔═══██╗╚══██╔══╝██╔══██╗╚══██╔══╝██╔════╝ | |
* ██████╔╝██║ ██║ ██║ ███████║ ██║ █████╗ | |
* ██╔══██╗██║ ██║ ██║ ██╔══██║ ██║ ██╔══╝ | |
* ██║ ██║╚██████╔╝ ██║ ██║ ██║ ██║ ███████╗ | |
* ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚══════╝ | |
*/ | |
vec4 rotateX (vec4 vertex, float degrees){ | |
float alpha = degrees * PI / 180.0; | |
float sina = sin(alpha); | |
float cosa = sin(alpha); | |
mat2 m = mat2(cosa, -sina, sina, cosa); | |
return vec4( m * vertex.yz, vertex.xw).zyxw; | |
} | |
vec4 rotateY (vec4 vertex, float degrees){ | |
float alpha = degrees * PI / 180.0; | |
float sina = sin(alpha); | |
float cosa = sin(alpha); | |
mat2 m = mat2(cosa, -sina, sina, cosa); | |
return vec4( m * vertex.xz, vertex.yw).xzyw; | |
} | |
vec4 rotateZ (vec4 vertex, float degrees){ | |
float alpha = degrees * PI / 180.0; | |
float sina = sin(alpha); | |
float cosa = sin(alpha); | |
mat2 m = mat2(cosa, -sina, sina, cosa); | |
return vec4( m * vertex.xy, vertex.zw).xyzw; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment