Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- util.Quaternion.fromLookAt = function(direction, up) {
- up = up || <0, 1, 0>;
- direction = direction.normal();
- // Orient the -z axis to be along direction.
- var firstQuat;
- if ((direction - <0, 0, -1>).lengthSquared() < 1e-08) {
- firstQuat = new util.Quaternion(0, 0, 0, 1);
- } else if ((direction - <0, 0, 1>).lengthSquared() < 1e-08) {
- firstQuat = new util.Quaternion(0, 1, 0, 0);
- } else {
- var quatAxis = <0, 0, -1>.cross(direction);
- var angle = util.acos(<0, 0, -1>.dot(direction));
- quatAxis = quatAxis.normal();
- firstQuat = new util.Quaternion(quatAxis, angle);
- }
- // Compute new up vector and orient the y axis to be along that direction.
- var secondQuat;
- if (direction != up) {
- var left = direction.cross(up);
- var newUp = left.cross(direction);
- newUp = newUp.normal();
- var quatAxis = <0, 1, 0>.cross(newUp);
- var angle = util.acos(<0, 1, 0>.dot(newUp));
- quatAxis = quatAxis.normal();
- secondQuat = new util.Quaternion(quatAxis, angle);
- } else {
- secondQuat = new util.Quaternion(0, 0, 0, 1);
- }
- return secondQuat.mul(firstQuat);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement