ArcballControls.js 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995
  1. ( function () {
  2. const STATE = {
  3. IDLE: Symbol(),
  4. ROTATE: Symbol(),
  5. PAN: Symbol(),
  6. SCALE: Symbol(),
  7. FOV: Symbol(),
  8. FOCUS: Symbol(),
  9. ZROTATE: Symbol(),
  10. TOUCH_MULTI: Symbol(),
  11. ANIMATION_FOCUS: Symbol(),
  12. ANIMATION_ROTATE: Symbol()
  13. };
  14. const INPUT = {
  15. NONE: Symbol(),
  16. ONE_FINGER: Symbol(),
  17. ONE_FINGER_SWITCHED: Symbol(),
  18. TWO_FINGER: Symbol(),
  19. MULT_FINGER: Symbol(),
  20. CURSOR: Symbol()
  21. }; //cursor center coordinates
  22. const _center = {
  23. x: 0,
  24. y: 0
  25. }; //transformation matrices for gizmos and camera
  26. const _transformation = {
  27. camera: new THREE.Matrix4(),
  28. gizmos: new THREE.Matrix4()
  29. }; //events
  30. const _changeEvent = {
  31. type: 'change'
  32. };
  33. const _startEvent = {
  34. type: 'start'
  35. };
  36. const _endEvent = {
  37. type: 'end'
  38. };
  39. /**
  40. *
  41. * @param {Camera} camera Virtual camera used in the scene
  42. * @param {HTMLElement} domElement Renderer's dom element
  43. * @param {Scene} scene The scene to be rendered
  44. */
  45. class ArcballControls extends THREE.Object3D {
  46. constructor( _camera, domElement, scene = null ) {
  47. super();
  48. this.onWindowResize = () => {
  49. const scale = ( this._gizmos.scale.x + this._gizmos.scale.y + this._gizmos.scale.z ) / 3;
  50. this._tbRadius = this.calculateTbRadius( this.camera );
  51. const newRadius = this._tbRadius / scale;
  52. const curve = new THREE.EllipseCurve( 0, 0, newRadius, newRadius );
  53. const points = curve.getPoints( this._curvePts );
  54. const curveGeometry = new THREE.BufferGeometry().setFromPoints( points );
  55. for ( const gizmo in this._gizmos.children ) {
  56. this._gizmos.children[ gizmo ].geometry = curveGeometry;
  57. }
  58. this.dispatchEvent( _changeEvent );
  59. };
  60. this.onContextMenu = event => {
  61. if ( ! this.enabled ) {
  62. return;
  63. }
  64. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  65. if ( this.mouseActions[ i ].mouse == 2 ) {
  66. //prevent only if button 2 is actually used
  67. event.preventDefault();
  68. break;
  69. }
  70. }
  71. };
  72. this.onPointerCancel = () => {
  73. this._touchStart.splice( 0, this._touchStart.length );
  74. this._touchCurrent.splice( 0, this._touchCurrent.length );
  75. this._input = INPUT.NONE;
  76. };
  77. this.onPointerDown = event => {
  78. if ( event.button == 0 && event.isPrimary ) {
  79. this._downValid = true;
  80. this._downEvents.push( event );
  81. this._downStart = performance.now();
  82. } else {
  83. this._downValid = false;
  84. }
  85. if ( event.pointerType == 'touch' && this._input != INPUT.CURSOR ) {
  86. this._touchStart.push( event );
  87. this._touchCurrent.push( event );
  88. switch ( this._input ) {
  89. case INPUT.NONE:
  90. //singleStart
  91. this._input = INPUT.ONE_FINGER;
  92. this.onSinglePanStart( event, 'ROTATE' );
  93. window.addEventListener( 'pointermove', this.onPointerMove );
  94. window.addEventListener( 'pointerup', this.onPointerUp );
  95. break;
  96. case INPUT.ONE_FINGER:
  97. case INPUT.ONE_FINGER_SWITCHED:
  98. //doubleStart
  99. this._input = INPUT.TWO_FINGER;
  100. this.onRotateStart();
  101. this.onPinchStart();
  102. this.onDoublePanStart();
  103. break;
  104. case INPUT.TWO_FINGER:
  105. //multipleStart
  106. this._input = INPUT.MULT_FINGER;
  107. this.onTriplePanStart( event );
  108. break;
  109. }
  110. } else if ( event.pointerType != 'touch' && this._input == INPUT.NONE ) {
  111. let modifier = null;
  112. if ( event.ctrlKey || event.metaKey ) {
  113. modifier = 'CTRL';
  114. } else if ( event.shiftKey ) {
  115. modifier = 'SHIFT';
  116. }
  117. this._mouseOp = this.getOpFromAction( event.button, modifier );
  118. if ( this._mouseOp != null ) {
  119. window.addEventListener( 'pointermove', this.onPointerMove );
  120. window.addEventListener( 'pointerup', this.onPointerUp ); //singleStart
  121. this._input = INPUT.CURSOR;
  122. this._button = event.button;
  123. this.onSinglePanStart( event, this._mouseOp );
  124. }
  125. }
  126. };
  127. this.onPointerMove = event => {
  128. if ( event.pointerType == 'touch' && this._input != INPUT.CURSOR ) {
  129. switch ( this._input ) {
  130. case INPUT.ONE_FINGER:
  131. //singleMove
  132. this.updateTouchEvent( event );
  133. this.onSinglePanMove( event, STATE.ROTATE );
  134. break;
  135. case INPUT.ONE_FINGER_SWITCHED:
  136. const movement = this.calculatePointersDistance( this._touchCurrent[ 0 ], event ) * this._devPxRatio;
  137. if ( movement >= this._switchSensibility ) {
  138. //singleMove
  139. this._input = INPUT.ONE_FINGER;
  140. this.updateTouchEvent( event );
  141. this.onSinglePanStart( event, 'ROTATE' );
  142. break;
  143. }
  144. break;
  145. case INPUT.TWO_FINGER:
  146. //rotate/pan/pinchMove
  147. this.updateTouchEvent( event );
  148. this.onRotateMove();
  149. this.onPinchMove();
  150. this.onDoublePanMove();
  151. break;
  152. case INPUT.MULT_FINGER:
  153. //multMove
  154. this.updateTouchEvent( event );
  155. this.onTriplePanMove( event );
  156. break;
  157. }
  158. } else if ( event.pointerType != 'touch' && this._input == INPUT.CURSOR ) {
  159. let modifier = null;
  160. if ( event.ctrlKey || event.metaKey ) {
  161. modifier = 'CTRL';
  162. } else if ( event.shiftKey ) {
  163. modifier = 'SHIFT';
  164. }
  165. const mouseOpState = this.getOpStateFromAction( this._button, modifier );
  166. if ( mouseOpState != null ) {
  167. this.onSinglePanMove( event, mouseOpState );
  168. }
  169. } //checkDistance
  170. if ( this._downValid ) {
  171. const movement = this.calculatePointersDistance( this._downEvents[ this._downEvents.length - 1 ], event ) * this._devPxRatio;
  172. if ( movement > this._movementThreshold ) {
  173. this._downValid = false;
  174. }
  175. }
  176. };
  177. this.onPointerUp = event => {
  178. if ( event.pointerType == 'touch' && this._input != INPUT.CURSOR ) {
  179. const nTouch = this._touchCurrent.length;
  180. for ( let i = 0; i < nTouch; i ++ ) {
  181. if ( this._touchCurrent[ i ].pointerId == event.pointerId ) {
  182. this._touchCurrent.splice( i, 1 );
  183. this._touchStart.splice( i, 1 );
  184. break;
  185. }
  186. }
  187. switch ( this._input ) {
  188. case INPUT.ONE_FINGER:
  189. case INPUT.ONE_FINGER_SWITCHED:
  190. //singleEnd
  191. window.removeEventListener( 'pointermove', this.onPointerMove );
  192. window.removeEventListener( 'pointerup', this.onPointerUp );
  193. this._input = INPUT.NONE;
  194. this.onSinglePanEnd();
  195. break;
  196. case INPUT.TWO_FINGER:
  197. //doubleEnd
  198. this.onDoublePanEnd( event );
  199. this.onPinchEnd( event );
  200. this.onRotateEnd( event ); //switching to singleStart
  201. this._input = INPUT.ONE_FINGER_SWITCHED;
  202. break;
  203. case INPUT.MULT_FINGER:
  204. if ( this._touchCurrent.length == 0 ) {
  205. window.removeEventListener( 'pointermove', this.onPointerMove );
  206. window.removeEventListener( 'pointerup', this.onPointerUp ); //multCancel
  207. this._input = INPUT.NONE;
  208. this.onTriplePanEnd();
  209. }
  210. break;
  211. }
  212. } else if ( event.pointerType != 'touch' && this._input == INPUT.CURSOR ) {
  213. window.removeEventListener( 'pointermove', this.onPointerMove );
  214. window.removeEventListener( 'pointerup', this.onPointerUp );
  215. this._input = INPUT.NONE;
  216. this.onSinglePanEnd();
  217. this._button = - 1;
  218. }
  219. if ( event.isPrimary ) {
  220. if ( this._downValid ) {
  221. const downTime = event.timeStamp - this._downEvents[ this._downEvents.length - 1 ].timeStamp;
  222. if ( downTime <= this._maxDownTime ) {
  223. if ( this._nclicks == 0 ) {
  224. //first valid click detected
  225. this._nclicks = 1;
  226. this._clickStart = performance.now();
  227. } else {
  228. const clickInterval = event.timeStamp - this._clickStart;
  229. const movement = this.calculatePointersDistance( this._downEvents[ 1 ], this._downEvents[ 0 ] ) * this._devPxRatio;
  230. if ( clickInterval <= this._maxInterval && movement <= this._posThreshold ) {
  231. //second valid click detected
  232. //fire double tap and reset values
  233. this._nclicks = 0;
  234. this._downEvents.splice( 0, this._downEvents.length );
  235. this.onDoubleTap( event );
  236. } else {
  237. //new 'first click'
  238. this._nclicks = 1;
  239. this._downEvents.shift();
  240. this._clickStart = performance.now();
  241. }
  242. }
  243. } else {
  244. this._downValid = false;
  245. this._nclicks = 0;
  246. this._downEvents.splice( 0, this._downEvents.length );
  247. }
  248. } else {
  249. this._nclicks = 0;
  250. this._downEvents.splice( 0, this._downEvents.length );
  251. }
  252. }
  253. };
  254. this.onWheel = event => {
  255. if ( this.enabled && this.enableZoom ) {
  256. let modifier = null;
  257. if ( event.ctrlKey || event.metaKey ) {
  258. modifier = 'CTRL';
  259. } else if ( event.shiftKey ) {
  260. modifier = 'SHIFT';
  261. }
  262. const mouseOp = this.getOpFromAction( 'WHEEL', modifier );
  263. if ( mouseOp != null ) {
  264. event.preventDefault();
  265. this.dispatchEvent( _startEvent );
  266. const notchDeltaY = 125; //distance of one notch of mouse wheel
  267. let sgn = event.deltaY / notchDeltaY;
  268. let size = 1;
  269. if ( sgn > 0 ) {
  270. size = 1 / this.scaleFactor;
  271. } else if ( sgn < 0 ) {
  272. size = this.scaleFactor;
  273. }
  274. switch ( mouseOp ) {
  275. case 'ZOOM':
  276. this.updateTbState( STATE.SCALE, true );
  277. if ( sgn > 0 ) {
  278. size = 1 / Math.pow( this.scaleFactor, sgn );
  279. } else if ( sgn < 0 ) {
  280. size = Math.pow( this.scaleFactor, - sgn );
  281. }
  282. if ( this.cursorZoom && this.enablePan ) {
  283. let scalePoint;
  284. if ( this.camera.isOrthographicCamera ) {
  285. scalePoint = this.unprojectOnTbPlane( this.camera, event.clientX, event.clientY, this.domElement ).applyQuaternion( this.camera.quaternion ).multiplyScalar( 1 / this.camera.zoom ).add( this._gizmos.position );
  286. } else if ( this.camera.isPerspectiveCamera ) {
  287. scalePoint = this.unprojectOnTbPlane( this.camera, event.clientX, event.clientY, this.domElement ).applyQuaternion( this.camera.quaternion ).add( this._gizmos.position );
  288. }
  289. this.applyTransformMatrix( this.scale( size, scalePoint ) );
  290. } else {
  291. this.applyTransformMatrix( this.scale( size, this._gizmos.position ) );
  292. }
  293. if ( this._grid != null ) {
  294. this.disposeGrid();
  295. this.drawGrid();
  296. }
  297. this.updateTbState( STATE.IDLE, false );
  298. this.dispatchEvent( _changeEvent );
  299. this.dispatchEvent( _endEvent );
  300. break;
  301. case 'FOV':
  302. if ( this.camera.isPerspectiveCamera ) {
  303. this.updateTbState( STATE.FOV, true ); //Vertigo effect
  304. // fov / 2
  305. // |\
  306. // | \
  307. // | \
  308. // x | \
  309. // | \
  310. // | \
  311. // | _ _ _\
  312. // y
  313. //check for iOs shift shortcut
  314. if ( event.deltaX != 0 ) {
  315. sgn = event.deltaX / notchDeltaY;
  316. size = 1;
  317. if ( sgn > 0 ) {
  318. size = 1 / Math.pow( this.scaleFactor, sgn );
  319. } else if ( sgn < 0 ) {
  320. size = Math.pow( this.scaleFactor, - sgn );
  321. }
  322. }
  323. this._v3_1.setFromMatrixPosition( this._cameraMatrixState );
  324. const x = this._v3_1.distanceTo( this._gizmos.position );
  325. let xNew = x / size; //distance between camera and gizmos if scale(size, scalepoint) would be performed
  326. //check min and max distance
  327. xNew = THREE.MathUtils.clamp( xNew, this.minDistance, this.maxDistance );
  328. const y = x * Math.tan( THREE.MathUtils.DEG2RAD * this.camera.fov * 0.5 ); //calculate new fov
  329. let newFov = THREE.MathUtils.RAD2DEG * ( Math.atan( y / xNew ) * 2 ); //check min and max fov
  330. if ( newFov > this.maxFov ) {
  331. newFov = this.maxFov;
  332. } else if ( newFov < this.minFov ) {
  333. newFov = this.minFov;
  334. }
  335. const newDistance = y / Math.tan( THREE.MathUtils.DEG2RAD * ( newFov / 2 ) );
  336. size = x / newDistance;
  337. this.setFov( newFov );
  338. this.applyTransformMatrix( this.scale( size, this._gizmos.position, false ) );
  339. }
  340. if ( this._grid != null ) {
  341. this.disposeGrid();
  342. this.drawGrid();
  343. }
  344. this.updateTbState( STATE.IDLE, false );
  345. this.dispatchEvent( _changeEvent );
  346. this.dispatchEvent( _endEvent );
  347. break;
  348. }
  349. }
  350. }
  351. };
  352. this.onKeyDown = event => {
  353. if ( event.key == 'c' ) {
  354. if ( event.ctrlKey || event.metaKey ) {
  355. this.copyState();
  356. }
  357. } else if ( event.key == 'v' ) {
  358. if ( event.ctrlKey || event.metaKey ) {
  359. this.pasteState();
  360. }
  361. }
  362. };
  363. this.onSinglePanStart = ( event, operation ) => {
  364. if ( this.enabled ) {
  365. this.dispatchEvent( _startEvent );
  366. this.setCenter( event.clientX, event.clientY );
  367. switch ( operation ) {
  368. case 'PAN':
  369. if ( ! this.enablePan ) {
  370. return;
  371. }
  372. if ( this._animationId != - 1 ) {
  373. cancelAnimationFrame( this._animationId );
  374. this._animationId = - 1;
  375. this._timeStart = - 1;
  376. this.activateGizmos( false );
  377. this.dispatchEvent( _changeEvent );
  378. }
  379. this.updateTbState( STATE.PAN, true );
  380. this._startCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ) );
  381. if ( this.enableGrid ) {
  382. this.drawGrid();
  383. this.dispatchEvent( _changeEvent );
  384. }
  385. break;
  386. case 'ROTATE':
  387. if ( ! this.enableRotate ) {
  388. return;
  389. }
  390. if ( this._animationId != - 1 ) {
  391. cancelAnimationFrame( this._animationId );
  392. this._animationId = - 1;
  393. this._timeStart = - 1;
  394. }
  395. this.updateTbState( STATE.ROTATE, true );
  396. this._startCursorPosition.copy( this.unprojectOnTbSurface( this.camera, _center.x, _center.y, this.domElement, this._tbRadius ) );
  397. this.activateGizmos( true );
  398. if ( this.enableAnimations ) {
  399. this._timePrev = this._timeCurrent = performance.now();
  400. this._angleCurrent = this._anglePrev = 0;
  401. this._cursorPosPrev.copy( this._startCursorPosition );
  402. this._cursorPosCurr.copy( this._cursorPosPrev );
  403. this._wCurr = 0;
  404. this._wPrev = this._wCurr;
  405. }
  406. this.dispatchEvent( _changeEvent );
  407. break;
  408. case 'FOV':
  409. if ( ! this.camera.isPerspectiveCamera || ! this.enableZoom ) {
  410. return;
  411. }
  412. if ( this._animationId != - 1 ) {
  413. cancelAnimationFrame( this._animationId );
  414. this._animationId = - 1;
  415. this._timeStart = - 1;
  416. this.activateGizmos( false );
  417. this.dispatchEvent( _changeEvent );
  418. }
  419. this.updateTbState( STATE.FOV, true );
  420. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  421. this._currentCursorPosition.copy( this._startCursorPosition );
  422. break;
  423. case 'ZOOM':
  424. if ( ! this.enableZoom ) {
  425. return;
  426. }
  427. if ( this._animationId != - 1 ) {
  428. cancelAnimationFrame( this._animationId );
  429. this._animationId = - 1;
  430. this._timeStart = - 1;
  431. this.activateGizmos( false );
  432. this.dispatchEvent( _changeEvent );
  433. }
  434. this.updateTbState( STATE.SCALE, true );
  435. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  436. this._currentCursorPosition.copy( this._startCursorPosition );
  437. break;
  438. }
  439. }
  440. };
  441. this.onSinglePanMove = ( event, opState ) => {
  442. if ( this.enabled ) {
  443. const restart = opState != this._state;
  444. this.setCenter( event.clientX, event.clientY );
  445. switch ( opState ) {
  446. case STATE.PAN:
  447. if ( this.enablePan ) {
  448. if ( restart ) {
  449. //switch to pan operation
  450. this.dispatchEvent( _endEvent );
  451. this.dispatchEvent( _startEvent );
  452. this.updateTbState( opState, true );
  453. this._startCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ) );
  454. if ( this.enableGrid ) {
  455. this.drawGrid();
  456. }
  457. this.activateGizmos( false );
  458. } else {
  459. //continue with pan operation
  460. this._currentCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ) );
  461. this.applyTransformMatrix( this.pan( this._startCursorPosition, this._currentCursorPosition ) );
  462. }
  463. }
  464. break;
  465. case STATE.ROTATE:
  466. if ( this.enableRotate ) {
  467. if ( restart ) {
  468. //switch to rotate operation
  469. this.dispatchEvent( _endEvent );
  470. this.dispatchEvent( _startEvent );
  471. this.updateTbState( opState, true );
  472. this._startCursorPosition.copy( this.unprojectOnTbSurface( this.camera, _center.x, _center.y, this.domElement, this._tbRadius ) );
  473. if ( this.enableGrid ) {
  474. this.disposeGrid();
  475. }
  476. this.activateGizmos( true );
  477. } else {
  478. //continue with rotate operation
  479. this._currentCursorPosition.copy( this.unprojectOnTbSurface( this.camera, _center.x, _center.y, this.domElement, this._tbRadius ) );
  480. const distance = this._startCursorPosition.distanceTo( this._currentCursorPosition );
  481. const angle = this._startCursorPosition.angleTo( this._currentCursorPosition );
  482. const amount = Math.max( distance / this._tbRadius, angle ); //effective rotation angle
  483. this.applyTransformMatrix( this.rotate( this.calculateRotationAxis( this._startCursorPosition, this._currentCursorPosition ), amount ) );
  484. if ( this.enableAnimations ) {
  485. this._timePrev = this._timeCurrent;
  486. this._timeCurrent = performance.now();
  487. this._anglePrev = this._angleCurrent;
  488. this._angleCurrent = amount;
  489. this._cursorPosPrev.copy( this._cursorPosCurr );
  490. this._cursorPosCurr.copy( this._currentCursorPosition );
  491. this._wPrev = this._wCurr;
  492. this._wCurr = this.calculateAngularSpeed( this._anglePrev, this._angleCurrent, this._timePrev, this._timeCurrent );
  493. }
  494. }
  495. }
  496. break;
  497. case STATE.SCALE:
  498. if ( this.enableZoom ) {
  499. if ( restart ) {
  500. //switch to zoom operation
  501. this.dispatchEvent( _endEvent );
  502. this.dispatchEvent( _startEvent );
  503. this.updateTbState( opState, true );
  504. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  505. this._currentCursorPosition.copy( this._startCursorPosition );
  506. if ( this.enableGrid ) {
  507. this.disposeGrid();
  508. }
  509. this.activateGizmos( false );
  510. } else {
  511. //continue with zoom operation
  512. const screenNotches = 8; //how many wheel notches corresponds to a full screen pan
  513. this._currentCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  514. const movement = this._currentCursorPosition.y - this._startCursorPosition.y;
  515. let size = 1;
  516. if ( movement < 0 ) {
  517. size = 1 / Math.pow( this.scaleFactor, - movement * screenNotches );
  518. } else if ( movement > 0 ) {
  519. size = Math.pow( this.scaleFactor, movement * screenNotches );
  520. }
  521. this.applyTransformMatrix( this.scale( size, this._gizmos.position ) );
  522. }
  523. }
  524. break;
  525. case STATE.FOV:
  526. if ( this.enableZoom && this.camera.isPerspectiveCamera ) {
  527. if ( restart ) {
  528. //switch to fov operation
  529. this.dispatchEvent( _endEvent );
  530. this.dispatchEvent( _startEvent );
  531. this.updateTbState( opState, true );
  532. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  533. this._currentCursorPosition.copy( this._startCursorPosition );
  534. if ( this.enableGrid ) {
  535. this.disposeGrid();
  536. }
  537. this.activateGizmos( false );
  538. } else {
  539. //continue with fov operation
  540. const screenNotches = 8; //how many wheel notches corresponds to a full screen pan
  541. this._currentCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  542. const movement = this._currentCursorPosition.y - this._startCursorPosition.y;
  543. let size = 1;
  544. if ( movement < 0 ) {
  545. size = 1 / Math.pow( this.scaleFactor, - movement * screenNotches );
  546. } else if ( movement > 0 ) {
  547. size = Math.pow( this.scaleFactor, movement * screenNotches );
  548. }
  549. this._v3_1.setFromMatrixPosition( this._cameraMatrixState );
  550. const x = this._v3_1.distanceTo( this._gizmos.position );
  551. let xNew = x / size; //distance between camera and gizmos if scale(size, scalepoint) would be performed
  552. //check min and max distance
  553. xNew = THREE.MathUtils.clamp( xNew, this.minDistance, this.maxDistance );
  554. const y = x * Math.tan( THREE.MathUtils.DEG2RAD * this._fovState * 0.5 ); //calculate new fov
  555. let newFov = THREE.MathUtils.RAD2DEG * ( Math.atan( y / xNew ) * 2 ); //check min and max fov
  556. newFov = THREE.MathUtils.clamp( newFov, this.minFov, this.maxFov );
  557. const newDistance = y / Math.tan( THREE.MathUtils.DEG2RAD * ( newFov / 2 ) );
  558. size = x / newDistance;
  559. this._v3_2.setFromMatrixPosition( this._gizmoMatrixState );
  560. this.setFov( newFov );
  561. this.applyTransformMatrix( this.scale( size, this._v3_2, false ) ); //adjusting distance
  562. const direction = this._gizmos.position.clone().sub( this.camera.position ).normalize().multiplyScalar( newDistance / x );
  563. this._m4_1.makeTranslation( direction.x, direction.y, direction.z );
  564. }
  565. }
  566. break;
  567. }
  568. this.dispatchEvent( _changeEvent );
  569. }
  570. };
  571. this.onSinglePanEnd = () => {
  572. if ( this._state == STATE.ROTATE ) {
  573. if ( ! this.enableRotate ) {
  574. return;
  575. }
  576. if ( this.enableAnimations ) {
  577. //perform rotation animation
  578. const deltaTime = performance.now() - this._timeCurrent;
  579. if ( deltaTime < 120 ) {
  580. const w = Math.abs( ( this._wPrev + this._wCurr ) / 2 );
  581. const self = this;
  582. this._animationId = window.requestAnimationFrame( function ( t ) {
  583. self.updateTbState( STATE.ANIMATION_ROTATE, true );
  584. const rotationAxis = self.calculateRotationAxis( self._cursorPosPrev, self._cursorPosCurr );
  585. self.onRotationAnim( t, rotationAxis, Math.min( w, self.wMax ) );
  586. } );
  587. } else {
  588. //cursor has been standing still for over 120 ms since last movement
  589. this.updateTbState( STATE.IDLE, false );
  590. this.activateGizmos( false );
  591. this.dispatchEvent( _changeEvent );
  592. }
  593. } else {
  594. this.updateTbState( STATE.IDLE, false );
  595. this.activateGizmos( false );
  596. this.dispatchEvent( _changeEvent );
  597. }
  598. } else if ( this._state == STATE.PAN || this._state == STATE.IDLE ) {
  599. this.updateTbState( STATE.IDLE, false );
  600. if ( this.enableGrid ) {
  601. this.disposeGrid();
  602. }
  603. this.activateGizmos( false );
  604. this.dispatchEvent( _changeEvent );
  605. }
  606. this.dispatchEvent( _endEvent );
  607. };
  608. this.onDoubleTap = event => {
  609. if ( this.enabled && this.enablePan && this.scene != null ) {
  610. this.dispatchEvent( _startEvent );
  611. this.setCenter( event.clientX, event.clientY );
  612. const hitP = this.unprojectOnObj( this.getCursorNDC( _center.x, _center.y, this.domElement ), this.camera );
  613. if ( hitP != null && this.enableAnimations ) {
  614. const self = this;
  615. if ( this._animationId != - 1 ) {
  616. window.cancelAnimationFrame( this._animationId );
  617. }
  618. this._timeStart = - 1;
  619. this._animationId = window.requestAnimationFrame( function ( t ) {
  620. self.updateTbState( STATE.ANIMATION_FOCUS, true );
  621. self.onFocusAnim( t, hitP, self._cameraMatrixState, self._gizmoMatrixState );
  622. } );
  623. } else if ( hitP != null && ! this.enableAnimations ) {
  624. this.updateTbState( STATE.FOCUS, true );
  625. this.focus( hitP, this.scaleFactor );
  626. this.updateTbState( STATE.IDLE, false );
  627. this.dispatchEvent( _changeEvent );
  628. }
  629. }
  630. this.dispatchEvent( _endEvent );
  631. };
  632. this.onDoublePanStart = () => {
  633. if ( this.enabled && this.enablePan ) {
  634. this.dispatchEvent( _startEvent );
  635. this.updateTbState( STATE.PAN, true );
  636. this.setCenter( ( this._touchCurrent[ 0 ].clientX + this._touchCurrent[ 1 ].clientX ) / 2, ( this._touchCurrent[ 0 ].clientY + this._touchCurrent[ 1 ].clientY ) / 2 );
  637. this._startCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement, true ) );
  638. this._currentCursorPosition.copy( this._startCursorPosition );
  639. this.activateGizmos( false );
  640. }
  641. };
  642. this.onDoublePanMove = () => {
  643. if ( this.enabled && this.enablePan ) {
  644. this.setCenter( ( this._touchCurrent[ 0 ].clientX + this._touchCurrent[ 1 ].clientX ) / 2, ( this._touchCurrent[ 0 ].clientY + this._touchCurrent[ 1 ].clientY ) / 2 );
  645. if ( this._state != STATE.PAN ) {
  646. this.updateTbState( STATE.PAN, true );
  647. this._startCursorPosition.copy( this._currentCursorPosition );
  648. }
  649. this._currentCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement, true ) );
  650. this.applyTransformMatrix( this.pan( this._startCursorPosition, this._currentCursorPosition, true ) );
  651. this.dispatchEvent( _changeEvent );
  652. }
  653. };
  654. this.onDoublePanEnd = () => {
  655. this.updateTbState( STATE.IDLE, false );
  656. this.dispatchEvent( _endEvent );
  657. };
  658. this.onRotateStart = () => {
  659. if ( this.enabled && this.enableRotate ) {
  660. this.dispatchEvent( _startEvent );
  661. this.updateTbState( STATE.ZROTATE, true ); //this._startFingerRotation = event.rotation;
  662. this._startFingerRotation = this.getAngle( this._touchCurrent[ 1 ], this._touchCurrent[ 0 ] ) + this.getAngle( this._touchStart[ 1 ], this._touchStart[ 0 ] );
  663. this._currentFingerRotation = this._startFingerRotation;
  664. this.camera.getWorldDirection( this._rotationAxis ); //rotation axis
  665. if ( ! this.enablePan && ! this.enableZoom ) {
  666. this.activateGizmos( true );
  667. }
  668. }
  669. };
  670. this.onRotateMove = () => {
  671. if ( this.enabled && this.enableRotate ) {
  672. this.setCenter( ( this._touchCurrent[ 0 ].clientX + this._touchCurrent[ 1 ].clientX ) / 2, ( this._touchCurrent[ 0 ].clientY + this._touchCurrent[ 1 ].clientY ) / 2 );
  673. let rotationPoint;
  674. if ( this._state != STATE.ZROTATE ) {
  675. this.updateTbState( STATE.ZROTATE, true );
  676. this._startFingerRotation = this._currentFingerRotation;
  677. } //this._currentFingerRotation = event.rotation;
  678. this._currentFingerRotation = this.getAngle( this._touchCurrent[ 1 ], this._touchCurrent[ 0 ] ) + this.getAngle( this._touchStart[ 1 ], this._touchStart[ 0 ] );
  679. if ( ! this.enablePan ) {
  680. rotationPoint = new THREE.Vector3().setFromMatrixPosition( this._gizmoMatrixState );
  681. } else {
  682. this._v3_2.setFromMatrixPosition( this._gizmoMatrixState );
  683. rotationPoint = this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ).applyQuaternion( this.camera.quaternion ).multiplyScalar( 1 / this.camera.zoom ).add( this._v3_2 );
  684. }
  685. const amount = THREE.MathUtils.DEG2RAD * ( this._startFingerRotation - this._currentFingerRotation );
  686. this.applyTransformMatrix( this.zRotate( rotationPoint, amount ) );
  687. this.dispatchEvent( _changeEvent );
  688. }
  689. };
  690. this.onRotateEnd = () => {
  691. this.updateTbState( STATE.IDLE, false );
  692. this.activateGizmos( false );
  693. this.dispatchEvent( _endEvent );
  694. };
  695. this.onPinchStart = () => {
  696. if ( this.enabled && this.enableZoom ) {
  697. this.dispatchEvent( _startEvent );
  698. this.updateTbState( STATE.SCALE, true );
  699. this._startFingerDistance = this.calculatePointersDistance( this._touchCurrent[ 0 ], this._touchCurrent[ 1 ] );
  700. this._currentFingerDistance = this._startFingerDistance;
  701. this.activateGizmos( false );
  702. }
  703. };
  704. this.onPinchMove = () => {
  705. if ( this.enabled && this.enableZoom ) {
  706. this.setCenter( ( this._touchCurrent[ 0 ].clientX + this._touchCurrent[ 1 ].clientX ) / 2, ( this._touchCurrent[ 0 ].clientY + this._touchCurrent[ 1 ].clientY ) / 2 );
  707. const minDistance = 12; //minimum distance between fingers (in css pixels)
  708. if ( this._state != STATE.SCALE ) {
  709. this._startFingerDistance = this._currentFingerDistance;
  710. this.updateTbState( STATE.SCALE, true );
  711. }
  712. this._currentFingerDistance = Math.max( this.calculatePointersDistance( this._touchCurrent[ 0 ], this._touchCurrent[ 1 ] ), minDistance * this._devPxRatio );
  713. const amount = this._currentFingerDistance / this._startFingerDistance;
  714. let scalePoint;
  715. if ( ! this.enablePan ) {
  716. scalePoint = this._gizmos.position;
  717. } else {
  718. if ( this.camera.isOrthographicCamera ) {
  719. scalePoint = this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ).applyQuaternion( this.camera.quaternion ).multiplyScalar( 1 / this.camera.zoom ).add( this._gizmos.position );
  720. } else if ( this.camera.isPerspectiveCamera ) {
  721. scalePoint = this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ).applyQuaternion( this.camera.quaternion ).add( this._gizmos.position );
  722. }
  723. }
  724. this.applyTransformMatrix( this.scale( amount, scalePoint ) );
  725. this.dispatchEvent( _changeEvent );
  726. }
  727. };
  728. this.onPinchEnd = () => {
  729. this.updateTbState( STATE.IDLE, false );
  730. this.dispatchEvent( _endEvent );
  731. };
  732. this.onTriplePanStart = () => {
  733. if ( this.enabled && this.enableZoom ) {
  734. this.dispatchEvent( _startEvent );
  735. this.updateTbState( STATE.SCALE, true ); //const center = event.center;
  736. let clientX = 0;
  737. let clientY = 0;
  738. const nFingers = this._touchCurrent.length;
  739. for ( let i = 0; i < nFingers; i ++ ) {
  740. clientX += this._touchCurrent[ i ].clientX;
  741. clientY += this._touchCurrent[ i ].clientY;
  742. }
  743. this.setCenter( clientX / nFingers, clientY / nFingers );
  744. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  745. this._currentCursorPosition.copy( this._startCursorPosition );
  746. }
  747. };
  748. this.onTriplePanMove = () => {
  749. if ( this.enabled && this.enableZoom ) {
  750. // fov / 2
  751. // |\
  752. // | \
  753. // | \
  754. // x | \
  755. // | \
  756. // | \
  757. // | _ _ _\
  758. // y
  759. //const center = event.center;
  760. let clientX = 0;
  761. let clientY = 0;
  762. const nFingers = this._touchCurrent.length;
  763. for ( let i = 0; i < nFingers; i ++ ) {
  764. clientX += this._touchCurrent[ i ].clientX;
  765. clientY += this._touchCurrent[ i ].clientY;
  766. }
  767. this.setCenter( clientX / nFingers, clientY / nFingers );
  768. const screenNotches = 8; //how many wheel notches corresponds to a full screen pan
  769. this._currentCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  770. const movement = this._currentCursorPosition.y - this._startCursorPosition.y;
  771. let size = 1;
  772. if ( movement < 0 ) {
  773. size = 1 / Math.pow( this.scaleFactor, - movement * screenNotches );
  774. } else if ( movement > 0 ) {
  775. size = Math.pow( this.scaleFactor, movement * screenNotches );
  776. }
  777. this._v3_1.setFromMatrixPosition( this._cameraMatrixState );
  778. const x = this._v3_1.distanceTo( this._gizmos.position );
  779. let xNew = x / size; //distance between camera and gizmos if scale(size, scalepoint) would be performed
  780. //check min and max distance
  781. xNew = THREE.MathUtils.clamp( xNew, this.minDistance, this.maxDistance );
  782. const y = x * Math.tan( THREE.MathUtils.DEG2RAD * this._fovState * 0.5 ); //calculate new fov
  783. let newFov = THREE.MathUtils.RAD2DEG * ( Math.atan( y / xNew ) * 2 ); //check min and max fov
  784. newFov = THREE.MathUtils.clamp( newFov, this.minFov, this.maxFov );
  785. const newDistance = y / Math.tan( THREE.MathUtils.DEG2RAD * ( newFov / 2 ) );
  786. size = x / newDistance;
  787. this._v3_2.setFromMatrixPosition( this._gizmoMatrixState );
  788. this.setFov( newFov );
  789. this.applyTransformMatrix( this.scale( size, this._v3_2, false ) ); //adjusting distance
  790. const direction = this._gizmos.position.clone().sub( this.camera.position ).normalize().multiplyScalar( newDistance / x );
  791. this._m4_1.makeTranslation( direction.x, direction.y, direction.z );
  792. this.dispatchEvent( _changeEvent );
  793. }
  794. };
  795. this.onTriplePanEnd = () => {
  796. this.updateTbState( STATE.IDLE, false );
  797. this.dispatchEvent( _endEvent ); //this.dispatchEvent( _changeEvent );
  798. };
  799. this.setCenter = ( clientX, clientY ) => {
  800. _center.x = clientX;
  801. _center.y = clientY;
  802. };
  803. this.initializeMouseActions = () => {
  804. this.setMouseAction( 'PAN', 0, 'CTRL' );
  805. this.setMouseAction( 'PAN', 2 );
  806. this.setMouseAction( 'ROTATE', 0 );
  807. this.setMouseAction( 'ZOOM', 'WHEEL' );
  808. this.setMouseAction( 'ZOOM', 1 );
  809. this.setMouseAction( 'FOV', 'WHEEL', 'SHIFT' );
  810. this.setMouseAction( 'FOV', 1, 'SHIFT' );
  811. };
  812. this.compareMouseAction = ( action1, action2 ) => {
  813. if ( action1.operation == action2.operation ) {
  814. if ( action1.mouse == action2.mouse && action1.key == action2.key ) {
  815. return true;
  816. } else {
  817. return false;
  818. }
  819. } else {
  820. return false;
  821. }
  822. };
  823. this.setMouseAction = ( operation, mouse, key = null ) => {
  824. const operationInput = [ 'PAN', 'ROTATE', 'ZOOM', 'FOV' ];
  825. const mouseInput = [ 0, 1, 2, 'WHEEL' ];
  826. const keyInput = [ 'CTRL', 'SHIFT', null ];
  827. let state;
  828. if ( ! operationInput.includes( operation ) || ! mouseInput.includes( mouse ) || ! keyInput.includes( key ) ) {
  829. //invalid parameters
  830. return false;
  831. }
  832. if ( mouse == 'WHEEL' ) {
  833. if ( operation != 'ZOOM' && operation != 'FOV' ) {
  834. //cannot associate 2D operation to 1D input
  835. return false;
  836. }
  837. }
  838. switch ( operation ) {
  839. case 'PAN':
  840. state = STATE.PAN;
  841. break;
  842. case 'ROTATE':
  843. state = STATE.ROTATE;
  844. break;
  845. case 'ZOOM':
  846. state = STATE.SCALE;
  847. break;
  848. case 'FOV':
  849. state = STATE.FOV;
  850. break;
  851. }
  852. const action = {
  853. operation: operation,
  854. mouse: mouse,
  855. key: key,
  856. state: state
  857. };
  858. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  859. if ( this.mouseActions[ i ].mouse == action.mouse && this.mouseActions[ i ].key == action.key ) {
  860. this.mouseActions.splice( i, 1, action );
  861. return true;
  862. }
  863. }
  864. this.mouseActions.push( action );
  865. return true;
  866. };
  867. this.unsetMouseAction = ( mouse, key = null ) => {
  868. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  869. if ( this.mouseActions[ i ].mouse == mouse && this.mouseActions[ i ].key == key ) {
  870. this.mouseActions.splice( i, 1 );
  871. return true;
  872. }
  873. }
  874. return false;
  875. };
  876. this.getOpFromAction = ( mouse, key ) => {
  877. let action;
  878. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  879. action = this.mouseActions[ i ];
  880. if ( action.mouse == mouse && action.key == key ) {
  881. return action.operation;
  882. }
  883. }
  884. if ( key != null ) {
  885. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  886. action = this.mouseActions[ i ];
  887. if ( action.mouse == mouse && action.key == null ) {
  888. return action.operation;
  889. }
  890. }
  891. }
  892. return null;
  893. };
  894. this.getOpStateFromAction = ( mouse, key ) => {
  895. let action;
  896. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  897. action = this.mouseActions[ i ];
  898. if ( action.mouse == mouse && action.key == key ) {
  899. return action.state;
  900. }
  901. }
  902. if ( key != null ) {
  903. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  904. action = this.mouseActions[ i ];
  905. if ( action.mouse == mouse && action.key == null ) {
  906. return action.state;
  907. }
  908. }
  909. }
  910. return null;
  911. };
  912. this.getAngle = ( p1, p2 ) => {
  913. return Math.atan2( p2.clientY - p1.clientY, p2.clientX - p1.clientX ) * 180 / Math.PI;
  914. };
  915. this.updateTouchEvent = event => {
  916. for ( let i = 0; i < this._touchCurrent.length; i ++ ) {
  917. if ( this._touchCurrent[ i ].pointerId == event.pointerId ) {
  918. this._touchCurrent.splice( i, 1, event );
  919. break;
  920. }
  921. }
  922. };
  923. this.calculateAngularSpeed = ( p0, p1, t0, t1 ) => {
  924. const s = p1 - p0;
  925. const t = ( t1 - t0 ) / 1000;
  926. if ( t == 0 ) {
  927. return 0;
  928. }
  929. return s / t;
  930. };
  931. this.calculatePointersDistance = ( p0, p1 ) => {
  932. return Math.sqrt( Math.pow( p1.clientX - p0.clientX, 2 ) + Math.pow( p1.clientY - p0.clientY, 2 ) );
  933. };
  934. this.calculateRotationAxis = ( vec1, vec2 ) => {
  935. this._rotationMatrix.extractRotation( this._cameraMatrixState );
  936. this._quat.setFromRotationMatrix( this._rotationMatrix );
  937. this._rotationAxis.crossVectors( vec1, vec2 ).applyQuaternion( this._quat );
  938. return this._rotationAxis.normalize().clone();
  939. };
  940. this.calculateTbRadius = camera => {
  941. const factor = 0.67;
  942. const distance = camera.position.distanceTo( this._gizmos.position );
  943. if ( camera.type == 'PerspectiveCamera' ) {
  944. const halfFovV = THREE.MathUtils.DEG2RAD * camera.fov * 0.5; //vertical fov/2 in radians
  945. const halfFovH = Math.atan( camera.aspect * Math.tan( halfFovV ) ); //horizontal fov/2 in radians
  946. return Math.tan( Math.min( halfFovV, halfFovH ) ) * distance * factor;
  947. } else if ( camera.type == 'OrthographicCamera' ) {
  948. return Math.min( camera.top, camera.right ) * factor;
  949. }
  950. };
  951. this.focus = ( point, size, amount = 1 ) => {
  952. const focusPoint = point.clone(); //move center of camera (along with gizmos) towards point of interest
  953. focusPoint.sub( this._gizmos.position ).multiplyScalar( amount );
  954. this._translationMatrix.makeTranslation( focusPoint.x, focusPoint.y, focusPoint.z );
  955. const gizmoStateTemp = this._gizmoMatrixState.clone();
  956. this._gizmoMatrixState.premultiply( this._translationMatrix );
  957. this._gizmoMatrixState.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  958. const cameraStateTemp = this._cameraMatrixState.clone();
  959. this._cameraMatrixState.premultiply( this._translationMatrix );
  960. this._cameraMatrixState.decompose( this.camera.position, this.camera.quaternion, this.camera.scale ); //apply zoom
  961. if ( this.enableZoom ) {
  962. this.applyTransformMatrix( this.scale( size, this._gizmos.position ) );
  963. }
  964. this._gizmoMatrixState.copy( gizmoStateTemp );
  965. this._cameraMatrixState.copy( cameraStateTemp );
  966. };
  967. this.drawGrid = () => {
  968. if ( this.scene != null ) {
  969. const color = 0x888888;
  970. const multiplier = 3;
  971. let size, divisions, maxLength, tick;
  972. if ( this.camera.isOrthographicCamera ) {
  973. const width = this.camera.right - this.camera.left;
  974. const height = this.camera.bottom - this.camera.top;
  975. maxLength = Math.max( width, height );
  976. tick = maxLength / 20;
  977. size = maxLength / this.camera.zoom * multiplier;
  978. divisions = size / tick * this.camera.zoom;
  979. } else if ( this.camera.isPerspectiveCamera ) {
  980. const distance = this.camera.position.distanceTo( this._gizmos.position );
  981. const halfFovV = THREE.MathUtils.DEG2RAD * this.camera.fov * 0.5;
  982. const halfFovH = Math.atan( this.camera.aspect * Math.tan( halfFovV ) );
  983. maxLength = Math.tan( Math.max( halfFovV, halfFovH ) ) * distance * 2;
  984. tick = maxLength / 20;
  985. size = maxLength * multiplier;
  986. divisions = size / tick;
  987. }
  988. if ( this._grid == null ) {
  989. this._grid = new THREE.GridHelper( size, divisions, color, color );
  990. this._grid.position.copy( this._gizmos.position );
  991. this._gridPosition.copy( this._grid.position );
  992. this._grid.quaternion.copy( this.camera.quaternion );
  993. this._grid.rotateX( Math.PI * 0.5 );
  994. this.scene.add( this._grid );
  995. }
  996. }
  997. };
  998. this.dispose = () => {
  999. if ( this._animationId != - 1 ) {
  1000. window.cancelAnimationFrame( this._animationId );
  1001. }
  1002. this.domElement.removeEventListener( 'pointerdown', this.onPointerDown );
  1003. this.domElement.removeEventListener( 'pointercancel', this.onPointerCancel );
  1004. this.domElement.removeEventListener( 'wheel', this.onWheel );
  1005. this.domElement.removeEventListener( 'contextmenu', this.onContextMenu );
  1006. window.removeEventListener( 'pointermove', this.onPointerMove );
  1007. window.removeEventListener( 'pointerup', this.onPointerUp );
  1008. window.removeEventListener( 'resize', this.onWindowResize );
  1009. window.removeEventListener( 'keydown', this.onKeyDown );
  1010. if ( this.scene !== null ) this.scene.remove( this._gizmos );
  1011. this.disposeGrid();
  1012. };
  1013. this.disposeGrid = () => {
  1014. if ( this._grid != null && this.scene != null ) {
  1015. this.scene.remove( this._grid );
  1016. this._grid = null;
  1017. }
  1018. };
  1019. this.easeOutCubic = t => {
  1020. return 1 - Math.pow( 1 - t, 3 );
  1021. };
  1022. this.activateGizmos = isActive => {
  1023. const gizmoX = this._gizmos.children[ 0 ];
  1024. const gizmoY = this._gizmos.children[ 1 ];
  1025. const gizmoZ = this._gizmos.children[ 2 ];
  1026. if ( isActive ) {
  1027. gizmoX.material.setValues( {
  1028. opacity: 1
  1029. } );
  1030. gizmoY.material.setValues( {
  1031. opacity: 1
  1032. } );
  1033. gizmoZ.material.setValues( {
  1034. opacity: 1
  1035. } );
  1036. } else {
  1037. gizmoX.material.setValues( {
  1038. opacity: 0.6
  1039. } );
  1040. gizmoY.material.setValues( {
  1041. opacity: 0.6
  1042. } );
  1043. gizmoZ.material.setValues( {
  1044. opacity: 0.6
  1045. } );
  1046. }
  1047. };
  1048. this.getCursorNDC = ( cursorX, cursorY, canvas ) => {
  1049. const canvasRect = canvas.getBoundingClientRect();
  1050. this._v2_1.setX( ( cursorX - canvasRect.left ) / canvasRect.width * 2 - 1 );
  1051. this._v2_1.setY( ( canvasRect.bottom - cursorY ) / canvasRect.height * 2 - 1 );
  1052. return this._v2_1.clone();
  1053. };
  1054. this.getCursorPosition = ( cursorX, cursorY, canvas ) => {
  1055. this._v2_1.copy( this.getCursorNDC( cursorX, cursorY, canvas ) );
  1056. this._v2_1.x *= ( this.camera.right - this.camera.left ) * 0.5;
  1057. this._v2_1.y *= ( this.camera.top - this.camera.bottom ) * 0.5;
  1058. return this._v2_1.clone();
  1059. };
  1060. this.setCamera = camera => {
  1061. camera.lookAt( this.target );
  1062. camera.updateMatrix(); //setting state
  1063. if ( camera.type == 'PerspectiveCamera' ) {
  1064. this._fov0 = camera.fov;
  1065. this._fovState = camera.fov;
  1066. }
  1067. this._cameraMatrixState0.copy( camera.matrix );
  1068. this._cameraMatrixState.copy( this._cameraMatrixState0 );
  1069. this._cameraProjectionState.copy( camera.projectionMatrix );
  1070. this._zoom0 = camera.zoom;
  1071. this._zoomState = this._zoom0;
  1072. this._initialNear = camera.near;
  1073. this._nearPos0 = camera.position.distanceTo( this.target ) - camera.near;
  1074. this._nearPos = this._initialNear;
  1075. this._initialFar = camera.far;
  1076. this._farPos0 = camera.position.distanceTo( this.target ) - camera.far;
  1077. this._farPos = this._initialFar;
  1078. this._up0.copy( camera.up );
  1079. this._upState.copy( camera.up );
  1080. this.camera = camera;
  1081. this.camera.updateProjectionMatrix(); //making gizmos
  1082. this._tbRadius = this.calculateTbRadius( camera );
  1083. this.makeGizmos( this.target, this._tbRadius );
  1084. };
  1085. this.makeGizmos = ( tbCenter, tbRadius ) => {
  1086. const curve = new THREE.EllipseCurve( 0, 0, tbRadius, tbRadius );
  1087. const points = curve.getPoints( this._curvePts ); //geometry
  1088. const curveGeometry = new THREE.BufferGeometry().setFromPoints( points ); //material
  1089. const curveMaterialX = new THREE.LineBasicMaterial( {
  1090. color: 0xff8080,
  1091. fog: false,
  1092. transparent: true,
  1093. opacity: 0.6
  1094. } );
  1095. const curveMaterialY = new THREE.LineBasicMaterial( {
  1096. color: 0x80ff80,
  1097. fog: false,
  1098. transparent: true,
  1099. opacity: 0.6
  1100. } );
  1101. const curveMaterialZ = new THREE.LineBasicMaterial( {
  1102. color: 0x8080ff,
  1103. fog: false,
  1104. transparent: true,
  1105. opacity: 0.6
  1106. } ); //line
  1107. const gizmoX = new THREE.Line( curveGeometry, curveMaterialX );
  1108. const gizmoY = new THREE.Line( curveGeometry, curveMaterialY );
  1109. const gizmoZ = new THREE.Line( curveGeometry, curveMaterialZ );
  1110. const rotation = Math.PI * 0.5;
  1111. gizmoX.rotation.x = rotation;
  1112. gizmoY.rotation.y = rotation; //setting state
  1113. this._gizmoMatrixState0.identity().setPosition( tbCenter );
  1114. this._gizmoMatrixState.copy( this._gizmoMatrixState0 );
  1115. if ( this.camera.zoom != 1 ) {
  1116. //adapt gizmos size to camera zoom
  1117. const size = 1 / this.camera.zoom;
  1118. this._scaleMatrix.makeScale( size, size, size );
  1119. this._translationMatrix.makeTranslation( - tbCenter.x, - tbCenter.y, - tbCenter.z );
  1120. this._gizmoMatrixState.premultiply( this._translationMatrix ).premultiply( this._scaleMatrix );
  1121. this._translationMatrix.makeTranslation( tbCenter.x, tbCenter.y, tbCenter.z );
  1122. this._gizmoMatrixState.premultiply( this._translationMatrix );
  1123. }
  1124. this._gizmoMatrixState.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1125. this._gizmos.clear();
  1126. this._gizmos.add( gizmoX );
  1127. this._gizmos.add( gizmoY );
  1128. this._gizmos.add( gizmoZ );
  1129. };
  1130. this.onFocusAnim = ( time, point, cameraMatrix, gizmoMatrix ) => {
  1131. if ( this._timeStart == - 1 ) {
  1132. //animation start
  1133. this._timeStart = time;
  1134. }
  1135. if ( this._state == STATE.ANIMATION_FOCUS ) {
  1136. const deltaTime = time - this._timeStart;
  1137. const animTime = deltaTime / this.focusAnimationTime;
  1138. this._gizmoMatrixState.copy( gizmoMatrix );
  1139. if ( animTime >= 1 ) {
  1140. //animation end
  1141. this._gizmoMatrixState.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1142. this.focus( point, this.scaleFactor );
  1143. this._timeStart = - 1;
  1144. this.updateTbState( STATE.IDLE, false );
  1145. this.activateGizmos( false );
  1146. this.dispatchEvent( _changeEvent );
  1147. } else {
  1148. const amount = this.easeOutCubic( animTime );
  1149. const size = 1 - amount + this.scaleFactor * amount;
  1150. this._gizmoMatrixState.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1151. this.focus( point, size, amount );
  1152. this.dispatchEvent( _changeEvent );
  1153. const self = this;
  1154. this._animationId = window.requestAnimationFrame( function ( t ) {
  1155. self.onFocusAnim( t, point, cameraMatrix, gizmoMatrix.clone() );
  1156. } );
  1157. }
  1158. } else {
  1159. //interrupt animation
  1160. this._animationId = - 1;
  1161. this._timeStart = - 1;
  1162. }
  1163. };
  1164. this.onRotationAnim = ( time, rotationAxis, w0 ) => {
  1165. if ( this._timeStart == - 1 ) {
  1166. //animation start
  1167. this._anglePrev = 0;
  1168. this._angleCurrent = 0;
  1169. this._timeStart = time;
  1170. }
  1171. if ( this._state == STATE.ANIMATION_ROTATE ) {
  1172. //w = w0 + alpha * t
  1173. const deltaTime = ( time - this._timeStart ) / 1000;
  1174. const w = w0 + - this.dampingFactor * deltaTime;
  1175. if ( w > 0 ) {
  1176. //tetha = 0.5 * alpha * t^2 + w0 * t + tetha0
  1177. this._angleCurrent = 0.5 * - this.dampingFactor * Math.pow( deltaTime, 2 ) + w0 * deltaTime + 0;
  1178. this.applyTransformMatrix( this.rotate( rotationAxis, this._angleCurrent ) );
  1179. this.dispatchEvent( _changeEvent );
  1180. const self = this;
  1181. this._animationId = window.requestAnimationFrame( function ( t ) {
  1182. self.onRotationAnim( t, rotationAxis, w0 );
  1183. } );
  1184. } else {
  1185. this._animationId = - 1;
  1186. this._timeStart = - 1;
  1187. this.updateTbState( STATE.IDLE, false );
  1188. this.activateGizmos( false );
  1189. this.dispatchEvent( _changeEvent );
  1190. }
  1191. } else {
  1192. //interrupt animation
  1193. this._animationId = - 1;
  1194. this._timeStart = - 1;
  1195. if ( this._state != STATE.ROTATE ) {
  1196. this.activateGizmos( false );
  1197. this.dispatchEvent( _changeEvent );
  1198. }
  1199. }
  1200. };
  1201. this.pan = ( p0, p1, adjust = false ) => {
  1202. const movement = p0.clone().sub( p1 );
  1203. if ( this.camera.isOrthographicCamera ) {
  1204. //adjust movement amount
  1205. movement.multiplyScalar( 1 / this.camera.zoom );
  1206. } else if ( this.camera.isPerspectiveCamera && adjust ) {
  1207. //adjust movement amount
  1208. this._v3_1.setFromMatrixPosition( this._cameraMatrixState0 ); //camera's initial position
  1209. this._v3_2.setFromMatrixPosition( this._gizmoMatrixState0 ); //gizmo's initial position
  1210. const distanceFactor = this._v3_1.distanceTo( this._v3_2 ) / this.camera.position.distanceTo( this._gizmos.position );
  1211. movement.multiplyScalar( 1 / distanceFactor );
  1212. }
  1213. this._v3_1.set( movement.x, movement.y, 0 ).applyQuaternion( this.camera.quaternion );
  1214. this._m4_1.makeTranslation( this._v3_1.x, this._v3_1.y, this._v3_1.z );
  1215. this.setTransformationMatrices( this._m4_1, this._m4_1 );
  1216. return _transformation;
  1217. };
  1218. this.reset = () => {
  1219. this.camera.zoom = this._zoom0;
  1220. if ( this.camera.isPerspectiveCamera ) {
  1221. this.camera.fov = this._fov0;
  1222. }
  1223. this.camera.near = this._nearPos;
  1224. this.camera.far = this._farPos;
  1225. this._cameraMatrixState.copy( this._cameraMatrixState0 );
  1226. this._cameraMatrixState.decompose( this.camera.position, this.camera.quaternion, this.camera.scale );
  1227. this.camera.up.copy( this._up0 );
  1228. this.camera.updateMatrix();
  1229. this.camera.updateProjectionMatrix();
  1230. this._gizmoMatrixState.copy( this._gizmoMatrixState0 );
  1231. this._gizmoMatrixState0.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1232. this._gizmos.updateMatrix();
  1233. this._tbRadius = this.calculateTbRadius( this.camera );
  1234. this.makeGizmos( this._gizmos.position, this._tbRadius );
  1235. this.camera.lookAt( this._gizmos.position );
  1236. this.updateTbState( STATE.IDLE, false );
  1237. this.dispatchEvent( _changeEvent );
  1238. };
  1239. this.rotate = ( axis, angle ) => {
  1240. const point = this._gizmos.position; //rotation center
  1241. this._translationMatrix.makeTranslation( - point.x, - point.y, - point.z );
  1242. this._rotationMatrix.makeRotationAxis( axis, - angle ); //rotate camera
  1243. this._m4_1.makeTranslation( point.x, point.y, point.z );
  1244. this._m4_1.multiply( this._rotationMatrix );
  1245. this._m4_1.multiply( this._translationMatrix );
  1246. this.setTransformationMatrices( this._m4_1 );
  1247. return _transformation;
  1248. };
  1249. this.copyState = () => {
  1250. let state;
  1251. if ( this.camera.isOrthographicCamera ) {
  1252. state = JSON.stringify( {
  1253. arcballState: {
  1254. cameraFar: this.camera.far,
  1255. cameraMatrix: this.camera.matrix,
  1256. cameraNear: this.camera.near,
  1257. cameraUp: this.camera.up,
  1258. cameraZoom: this.camera.zoom,
  1259. gizmoMatrix: this._gizmos.matrix
  1260. }
  1261. } );
  1262. } else if ( this.camera.isPerspectiveCamera ) {
  1263. state = JSON.stringify( {
  1264. arcballState: {
  1265. cameraFar: this.camera.far,
  1266. cameraFov: this.camera.fov,
  1267. cameraMatrix: this.camera.matrix,
  1268. cameraNear: this.camera.near,
  1269. cameraUp: this.camera.up,
  1270. cameraZoom: this.camera.zoom,
  1271. gizmoMatrix: this._gizmos.matrix
  1272. }
  1273. } );
  1274. }
  1275. navigator.clipboard.writeText( state );
  1276. };
  1277. this.pasteState = () => {
  1278. const self = this;
  1279. navigator.clipboard.readText().then( function resolved( value ) {
  1280. self.setStateFromJSON( value );
  1281. } );
  1282. };
  1283. this.saveState = () => {
  1284. this._cameraMatrixState0.copy( this.camera.matrix );
  1285. this._gizmoMatrixState0.copy( this._gizmos.matrix );
  1286. this._nearPos = this.camera.near;
  1287. this._farPos = this.camera.far;
  1288. this._zoom0 = this.camera.zoom;
  1289. this._up0.copy( this.camera.up );
  1290. if ( this.camera.isPerspectiveCamera ) {
  1291. this._fov0 = this.camera.fov;
  1292. }
  1293. };
  1294. this.scale = ( size, point, scaleGizmos = true ) => {
  1295. const scalePoint = point.clone();
  1296. let sizeInverse = 1 / size;
  1297. if ( this.camera.isOrthographicCamera ) {
  1298. //camera zoom
  1299. this.camera.zoom = this._zoomState;
  1300. this.camera.zoom *= size; //check min and max zoom
  1301. if ( this.camera.zoom > this.maxZoom ) {
  1302. this.camera.zoom = this.maxZoom;
  1303. sizeInverse = this._zoomState / this.maxZoom;
  1304. } else if ( this.camera.zoom < this.minZoom ) {
  1305. this.camera.zoom = this.minZoom;
  1306. sizeInverse = this._zoomState / this.minZoom;
  1307. }
  1308. this.camera.updateProjectionMatrix();
  1309. this._v3_1.setFromMatrixPosition( this._gizmoMatrixState ); //gizmos position
  1310. //scale gizmos so they appear in the same spot having the same dimension
  1311. this._scaleMatrix.makeScale( sizeInverse, sizeInverse, sizeInverse );
  1312. this._translationMatrix.makeTranslation( - this._v3_1.x, - this._v3_1.y, - this._v3_1.z );
  1313. this._m4_2.makeTranslation( this._v3_1.x, this._v3_1.y, this._v3_1.z ).multiply( this._scaleMatrix );
  1314. this._m4_2.multiply( this._translationMatrix ); //move camera and gizmos to obtain pinch effect
  1315. scalePoint.sub( this._v3_1 );
  1316. const amount = scalePoint.clone().multiplyScalar( sizeInverse );
  1317. scalePoint.sub( amount );
  1318. this._m4_1.makeTranslation( scalePoint.x, scalePoint.y, scalePoint.z );
  1319. this._m4_2.premultiply( this._m4_1 );
  1320. this.setTransformationMatrices( this._m4_1, this._m4_2 );
  1321. return _transformation;
  1322. } else if ( this.camera.isPerspectiveCamera ) {
  1323. this._v3_1.setFromMatrixPosition( this._cameraMatrixState );
  1324. this._v3_2.setFromMatrixPosition( this._gizmoMatrixState ); //move camera
  1325. let distance = this._v3_1.distanceTo( scalePoint );
  1326. let amount = distance - distance * sizeInverse; //check min and max distance
  1327. const newDistance = distance - amount;
  1328. if ( newDistance < this.minDistance ) {
  1329. sizeInverse = this.minDistance / distance;
  1330. amount = distance - distance * sizeInverse;
  1331. } else if ( newDistance > this.maxDistance ) {
  1332. sizeInverse = this.maxDistance / distance;
  1333. amount = distance - distance * sizeInverse;
  1334. }
  1335. let direction = scalePoint.clone().sub( this._v3_1 ).normalize().multiplyScalar( amount );
  1336. this._m4_1.makeTranslation( direction.x, direction.y, direction.z );
  1337. if ( scaleGizmos ) {
  1338. //scale gizmos so they appear in the same spot having the same dimension
  1339. const pos = this._v3_2;
  1340. distance = pos.distanceTo( scalePoint );
  1341. amount = distance - distance * sizeInverse;
  1342. direction = scalePoint.clone().sub( this._v3_2 ).normalize().multiplyScalar( amount );
  1343. this._translationMatrix.makeTranslation( pos.x, pos.y, pos.z );
  1344. this._scaleMatrix.makeScale( sizeInverse, sizeInverse, sizeInverse );
  1345. this._m4_2.makeTranslation( direction.x, direction.y, direction.z ).multiply( this._translationMatrix );
  1346. this._m4_2.multiply( this._scaleMatrix );
  1347. this._translationMatrix.makeTranslation( - pos.x, - pos.y, - pos.z );
  1348. this._m4_2.multiply( this._translationMatrix );
  1349. this.setTransformationMatrices( this._m4_1, this._m4_2 );
  1350. } else {
  1351. this.setTransformationMatrices( this._m4_1 );
  1352. }
  1353. return _transformation;
  1354. }
  1355. };
  1356. this.setFov = value => {
  1357. if ( this.camera.isPerspectiveCamera ) {
  1358. this.camera.fov = THREE.MathUtils.clamp( value, this.minFov, this.maxFov );
  1359. this.camera.updateProjectionMatrix();
  1360. }
  1361. };
  1362. this.setTarget = ( x, y, z ) => {
  1363. this.target.set( x, y, z );
  1364. this._gizmos.position.set( x, y, z ); //for correct radius calculation
  1365. this._tbRadius = this.calculateTbRadius( this.camera );
  1366. this.makeGizmos( this.target, this._tbRadius );
  1367. this.camera.lookAt( this.target );
  1368. };
  1369. this.zRotate = ( point, angle ) => {
  1370. this._rotationMatrix.makeRotationAxis( this._rotationAxis, angle );
  1371. this._translationMatrix.makeTranslation( - point.x, - point.y, - point.z );
  1372. this._m4_1.makeTranslation( point.x, point.y, point.z );
  1373. this._m4_1.multiply( this._rotationMatrix );
  1374. this._m4_1.multiply( this._translationMatrix );
  1375. this._v3_1.setFromMatrixPosition( this._gizmoMatrixState ).sub( point ); //vector from rotation center to gizmos position
  1376. this._v3_2.copy( this._v3_1 ).applyAxisAngle( this._rotationAxis, angle ); //apply rotation
  1377. this._v3_2.sub( this._v3_1 );
  1378. this._m4_2.makeTranslation( this._v3_2.x, this._v3_2.y, this._v3_2.z );
  1379. this.setTransformationMatrices( this._m4_1, this._m4_2 );
  1380. return _transformation;
  1381. };
  1382. this.unprojectOnObj = ( cursor, camera ) => {
  1383. const raycaster = new THREE.Raycaster();
  1384. raycaster.near = camera.near;
  1385. raycaster.far = camera.far;
  1386. raycaster.setFromCamera( cursor, camera );
  1387. const intersect = raycaster.intersectObjects( this.scene.children, true );
  1388. for ( let i = 0; i < intersect.length; i ++ ) {
  1389. if ( intersect[ i ].object.uuid != this._gizmos.uuid && intersect[ i ].face != null ) {
  1390. return intersect[ i ].point.clone();
  1391. }
  1392. }
  1393. return null;
  1394. };
  1395. this.unprojectOnTbSurface = ( camera, cursorX, cursorY, canvas, tbRadius ) => {
  1396. if ( camera.type == 'OrthographicCamera' ) {
  1397. this._v2_1.copy( this.getCursorPosition( cursorX, cursorY, canvas ) );
  1398. this._v3_1.set( this._v2_1.x, this._v2_1.y, 0 );
  1399. const x2 = Math.pow( this._v2_1.x, 2 );
  1400. const y2 = Math.pow( this._v2_1.y, 2 );
  1401. const r2 = Math.pow( this._tbRadius, 2 );
  1402. if ( x2 + y2 <= r2 * 0.5 ) {
  1403. //intersection with sphere
  1404. this._v3_1.setZ( Math.sqrt( r2 - ( x2 + y2 ) ) );
  1405. } else {
  1406. //intersection with hyperboloid
  1407. this._v3_1.setZ( r2 * 0.5 / Math.sqrt( x2 + y2 ) );
  1408. }
  1409. return this._v3_1;
  1410. } else if ( camera.type == 'PerspectiveCamera' ) {
  1411. //unproject cursor on the near plane
  1412. this._v2_1.copy( this.getCursorNDC( cursorX, cursorY, canvas ) );
  1413. this._v3_1.set( this._v2_1.x, this._v2_1.y, - 1 );
  1414. this._v3_1.applyMatrix4( camera.projectionMatrixInverse );
  1415. const rayDir = this._v3_1.clone().normalize(); //unprojected ray direction
  1416. const cameraGizmoDistance = camera.position.distanceTo( this._gizmos.position );
  1417. const radius2 = Math.pow( tbRadius, 2 ); // camera
  1418. // |\
  1419. // | \
  1420. // | \
  1421. // h | \
  1422. // | \
  1423. // | \
  1424. // _ _ | _ _ _\ _ _ near plane
  1425. // l
  1426. const h = this._v3_1.z;
  1427. const l = Math.sqrt( Math.pow( this._v3_1.x, 2 ) + Math.pow( this._v3_1.y, 2 ) );
  1428. if ( l == 0 ) {
  1429. //ray aligned with camera
  1430. rayDir.set( this._v3_1.x, this._v3_1.y, tbRadius );
  1431. return rayDir;
  1432. }
  1433. const m = h / l;
  1434. const q = cameraGizmoDistance;
  1435. /*
  1436. * calculate intersection point between unprojected ray and trackball surface
  1437. *|y = m * x + q
  1438. *|x^2 + y^2 = r^2
  1439. *
  1440. * (m^2 + 1) * x^2 + (2 * m * q) * x + q^2 - r^2 = 0
  1441. */
  1442. let a = Math.pow( m, 2 ) + 1;
  1443. let b = 2 * m * q;
  1444. let c = Math.pow( q, 2 ) - radius2;
  1445. let delta = Math.pow( b, 2 ) - 4 * a * c;
  1446. if ( delta >= 0 ) {
  1447. //intersection with sphere
  1448. this._v2_1.setX( ( - b - Math.sqrt( delta ) ) / ( 2 * a ) );
  1449. this._v2_1.setY( m * this._v2_1.x + q );
  1450. const angle = THREE.MathUtils.RAD2DEG * this._v2_1.angle();
  1451. if ( angle >= 45 ) {
  1452. //if angle between intersection point and X' axis is >= 45°, return that point
  1453. //otherwise, calculate intersection point with hyperboloid
  1454. const rayLength = Math.sqrt( Math.pow( this._v2_1.x, 2 ) + Math.pow( cameraGizmoDistance - this._v2_1.y, 2 ) );
  1455. rayDir.multiplyScalar( rayLength );
  1456. rayDir.z += cameraGizmoDistance;
  1457. return rayDir;
  1458. }
  1459. } //intersection with hyperboloid
  1460. /*
  1461. *|y = m * x + q
  1462. *|y = (1 / x) * (r^2 / 2)
  1463. *
  1464. * m * x^2 + q * x - r^2 / 2 = 0
  1465. */
  1466. a = m;
  1467. b = q;
  1468. c = - radius2 * 0.5;
  1469. delta = Math.pow( b, 2 ) - 4 * a * c;
  1470. this._v2_1.setX( ( - b - Math.sqrt( delta ) ) / ( 2 * a ) );
  1471. this._v2_1.setY( m * this._v2_1.x + q );
  1472. const rayLength = Math.sqrt( Math.pow( this._v2_1.x, 2 ) + Math.pow( cameraGizmoDistance - this._v2_1.y, 2 ) );
  1473. rayDir.multiplyScalar( rayLength );
  1474. rayDir.z += cameraGizmoDistance;
  1475. return rayDir;
  1476. }
  1477. };
  1478. this.unprojectOnTbPlane = ( camera, cursorX, cursorY, canvas, initialDistance = false ) => {
  1479. if ( camera.type == 'OrthographicCamera' ) {
  1480. this._v2_1.copy( this.getCursorPosition( cursorX, cursorY, canvas ) );
  1481. this._v3_1.set( this._v2_1.x, this._v2_1.y, 0 );
  1482. return this._v3_1.clone();
  1483. } else if ( camera.type == 'PerspectiveCamera' ) {
  1484. this._v2_1.copy( this.getCursorNDC( cursorX, cursorY, canvas ) ); //unproject cursor on the near plane
  1485. this._v3_1.set( this._v2_1.x, this._v2_1.y, - 1 );
  1486. this._v3_1.applyMatrix4( camera.projectionMatrixInverse );
  1487. const rayDir = this._v3_1.clone().normalize(); //unprojected ray direction
  1488. // camera
  1489. // |\
  1490. // | \
  1491. // | \
  1492. // h | \
  1493. // | \
  1494. // | \
  1495. // _ _ | _ _ _\ _ _ near plane
  1496. // l
  1497. const h = this._v3_1.z;
  1498. const l = Math.sqrt( Math.pow( this._v3_1.x, 2 ) + Math.pow( this._v3_1.y, 2 ) );
  1499. let cameraGizmoDistance;
  1500. if ( initialDistance ) {
  1501. cameraGizmoDistance = this._v3_1.setFromMatrixPosition( this._cameraMatrixState0 ).distanceTo( this._v3_2.setFromMatrixPosition( this._gizmoMatrixState0 ) );
  1502. } else {
  1503. cameraGizmoDistance = camera.position.distanceTo( this._gizmos.position );
  1504. }
  1505. /*
  1506. * calculate intersection point between unprojected ray and the plane
  1507. *|y = mx + q
  1508. *|y = 0
  1509. *
  1510. * x = -q/m
  1511. */
  1512. if ( l == 0 ) {
  1513. //ray aligned with camera
  1514. rayDir.set( 0, 0, 0 );
  1515. return rayDir;
  1516. }
  1517. const m = h / l;
  1518. const q = cameraGizmoDistance;
  1519. const x = - q / m;
  1520. const rayLength = Math.sqrt( Math.pow( q, 2 ) + Math.pow( x, 2 ) );
  1521. rayDir.multiplyScalar( rayLength );
  1522. rayDir.z = 0;
  1523. return rayDir;
  1524. }
  1525. };
  1526. this.updateMatrixState = () => {
  1527. //update camera and gizmos state
  1528. this._cameraMatrixState.copy( this.camera.matrix );
  1529. this._gizmoMatrixState.copy( this._gizmos.matrix );
  1530. if ( this.camera.isOrthographicCamera ) {
  1531. this._cameraProjectionState.copy( this.camera.projectionMatrix );
  1532. this.camera.updateProjectionMatrix();
  1533. this._zoomState = this.camera.zoom;
  1534. } else if ( this.camera.isPerspectiveCamera ) {
  1535. this._fovState = this.camera.fov;
  1536. }
  1537. };
  1538. this.updateTbState = ( newState, updateMatrices ) => {
  1539. this._state = newState;
  1540. if ( updateMatrices ) {
  1541. this.updateMatrixState();
  1542. }
  1543. };
  1544. this.update = () => {
  1545. const EPS = 0.000001; //check min/max parameters
  1546. if ( this.camera.isOrthographicCamera ) {
  1547. //check zoom
  1548. if ( this.camera.zoom > this.maxZoom || this.camera.zoom < this.minZoom ) {
  1549. const newZoom = THREE.MathUtils.clamp( this.camera.zoom, this.minZoom, this.maxZoom );
  1550. this.applyTransformMatrix( this.scale( newZoom / this.camera.zoom, this._gizmos.position, true ) );
  1551. }
  1552. } else if ( this.camera.isPerspectiveCamera ) {
  1553. //check distance
  1554. const distance = this.camera.position.distanceTo( this._gizmos.position );
  1555. if ( distance > this.maxDistance + EPS || distance < this.minDistance - EPS ) {
  1556. const newDistance = THREE.MathUtils.clamp( distance, this.minDistance, this.maxDistance );
  1557. this.applyTransformMatrix( this.scale( newDistance / distance, this._gizmos.position ) );
  1558. this.updateMatrixState();
  1559. } //check fov
  1560. if ( this.camera.fov < this.minFov || this.camera.fov > this.maxFov ) {
  1561. this.camera.fov = THREE.MathUtils.clamp( this.camera.fov, this.minFov, this.maxFov );
  1562. this.camera.updateProjectionMatrix();
  1563. }
  1564. const oldRadius = this._tbRadius;
  1565. this._tbRadius = this.calculateTbRadius( this.camera );
  1566. if ( oldRadius < this._tbRadius - EPS || oldRadius > this._tbRadius + EPS ) {
  1567. const scale = ( this._gizmos.scale.x + this._gizmos.scale.y + this._gizmos.scale.z ) / 3;
  1568. const newRadius = this._tbRadius / scale;
  1569. const curve = new THREE.EllipseCurve( 0, 0, newRadius, newRadius );
  1570. const points = curve.getPoints( this._curvePts );
  1571. const curveGeometry = new THREE.BufferGeometry().setFromPoints( points );
  1572. for ( const gizmo in this._gizmos.children ) {
  1573. this._gizmos.children[ gizmo ].geometry = curveGeometry;
  1574. }
  1575. }
  1576. }
  1577. this.camera.lookAt( this._gizmos.position );
  1578. };
  1579. this.setStateFromJSON = json => {
  1580. const state = JSON.parse( json );
  1581. if ( state.arcballState != undefined ) {
  1582. this._cameraMatrixState.fromArray( state.arcballState.cameraMatrix.elements );
  1583. this._cameraMatrixState.decompose( this.camera.position, this.camera.quaternion, this.camera.scale );
  1584. this.camera.up.copy( state.arcballState.cameraUp );
  1585. this.camera.near = state.arcballState.cameraNear;
  1586. this.camera.far = state.arcballState.cameraFar;
  1587. this.camera.zoom = state.arcballState.cameraZoom;
  1588. if ( this.camera.isPerspectiveCamera ) {
  1589. this.camera.fov = state.arcballState.cameraFov;
  1590. }
  1591. this._gizmoMatrixState.fromArray( state.arcballState.gizmoMatrix.elements );
  1592. this._gizmoMatrixState.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1593. this.camera.updateMatrix();
  1594. this.camera.updateProjectionMatrix();
  1595. this._gizmos.updateMatrix();
  1596. this._tbRadius = this.calculateTbRadius( this.camera );
  1597. const gizmoTmp = new THREE.Matrix4().copy( this._gizmoMatrixState0 );
  1598. this.makeGizmos( this._gizmos.position, this._tbRadius );
  1599. this._gizmoMatrixState0.copy( gizmoTmp );
  1600. this.camera.lookAt( this._gizmos.position );
  1601. this.updateTbState( STATE.IDLE, false );
  1602. this.dispatchEvent( _changeEvent );
  1603. }
  1604. };
  1605. this.camera = null;
  1606. this.domElement = domElement;
  1607. this.scene = scene;
  1608. this.target = new THREE.Vector3( 0, 0, 0 );
  1609. this.mouseActions = [];
  1610. this._mouseOp = null; //global vectors and matrices that are used in some operations to avoid creating new objects every time (e.g. every time cursor moves)
  1611. this._v2_1 = new THREE.Vector2();
  1612. this._v3_1 = new THREE.Vector3();
  1613. this._v3_2 = new THREE.Vector3();
  1614. this._m4_1 = new THREE.Matrix4();
  1615. this._m4_2 = new THREE.Matrix4();
  1616. this._quat = new THREE.Quaternion(); //transformation matrices
  1617. this._translationMatrix = new THREE.Matrix4(); //matrix for translation operation
  1618. this._rotationMatrix = new THREE.Matrix4(); //matrix for rotation operation
  1619. this._scaleMatrix = new THREE.Matrix4(); //matrix for scaling operation
  1620. this._rotationAxis = new THREE.Vector3(); //axis for rotate operation
  1621. //camera state
  1622. this._cameraMatrixState = new THREE.Matrix4();
  1623. this._cameraProjectionState = new THREE.Matrix4();
  1624. this._fovState = 1;
  1625. this._upState = new THREE.Vector3();
  1626. this._zoomState = 1;
  1627. this._nearPos = 0;
  1628. this._farPos = 0;
  1629. this._gizmoMatrixState = new THREE.Matrix4(); //initial values
  1630. this._up0 = new THREE.Vector3();
  1631. this._zoom0 = 1;
  1632. this._fov0 = 0;
  1633. this._initialNear = 0;
  1634. this._nearPos0 = 0;
  1635. this._initialFar = 0;
  1636. this._farPos0 = 0;
  1637. this._cameraMatrixState0 = new THREE.Matrix4();
  1638. this._gizmoMatrixState0 = new THREE.Matrix4(); //pointers array
  1639. this._button = - 1;
  1640. this._touchStart = [];
  1641. this._touchCurrent = [];
  1642. this._input = INPUT.NONE; //two fingers touch interaction
  1643. this._switchSensibility = 32; //minimum movement to be performed to fire single pan start after the second finger has been released
  1644. this._startFingerDistance = 0; //distance between two fingers
  1645. this._currentFingerDistance = 0;
  1646. this._startFingerRotation = 0; //amount of rotation performed with two fingers
  1647. this._currentFingerRotation = 0; //double tap
  1648. this._devPxRatio = 0;
  1649. this._downValid = true;
  1650. this._nclicks = 0;
  1651. this._downEvents = [];
  1652. this._downStart = 0; //pointerDown time
  1653. this._clickStart = 0; //first click time
  1654. this._maxDownTime = 250;
  1655. this._maxInterval = 300;
  1656. this._posThreshold = 24;
  1657. this._movementThreshold = 24; //cursor positions
  1658. this._currentCursorPosition = new THREE.Vector3();
  1659. this._startCursorPosition = new THREE.Vector3(); //grid
  1660. this._grid = null; //grid to be visualized during pan operation
  1661. this._gridPosition = new THREE.Vector3(); //gizmos
  1662. this._gizmos = new THREE.Group();
  1663. this._curvePts = 128; //animations
  1664. this._timeStart = - 1; //initial time
  1665. this._animationId = - 1; //focus animation
  1666. this.focusAnimationTime = 500; //duration of focus animation in ms
  1667. //rotate animation
  1668. this._timePrev = 0; //time at which previous rotate operation has been detected
  1669. this._timeCurrent = 0; //time at which current rotate operation has been detected
  1670. this._anglePrev = 0; //angle of previous rotation
  1671. this._angleCurrent = 0; //angle of current rotation
  1672. this._cursorPosPrev = new THREE.Vector3(); //cursor position when previous rotate operation has been detected
  1673. this._cursorPosCurr = new THREE.Vector3(); //cursor position when current rotate operation has been detected
  1674. this._wPrev = 0; //angular velocity of the previous rotate operation
  1675. this._wCurr = 0; //angular velocity of the current rotate operation
  1676. //parameters
  1677. this.adjustNearFar = false;
  1678. this.scaleFactor = 1.1; //zoom/distance multiplier
  1679. this.dampingFactor = 25;
  1680. this.wMax = 20; //maximum angular velocity allowed
  1681. this.enableAnimations = true; //if animations should be performed
  1682. this.enableGrid = false; //if grid should be showed during pan operation
  1683. this.cursorZoom = false; //if wheel zoom should be cursor centered
  1684. this.minFov = 5;
  1685. this.maxFov = 90;
  1686. this.enabled = true;
  1687. this.enablePan = true;
  1688. this.enableRotate = true;
  1689. this.enableZoom = true;
  1690. this.enableGizmos = true;
  1691. this.minDistance = 0;
  1692. this.maxDistance = Infinity;
  1693. this.minZoom = 0;
  1694. this.maxZoom = Infinity; //trackball parameters
  1695. this._tbRadius = 1; //FSA
  1696. this._state = STATE.IDLE;
  1697. this.setCamera( _camera );
  1698. if ( this.scene != null ) {
  1699. this.scene.add( this._gizmos );
  1700. }
  1701. this.domElement.style.touchAction = 'none';
  1702. this._devPxRatio = window.devicePixelRatio;
  1703. this.initializeMouseActions();
  1704. this.domElement.addEventListener( 'contextmenu', this.onContextMenu );
  1705. this.domElement.addEventListener( 'wheel', this.onWheel );
  1706. this.domElement.addEventListener( 'pointerdown', this.onPointerDown );
  1707. this.domElement.addEventListener( 'pointercancel', this.onPointerCancel );
  1708. window.addEventListener( 'keydown', this.onKeyDown );
  1709. window.addEventListener( 'resize', this.onWindowResize );
  1710. } //listeners
  1711. /**
  1712. * Apply a transformation matrix, to the camera and gizmos
  1713. * @param {Object} transformation Object containing matrices to apply to camera and gizmos
  1714. */
  1715. applyTransformMatrix( transformation ) {
  1716. if ( transformation.camera != null ) {
  1717. this._m4_1.copy( this._cameraMatrixState ).premultiply( transformation.camera );
  1718. this._m4_1.decompose( this.camera.position, this.camera.quaternion, this.camera.scale );
  1719. this.camera.updateMatrix(); //update camera up vector
  1720. if ( this._state == STATE.ROTATE || this._state == STATE.ZROTATE || this._state == STATE.ANIMATION_ROTATE ) {
  1721. this.camera.up.copy( this._upState ).applyQuaternion( this.camera.quaternion );
  1722. }
  1723. }
  1724. if ( transformation.gizmos != null ) {
  1725. this._m4_1.copy( this._gizmoMatrixState ).premultiply( transformation.gizmos );
  1726. this._m4_1.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1727. this._gizmos.updateMatrix();
  1728. }
  1729. if ( this._state == STATE.SCALE || this._state == STATE.FOCUS || this._state == STATE.ANIMATION_FOCUS ) {
  1730. this._tbRadius = this.calculateTbRadius( this.camera );
  1731. if ( this.adjustNearFar ) {
  1732. const cameraDistance = this.camera.position.distanceTo( this._gizmos.position );
  1733. const bb = new THREE.Box3();
  1734. bb.setFromObject( this._gizmos );
  1735. const sphere = new THREE.Sphere();
  1736. bb.getBoundingSphere( sphere );
  1737. const adjustedNearPosition = Math.max( this._nearPos0, sphere.radius + sphere.center.length() );
  1738. const regularNearPosition = cameraDistance - this._initialNear;
  1739. const minNearPos = Math.min( adjustedNearPosition, regularNearPosition );
  1740. this.camera.near = cameraDistance - minNearPos;
  1741. const adjustedFarPosition = Math.min( this._farPos0, - sphere.radius + sphere.center.length() );
  1742. const regularFarPosition = cameraDistance - this._initialFar;
  1743. const minFarPos = Math.min( adjustedFarPosition, regularFarPosition );
  1744. this.camera.far = cameraDistance - minFarPos;
  1745. this.camera.updateProjectionMatrix();
  1746. } else {
  1747. let update = false;
  1748. if ( this.camera.near != this._initialNear ) {
  1749. this.camera.near = this._initialNear;
  1750. update = true;
  1751. }
  1752. if ( this.camera.far != this._initialFar ) {
  1753. this.camera.far = this._initialFar;
  1754. update = true;
  1755. }
  1756. if ( update ) {
  1757. this.camera.updateProjectionMatrix();
  1758. }
  1759. }
  1760. }
  1761. }
  1762. /**
  1763. * Calculate the angular speed
  1764. * @param {Number} p0 Position at t0
  1765. * @param {Number} p1 Position at t1
  1766. * @param {Number} t0 Initial time in milliseconds
  1767. * @param {Number} t1 Ending time in milliseconds
  1768. */
  1769. /**
  1770. * Set gizmos visibility
  1771. * @param {Boolean} value Value of gizmos visibility
  1772. */
  1773. setGizmosVisible( value ) {
  1774. this._gizmos.visible = value;
  1775. this.dispatchEvent( _changeEvent );
  1776. }
  1777. /**
  1778. * Creates the rotation gizmos matching trackball center and radius
  1779. * @param {Vector3} tbCenter The trackball center
  1780. * @param {number} tbRadius The trackball radius
  1781. */
  1782. /**
  1783. * Set values in transformation object
  1784. * @param {Matrix4} camera Transformation to be applied to the camera
  1785. * @param {Matrix4} gizmos Transformation to be applied to gizmos
  1786. */
  1787. setTransformationMatrices( camera = null, gizmos = null ) {
  1788. if ( camera != null ) {
  1789. if ( _transformation.camera != null ) {
  1790. _transformation.camera.copy( camera );
  1791. } else {
  1792. _transformation.camera = camera.clone();
  1793. }
  1794. } else {
  1795. _transformation.camera = null;
  1796. }
  1797. if ( gizmos != null ) {
  1798. if ( _transformation.gizmos != null ) {
  1799. _transformation.gizmos.copy( gizmos );
  1800. } else {
  1801. _transformation.gizmos = gizmos.clone();
  1802. }
  1803. } else {
  1804. _transformation.gizmos = null;
  1805. }
  1806. }
  1807. /**
  1808. * Rotate camera around its direction axis passing by a given point by a given angle
  1809. * @param {Vector3} point The point where the rotation axis is passing trough
  1810. * @param {Number} angle Angle in radians
  1811. * @returns The computed transormation matix
  1812. */
  1813. }
  1814. THREE.ArcballControls = ArcballControls;
  1815. } )();