EditorControls.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.rotate = function ( delta ) {
  33. var position = this.object.position;
  34. var offset = position.clone().sub( this.center );
  35. var theta = Math.atan2( offset.x, offset.z );
  36. var phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
  37. theta += delta.x;
  38. phi += delta.y;
  39. var EPS = 0.000001;
  40. phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
  41. var radius = offset.length();
  42. offset.x = radius * Math.sin( phi ) * Math.sin( theta );
  43. offset.y = radius * Math.cos( phi );
  44. offset.z = radius * Math.sin( phi ) * Math.cos( theta );
  45. position.copy( this.center ).add( offset );
  46. this.object.lookAt( this.center );
  47. this.dispatchEvent( changeEvent );
  48. };
  49. function onMouseDown( event ) {
  50. if ( scope.enabled === false ) return;
  51. event.preventDefault();
  52. if ( event.button === 0 ) {
  53. state = STATE.ROTATE;
  54. } else if ( event.button === 1 ) {
  55. state = STATE.ZOOM;
  56. } else if ( event.button === 2 ) {
  57. state = STATE.PAN;
  58. }
  59. document.addEventListener( 'mousemove', onMouseMove, false );
  60. document.addEventListener( 'mouseup', onMouseUp, false );
  61. }
  62. function onMouseMove( event ) {
  63. if ( scope.enabled === false ) return;
  64. event.preventDefault();
  65. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  66. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  67. if ( state === STATE.ROTATE ) {
  68. scope.rotate( new THREE.Vector3( - movementX * 0.005, - movementY * 0.005, 0 ) );
  69. } else if ( state === STATE.ZOOM ) {
  70. scope.pan( new THREE.Vector3( 0, 0, movementY ) );
  71. } else if ( state === STATE.PAN ) {
  72. scope.pan( new THREE.Vector3( - movementX * 0.5, movementY * 0.5, 0 ) );
  73. }
  74. }
  75. function onMouseUp( event ) {
  76. if ( scope.enabled === false ) return;
  77. document.removeEventListener( 'mousemove', onMouseMove, false );
  78. document.removeEventListener( 'mouseup', onMouseUp, false );
  79. state = STATE.NONE;
  80. }
  81. function onMouseWheel( event ) {
  82. if ( scope.enabled === false ) return;
  83. var delta = 0;
  84. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  85. delta = - event.wheelDelta * 0.1;
  86. } else if ( event.detail ) { // Firefox
  87. delta = event.detail * 5;
  88. }
  89. scope.pan( new THREE.Vector3( 0, 0, delta ) );
  90. }
  91. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  92. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  93. this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  94. this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
  95. };
  96. THREE.EditorControls.prototype = Object.create( THREE.EventDispatcher.prototype );