-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
37 lines (33 loc) · 954 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Original implementation:
// http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
var dot = require('gl-vec3/dot')
var set = require('gl-vec3/set')
var normalize = require('gl-quat/normalize')
var cross = require('gl-vec3/cross')
var tmp = [0, 0, 0]
var EPS = 1e-6
module.exports = quatFromUnitVec3
function quatFromUnitVec3 (out, a, b) {
// assumes a and b are normalized
var r = dot(a, b) + 1
if (r < EPS) {
/* If u and v are exactly opposite, rotate 180 degrees
* around an arbitrary orthogonal axis. Axis normalisation
* can happen later, when we normalise the quaternion. */
r = 0
if (Math.abs(a[0]) > Math.abs(a[2])) {
set(tmp, -a[1], a[0], 0)
} else {
set(tmp, 0, -a[2], a[1])
}
} else {
/* Otherwise, build quaternion the standard way. */
cross(tmp, a, b)
}
out[0] = tmp[0]
out[1] = tmp[1]
out[2] = tmp[2]
out[3] = r
normalize(out, out)
return out
}