Advertisement
salahzar

inspect.js hf

Jun 26th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  inspect.js
  3. //  examples
  4. //
  5. //  Created by Clément Brisset on March 20, 2014
  6. //  Copyright 2014 High Fidelity, Inc.
  7. //
  8. //  Allows you to inspect non moving objects (Voxels or Avatars) using Atl, Control (Command on Mac) and Shift
  9. //
  10. //  radial mode = hold ALT
  11. //  orbit mode  = hold ALT + CONTROL
  12. //  pan mode    = hold ALT + CONTROL + SHIFT
  13. //  Once you are in a mode left click on the object to inspect and hold the click
  14. //  Dragging the mouse will move your camera according to the mode you are in.
  15. //
  16. //  CtrlAltStudio modifications:
  17. //  - Leave camera where it is when release Alt key.
  18. //  - Restore camera to default position when press Esc key or move avatar.
  19. //  - Make camera control start immediately rather than having a 0.5s delay at the start.
  20. //  - Add ability to orbit about a point in space if no object intersects mouse click.
  21. //
  22. //  CtrlAltStudio information: http://ctrlaltstudio.com/hifi/inspect
  23. //
  24. //  Distributed under the Apache License, Version 2.0.
  25. //  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
  26. //
  27.  
  28. var PI = Math.PI;
  29. var RAD_TO_DEG = 180.0 / PI;
  30.  
  31. var AZIMUTH_RATE = 90.0;
  32. var ALTITUDE_RATE = 200.0;
  33. var RADIUS_RATE = 1.0 / 100.0;
  34. var PAN_RATE = 250.0;
  35.  
  36. var Y_AXIS = {
  37.   x: 0,
  38.   y: 1,
  39.   z: 0
  40. };
  41. var X_AXIS = {
  42.   x: 1,
  43.   y: 0,
  44.   z: 0
  45. };
  46.  
  47. var LOOK_AT_TIME = 500;
  48.  
  49. // CAS...
  50. var AVATAR_POSITION_SLOP = 0.1;
  51. var AVATAR_ROTATION_SLOP = 0.09;  // 5 degrees
  52. // ... CAS
  53.  
  54. var alt = false;
  55. var shift = false;
  56. var control = false;
  57.  
  58. var isActive = false;
  59.  
  60. var oldMode = Camera.mode;
  61. var noMode = 0;
  62. var orbitMode = 1;
  63. var radialMode = 2;
  64. var panningMode = 3;
  65. var detachedMode = 4;
  66.  
  67. var mode = noMode;
  68.  
  69. var mouseLastX = 0;
  70. var mouseLastY = 0;
  71.  
  72.  
  73. var center = {
  74.   x: 0,
  75.   y: 0,
  76.   z: 0
  77. };
  78. var position = {
  79.   x: 0,
  80.   y: 0,
  81.   z: 0
  82. };
  83. var vector = {
  84.   x: 0,
  85.   y: 0,
  86.   z: 0
  87. };
  88. var radius = 0.0;
  89. var azimuth = 0.0;
  90. var altitude = 0.0;
  91.  
  92. var avatarPosition;
  93. var avatarOrientation;
  94.  
  95. var rotatingTowardsTarget = false;
  96. var targetCamOrientation;
  97. var oldPosition, oldOrientation;
  98.  
  99.  
  100. function orientationOf(vector) {
  101.   var direction,
  102.     yaw,
  103.     pitch;
  104.  
  105.   direction = Vec3.normalize(vector);
  106.   yaw = Quat.angleAxis(Math.atan2(direction.x, direction.z) * RAD_TO_DEG, Y_AXIS);
  107.   pitch = Quat.angleAxis(Math.asin(-direction.y) * RAD_TO_DEG, X_AXIS);
  108.   return Quat.multiply(yaw, pitch);
  109. }
  110.  
  111.  
  112. function handleRadialMode(dx, dy) {
  113.   azimuth += dx / AZIMUTH_RATE;
  114.   radius += radius * dy * RADIUS_RATE;
  115.   if (radius < 1) {
  116.     radius = 1;
  117.   }
  118.  
  119.   vector = {
  120.     x: (Math.cos(altitude) * Math.cos(azimuth)) * radius,
  121.     y: Math.sin(altitude) * radius,
  122.     z: (Math.cos(altitude) * Math.sin(azimuth)) * radius
  123.   };
  124.   position = Vec3.sum(center, vector);
  125.   Camera.setPosition(position);
  126.   Camera.setOrientation(orientationOf(vector));
  127. }
  128.  
  129. function handleOrbitMode(dx, dy) {
  130.   azimuth += dx / AZIMUTH_RATE;
  131.   altitude += dy / ALTITUDE_RATE;
  132.   if (altitude > PI / 2.0) {
  133.     altitude = PI / 2.0;
  134.   }
  135.   if (altitude < -PI / 2.0) {
  136.     altitude = -PI / 2.0;
  137.   }
  138.  
  139.   vector = {
  140.     x: (Math.cos(altitude) * Math.cos(azimuth)) * radius,
  141.     y: Math.sin(altitude) * radius,
  142.     z: (Math.cos(altitude) * Math.sin(azimuth)) * radius
  143.   };
  144.   position = Vec3.sum(center, vector);
  145.   Camera.setPosition(position);
  146.   Camera.setOrientation(orientationOf(vector));
  147. }
  148.  
  149.  
  150. function handlePanMode(dx, dy) {
  151.   var up = Quat.getUp(Camera.getOrientation());
  152.   var right = Quat.getRight(Camera.getOrientation());
  153.   var distance = Vec3.length(vector);
  154.  
  155.   var dv = Vec3.sum(Vec3.multiply(up, distance * dy / PAN_RATE), Vec3.multiply(right, -distance * dx / PAN_RATE));
  156.  
  157.   center = Vec3.sum(center, dv);
  158.   position = Vec3.sum(position, dv);
  159.  
  160.   Camera.setPosition(position);
  161.   Camera.setOrientation(orientationOf(vector));
  162. }
  163.  
  164. function saveCameraState() {
  165.   oldMode = Camera.mode;
  166.   oldPosition = Camera.getPosition();
  167.   oldOrientation = Camera.getOrientation();
  168.  
  169.   Camera.mode = "independent";
  170.   Camera.setPosition(oldPosition);
  171. }
  172.  
  173. function restoreCameraState() {
  174.   Camera.mode = oldMode;
  175.   Camera.setPosition(oldPosition);
  176.   Camera.setOrientation(oldOrientation);
  177. }
  178.  
  179. function handleModes() {
  180.   var newMode = (mode == noMode) ? noMode : detachedMode;
  181.   if (alt) {
  182.     if (control) {
  183.       if (shift) {
  184.         newMode = panningMode;
  185.       } else {
  186.         newMode = orbitMode;
  187.       }
  188.     } else {
  189.       newMode = radialMode;
  190.     }
  191.   }
  192.  
  193.   // if entering detachMode
  194.   if (newMode == detachedMode && mode != detachedMode) {
  195.     avatarPosition = MyAvatar.position;
  196.     avatarOrientation = MyAvatar.orientation;
  197.   }
  198.   // if leaving detachMode
  199.   // CAS...
  200.   /*
  201.   if (mode == detachedMode && newMode == detachedMode &&
  202.     (avatarPosition.x != MyAvatar.position.x ||
  203.       avatarPosition.y != MyAvatar.position.y ||
  204.       avatarPosition.z != MyAvatar.position.z ||
  205.       avatarOrientation.x != MyAvatar.orientation.x ||
  206.       avatarOrientation.y != MyAvatar.orientation.y ||
  207.       avatarOrientation.z != MyAvatar.orientation.z ||
  208.       avatarOrientation.w != MyAvatar.orientation.w)) {
  209.     newMode = noMode;
  210.   }
  211.   */
  212.   //... CAS
  213.   if (mode == detachedMode && newMode == detachedMode && (
  214.       Vec3.length(Vec3.subtract(avatarPosition, MyAvatar.position)) > AVATAR_POSITION_SLOP
  215.       || Vec3.length(Vec3.subtract(Quat.getFront(avatarOrientation), Quat.getFront(MyAvatar.orientation))) > AVATAR_ROTATION_SLOP)) {
  216.       newMode = noMode;
  217.   }
  218.  
  219.   if (mode == noMode && newMode != noMode && Camera.mode == "independent") {
  220.     newMode = noMode;
  221.   }
  222.  
  223.   // if leaving noMode
  224.   if (mode == noMode && newMode != noMode) {
  225.     saveCameraState();
  226.   }
  227.   // if entering noMode
  228.   if (newMode == noMode && mode != noMode) {
  229.     restoreCameraState();
  230.   }
  231.  
  232.   mode = newMode;
  233. }
  234.  
  235. function keyPressEvent(event) {
  236.   var changed = false;
  237.  
  238.   if (event.text == "ALT") {
  239.     alt = true;
  240.     changed = true;
  241.   }
  242.   if (event.text == "CONTROL") {
  243.     control = true;
  244.     changed = true;
  245.   }
  246.   if (event.text == "SHIFT") {
  247.     shift = true;
  248.     changed = true;
  249.   }
  250.  
  251.   // CAS...
  252.   if (mode !== noMode && !alt && !control && !shift && /^ESC|LEFT|RIGHT|UP|DOWN|[wasdWASD]$/.test(event.text)) {
  253.     mode = noMode;
  254.     restoreCameraState();
  255.     changed = true;
  256.   }
  257.   // ...CAS
  258.  
  259.   if (changed) {
  260.     handleModes();
  261.   }
  262. }
  263.  
  264. function keyReleaseEvent(event) {
  265.   var changed = false;
  266.  
  267.   if (event.text == "ALT") {
  268.     alt = false;
  269.     changed = true;
  270.     // CAS...
  271.     /*
  272.     mode = noMode;
  273.     restoreCameraState();
  274.     */
  275.     // ...CAS
  276.   }
  277.   if (event.text == "CONTROL") {
  278.     control = false;
  279.     changed = true;
  280.   }
  281.   if (event.text == "SHIFT") {
  282.     shift = false;
  283.     changed = true;
  284.   }
  285.  
  286.   if (changed) {
  287.     handleModes();
  288.   }
  289. }
  290.  
  291.  
  292.  
  293. function mousePressEvent(event) {
  294.   if (alt && !isActive) {
  295.     mouseLastX = event.x;
  296.     mouseLastY = event.y;
  297.  
  298.     // Compute trajectories related values
  299.     var pickRay = Camera.computePickRay(mouseLastX, mouseLastY);
  300.     var modelIntersection = Entities.findRayIntersection(pickRay, true);
  301.  
  302.     position = Camera.getPosition();
  303.  
  304.     var avatarTarget = MyAvatar.getTargetAvatarPosition();
  305.  
  306.  
  307.     var distance = -1;
  308.     var string;
  309.  
  310.     if (modelIntersection.intersects && modelIntersection.accurate) {
  311.       distance = modelIntersection.distance;
  312.       center = modelIntersection.intersection;
  313.       // CAS...
  314.     } else {
  315.       // Orbit about a point in the air.
  316.       var ORBIT_DISTANCE = 10;
  317.       center = Vec3.sum(Camera.position, Vec3.multiply(ORBIT_DISTANCE, pickRay.direction));
  318.     }
  319.     {
  320.       // ...CAS
  321.       string = "Inspecting model";
  322.       // CAS...
  323.       /*
  324.       //We've selected our target, now orbit towards it automatically
  325.       rotatingTowardsTarget = true;
  326.       //calculate our target cam rotation
  327.       Script.setTimeout(function() {
  328.         rotatingTowardsTarget = false;
  329.       }, LOOK_AT_TIME);
  330.       */
  331.       // ... CAS
  332.  
  333.       vector = Vec3.subtract(position, center);
  334.       targetCamOrientation = orientationOf(vector);
  335.       radius = Vec3.length(vector);
  336.       azimuth = Math.atan2(vector.z, vector.x);
  337.       altitude = Math.asin(vector.y / Vec3.length(vector));
  338.  
  339.       isActive = true;
  340.     }
  341.   }
  342. }
  343.  
  344. function mouseReleaseEvent(event) {
  345.   if (isActive) {
  346.     isActive = false;
  347.   }
  348. }
  349.  
  350. function mouseMoveEvent(event) {
  351.   if (isActive && mode != noMode && !rotatingTowardsTarget) {
  352.     if (mode == radialMode) {
  353.       handleRadialMode(event.x - mouseLastX, event.y - mouseLastY);
  354.     }
  355.     if (mode == orbitMode) {
  356.       handleOrbitMode(event.x - mouseLastX, event.y - mouseLastY);
  357.     }
  358.     if (mode == panningMode) {
  359.       handlePanMode(event.x - mouseLastX, event.y - mouseLastY);
  360.     }
  361.  
  362.   }
  363.   mouseLastX = event.x;
  364.   mouseLastY = event.y;
  365. }
  366.  
  367. function update() {
  368.   handleModes();
  369.   if (rotatingTowardsTarget) {
  370.     rotateTowardsTarget();
  371.   }
  372. }
  373.  
  374. function rotateTowardsTarget() {
  375.   var newOrientation = Quat.mix(Camera.getOrientation(), targetCamOrientation, .1);
  376.   Camera.setOrientation(newOrientation);
  377. }
  378.  
  379. function scriptEnding() {
  380.   if (mode != noMode) {
  381.     restoreCameraState();
  382.   }
  383. }
  384.  
  385. Controller.keyPressEvent.connect(keyPressEvent);
  386. Controller.keyReleaseEvent.connect(keyReleaseEvent);
  387.  
  388. Controller.mousePressEvent.connect(mousePressEvent);
  389. Controller.mouseReleaseEvent.connect(mouseReleaseEvent);
  390. Controller.mouseMoveEvent.connect(mouseMoveEvent);
  391.  
  392. Script.update.connect(update);
  393. Script.scriptEnding.connect(scriptEnding);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement