Last active
December 21, 2015 01:29
-
-
Save shama/6227877 to your computer and use it in GitHub Desktop.
calc surface normal
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
function crossprod(a, b) { | |
return [ | |
(a[1] * b[2]) - (a[2] * b[1]), | |
(a[2] * b[0]) - (a[0] * b[2]), | |
(a[0] * b[1]) - (a[1] * b[0]) | |
] | |
} | |
function subvec(a, b) { | |
return [ | |
b[0] - a[0], | |
b[1] - a[1], | |
b[2] - a[2], | |
] | |
} | |
function surfaceNormal(verts) { | |
return crossprod(subvec(verts[2], verts[1]), subvec(verts[0], verts[1])) | |
} | |
function normalize(v) { | |
var len = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]) | |
if (len === 0) return [0, 0, 0] | |
var i = 1 / len | |
return [v[0] * i, v[1] * i, v[2] * i] | |
} | |
var a = [0, 1, 0] | |
var b = [0, 2.9, 0] | |
var c = [11, 10, 1] | |
var res = surfaceNormal([a, b, c]) | |
res = normalize(res) | |
console.log(res) |
max-mapper
commented
Aug 14, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment