ArcballControls.js 75 KB

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