-
-
Save li6in9muyou/08105141de113513a3dfee31a6844405 to your computer and use it in GitHub Desktop.
use THREE.js to calculate matrix transformation
This file contains hidden or 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
"use strict" | |
function matprint(mat) { | |
const shape = [mat.length, mat[0].length]; | |
function col(mat, i) { | |
return mat.map(row => row[i]); | |
} | |
const colMaxes = []; | |
for (let i = 0; i < shape[1]; i++) { | |
colMaxes.push(Math.max.apply(null, col(mat, i).map(n => n.toString().length))); | |
} | |
mat.forEach(row => { | |
console.log.apply(null, row.map((val, j) => { | |
return new Array(colMaxes[j]-val.toString().length+1).join(" ") + val.toString() + " "; | |
})); | |
}); | |
} | |
function chunk(chunkSize) { | |
let R = []; | |
for (let i = 0; i < this.length; i += chunkSize) | |
R.push(this.slice(i, i + chunkSize)); | |
return R; | |
} | |
fetch('https://cdnjs.cloudflare.com/ajax/libs/three.js/0.144.0/three.js') | |
.then(response => response.text()) | |
.then(text => eval(text)) | |
const translate = (...args) => new THREE.Matrix4().makeTranslation(...args) | |
const rotate = (...args) => new THREE.Matrix4().makeRotationZ(...args) | |
const scale = (...args) => new THREE.Matrix4().makeScale(...args) | |
const m = (...matrices) => matprint( | |
matrices.reduce( | |
(ans, m) => ans.premultiply(m), new THREE.Matrix4() | |
).transpose().toArray().map(val=>Math.round(val*1e3)/1e3).chunk(4) | |
) | |
// Try it! | |
m( | |
rotate(Math.PI/6), | |
scale(1989,1,1), | |
rotate(-Math.PI/6), | |
rotate(Math.PI/2 + Math.PI/6), | |
scale(64,1,1), | |
rotate(-1*(Math.PI/2 + Math.PI/6)), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment