OrbitControls.js 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. * @author erich666 / http://erichaines.com
  7. */
  8. // This set of controls performs orbiting, dollying (zooming), and panning.
  9. // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default).
  10. //
  11. // Orbit - left mouse / touch: one-finger move
  12. // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish
  13. // Pan - right mouse, or arrow keys / touch: two-finger move
  14. THREE.OrbitControls = function ( object, domElement ) {
  15. // object:指定相机对象,比如多个相机对象(场景相机对象、UI相机对象),只有一个起作用
  16. this.object = object;
  17. // domElement:指定鼠标事件起作用的范围,默认是整个document,所有html元素
  18. // 比如3D建模软件,可以把范围指定为renderer.domElement,渲染器引用render的domElement属性引用指向绘图区canvas
  19. this.domElement = ( domElement !== undefined ) ? domElement : document;
  20. // Set to false to disable this control
  21. // 设置为false以禁用此控件
  22. this.enabled = true;
  23. // "target" sets the location of focus, where the object orbits around
  24. // "target"设置对象绕过的焦点位置
  25. // 与lookAt相关:.lookAt( scope.target ); 视角绕着谁旋转
  26. // 平移之后,旋转中心仍然是默认的原点,是否可以以鼠标为中心
  27. // 缩放的中心也是原点 能不能也改成以鼠标为中心
  28. // 任何时候都以鼠标为中心进行旋转、缩放、平移
  29. this.target = new THREE.Vector3();
  30. // How far you can dolly in and out ( PerspectiveCamera only )
  31. // 您可以进出多远(仅限PerspectiveCamera)
  32. this.minDistance = 0;
  33. this.maxDistance = Infinity;
  34. // How far you can zoom in and out ( OrthographicCamera only )
  35. // 您可以放大或缩小多少(仅限OrthographicCamera)
  36. // 相对你原来设置相机视觉效果 设置缩放倍数
  37. // 比如商品展示 0.5~2之间
  38. this.minZoom = 0;
  39. this.maxZoom = Infinity;
  40. // How far you can orbit vertically, upper and lower limits.
  41. // Range is 0 to Math.PI radians.
  42. //你可以垂直绕行多远,上限和下限。
  43. //范围从0到Math.PI弧度。
  44. // 上下旋转范围
  45. this.minPolarAngle = 0; // radians
  46. this.maxPolarAngle = Math.PI; // radians
  47. // How far you can orbit horizontally, upper and lower limits.
  48. // If set, must be a sub-interval of the interval [ - Math.PI, Math.PI ].
  49. //你可以绕水平方向运行多远,上限和下限。
  50. //如果设置,则必须是区间[ - Math.PI,Math.PI]的子区间。
  51. // 左右旋转范围
  52. this.minAzimuthAngle = - Infinity; // radians
  53. this.maxAzimuthAngle = Infinity; // radians
  54. // Set to true to enable damping (inertia)
  55. // If damping is enabled, you must call controls.update() in your animation loop
  56. //设置为true以启用阻尼(惯性)
  57. //如果启用阻尼,则必须在动画循环中调用controls.update()
  58. this.enableDamping = false;
  59. this.dampingFactor = 0.25;
  60. // This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
  61. // Set to false to disable zooming
  62. //这个选项实际上可以使进出进入; 为了向后兼容,保留为“缩放”。
  63. //设置为false以禁用缩放
  64. this.enableZoom = true;
  65. this.zoomSpeed = 1.0;
  66. // Set to false to disable rotating
  67. //设置为false以禁用旋转
  68. this.enableRotate = true;
  69. this.rotateSpeed = 1.0;
  70. // Set to false to disable panning平移
  71. this.enablePan = true;
  72. this.panSpeed = 1.0;
  73. // 如果为true,则在屏幕空间中平移
  74. this.screenSpacePanning = false; // if true, pan in screen-space
  75. this.keyPanSpeed = 7.0; // pixels moved per arrow key push 像素按箭头键移动
  76. // Set to true to automatically rotate around the target
  77. // If auto-rotate is enabled, you must call controls.update() in your animation loop
  78. //设置为true以自动围绕目标旋转
  79. //如果启用自动旋转,则必须在动画循环中调用controls.update()
  80. this.autoRotate = false;
  81. // 当fps是60时,每轮30秒
  82. this.autoRotateSpeed = 2.0; // 30 seconds per round when fps is 60
  83. // Set to false to disable use of the keys禁用键使用
  84. this.enableKeys = true;
  85. // The four arrow keys 四个箭头键
  86. this.keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40 };
  87. // Mouse buttons
  88. // orbit:轨道 ZOOM:放大 pan对应平移
  89. // 给特定的键命名
  90. this.mouseButtons = { ORBIT: THREE.MOUSE.LEFT, ZOOM: THREE.MOUSE.MIDDLE, PAN: THREE.MOUSE.RIGHT };
  91. // for reset 用于复位、重置
  92. this.target0 = this.target.clone();
  93. this.position0 = this.object.position.clone();
  94. this.zoom0 = this.object.zoom;
  95. //
  96. // public methods
  97. //
  98. // getPolarAngle获得极化角 Polar:极地的
  99. this.getPolarAngle = function () {
  100. // phi:表示希腊文的第21个字母,角度φ
  101. // spherical:球形的
  102. return spherical.phi;
  103. };
  104. // Azimuthal:方位角
  105. this.getAzimuthalAngle = function () {
  106. return spherical.theta;
  107. };
  108. // 记录目标点、相机位置、相机缩放状态
  109. this.saveState = function () {
  110. scope.target0.copy( scope.target );
  111. scope.position0.copy( scope.object.position );
  112. scope.zoom0 = scope.object.zoom;
  113. };
  114. // 重置
  115. this.reset = function () {
  116. scope.target.copy( scope.target0 );
  117. scope.object.position.copy( scope.position0 );
  118. scope.object.zoom = scope.zoom0;
  119. scope.object.updateProjectionMatrix();
  120. scope.dispatchEvent( changeEvent );
  121. scope.update();
  122. state = STATE.NONE;
  123. };
  124. // this method is exposed, but perhaps it would be better if we can make it private...
  125. this.update = function () {
  126. var offset = new THREE.Vector3();
  127. // so camera.up is the orbit axis
  128. var quat = new THREE.Quaternion().setFromUnitVectors( object.up, new THREE.Vector3( 0, 1, 0 ) );
  129. var quatInverse = quat.clone().inverse();
  130. var lastPosition = new THREE.Vector3();
  131. var lastQuaternion = new THREE.Quaternion();
  132. return function update() {
  133. var position = scope.object.position;
  134. offset.copy( position ).sub( scope.target );
  135. // rotate offset to "y-axis-is-up" space
  136. offset.applyQuaternion( quat );
  137. // angle from z-axis around y-axis
  138. spherical.setFromVector3( offset );
  139. if ( scope.autoRotate && state === STATE.NONE ) {
  140. rotateLeft( getAutoRotationAngle() );
  141. }
  142. spherical.theta += sphericalDelta.theta;
  143. spherical.phi += sphericalDelta.phi;
  144. // restrict theta to be between desired limits
  145. spherical.theta = Math.max( scope.minAzimuthAngle, Math.min( scope.maxAzimuthAngle, spherical.theta ) );
  146. // restrict phi to be between desired limits
  147. spherical.phi = Math.max( scope.minPolarAngle, Math.min( scope.maxPolarAngle, spherical.phi ) );
  148. spherical.makeSafe();
  149. spherical.radius *= scale;
  150. // restrict radius to be between desired limits
  151. spherical.radius = Math.max( scope.minDistance, Math.min( scope.maxDistance, spherical.radius ) );
  152. // move target to panned location
  153. scope.target.add( panOffset );
  154. offset.setFromSpherical( spherical );
  155. // rotate offset back to "camera-up-vector-is-up" space
  156. offset.applyQuaternion( quatInverse );
  157. position.copy( scope.target ).add( offset );
  158. scope.object.lookAt( scope.target );
  159. if ( scope.enableDamping === true ) {
  160. sphericalDelta.theta *= ( 1 - scope.dampingFactor );
  161. sphericalDelta.phi *= ( 1 - scope.dampingFactor );
  162. panOffset.multiplyScalar( 1 - scope.dampingFactor );
  163. } else {
  164. sphericalDelta.set( 0, 0, 0 );
  165. panOffset.set( 0, 0, 0 );
  166. }
  167. scale = 1;
  168. // update condition is:
  169. // min(camera displacement, camera rotation in radians)^2 > EPS
  170. // using small-angle approximation cos(x/2) = 1 - x^2 / 8
  171. if ( zoomChanged ||
  172. lastPosition.distanceToSquared( scope.object.position ) > EPS ||
  173. 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  174. scope.dispatchEvent( changeEvent );
  175. lastPosition.copy( scope.object.position );
  176. lastQuaternion.copy( scope.object.quaternion );
  177. zoomChanged = false;
  178. return true;
  179. }
  180. return false;
  181. };
  182. }();
  183. this.dispose = function () {
  184. scope.domElement.removeEventListener( 'contextmenu', onContextMenu, false );
  185. scope.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  186. scope.domElement.removeEventListener( 'wheel', onMouseWheel, false );
  187. scope.domElement.removeEventListener( 'touchstart', onTouchStart, false );
  188. scope.domElement.removeEventListener( 'touchend', onTouchEnd, false );
  189. scope.domElement.removeEventListener( 'touchmove', onTouchMove, false );
  190. document.removeEventListener( 'mousemove', onMouseMove, false );
  191. document.removeEventListener( 'mouseup', onMouseUp, false );
  192. window.removeEventListener( 'keydown', onKeyDown, false );
  193. //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here?
  194. };
  195. //
  196. // internals
  197. //
  198. var scope = this;
  199. var changeEvent = { type: 'change' };
  200. var startEvent = { type: 'start' };
  201. var endEvent = { type: 'end' };
  202. var STATE = { NONE: - 1, ROTATE: 0, DOLLY: 1, PAN: 2, TOUCH_ROTATE: 3, TOUCH_DOLLY_PAN: 4 };
  203. var state = STATE.NONE;
  204. var EPS = 0.000001;
  205. // current position in spherical coordinates
  206. var spherical = new THREE.Spherical();
  207. var sphericalDelta = new THREE.Spherical();
  208. var scale = 1;
  209. var panOffset = new THREE.Vector3();
  210. var zoomChanged = false;
  211. var rotateStart = new THREE.Vector2();
  212. var rotateEnd = new THREE.Vector2();
  213. var rotateDelta = new THREE.Vector2();
  214. var panStart = new THREE.Vector2();
  215. var panEnd = new THREE.Vector2();
  216. var panDelta = new THREE.Vector2();
  217. var dollyStart = new THREE.Vector2();
  218. var dollyEnd = new THREE.Vector2();
  219. var dollyDelta = new THREE.Vector2();
  220. function getAutoRotationAngle() {
  221. return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed;
  222. }
  223. function getZoomScale() {
  224. return Math.pow( 0.95, scope.zoomSpeed );
  225. }
  226. function rotateLeft( angle ) {
  227. sphericalDelta.theta -= angle;
  228. }
  229. function rotateUp( angle ) {
  230. sphericalDelta.phi -= angle;
  231. }
  232. var panLeft = function () {
  233. var v = new THREE.Vector3();
  234. return function panLeft( distance, objectMatrix ) {
  235. v.setFromMatrixColumn( objectMatrix, 0 ); // get X column of objectMatrix
  236. v.multiplyScalar( - distance );
  237. panOffset.add( v );
  238. };
  239. }();
  240. var panUp = function () {
  241. var v = new THREE.Vector3();
  242. return function panUp( distance, objectMatrix ) {
  243. if ( scope.screenSpacePanning === true ) {
  244. v.setFromMatrixColumn( objectMatrix, 1 );
  245. } else {
  246. v.setFromMatrixColumn( objectMatrix, 0 );
  247. v.crossVectors( scope.object.up, v );
  248. }
  249. v.multiplyScalar( distance );
  250. panOffset.add( v );
  251. };
  252. }();
  253. // deltaX and deltaY are in pixels; right and down are positive
  254. var pan = function () {
  255. var offset = new THREE.Vector3();
  256. return function pan( deltaX, deltaY ) {
  257. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  258. if ( scope.object.isPerspectiveCamera ) {
  259. // perspective
  260. var position = scope.object.position;
  261. offset.copy( position ).sub( scope.target );
  262. var targetDistance = offset.length();
  263. // half of the fov is center to top of screen
  264. targetDistance *= Math.tan( ( scope.object.fov / 2 ) * Math.PI / 180.0 );
  265. // we use only clientHeight here so aspect ratio does not distort speed
  266. panLeft( 2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix );
  267. panUp( 2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix );
  268. } else if ( scope.object.isOrthographicCamera ) {
  269. // orthographic
  270. panLeft( deltaX * ( scope.object.right - scope.object.left ) / scope.object.zoom / element.clientWidth, scope.object.matrix );
  271. panUp( deltaY * ( scope.object.top - scope.object.bottom ) / scope.object.zoom / element.clientHeight, scope.object.matrix );
  272. } else {
  273. // camera neither orthographic nor perspective
  274. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.' );
  275. scope.enablePan = false;
  276. }
  277. };
  278. }();
  279. function dollyIn( dollyScale ) {
  280. if ( scope.object.isPerspectiveCamera ) {
  281. scale /= dollyScale;
  282. } else if ( scope.object.isOrthographicCamera ) {
  283. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom * dollyScale ) );
  284. scope.object.updateProjectionMatrix();
  285. zoomChanged = true;
  286. } else {
  287. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  288. scope.enableZoom = false;
  289. }
  290. }
  291. function dollyOut( dollyScale ) {
  292. if ( scope.object.isPerspectiveCamera ) {
  293. scale *= dollyScale;
  294. } else if ( scope.object.isOrthographicCamera ) {
  295. scope.object.zoom = Math.max( scope.minZoom, Math.min( scope.maxZoom, scope.object.zoom / dollyScale ) );
  296. scope.object.updateProjectionMatrix();
  297. zoomChanged = true;
  298. } else {
  299. console.warn( 'WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.' );
  300. scope.enableZoom = false;
  301. }
  302. }
  303. //
  304. // event callbacks - update the object state
  305. //
  306. function handleMouseDownRotate( event ) {
  307. //console.log( 'handleMouseDownRotate' );
  308. rotateStart.set( event.clientX, event.clientY );
  309. }
  310. function handleMouseDownDolly( event ) {
  311. //console.log( 'handleMouseDownDolly' );
  312. dollyStart.set( event.clientX, event.clientY );
  313. }
  314. function handleMouseDownPan( event ) {
  315. //console.log( 'handleMouseDownPan' );
  316. panStart.set( event.clientX, event.clientY );
  317. }
  318. function handleMouseMoveRotate( event ) {
  319. //console.log( 'handleMouseMoveRotate' );
  320. rotateEnd.set( event.clientX, event.clientY );
  321. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  322. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  323. // rotating across whole screen goes 360 degrees around
  324. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth );
  325. // rotating up and down along whole screen attempts to go 360, but limited to 180
  326. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  327. rotateStart.copy( rotateEnd );
  328. scope.update();
  329. }
  330. function handleMouseMoveDolly( event ) {
  331. //console.log( 'handleMouseMoveDolly' );
  332. dollyEnd.set( event.clientX, event.clientY );
  333. dollyDelta.subVectors( dollyEnd, dollyStart );
  334. if ( dollyDelta.y > 0 ) {
  335. dollyIn( getZoomScale() );
  336. } else if ( dollyDelta.y < 0 ) {
  337. dollyOut( getZoomScale() );
  338. }
  339. dollyStart.copy( dollyEnd );
  340. scope.update();
  341. }
  342. function handleMouseMovePan( event ) {
  343. //console.log( 'handleMouseMovePan' );
  344. panEnd.set( event.clientX, event.clientY );
  345. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  346. pan( panDelta.x, panDelta.y );
  347. panStart.copy( panEnd );
  348. scope.update();
  349. }
  350. function handleMouseUp( event ) {
  351. // console.log( 'handleMouseUp' );
  352. }
  353. function handleMouseWheel( event ) {
  354. // console.log( 'handleMouseWheel' );
  355. if ( event.deltaY < 0 ) {
  356. dollyOut( getZoomScale() );
  357. } else if ( event.deltaY > 0 ) {
  358. dollyIn( getZoomScale() );
  359. }
  360. scope.update();
  361. }
  362. function handleKeyDown( event ) {
  363. //console.log( 'handleKeyDown' );
  364. switch ( event.keyCode ) {
  365. case scope.keys.UP:
  366. pan( 0, scope.keyPanSpeed );
  367. scope.update();
  368. break;
  369. case scope.keys.BOTTOM:
  370. pan( 0, - scope.keyPanSpeed );
  371. scope.update();
  372. break;
  373. case scope.keys.LEFT:
  374. pan( scope.keyPanSpeed, 0 );
  375. scope.update();
  376. break;
  377. case scope.keys.RIGHT:
  378. pan( - scope.keyPanSpeed, 0 );
  379. scope.update();
  380. break;
  381. }
  382. }
  383. function handleTouchStartRotate( event ) {
  384. //console.log( 'handleTouchStartRotate' );
  385. rotateStart.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  386. }
  387. function handleTouchStartDollyPan( event ) {
  388. //console.log( 'handleTouchStartDollyPan' );
  389. if ( scope.enableZoom ) {
  390. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  391. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  392. var distance = Math.sqrt( dx * dx + dy * dy );
  393. dollyStart.set( 0, distance );
  394. }
  395. if ( scope.enablePan ) {
  396. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  397. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  398. panStart.set( x, y );
  399. }
  400. }
  401. function handleTouchMoveRotate( event ) {
  402. //console.log( 'handleTouchMoveRotate' );
  403. rotateEnd.set( event.touches[ 0 ].pageX, event.touches[ 0 ].pageY );
  404. rotateDelta.subVectors( rotateEnd, rotateStart ).multiplyScalar( scope.rotateSpeed );
  405. var element = scope.domElement === document ? scope.domElement.body : scope.domElement;
  406. // rotating across whole screen goes 360 degrees around
  407. rotateLeft( 2 * Math.PI * rotateDelta.x / element.clientWidth );
  408. // rotating up and down along whole screen attempts to go 360, but limited to 180
  409. rotateUp( 2 * Math.PI * rotateDelta.y / element.clientHeight );
  410. rotateStart.copy( rotateEnd );
  411. scope.update();
  412. }
  413. function handleTouchMoveDollyPan( event ) {
  414. //console.log( 'handleTouchMoveDollyPan' );
  415. if ( scope.enableZoom ) {
  416. var dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX;
  417. var dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY;
  418. var distance = Math.sqrt( dx * dx + dy * dy );
  419. dollyEnd.set( 0, distance );
  420. dollyDelta.set( 0, Math.pow( dollyEnd.y / dollyStart.y, scope.zoomSpeed ) );
  421. dollyIn( dollyDelta.y );
  422. dollyStart.copy( dollyEnd );
  423. }
  424. if ( scope.enablePan ) {
  425. var x = 0.5 * ( event.touches[ 0 ].pageX + event.touches[ 1 ].pageX );
  426. var y = 0.5 * ( event.touches[ 0 ].pageY + event.touches[ 1 ].pageY );
  427. panEnd.set( x, y );
  428. panDelta.subVectors( panEnd, panStart ).multiplyScalar( scope.panSpeed );
  429. pan( panDelta.x, panDelta.y );
  430. panStart.copy( panEnd );
  431. }
  432. scope.update();
  433. }
  434. function handleTouchEnd( event ) {
  435. //console.log( 'handleTouchEnd' );
  436. }
  437. //
  438. // event handlers - FSM: listen for events and reset state
  439. //
  440. function onMouseDown( event ) {
  441. if ( scope.enabled === false ) return;
  442. event.preventDefault();
  443. switch ( event.button ) {
  444. case scope.mouseButtons.ORBIT:
  445. if ( scope.enableRotate === false ) return;
  446. handleMouseDownRotate( event );
  447. state = STATE.ROTATE;
  448. break;
  449. case scope.mouseButtons.ZOOM:
  450. if ( scope.enableZoom === false ) return;
  451. handleMouseDownDolly( event );
  452. state = STATE.DOLLY;
  453. break;
  454. case scope.mouseButtons.PAN:
  455. if ( scope.enablePan === false ) return;
  456. handleMouseDownPan( event );
  457. state = STATE.PAN;
  458. break;
  459. }
  460. if ( state !== STATE.NONE ) {
  461. document.addEventListener( 'mousemove', onMouseMove, false );
  462. document.addEventListener( 'mouseup', onMouseUp, false );
  463. scope.dispatchEvent( startEvent );
  464. }
  465. }
  466. function onMouseMove( event ) {
  467. if ( scope.enabled === false ) return;
  468. event.preventDefault();
  469. switch ( state ) {
  470. case STATE.ROTATE:
  471. if ( scope.enableRotate === false ) return;
  472. handleMouseMoveRotate( event );
  473. break;
  474. case STATE.DOLLY:
  475. if ( scope.enableZoom === false ) return;
  476. handleMouseMoveDolly( event );
  477. break;
  478. case STATE.PAN:
  479. if ( scope.enablePan === false ) return;
  480. handleMouseMovePan( event );
  481. break;
  482. }
  483. }
  484. function onMouseUp( event ) {
  485. if ( scope.enabled === false ) return;
  486. handleMouseUp( event );
  487. document.removeEventListener( 'mousemove', onMouseMove, false );
  488. document.removeEventListener( 'mouseup', onMouseUp, false );
  489. scope.dispatchEvent( endEvent );
  490. state = STATE.NONE;
  491. }
  492. function onMouseWheel( event ) {
  493. if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
  494. event.preventDefault();
  495. event.stopPropagation();
  496. scope.dispatchEvent( startEvent );
  497. handleMouseWheel( event );
  498. scope.dispatchEvent( endEvent );
  499. }
  500. function onKeyDown( event ) {
  501. if ( scope.enabled === false || scope.enableKeys === false || scope.enablePan === false ) return;
  502. handleKeyDown( event );
  503. }
  504. function onTouchStart( event ) {
  505. if ( scope.enabled === false ) return;
  506. event.preventDefault();
  507. switch ( event.touches.length ) {
  508. case 1: // one-fingered touch: rotate
  509. if ( scope.enableRotate === false ) return;
  510. handleTouchStartRotate( event );
  511. state = STATE.TOUCH_ROTATE;
  512. break;
  513. case 2: // two-fingered touch: dolly-pan
  514. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  515. handleTouchStartDollyPan( event );
  516. state = STATE.TOUCH_DOLLY_PAN;
  517. break;
  518. default:
  519. state = STATE.NONE;
  520. }
  521. if ( state !== STATE.NONE ) {
  522. scope.dispatchEvent( startEvent );
  523. }
  524. }
  525. function onTouchMove( event ) {
  526. if ( scope.enabled === false ) return;
  527. event.preventDefault();
  528. event.stopPropagation();
  529. switch ( event.touches.length ) {
  530. case 1: // one-fingered touch: rotate
  531. if ( scope.enableRotate === false ) return;
  532. if ( state !== STATE.TOUCH_ROTATE ) return; // is this needed?
  533. handleTouchMoveRotate( event );
  534. break;
  535. case 2: // two-fingered touch: dolly-pan
  536. if ( scope.enableZoom === false && scope.enablePan === false ) return;
  537. if ( state !== STATE.TOUCH_DOLLY_PAN ) return; // is this needed?
  538. handleTouchMoveDollyPan( event );
  539. break;
  540. default:
  541. state = STATE.NONE;
  542. }
  543. }
  544. function onTouchEnd( event ) {
  545. if ( scope.enabled === false ) return;
  546. handleTouchEnd( event );
  547. scope.dispatchEvent( endEvent );
  548. state = STATE.NONE;
  549. }
  550. function onContextMenu( event ) {
  551. if ( scope.enabled === false ) return;
  552. event.preventDefault();
  553. }
  554. //
  555. scope.domElement.addEventListener( 'contextmenu', onContextMenu, false );
  556. scope.domElement.addEventListener( 'mousedown', onMouseDown, false );
  557. scope.domElement.addEventListener( 'wheel', onMouseWheel, false );
  558. scope.domElement.addEventListener( 'touchstart', onTouchStart, false );
  559. scope.domElement.addEventListener( 'touchend', onTouchEnd, false );
  560. scope.domElement.addEventListener( 'touchmove', onTouchMove, false );
  561. window.addEventListener( 'keydown', onKeyDown, false );
  562. // force an update at start
  563. this.update();
  564. };
  565. THREE.OrbitControls.prototype = Object.create( THREE.EventDispatcher.prototype );
  566. THREE.OrbitControls.prototype.constructor = THREE.OrbitControls;
  567. Object.defineProperties( THREE.OrbitControls.prototype, {
  568. center: {
  569. get: function () {
  570. console.warn( 'THREE.OrbitControls: .center has been renamed to .target' );
  571. return this.target;
  572. }
  573. },
  574. // backward compatibility
  575. noZoom: {
  576. get: function () {
  577. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  578. return ! this.enableZoom;
  579. },
  580. set: function ( value ) {
  581. console.warn( 'THREE.OrbitControls: .noZoom has been deprecated. Use .enableZoom instead.' );
  582. this.enableZoom = ! value;
  583. }
  584. },
  585. noRotate: {
  586. get: function () {
  587. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  588. return ! this.enableRotate;
  589. },
  590. set: function ( value ) {
  591. console.warn( 'THREE.OrbitControls: .noRotate has been deprecated. Use .enableRotate instead.' );
  592. this.enableRotate = ! value;
  593. }
  594. },
  595. noPan: {
  596. get: function () {
  597. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  598. return ! this.enablePan;
  599. },
  600. set: function ( value ) {
  601. console.warn( 'THREE.OrbitControls: .noPan has been deprecated. Use .enablePan instead.' );
  602. this.enablePan = ! value;
  603. }
  604. },
  605. noKeys: {
  606. get: function () {
  607. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  608. return ! this.enableKeys;
  609. },
  610. set: function ( value ) {
  611. console.warn( 'THREE.OrbitControls: .noKeys has been deprecated. Use .enableKeys instead.' );
  612. this.enableKeys = ! value;
  613. }
  614. },
  615. staticMoving: {
  616. get: function () {
  617. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  618. return ! this.enableDamping;
  619. },
  620. set: function ( value ) {
  621. console.warn( 'THREE.OrbitControls: .staticMoving has been deprecated. Use .enableDamping instead.' );
  622. this.enableDamping = ! value;
  623. }
  624. },
  625. dynamicDampingFactor: {
  626. get: function () {
  627. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  628. return this.dampingFactor;
  629. },
  630. set: function ( value ) {
  631. console.warn( 'THREE.OrbitControls: .dynamicDampingFactor has been renamed. Use .dampingFactor instead.' );
  632. this.dampingFactor = value;
  633. }
  634. }
  635. } );