Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. util.Quaternion.fromLookAt = function(direction, up) {
  2.     up = up || <0, 1, 0>;
  3.     direction = direction.normal();
  4.  
  5.     // Orient the -z axis to be along direction.
  6.     var firstQuat;
  7.     if ((direction - <0, 0, -1>).lengthSquared() < 1e-08) {
  8.         firstQuat = new util.Quaternion(0, 0, 0, 1);
  9.     } else if ((direction - <0, 0, 1>).lengthSquared() < 1e-08) {
  10.         firstQuat = new util.Quaternion(0, 1, 0, 0);
  11.     } else {
  12.         var quatAxis = <0, 0, -1>.cross(direction);
  13.         var angle = util.acos(<0, 0, -1>.dot(direction));
  14.         quatAxis = quatAxis.normal();
  15.         firstQuat = new util.Quaternion(quatAxis, angle);
  16.     }
  17.  
  18.     // Compute new up vector and orient the y axis to be along that direction.
  19.     var secondQuat;
  20.     if (direction != up) {
  21.         var left = direction.cross(up);
  22.         var newUp = left.cross(direction);
  23.         newUp = newUp.normal();
  24.         var quatAxis = <0, 1, 0>.cross(newUp);
  25.         var angle = util.acos(<0, 1, 0>.dot(newUp));
  26.         quatAxis = quatAxis.normal();
  27.         secondQuat = new util.Quaternion(quatAxis, angle);
  28.     } else {
  29.         secondQuat = new util.Quaternion(0, 0, 0, 1);
  30.     }
  31.  
  32.     return secondQuat.mul(firstQuat);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement