ArcballControls.js 74 KB

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