EditorControls.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @author qiao / https://github.com/qiao
  3. * @author mrdoob / http://mrdoob.com
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author WestLangley / http://github.com/WestLangley
  6. */
  7. THREE.EditorControls = function ( object, domElement ) {
  8. this.object = object;
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. // API
  11. this.enabled = true;
  12. this.center = new THREE.Vector3();
  13. // internals
  14. var scope = this;
  15. var normalMatrix = new THREE.Matrix3();
  16. var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2 };
  17. var state = STATE.NONE;
  18. // events
  19. var changeEvent = { type: 'change' };
  20. this.focus = function ( object ) {
  21. this.center.getPositionFromMatrix( object.matrixWorld );
  22. this.object.lookAt( this.center );
  23. this.dispatchEvent( changeEvent );
  24. };
  25. this.pan = function ( distance ) {
  26. normalMatrix.getNormalMatrix( this.object.matrix );
  27. distance.applyMatrix3( normalMatrix );
  28. this.object.position.add( distance );
  29. this.center.add( distance );
  30. this.dispatchEvent( changeEvent );
  31. };
  32. this.zoom = function ( distance ) {
  33. normalMatrix.getNormalMatrix( this.object.matrix );
  34. distance.applyMatrix3( normalMatrix );
  35. this.object.position.add( distance );
  36. this.dispatchEvent( changeEvent );
  37. };
  38. this.rotate = function ( delta ) {
  39. var position = this.object.position;
  40. var offset = position.clone().sub( this.center );
  41. var theta = Math.atan2( offset.x, offset.z );
  42. var phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
  43. theta += delta.x;
  44. phi += delta.y;
  45. var EPS = 0.000001;
  46. phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
  47. var radius = offset.length();
  48. offset.x = radius * Math.sin( phi ) * Math.sin( theta );
  49. offset.y = radius * Math.cos( phi );
  50. offset.z = radius * Math.sin( phi ) * Math.cos( theta );
  51. position.copy( this.center ).add( offset );
  52. this.object.lookAt( this.center );
  53. this.dispatchEvent( changeEvent );
  54. };
  55. function onMouseDown( event ) {
  56. if ( scope.enabled === false ) return;
  57. event.preventDefault();
  58. if ( event.button === 0 ) {
  59. state = STATE.ROTATE;
  60. } else if ( event.button === 1 ) {
  61. state = STATE.ZOOM;
  62. } else if ( event.button === 2 ) {
  63. state = STATE.PAN;
  64. }
  65. document.addEventListener( 'mousemove', onMouseMove, false );
  66. document.addEventListener( 'mouseup', onMouseUp, false );
  67. }
  68. function onMouseMove( event ) {
  69. if ( scope.enabled === false ) return;
  70. event.preventDefault();
  71. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  72. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  73. if ( state === STATE.ROTATE ) {
  74. scope.rotate( new THREE.Vector3( - movementX * 0.005, - movementY * 0.005, 0 ) );
  75. } else if ( state === STATE.ZOOM ) {
  76. scope.zoom( new THREE.Vector3( 0, 0, movementY ) );
  77. } else if ( state === STATE.PAN ) {
  78. scope.pan( new THREE.Vector3( - movementX * 0.5, movementY * 0.5, 0 ) );
  79. }
  80. }
  81. function onMouseUp( event ) {
  82. if ( scope.enabled === false ) return;
  83. document.removeEventListener( 'mousemove', onMouseMove, false );
  84. document.removeEventListener( 'mouseup', onMouseUp, false );
  85. state = STATE.NONE;
  86. }
  87. function onMouseWheel( event ) {
  88. if ( scope.enabled === false ) return;
  89. var delta = 0;
  90. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  91. delta = - event.wheelDelta * 0.1;
  92. } else if ( event.detail ) { // Firefox
  93. delta = event.detail * 5;
  94. }
  95. scope.zoom( new THREE.Vector3( 0, 0, delta ) );
  96. }
  97. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  98. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  99. this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  100. this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
  101. };
  102. THREE.EditorControls.prototype = Object.create( THREE.EventDispatcher.prototype );