Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

407 lignes
7.8KB

  1. // three.js - http://github.com/mrdoob/three.js
  2. /**
  3. * @author qiao / https://github.com/qiao
  4. * @author mrdoob / http://mrdoob.com
  5. * @author alteredq / http://alteredqualia.com/
  6. * @author WestLangley / http://github.com/WestLangley
  7. */
  8. THREE.OrbitControls = function ( object, domElement ) {
  9. this.object = object;
  10. this.domElement = ( domElement !== undefined ) ? domElement : document;
  11. // API
  12. this.enabled = true;
  13. this.center = new THREE.Vector3();
  14. this.userZoom = true;
  15. this.userZoomSpeed = 1.0;
  16. this.userRotate = true;
  17. this.userRotateSpeed = 1.0;
  18. this.userPan = true;
  19. this.userPanSpeed = 0.1;
  20. this.autoRotate = false;
  21. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  22. this.minPolarAngle = 0; // radians
  23. this.maxPolarAngle = Math.PI; // radians
  24. this.minDistance = 0;
  25. this.maxDistance = Infinity;
  26. // 65 /*A*/, 83 /*S*/, 68 /*D*/
  27. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40, ROTATE: 65, ZOOM: 83, PAN: 68 };
  28. // internals
  29. var scope = this;
  30. var EPS = 0.000001;
  31. var PIXELS_PER_ROUND = 1800;
  32. var rotateStart = new THREE.Vector2();
  33. var rotateEnd = new THREE.Vector2();
  34. var rotateDelta = new THREE.Vector2();
  35. var zoomStart = new THREE.Vector2();
  36. var zoomEnd = new THREE.Vector2();
  37. var zoomDelta = new THREE.Vector2();
  38. var phiDelta = 0;
  39. var thetaDelta = 0;
  40. var scale = 1;
  41. var lastPosition = new THREE.Vector3();
  42. var STATE = { NONE: -1, ROTATE: 0, ZOOM: 1, PAN: 2 };
  43. var state = STATE.NONE;
  44. // events
  45. var changeEvent = { type: 'change' };
  46. this.rotateLeft = function ( angle ) {
  47. if ( angle === undefined ) {
  48. angle = getAutoRotationAngle();
  49. }
  50. thetaDelta -= angle;
  51. };
  52. this.rotateRight = function ( angle ) {
  53. if ( angle === undefined ) {
  54. angle = getAutoRotationAngle();
  55. }
  56. thetaDelta += angle;
  57. };
  58. this.rotateUp = function ( angle ) {
  59. if ( angle === undefined ) {
  60. angle = getAutoRotationAngle();
  61. }
  62. phiDelta -= angle;
  63. };
  64. this.rotateDown = function ( angle ) {
  65. if ( angle === undefined ) {
  66. angle = getAutoRotationAngle();
  67. }
  68. phiDelta += angle;
  69. };
  70. this.zoomIn = function ( zoomScale ) {
  71. if ( zoomScale === undefined ) {
  72. zoomScale = getZoomScale();
  73. }
  74. scale /= zoomScale;
  75. };
  76. this.zoomOut = function ( zoomScale ) {
  77. if ( zoomScale === undefined ) {
  78. zoomScale = getZoomScale();
  79. }
  80. scale *= zoomScale;
  81. };
  82. this.pan = function ( distance ) {
  83. distance.transformDirection( this.object.matrix );
  84. distance.multiplyScalar( scope.userPanSpeed );
  85. this.object.position.add( distance );
  86. this.center.add( distance );
  87. };
  88. this.update = function () {
  89. var position = this.object.position;
  90. var offset = position.clone().sub( this.center );
  91. // angle from z-axis around y-axis
  92. var theta = Math.atan2( offset.x, offset.z );
  93. // angle from y-axis
  94. var phi = Math.atan2( Math.sqrt( offset.x * offset.x + offset.z * offset.z ), offset.y );
  95. if ( this.autoRotate ) {
  96. this.rotateLeft( getAutoRotationAngle() );
  97. }
  98. theta += thetaDelta;
  99. phi += phiDelta;
  100. // restrict phi to be between desired limits
  101. phi = Math.max( this.minPolarAngle, Math.min( this.maxPolarAngle, phi ) );
  102. // restrict phi to be betwee EPS and PI-EPS
  103. phi = Math.max( EPS, Math.min( Math.PI - EPS, phi ) );
  104. var radius = offset.length() * scale;
  105. // restrict radius to be between desired limits
  106. radius = Math.max( this.minDistance, Math.min( this.maxDistance, radius ) );
  107. offset.x = radius * Math.sin( phi ) * Math.sin( theta );
  108. offset.y = radius * Math.cos( phi );
  109. offset.z = radius * Math.sin( phi ) * Math.cos( theta );
  110. position.copy( this.center ).add( offset );
  111. this.object.lookAt( this.center );
  112. thetaDelta = 0;
  113. phiDelta = 0;
  114. scale = 1;
  115. if ( lastPosition.distanceTo( this.object.position ) > 0 ) {
  116. this.dispatchEvent( changeEvent );
  117. lastPosition.copy( this.object.position );
  118. }
  119. };
  120. function getAutoRotationAngle() {
  121. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  122. }
  123. function getZoomScale() {
  124. return Math.pow( 0.95, scope.userZoomSpeed );
  125. }
  126. function onMouseDown( event ) {
  127. if ( scope.enabled === false ) return;
  128. if ( scope.userRotate === false ) return;
  129. event.preventDefault();
  130. if ( state === STATE.NONE )
  131. {
  132. if ( event.button === 0 )
  133. state = STATE.ROTATE;
  134. if ( event.button === 1 )
  135. state = STATE.ZOOM;
  136. if ( event.button === 2 )
  137. state = STATE.PAN;
  138. }
  139. if ( state === STATE.ROTATE ) {
  140. //state = STATE.ROTATE;
  141. rotateStart.set( event.clientX, event.clientY );
  142. } else if ( state === STATE.ZOOM ) {
  143. //state = STATE.ZOOM;
  144. zoomStart.set( event.clientX, event.clientY );
  145. } else if ( state === STATE.PAN ) {
  146. //state = STATE.PAN;
  147. }
  148. document.addEventListener( 'mousemove', onMouseMove, false );
  149. document.addEventListener( 'mouseup', onMouseUp, false );
  150. }
  151. function onMouseMove( event ) {
  152. if ( scope.enabled === false ) return;
  153. event.preventDefault();
  154. if ( state === STATE.ROTATE ) {
  155. rotateEnd.set( event.clientX, event.clientY );
  156. rotateDelta.subVectors( rotateEnd, rotateStart );
  157. scope.rotateLeft( 2 * Math.PI * rotateDelta.x / PIXELS_PER_ROUND * scope.userRotateSpeed );
  158. scope.rotateUp( 2 * Math.PI * rotateDelta.y / PIXELS_PER_ROUND * scope.userRotateSpeed );
  159. rotateStart.copy( rotateEnd );
  160. } else if ( state === STATE.ZOOM ) {
  161. zoomEnd.set( event.clientX, event.clientY );
  162. zoomDelta.subVectors( zoomEnd, zoomStart );
  163. if ( zoomDelta.y > 0 ) {
  164. scope.zoomIn();
  165. } else {
  166. scope.zoomOut();
  167. }
  168. zoomStart.copy( zoomEnd );
  169. } else if ( state === STATE.PAN ) {
  170. var movementX = event.movementX || event.mozMovementX || event.webkitMovementX || 0;
  171. var movementY = event.movementY || event.mozMovementY || event.webkitMovementY || 0;
  172. scope.pan( new THREE.Vector3( - movementX, movementY, 0 ) );
  173. }
  174. }
  175. function onMouseUp( event ) {
  176. if ( scope.enabled === false ) return;
  177. if ( scope.userRotate === false ) return;
  178. document.removeEventListener( 'mousemove', onMouseMove, false );
  179. document.removeEventListener( 'mouseup', onMouseUp, false );
  180. state = STATE.NONE;
  181. }
  182. function onMouseWheel( event ) {
  183. if ( scope.enabled === false ) return;
  184. if ( scope.userZoom === false ) return;
  185. var delta = 0;
  186. if ( event.wheelDelta ) { // WebKit / Opera / Explorer 9
  187. delta = event.wheelDelta;
  188. } else if ( event.detail ) { // Firefox
  189. delta = - event.detail;
  190. }
  191. if ( delta > 0 ) {
  192. scope.zoomOut();
  193. } else {
  194. scope.zoomIn();
  195. }
  196. }
  197. function onKeyDown( event ) {
  198. if ( scope.enabled === false ) return;
  199. if ( scope.userPan === false ) return;
  200. switch ( event.keyCode ) {
  201. /*case scope.keys.UP:
  202. scope.pan( new THREE.Vector3( 0, 1, 0 ) );
  203. break;
  204. case scope.keys.BOTTOM:
  205. scope.pan( new THREE.Vector3( 0, - 1, 0 ) );
  206. break;
  207. case scope.keys.LEFT:
  208. scope.pan( new THREE.Vector3( - 1, 0, 0 ) );
  209. break;
  210. case scope.keys.RIGHT:
  211. scope.pan( new THREE.Vector3( 1, 0, 0 ) );
  212. break;
  213. */
  214. case scope.keys.ROTATE:
  215. state = STATE.ROTATE;
  216. break;
  217. case scope.keys.ZOOM:
  218. state = STATE.ZOOM;
  219. break;
  220. case scope.keys.PAN:
  221. state = STATE.PAN;
  222. break;
  223. }
  224. }
  225. function onKeyUp( event ) {
  226. switch ( event.keyCode ) {
  227. case scope.keys.ROTATE:
  228. case scope.keys.ZOOM:
  229. case scope.keys.PAN:
  230. state = STATE.NONE;
  231. break;
  232. }
  233. }
  234. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  235. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  236. this.domElement.addEventListener( 'mousewheel', onMouseWheel, false );
  237. this.domElement.addEventListener( 'DOMMouseScroll', onMouseWheel, false ); // firefox
  238. window.addEventListener( 'keydown', onKeyDown, false );
  239. window.addEventListener( 'keyup', onKeyUp, false );
  240. };
  241. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );