ArcballControls.js 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036
  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. /**
  42. *
  43. * @param {Camera} camera Virtual camera used in the scene
  44. * @param {HTMLElement} domElement Renderer's dom element
  45. * @param {Scene} scene The scene to be rendered
  46. */
  47. class ArcballControls extends THREE.EventDispatcher {
  48. constructor( _camera, domElement, scene = null ) {
  49. super();
  50. this.onWindowResize = () => {
  51. const scale = ( this._gizmos.scale.x + this._gizmos.scale.y + this._gizmos.scale.z ) / 3;
  52. this._tbRadius = this.calculateTbRadius( this.camera );
  53. const newRadius = this._tbRadius / scale;
  54. const curve = new THREE.EllipseCurve( 0, 0, newRadius, newRadius );
  55. const points = curve.getPoints( this._curvePts );
  56. const curveGeometry = new THREE.BufferGeometry().setFromPoints( points );
  57. for ( const gizmo in this._gizmos.children ) {
  58. this._gizmos.children[ gizmo ].geometry = curveGeometry;
  59. }
  60. this.dispatchEvent( _changeEvent );
  61. };
  62. this.onContextMenu = event => {
  63. if ( ! this.enabled ) {
  64. return;
  65. }
  66. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  67. if ( this.mouseActions[ i ].mouse == 2 ) {
  68. //prevent only if button 2 is actually used
  69. event.preventDefault();
  70. break;
  71. }
  72. }
  73. };
  74. this.onPointerCancel = () => {
  75. this._touchStart.splice( 0, this._touchStart.length );
  76. this._touchCurrent.splice( 0, this._touchCurrent.length );
  77. this._input = INPUT.NONE;
  78. };
  79. this.onPointerDown = event => {
  80. if ( event.button == 0 && event.isPrimary ) {
  81. this._downValid = true;
  82. this._downEvents.push( event );
  83. this._downStart = performance.now();
  84. } else {
  85. this._downValid = false;
  86. }
  87. if ( event.pointerType == 'touch' && this._input != INPUT.CURSOR ) {
  88. this._touchStart.push( event );
  89. this._touchCurrent.push( event );
  90. switch ( this._input ) {
  91. case INPUT.NONE:
  92. //singleStart
  93. this._input = INPUT.ONE_FINGER;
  94. this.onSinglePanStart( event, 'ROTATE' );
  95. window.addEventListener( 'pointermove', this.onPointerMove );
  96. window.addEventListener( 'pointerup', this.onPointerUp );
  97. break;
  98. case INPUT.ONE_FINGER:
  99. case INPUT.ONE_FINGER_SWITCHED:
  100. //doubleStart
  101. this._input = INPUT.TWO_FINGER;
  102. this.onRotateStart();
  103. this.onPinchStart();
  104. this.onDoublePanStart();
  105. break;
  106. case INPUT.TWO_FINGER:
  107. //multipleStart
  108. this._input = INPUT.MULT_FINGER;
  109. this.onTriplePanStart( event );
  110. break;
  111. }
  112. } else if ( event.pointerType != 'touch' && this._input == INPUT.NONE ) {
  113. let modifier = null;
  114. if ( event.ctrlKey || event.metaKey ) {
  115. modifier = 'CTRL';
  116. } else if ( event.shiftKey ) {
  117. modifier = 'SHIFT';
  118. }
  119. this._mouseOp = this.getOpFromAction( event.button, modifier );
  120. if ( this._mouseOp != null ) {
  121. window.addEventListener( 'pointermove', this.onPointerMove );
  122. window.addEventListener( 'pointerup', this.onPointerUp ); //singleStart
  123. this._input = INPUT.CURSOR;
  124. this._button = event.button;
  125. this.onSinglePanStart( event, this._mouseOp );
  126. }
  127. }
  128. };
  129. this.onPointerMove = event => {
  130. if ( event.pointerType == 'touch' && this._input != INPUT.CURSOR ) {
  131. switch ( this._input ) {
  132. case INPUT.ONE_FINGER:
  133. //singleMove
  134. this.updateTouchEvent( event );
  135. this.onSinglePanMove( event, STATE.ROTATE );
  136. break;
  137. case INPUT.ONE_FINGER_SWITCHED:
  138. const movement = this.calculatePointersDistance( this._touchCurrent[ 0 ], event ) * this._devPxRatio;
  139. if ( movement >= this._switchSensibility ) {
  140. //singleMove
  141. this._input = INPUT.ONE_FINGER;
  142. this.updateTouchEvent( event );
  143. this.onSinglePanStart( event, 'ROTATE' );
  144. break;
  145. }
  146. break;
  147. case INPUT.TWO_FINGER:
  148. //rotate/pan/pinchMove
  149. this.updateTouchEvent( event );
  150. this.onRotateMove();
  151. this.onPinchMove();
  152. this.onDoublePanMove();
  153. break;
  154. case INPUT.MULT_FINGER:
  155. //multMove
  156. this.updateTouchEvent( event );
  157. this.onTriplePanMove( event );
  158. break;
  159. }
  160. } else if ( event.pointerType != 'touch' && this._input == INPUT.CURSOR ) {
  161. let modifier = null;
  162. if ( event.ctrlKey || event.metaKey ) {
  163. modifier = 'CTRL';
  164. } else if ( event.shiftKey ) {
  165. modifier = 'SHIFT';
  166. }
  167. const mouseOpState = this.getOpStateFromAction( this._button, modifier );
  168. if ( mouseOpState != null ) {
  169. this.onSinglePanMove( event, mouseOpState );
  170. }
  171. } //checkDistance
  172. if ( this._downValid ) {
  173. const movement = this.calculatePointersDistance( this._downEvents[ this._downEvents.length - 1 ], event ) * this._devPxRatio;
  174. if ( movement > this._movementThreshold ) {
  175. this._downValid = false;
  176. }
  177. }
  178. };
  179. this.onPointerUp = event => {
  180. if ( event.pointerType == 'touch' && this._input != INPUT.CURSOR ) {
  181. const nTouch = this._touchCurrent.length;
  182. for ( let i = 0; i < nTouch; i ++ ) {
  183. if ( this._touchCurrent[ i ].pointerId == event.pointerId ) {
  184. this._touchCurrent.splice( i, 1 );
  185. this._touchStart.splice( i, 1 );
  186. break;
  187. }
  188. }
  189. switch ( this._input ) {
  190. case INPUT.ONE_FINGER:
  191. case INPUT.ONE_FINGER_SWITCHED:
  192. //singleEnd
  193. window.removeEventListener( 'pointermove', this.onPointerMove );
  194. window.removeEventListener( 'pointerup', this.onPointerUp );
  195. this._input = INPUT.NONE;
  196. this.onSinglePanEnd();
  197. break;
  198. case INPUT.TWO_FINGER:
  199. //doubleEnd
  200. this.onDoublePanEnd( event );
  201. this.onPinchEnd( event );
  202. this.onRotateEnd( event ); //switching to singleStart
  203. this._input = INPUT.ONE_FINGER_SWITCHED;
  204. break;
  205. case INPUT.MULT_FINGER:
  206. if ( this._touchCurrent.length == 0 ) {
  207. window.removeEventListener( 'pointermove', this.onPointerMove );
  208. window.removeEventListener( 'pointerup', this.onPointerUp ); //multCancel
  209. this._input = INPUT.NONE;
  210. this.onTriplePanEnd();
  211. }
  212. break;
  213. }
  214. } else if ( event.pointerType != 'touch' && this._input == INPUT.CURSOR ) {
  215. window.removeEventListener( 'pointermove', this.onPointerMove );
  216. window.removeEventListener( 'pointerup', this.onPointerUp );
  217. this._input = INPUT.NONE;
  218. this.onSinglePanEnd();
  219. this._button = - 1;
  220. }
  221. if ( event.isPrimary ) {
  222. if ( this._downValid ) {
  223. const downTime = event.timeStamp - this._downEvents[ this._downEvents.length - 1 ].timeStamp;
  224. if ( downTime <= this._maxDownTime ) {
  225. if ( this._nclicks == 0 ) {
  226. //first valid click detected
  227. this._nclicks = 1;
  228. this._clickStart = performance.now();
  229. } else {
  230. const clickInterval = event.timeStamp - this._clickStart;
  231. const movement = this.calculatePointersDistance( this._downEvents[ 1 ], this._downEvents[ 0 ] ) * this._devPxRatio;
  232. if ( clickInterval <= this._maxInterval && movement <= this._posThreshold ) {
  233. //second valid click detected
  234. //fire double tap and reset values
  235. this._nclicks = 0;
  236. this._downEvents.splice( 0, this._downEvents.length );
  237. this.onDoubleTap( event );
  238. } else {
  239. //new 'first click'
  240. this._nclicks = 1;
  241. this._downEvents.shift();
  242. this._clickStart = performance.now();
  243. }
  244. }
  245. } else {
  246. this._downValid = false;
  247. this._nclicks = 0;
  248. this._downEvents.splice( 0, this._downEvents.length );
  249. }
  250. } else {
  251. this._nclicks = 0;
  252. this._downEvents.splice( 0, this._downEvents.length );
  253. }
  254. }
  255. };
  256. this.onWheel = event => {
  257. if ( this.enabled && this.enableZoom ) {
  258. let modifier = null;
  259. if ( event.ctrlKey || event.metaKey ) {
  260. modifier = 'CTRL';
  261. } else if ( event.shiftKey ) {
  262. modifier = 'SHIFT';
  263. }
  264. const mouseOp = this.getOpFromAction( 'WHEEL', modifier );
  265. if ( mouseOp != null ) {
  266. event.preventDefault();
  267. this.dispatchEvent( _startEvent );
  268. const notchDeltaY = 125; //distance of one notch of mouse wheel
  269. let sgn = event.deltaY / notchDeltaY;
  270. let size = 1;
  271. if ( sgn > 0 ) {
  272. size = 1 / this.scaleFactor;
  273. } else if ( sgn < 0 ) {
  274. size = this.scaleFactor;
  275. }
  276. switch ( mouseOp ) {
  277. case 'ZOOM':
  278. this.updateTbState( STATE.SCALE, true );
  279. if ( sgn > 0 ) {
  280. size = 1 / Math.pow( this.scaleFactor, sgn );
  281. } else if ( sgn < 0 ) {
  282. size = Math.pow( this.scaleFactor, - sgn );
  283. }
  284. if ( this.cursorZoom && this.enablePan ) {
  285. let scalePoint;
  286. if ( this.camera.isOrthographicCamera ) {
  287. scalePoint = this.unprojectOnTbPlane( this.camera, event.clientX, event.clientY, this.domElement ).applyQuaternion( this.camera.quaternion ).multiplyScalar( 1 / this.camera.zoom ).add( this._gizmos.position );
  288. } else if ( this.camera.isPerspectiveCamera ) {
  289. scalePoint = this.unprojectOnTbPlane( this.camera, event.clientX, event.clientY, this.domElement ).applyQuaternion( this.camera.quaternion ).add( this._gizmos.position );
  290. }
  291. this.applyTransformMatrix( this.scale( size, scalePoint ) );
  292. } else {
  293. this.applyTransformMatrix( this.scale( size, this._gizmos.position ) );
  294. }
  295. if ( this._grid != null ) {
  296. this.disposeGrid();
  297. this.drawGrid();
  298. }
  299. this.updateTbState( STATE.IDLE, false );
  300. this.dispatchEvent( _changeEvent );
  301. this.dispatchEvent( _endEvent );
  302. break;
  303. case 'FOV':
  304. if ( this.camera.isPerspectiveCamera ) {
  305. this.updateTbState( STATE.FOV, true ); //Vertigo effect
  306. // fov / 2
  307. // |\
  308. // | \
  309. // | \
  310. // x | \
  311. // | \
  312. // | \
  313. // | _ _ _\
  314. // y
  315. //check for iOs shift shortcut
  316. if ( event.deltaX != 0 ) {
  317. sgn = event.deltaX / notchDeltaY;
  318. size = 1;
  319. if ( sgn > 0 ) {
  320. size = 1 / Math.pow( this.scaleFactor, sgn );
  321. } else if ( sgn < 0 ) {
  322. size = Math.pow( this.scaleFactor, - sgn );
  323. }
  324. }
  325. this._v3_1.setFromMatrixPosition( this._cameraMatrixState );
  326. const x = this._v3_1.distanceTo( this._gizmos.position );
  327. let xNew = x / size; //distance between camera and gizmos if scale(size, scalepoint) would be performed
  328. //check min and max distance
  329. xNew = THREE.MathUtils.clamp( xNew, this.minDistance, this.maxDistance );
  330. const y = x * Math.tan( THREE.MathUtils.DEG2RAD * this.camera.fov * 0.5 ); //calculate new fov
  331. let newFov = THREE.MathUtils.RAD2DEG * ( Math.atan( y / xNew ) * 2 ); //check min and max fov
  332. if ( newFov > this.maxFov ) {
  333. newFov = this.maxFov;
  334. } else if ( newFov < this.minFov ) {
  335. newFov = this.minFov;
  336. }
  337. const newDistance = y / Math.tan( THREE.MathUtils.DEG2RAD * ( newFov / 2 ) );
  338. size = x / newDistance;
  339. this.setFov( newFov );
  340. this.applyTransformMatrix( this.scale( size, this._gizmos.position, false ) );
  341. }
  342. if ( this._grid != null ) {
  343. this.disposeGrid();
  344. this.drawGrid();
  345. }
  346. this.updateTbState( STATE.IDLE, false );
  347. this.dispatchEvent( _changeEvent );
  348. this.dispatchEvent( _endEvent );
  349. break;
  350. }
  351. }
  352. }
  353. };
  354. this.onKeyDown = event => {
  355. if ( event.key == 'c' ) {
  356. if ( event.ctrlKey || event.metaKey ) {
  357. this.copyState();
  358. }
  359. } else if ( event.key == 'v' ) {
  360. if ( event.ctrlKey || event.metaKey ) {
  361. this.pasteState();
  362. }
  363. }
  364. };
  365. this.onSinglePanStart = ( event, operation ) => {
  366. if ( this.enabled ) {
  367. this.dispatchEvent( _startEvent );
  368. this.setCenter( event.clientX, event.clientY );
  369. switch ( operation ) {
  370. case 'PAN':
  371. if ( ! this.enablePan ) {
  372. return;
  373. }
  374. if ( this._animationId != - 1 ) {
  375. cancelAnimationFrame( this._animationId );
  376. this._animationId = - 1;
  377. this._timeStart = - 1;
  378. this.activateGizmos( false );
  379. this.dispatchEvent( _changeEvent );
  380. }
  381. this.updateTbState( STATE.PAN, true );
  382. this._startCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ) );
  383. if ( this.enableGrid ) {
  384. this.drawGrid();
  385. this.dispatchEvent( _changeEvent );
  386. }
  387. break;
  388. case 'ROTATE':
  389. if ( ! this.enableRotate ) {
  390. return;
  391. }
  392. if ( this._animationId != - 1 ) {
  393. cancelAnimationFrame( this._animationId );
  394. this._animationId = - 1;
  395. this._timeStart = - 1;
  396. }
  397. this.updateTbState( STATE.ROTATE, true );
  398. this._startCursorPosition.copy( this.unprojectOnTbSurface( this.camera, _center.x, _center.y, this.domElement, this._tbRadius ) );
  399. this.activateGizmos( true );
  400. if ( this.enableAnimations ) {
  401. this._timePrev = this._timeCurrent = performance.now();
  402. this._angleCurrent = this._anglePrev = 0;
  403. this._cursorPosPrev.copy( this._startCursorPosition );
  404. this._cursorPosCurr.copy( this._cursorPosPrev );
  405. this._wCurr = 0;
  406. this._wPrev = this._wCurr;
  407. }
  408. this.dispatchEvent( _changeEvent );
  409. break;
  410. case 'FOV':
  411. if ( ! this.camera.isPerspectiveCamera || ! this.enableZoom ) {
  412. return;
  413. }
  414. if ( this._animationId != - 1 ) {
  415. cancelAnimationFrame( this._animationId );
  416. this._animationId = - 1;
  417. this._timeStart = - 1;
  418. this.activateGizmos( false );
  419. this.dispatchEvent( _changeEvent );
  420. }
  421. this.updateTbState( STATE.FOV, true );
  422. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  423. this._currentCursorPosition.copy( this._startCursorPosition );
  424. break;
  425. case 'ZOOM':
  426. if ( ! this.enableZoom ) {
  427. return;
  428. }
  429. if ( this._animationId != - 1 ) {
  430. cancelAnimationFrame( this._animationId );
  431. this._animationId = - 1;
  432. this._timeStart = - 1;
  433. this.activateGizmos( false );
  434. this.dispatchEvent( _changeEvent );
  435. }
  436. this.updateTbState( STATE.SCALE, true );
  437. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  438. this._currentCursorPosition.copy( this._startCursorPosition );
  439. break;
  440. }
  441. }
  442. };
  443. this.onSinglePanMove = ( event, opState ) => {
  444. if ( this.enabled ) {
  445. const restart = opState != this._state;
  446. this.setCenter( event.clientX, event.clientY );
  447. switch ( opState ) {
  448. case STATE.PAN:
  449. if ( this.enablePan ) {
  450. if ( restart ) {
  451. //switch to pan operation
  452. this.dispatchEvent( _endEvent );
  453. this.dispatchEvent( _startEvent );
  454. this.updateTbState( opState, true );
  455. this._startCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ) );
  456. if ( this.enableGrid ) {
  457. this.drawGrid();
  458. }
  459. this.activateGizmos( false );
  460. } else {
  461. //continue with pan operation
  462. this._currentCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ) );
  463. this.applyTransformMatrix( this.pan( this._startCursorPosition, this._currentCursorPosition ) );
  464. }
  465. }
  466. break;
  467. case STATE.ROTATE:
  468. if ( this.enableRotate ) {
  469. if ( restart ) {
  470. //switch to rotate operation
  471. this.dispatchEvent( _endEvent );
  472. this.dispatchEvent( _startEvent );
  473. this.updateTbState( opState, true );
  474. this._startCursorPosition.copy( this.unprojectOnTbSurface( this.camera, _center.x, _center.y, this.domElement, this._tbRadius ) );
  475. if ( this.enableGrid ) {
  476. this.disposeGrid();
  477. }
  478. this.activateGizmos( true );
  479. } else {
  480. //continue with rotate operation
  481. this._currentCursorPosition.copy( this.unprojectOnTbSurface( this.camera, _center.x, _center.y, this.domElement, this._tbRadius ) );
  482. const distance = this._startCursorPosition.distanceTo( this._currentCursorPosition );
  483. const angle = this._startCursorPosition.angleTo( this._currentCursorPosition );
  484. const amount = Math.max( distance / this._tbRadius, angle ); //effective rotation angle
  485. this.applyTransformMatrix( this.rotate( this.calculateRotationAxis( this._startCursorPosition, this._currentCursorPosition ), amount ) );
  486. if ( this.enableAnimations ) {
  487. this._timePrev = this._timeCurrent;
  488. this._timeCurrent = performance.now();
  489. this._anglePrev = this._angleCurrent;
  490. this._angleCurrent = amount;
  491. this._cursorPosPrev.copy( this._cursorPosCurr );
  492. this._cursorPosCurr.copy( this._currentCursorPosition );
  493. this._wPrev = this._wCurr;
  494. this._wCurr = this.calculateAngularSpeed( this._anglePrev, this._angleCurrent, this._timePrev, this._timeCurrent );
  495. }
  496. }
  497. }
  498. break;
  499. case STATE.SCALE:
  500. if ( this.enableZoom ) {
  501. if ( restart ) {
  502. //switch to zoom operation
  503. this.dispatchEvent( _endEvent );
  504. this.dispatchEvent( _startEvent );
  505. this.updateTbState( opState, true );
  506. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  507. this._currentCursorPosition.copy( this._startCursorPosition );
  508. if ( this.enableGrid ) {
  509. this.disposeGrid();
  510. }
  511. this.activateGizmos( false );
  512. } else {
  513. //continue with zoom operation
  514. const screenNotches = 8; //how many wheel notches corresponds to a full screen pan
  515. this._currentCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  516. const movement = this._currentCursorPosition.y - this._startCursorPosition.y;
  517. let size = 1;
  518. if ( movement < 0 ) {
  519. size = 1 / Math.pow( this.scaleFactor, - movement * screenNotches );
  520. } else if ( movement > 0 ) {
  521. size = Math.pow( this.scaleFactor, movement * screenNotches );
  522. }
  523. this.applyTransformMatrix( this.scale( size, this._gizmos.position ) );
  524. }
  525. }
  526. break;
  527. case STATE.FOV:
  528. if ( this.enableZoom && this.camera.isPerspectiveCamera ) {
  529. if ( restart ) {
  530. //switch to fov operation
  531. this.dispatchEvent( _endEvent );
  532. this.dispatchEvent( _startEvent );
  533. this.updateTbState( opState, true );
  534. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  535. this._currentCursorPosition.copy( this._startCursorPosition );
  536. if ( this.enableGrid ) {
  537. this.disposeGrid();
  538. }
  539. this.activateGizmos( false );
  540. } else {
  541. //continue with fov operation
  542. const screenNotches = 8; //how many wheel notches corresponds to a full screen pan
  543. this._currentCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  544. const movement = this._currentCursorPosition.y - this._startCursorPosition.y;
  545. let size = 1;
  546. if ( movement < 0 ) {
  547. size = 1 / Math.pow( this.scaleFactor, - movement * screenNotches );
  548. } else if ( movement > 0 ) {
  549. size = Math.pow( this.scaleFactor, movement * screenNotches );
  550. }
  551. this._v3_1.setFromMatrixPosition( this._cameraMatrixState );
  552. const x = this._v3_1.distanceTo( this._gizmos.position );
  553. let xNew = x / size; //distance between camera and gizmos if scale(size, scalepoint) would be performed
  554. //check min and max distance
  555. xNew = THREE.MathUtils.clamp( xNew, this.minDistance, this.maxDistance );
  556. const y = x * Math.tan( THREE.MathUtils.DEG2RAD * this._fovState * 0.5 ); //calculate new fov
  557. let newFov = THREE.MathUtils.RAD2DEG * ( Math.atan( y / xNew ) * 2 ); //check min and max fov
  558. newFov = THREE.MathUtils.clamp( newFov, this.minFov, this.maxFov );
  559. const newDistance = y / Math.tan( THREE.MathUtils.DEG2RAD * ( newFov / 2 ) );
  560. size = x / newDistance;
  561. this._v3_2.setFromMatrixPosition( this._gizmoMatrixState );
  562. this.setFov( newFov );
  563. this.applyTransformMatrix( this.scale( size, this._v3_2, false ) ); //adjusting distance
  564. _offset.copy( this._gizmos.position ).sub( this.camera.position ).normalize().multiplyScalar( newDistance / x );
  565. this._m4_1.makeTranslation( _offset.x, _offset.y, _offset.z );
  566. }
  567. }
  568. break;
  569. }
  570. this.dispatchEvent( _changeEvent );
  571. }
  572. };
  573. this.onSinglePanEnd = () => {
  574. if ( this._state == STATE.ROTATE ) {
  575. if ( ! this.enableRotate ) {
  576. return;
  577. }
  578. if ( this.enableAnimations ) {
  579. //perform rotation animation
  580. const deltaTime = performance.now() - this._timeCurrent;
  581. if ( deltaTime < 120 ) {
  582. const w = Math.abs( ( this._wPrev + this._wCurr ) / 2 );
  583. const self = this;
  584. this._animationId = window.requestAnimationFrame( function ( t ) {
  585. self.updateTbState( STATE.ANIMATION_ROTATE, true );
  586. const rotationAxis = self.calculateRotationAxis( self._cursorPosPrev, self._cursorPosCurr );
  587. self.onRotationAnim( t, rotationAxis, Math.min( w, self.wMax ) );
  588. } );
  589. } else {
  590. //cursor has been standing still for over 120 ms since last movement
  591. this.updateTbState( STATE.IDLE, false );
  592. this.activateGizmos( false );
  593. this.dispatchEvent( _changeEvent );
  594. }
  595. } else {
  596. this.updateTbState( STATE.IDLE, false );
  597. this.activateGizmos( false );
  598. this.dispatchEvent( _changeEvent );
  599. }
  600. } else if ( this._state == STATE.PAN || this._state == STATE.IDLE ) {
  601. this.updateTbState( STATE.IDLE, false );
  602. if ( this.enableGrid ) {
  603. this.disposeGrid();
  604. }
  605. this.activateGizmos( false );
  606. this.dispatchEvent( _changeEvent );
  607. }
  608. this.dispatchEvent( _endEvent );
  609. };
  610. this.onDoubleTap = event => {
  611. if ( this.enabled && this.enablePan && this.scene != null ) {
  612. this.dispatchEvent( _startEvent );
  613. this.setCenter( event.clientX, event.clientY );
  614. const hitP = this.unprojectOnObj( this.getCursorNDC( _center.x, _center.y, this.domElement ), this.camera );
  615. if ( hitP != null && this.enableAnimations ) {
  616. const self = this;
  617. if ( this._animationId != - 1 ) {
  618. window.cancelAnimationFrame( this._animationId );
  619. }
  620. this._timeStart = - 1;
  621. this._animationId = window.requestAnimationFrame( function ( t ) {
  622. self.updateTbState( STATE.ANIMATION_FOCUS, true );
  623. self.onFocusAnim( t, hitP, self._cameraMatrixState, self._gizmoMatrixState );
  624. } );
  625. } else if ( hitP != null && ! this.enableAnimations ) {
  626. this.updateTbState( STATE.FOCUS, true );
  627. this.focus( hitP, this.scaleFactor );
  628. this.updateTbState( STATE.IDLE, false );
  629. this.dispatchEvent( _changeEvent );
  630. }
  631. }
  632. this.dispatchEvent( _endEvent );
  633. };
  634. this.onDoublePanStart = () => {
  635. if ( this.enabled && this.enablePan ) {
  636. this.dispatchEvent( _startEvent );
  637. this.updateTbState( STATE.PAN, true );
  638. this.setCenter( ( this._touchCurrent[ 0 ].clientX + this._touchCurrent[ 1 ].clientX ) / 2, ( this._touchCurrent[ 0 ].clientY + this._touchCurrent[ 1 ].clientY ) / 2 );
  639. this._startCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement, true ) );
  640. this._currentCursorPosition.copy( this._startCursorPosition );
  641. this.activateGizmos( false );
  642. }
  643. };
  644. this.onDoublePanMove = () => {
  645. if ( this.enabled && this.enablePan ) {
  646. this.setCenter( ( this._touchCurrent[ 0 ].clientX + this._touchCurrent[ 1 ].clientX ) / 2, ( this._touchCurrent[ 0 ].clientY + this._touchCurrent[ 1 ].clientY ) / 2 );
  647. if ( this._state != STATE.PAN ) {
  648. this.updateTbState( STATE.PAN, true );
  649. this._startCursorPosition.copy( this._currentCursorPosition );
  650. }
  651. this._currentCursorPosition.copy( this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement, true ) );
  652. this.applyTransformMatrix( this.pan( this._startCursorPosition, this._currentCursorPosition, true ) );
  653. this.dispatchEvent( _changeEvent );
  654. }
  655. };
  656. this.onDoublePanEnd = () => {
  657. this.updateTbState( STATE.IDLE, false );
  658. this.dispatchEvent( _endEvent );
  659. };
  660. this.onRotateStart = () => {
  661. if ( this.enabled && this.enableRotate ) {
  662. this.dispatchEvent( _startEvent );
  663. this.updateTbState( STATE.ZROTATE, true ); //this._startFingerRotation = event.rotation;
  664. this._startFingerRotation = this.getAngle( this._touchCurrent[ 1 ], this._touchCurrent[ 0 ] ) + this.getAngle( this._touchStart[ 1 ], this._touchStart[ 0 ] );
  665. this._currentFingerRotation = this._startFingerRotation;
  666. this.camera.getWorldDirection( this._rotationAxis ); //rotation axis
  667. if ( ! this.enablePan && ! this.enableZoom ) {
  668. this.activateGizmos( true );
  669. }
  670. }
  671. };
  672. this.onRotateMove = () => {
  673. if ( this.enabled && this.enableRotate ) {
  674. this.setCenter( ( this._touchCurrent[ 0 ].clientX + this._touchCurrent[ 1 ].clientX ) / 2, ( this._touchCurrent[ 0 ].clientY + this._touchCurrent[ 1 ].clientY ) / 2 );
  675. let rotationPoint;
  676. if ( this._state != STATE.ZROTATE ) {
  677. this.updateTbState( STATE.ZROTATE, true );
  678. this._startFingerRotation = this._currentFingerRotation;
  679. } //this._currentFingerRotation = event.rotation;
  680. this._currentFingerRotation = this.getAngle( this._touchCurrent[ 1 ], this._touchCurrent[ 0 ] ) + this.getAngle( this._touchStart[ 1 ], this._touchStart[ 0 ] );
  681. if ( ! this.enablePan ) {
  682. rotationPoint = new THREE.Vector3().setFromMatrixPosition( this._gizmoMatrixState );
  683. } else {
  684. this._v3_2.setFromMatrixPosition( this._gizmoMatrixState );
  685. rotationPoint = this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ).applyQuaternion( this.camera.quaternion ).multiplyScalar( 1 / this.camera.zoom ).add( this._v3_2 );
  686. }
  687. const amount = THREE.MathUtils.DEG2RAD * ( this._startFingerRotation - this._currentFingerRotation );
  688. this.applyTransformMatrix( this.zRotate( rotationPoint, amount ) );
  689. this.dispatchEvent( _changeEvent );
  690. }
  691. };
  692. this.onRotateEnd = () => {
  693. this.updateTbState( STATE.IDLE, false );
  694. this.activateGizmos( false );
  695. this.dispatchEvent( _endEvent );
  696. };
  697. this.onPinchStart = () => {
  698. if ( this.enabled && this.enableZoom ) {
  699. this.dispatchEvent( _startEvent );
  700. this.updateTbState( STATE.SCALE, true );
  701. this._startFingerDistance = this.calculatePointersDistance( this._touchCurrent[ 0 ], this._touchCurrent[ 1 ] );
  702. this._currentFingerDistance = this._startFingerDistance;
  703. this.activateGizmos( false );
  704. }
  705. };
  706. this.onPinchMove = () => {
  707. if ( this.enabled && this.enableZoom ) {
  708. this.setCenter( ( this._touchCurrent[ 0 ].clientX + this._touchCurrent[ 1 ].clientX ) / 2, ( this._touchCurrent[ 0 ].clientY + this._touchCurrent[ 1 ].clientY ) / 2 );
  709. const minDistance = 12; //minimum distance between fingers (in css pixels)
  710. if ( this._state != STATE.SCALE ) {
  711. this._startFingerDistance = this._currentFingerDistance;
  712. this.updateTbState( STATE.SCALE, true );
  713. }
  714. this._currentFingerDistance = Math.max( this.calculatePointersDistance( this._touchCurrent[ 0 ], this._touchCurrent[ 1 ] ), minDistance * this._devPxRatio );
  715. const amount = this._currentFingerDistance / this._startFingerDistance;
  716. let scalePoint;
  717. if ( ! this.enablePan ) {
  718. scalePoint = this._gizmos.position;
  719. } else {
  720. if ( this.camera.isOrthographicCamera ) {
  721. scalePoint = this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ).applyQuaternion( this.camera.quaternion ).multiplyScalar( 1 / this.camera.zoom ).add( this._gizmos.position );
  722. } else if ( this.camera.isPerspectiveCamera ) {
  723. scalePoint = this.unprojectOnTbPlane( this.camera, _center.x, _center.y, this.domElement ).applyQuaternion( this.camera.quaternion ).add( this._gizmos.position );
  724. }
  725. }
  726. this.applyTransformMatrix( this.scale( amount, scalePoint ) );
  727. this.dispatchEvent( _changeEvent );
  728. }
  729. };
  730. this.onPinchEnd = () => {
  731. this.updateTbState( STATE.IDLE, false );
  732. this.dispatchEvent( _endEvent );
  733. };
  734. this.onTriplePanStart = () => {
  735. if ( this.enabled && this.enableZoom ) {
  736. this.dispatchEvent( _startEvent );
  737. this.updateTbState( STATE.SCALE, true ); //const center = event.center;
  738. let clientX = 0;
  739. let clientY = 0;
  740. const nFingers = this._touchCurrent.length;
  741. for ( let i = 0; i < nFingers; i ++ ) {
  742. clientX += this._touchCurrent[ i ].clientX;
  743. clientY += this._touchCurrent[ i ].clientY;
  744. }
  745. this.setCenter( clientX / nFingers, clientY / nFingers );
  746. this._startCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  747. this._currentCursorPosition.copy( this._startCursorPosition );
  748. }
  749. };
  750. this.onTriplePanMove = () => {
  751. if ( this.enabled && this.enableZoom ) {
  752. // fov / 2
  753. // |\
  754. // | \
  755. // | \
  756. // x | \
  757. // | \
  758. // | \
  759. // | _ _ _\
  760. // y
  761. //const center = event.center;
  762. let clientX = 0;
  763. let clientY = 0;
  764. const nFingers = this._touchCurrent.length;
  765. for ( let i = 0; i < nFingers; i ++ ) {
  766. clientX += this._touchCurrent[ i ].clientX;
  767. clientY += this._touchCurrent[ i ].clientY;
  768. }
  769. this.setCenter( clientX / nFingers, clientY / nFingers );
  770. const screenNotches = 8; //how many wheel notches corresponds to a full screen pan
  771. this._currentCursorPosition.setY( this.getCursorNDC( _center.x, _center.y, this.domElement ).y * 0.5 );
  772. const movement = this._currentCursorPosition.y - this._startCursorPosition.y;
  773. let size = 1;
  774. if ( movement < 0 ) {
  775. size = 1 / Math.pow( this.scaleFactor, - movement * screenNotches );
  776. } else if ( movement > 0 ) {
  777. size = Math.pow( this.scaleFactor, movement * screenNotches );
  778. }
  779. this._v3_1.setFromMatrixPosition( this._cameraMatrixState );
  780. const x = this._v3_1.distanceTo( this._gizmos.position );
  781. let xNew = x / size; //distance between camera and gizmos if scale(size, scalepoint) would be performed
  782. //check min and max distance
  783. xNew = THREE.MathUtils.clamp( xNew, this.minDistance, this.maxDistance );
  784. const y = x * Math.tan( THREE.MathUtils.DEG2RAD * this._fovState * 0.5 ); //calculate new fov
  785. let newFov = THREE.MathUtils.RAD2DEG * ( Math.atan( y / xNew ) * 2 ); //check min and max fov
  786. newFov = THREE.MathUtils.clamp( newFov, this.minFov, this.maxFov );
  787. const newDistance = y / Math.tan( THREE.MathUtils.DEG2RAD * ( newFov / 2 ) );
  788. size = x / newDistance;
  789. this._v3_2.setFromMatrixPosition( this._gizmoMatrixState );
  790. this.setFov( newFov );
  791. this.applyTransformMatrix( this.scale( size, this._v3_2, false ) ); //adjusting distance
  792. _offset.copy( this._gizmos.position ).sub( this.camera.position ).normalize().multiplyScalar( newDistance / x );
  793. this._m4_1.makeTranslation( _offset.x, _offset.y, _offset.z );
  794. this.dispatchEvent( _changeEvent );
  795. }
  796. };
  797. this.onTriplePanEnd = () => {
  798. this.updateTbState( STATE.IDLE, false );
  799. this.dispatchEvent( _endEvent ); //this.dispatchEvent( _changeEvent );
  800. };
  801. this.setCenter = ( clientX, clientY ) => {
  802. _center.x = clientX;
  803. _center.y = clientY;
  804. };
  805. this.initializeMouseActions = () => {
  806. this.setMouseAction( 'PAN', 0, 'CTRL' );
  807. this.setMouseAction( 'PAN', 2 );
  808. this.setMouseAction( 'ROTATE', 0 );
  809. this.setMouseAction( 'ZOOM', 'WHEEL' );
  810. this.setMouseAction( 'ZOOM', 1 );
  811. this.setMouseAction( 'FOV', 'WHEEL', 'SHIFT' );
  812. this.setMouseAction( 'FOV', 1, 'SHIFT' );
  813. };
  814. this.compareMouseAction = ( action1, action2 ) => {
  815. if ( action1.operation == action2.operation ) {
  816. if ( action1.mouse == action2.mouse && action1.key == action2.key ) {
  817. return true;
  818. } else {
  819. return false;
  820. }
  821. } else {
  822. return false;
  823. }
  824. };
  825. this.setMouseAction = ( operation, mouse, key = null ) => {
  826. const operationInput = [ 'PAN', 'ROTATE', 'ZOOM', 'FOV' ];
  827. const mouseInput = [ 0, 1, 2, 'WHEEL' ];
  828. const keyInput = [ 'CTRL', 'SHIFT', null ];
  829. let state;
  830. if ( ! operationInput.includes( operation ) || ! mouseInput.includes( mouse ) || ! keyInput.includes( key ) ) {
  831. //invalid parameters
  832. return false;
  833. }
  834. if ( mouse == 'WHEEL' ) {
  835. if ( operation != 'ZOOM' && operation != 'FOV' ) {
  836. //cannot associate 2D operation to 1D input
  837. return false;
  838. }
  839. }
  840. switch ( operation ) {
  841. case 'PAN':
  842. state = STATE.PAN;
  843. break;
  844. case 'ROTATE':
  845. state = STATE.ROTATE;
  846. break;
  847. case 'ZOOM':
  848. state = STATE.SCALE;
  849. break;
  850. case 'FOV':
  851. state = STATE.FOV;
  852. break;
  853. }
  854. const action = {
  855. operation: operation,
  856. mouse: mouse,
  857. key: key,
  858. state: state
  859. };
  860. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  861. if ( this.mouseActions[ i ].mouse == action.mouse && this.mouseActions[ i ].key == action.key ) {
  862. this.mouseActions.splice( i, 1, action );
  863. return true;
  864. }
  865. }
  866. this.mouseActions.push( action );
  867. return true;
  868. };
  869. this.unsetMouseAction = ( mouse, key = null ) => {
  870. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  871. if ( this.mouseActions[ i ].mouse == mouse && this.mouseActions[ i ].key == key ) {
  872. this.mouseActions.splice( i, 1 );
  873. return true;
  874. }
  875. }
  876. return false;
  877. };
  878. this.getOpFromAction = ( mouse, key ) => {
  879. let action;
  880. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  881. action = this.mouseActions[ i ];
  882. if ( action.mouse == mouse && action.key == key ) {
  883. return action.operation;
  884. }
  885. }
  886. if ( key != null ) {
  887. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  888. action = this.mouseActions[ i ];
  889. if ( action.mouse == mouse && action.key == null ) {
  890. return action.operation;
  891. }
  892. }
  893. }
  894. return null;
  895. };
  896. this.getOpStateFromAction = ( mouse, key ) => {
  897. let action;
  898. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  899. action = this.mouseActions[ i ];
  900. if ( action.mouse == mouse && action.key == key ) {
  901. return action.state;
  902. }
  903. }
  904. if ( key != null ) {
  905. for ( let i = 0; i < this.mouseActions.length; i ++ ) {
  906. action = this.mouseActions[ i ];
  907. if ( action.mouse == mouse && action.key == null ) {
  908. return action.state;
  909. }
  910. }
  911. }
  912. return null;
  913. };
  914. this.getAngle = ( p1, p2 ) => {
  915. return Math.atan2( p2.clientY - p1.clientY, p2.clientX - p1.clientX ) * 180 / Math.PI;
  916. };
  917. this.updateTouchEvent = event => {
  918. for ( let i = 0; i < this._touchCurrent.length; i ++ ) {
  919. if ( this._touchCurrent[ i ].pointerId == event.pointerId ) {
  920. this._touchCurrent.splice( i, 1, event );
  921. break;
  922. }
  923. }
  924. };
  925. this.calculateAngularSpeed = ( p0, p1, t0, t1 ) => {
  926. const s = p1 - p0;
  927. const t = ( t1 - t0 ) / 1000;
  928. if ( t == 0 ) {
  929. return 0;
  930. }
  931. return s / t;
  932. };
  933. this.calculatePointersDistance = ( p0, p1 ) => {
  934. return Math.sqrt( Math.pow( p1.clientX - p0.clientX, 2 ) + Math.pow( p1.clientY - p0.clientY, 2 ) );
  935. };
  936. this.calculateRotationAxis = ( vec1, vec2 ) => {
  937. this._rotationMatrix.extractRotation( this._cameraMatrixState );
  938. this._quat.setFromRotationMatrix( this._rotationMatrix );
  939. this._rotationAxis.crossVectors( vec1, vec2 ).applyQuaternion( this._quat );
  940. return this._rotationAxis.normalize().clone();
  941. };
  942. this.calculateTbRadius = camera => {
  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 * this.radiusFactor;
  948. } else if ( camera.type == 'OrthographicCamera' ) {
  949. return Math.min( camera.top, camera.right ) * this.radiusFactor;
  950. }
  951. };
  952. this.focus = ( point, size, amount = 1 ) => {
  953. //move center of camera (along with gizmos) towards point of interest
  954. _offset.copy( point ).sub( this._gizmos.position ).multiplyScalar( amount );
  955. this._translationMatrix.makeTranslation( _offset.x, _offset.y, _offset.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. _offset.copy( scalePoint ).sub( this._v3_1 ).normalize().multiplyScalar( amount );
  1337. this._m4_1.makeTranslation( _offset.x, _offset.y, _offset.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. _offset.copy( scalePoint ).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( _offset.x, _offset.y, _offset.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.unprojectOnObj = ( cursor, camera ) => {
  1384. const raycaster = this.getRaycaster();
  1385. raycaster.near = camera.near;
  1386. raycaster.far = camera.far;
  1387. raycaster.setFromCamera( cursor, camera );
  1388. const intersect = raycaster.intersectObjects( this.scene.children, true );
  1389. for ( let i = 0; i < intersect.length; i ++ ) {
  1390. if ( intersect[ i ].object.uuid != this._gizmos.uuid && intersect[ i ].face != null ) {
  1391. return intersect[ i ].point.clone();
  1392. }
  1393. }
  1394. return null;
  1395. };
  1396. this.unprojectOnTbSurface = ( camera, cursorX, cursorY, canvas, tbRadius ) => {
  1397. if ( camera.type == 'OrthographicCamera' ) {
  1398. this._v2_1.copy( this.getCursorPosition( cursorX, cursorY, canvas ) );
  1399. this._v3_1.set( this._v2_1.x, this._v2_1.y, 0 );
  1400. const x2 = Math.pow( this._v2_1.x, 2 );
  1401. const y2 = Math.pow( this._v2_1.y, 2 );
  1402. const r2 = Math.pow( this._tbRadius, 2 );
  1403. if ( x2 + y2 <= r2 * 0.5 ) {
  1404. //intersection with sphere
  1405. this._v3_1.setZ( Math.sqrt( r2 - ( x2 + y2 ) ) );
  1406. } else {
  1407. //intersection with hyperboloid
  1408. this._v3_1.setZ( r2 * 0.5 / Math.sqrt( x2 + y2 ) );
  1409. }
  1410. return this._v3_1;
  1411. } else if ( camera.type == 'PerspectiveCamera' ) {
  1412. //unproject cursor on the near plane
  1413. this._v2_1.copy( this.getCursorNDC( cursorX, cursorY, canvas ) );
  1414. this._v3_1.set( this._v2_1.x, this._v2_1.y, - 1 );
  1415. this._v3_1.applyMatrix4( camera.projectionMatrixInverse );
  1416. const rayDir = this._v3_1.clone().normalize(); //unprojected ray direction
  1417. const cameraGizmoDistance = camera.position.distanceTo( this._gizmos.position );
  1418. const radius2 = Math.pow( tbRadius, 2 ); // camera
  1419. // |\
  1420. // | \
  1421. // | \
  1422. // h | \
  1423. // | \
  1424. // | \
  1425. // _ _ | _ _ _\ _ _ near plane
  1426. // l
  1427. const h = this._v3_1.z;
  1428. const l = Math.sqrt( Math.pow( this._v3_1.x, 2 ) + Math.pow( this._v3_1.y, 2 ) );
  1429. if ( l == 0 ) {
  1430. //ray aligned with camera
  1431. rayDir.set( this._v3_1.x, this._v3_1.y, tbRadius );
  1432. return rayDir;
  1433. }
  1434. const m = h / l;
  1435. const q = cameraGizmoDistance;
  1436. /*
  1437. * calculate intersection point between unprojected ray and trackball surface
  1438. *|y = m * x + q
  1439. *|x^2 + y^2 = r^2
  1440. *
  1441. * (m^2 + 1) * x^2 + (2 * m * q) * x + q^2 - r^2 = 0
  1442. */
  1443. let a = Math.pow( m, 2 ) + 1;
  1444. let b = 2 * m * q;
  1445. let c = Math.pow( q, 2 ) - radius2;
  1446. let delta = Math.pow( b, 2 ) - 4 * a * c;
  1447. if ( delta >= 0 ) {
  1448. //intersection with sphere
  1449. this._v2_1.setX( ( - b - Math.sqrt( delta ) ) / ( 2 * a ) );
  1450. this._v2_1.setY( m * this._v2_1.x + q );
  1451. const angle = THREE.MathUtils.RAD2DEG * this._v2_1.angle();
  1452. if ( angle >= 45 ) {
  1453. //if angle between intersection point and X' axis is >= 45°, return that point
  1454. //otherwise, calculate intersection point with hyperboloid
  1455. const rayLength = Math.sqrt( Math.pow( this._v2_1.x, 2 ) + Math.pow( cameraGizmoDistance - this._v2_1.y, 2 ) );
  1456. rayDir.multiplyScalar( rayLength );
  1457. rayDir.z += cameraGizmoDistance;
  1458. return rayDir;
  1459. }
  1460. } //intersection with hyperboloid
  1461. /*
  1462. *|y = m * x + q
  1463. *|y = (1 / x) * (r^2 / 2)
  1464. *
  1465. * m * x^2 + q * x - r^2 / 2 = 0
  1466. */
  1467. a = m;
  1468. b = q;
  1469. c = - radius2 * 0.5;
  1470. delta = Math.pow( b, 2 ) - 4 * a * c;
  1471. this._v2_1.setX( ( - b - Math.sqrt( delta ) ) / ( 2 * a ) );
  1472. this._v2_1.setY( m * this._v2_1.x + q );
  1473. const rayLength = Math.sqrt( Math.pow( this._v2_1.x, 2 ) + Math.pow( cameraGizmoDistance - this._v2_1.y, 2 ) );
  1474. rayDir.multiplyScalar( rayLength );
  1475. rayDir.z += cameraGizmoDistance;
  1476. return rayDir;
  1477. }
  1478. };
  1479. this.unprojectOnTbPlane = ( camera, cursorX, cursorY, canvas, initialDistance = false ) => {
  1480. if ( camera.type == 'OrthographicCamera' ) {
  1481. this._v2_1.copy( this.getCursorPosition( cursorX, cursorY, canvas ) );
  1482. this._v3_1.set( this._v2_1.x, this._v2_1.y, 0 );
  1483. return this._v3_1.clone();
  1484. } else if ( camera.type == 'PerspectiveCamera' ) {
  1485. this._v2_1.copy( this.getCursorNDC( cursorX, cursorY, canvas ) ); //unproject cursor on the near plane
  1486. this._v3_1.set( this._v2_1.x, this._v2_1.y, - 1 );
  1487. this._v3_1.applyMatrix4( camera.projectionMatrixInverse );
  1488. const rayDir = this._v3_1.clone().normalize(); //unprojected ray direction
  1489. // camera
  1490. // |\
  1491. // | \
  1492. // | \
  1493. // h | \
  1494. // | \
  1495. // | \
  1496. // _ _ | _ _ _\ _ _ near plane
  1497. // l
  1498. const h = this._v3_1.z;
  1499. const l = Math.sqrt( Math.pow( this._v3_1.x, 2 ) + Math.pow( this._v3_1.y, 2 ) );
  1500. let cameraGizmoDistance;
  1501. if ( initialDistance ) {
  1502. cameraGizmoDistance = this._v3_1.setFromMatrixPosition( this._cameraMatrixState0 ).distanceTo( this._v3_2.setFromMatrixPosition( this._gizmoMatrixState0 ) );
  1503. } else {
  1504. cameraGizmoDistance = camera.position.distanceTo( this._gizmos.position );
  1505. }
  1506. /*
  1507. * calculate intersection point between unprojected ray and the plane
  1508. *|y = mx + q
  1509. *|y = 0
  1510. *
  1511. * x = -q/m
  1512. */
  1513. if ( l == 0 ) {
  1514. //ray aligned with camera
  1515. rayDir.set( 0, 0, 0 );
  1516. return rayDir;
  1517. }
  1518. const m = h / l;
  1519. const q = cameraGizmoDistance;
  1520. const x = - q / m;
  1521. const rayLength = Math.sqrt( Math.pow( q, 2 ) + Math.pow( x, 2 ) );
  1522. rayDir.multiplyScalar( rayLength );
  1523. rayDir.z = 0;
  1524. return rayDir;
  1525. }
  1526. };
  1527. this.updateMatrixState = () => {
  1528. //update camera and gizmos state
  1529. this._cameraMatrixState.copy( this.camera.matrix );
  1530. this._gizmoMatrixState.copy( this._gizmos.matrix );
  1531. if ( this.camera.isOrthographicCamera ) {
  1532. this._cameraProjectionState.copy( this.camera.projectionMatrix );
  1533. this.camera.updateProjectionMatrix();
  1534. this._zoomState = this.camera.zoom;
  1535. } else if ( this.camera.isPerspectiveCamera ) {
  1536. this._fovState = this.camera.fov;
  1537. }
  1538. };
  1539. this.updateTbState = ( newState, updateMatrices ) => {
  1540. this._state = newState;
  1541. if ( updateMatrices ) {
  1542. this.updateMatrixState();
  1543. }
  1544. };
  1545. this.update = () => {
  1546. const EPS = 0.000001; //check min/max parameters
  1547. if ( this.camera.isOrthographicCamera ) {
  1548. //check zoom
  1549. if ( this.camera.zoom > this.maxZoom || this.camera.zoom < this.minZoom ) {
  1550. const newZoom = THREE.MathUtils.clamp( this.camera.zoom, this.minZoom, this.maxZoom );
  1551. this.applyTransformMatrix( this.scale( newZoom / this.camera.zoom, this._gizmos.position, true ) );
  1552. }
  1553. } else if ( this.camera.isPerspectiveCamera ) {
  1554. //check distance
  1555. const distance = this.camera.position.distanceTo( this._gizmos.position );
  1556. if ( distance > this.maxDistance + EPS || distance < this.minDistance - EPS ) {
  1557. const newDistance = THREE.MathUtils.clamp( distance, this.minDistance, this.maxDistance );
  1558. this.applyTransformMatrix( this.scale( newDistance / distance, this._gizmos.position ) );
  1559. this.updateMatrixState();
  1560. } //check fov
  1561. if ( this.camera.fov < this.minFov || this.camera.fov > this.maxFov ) {
  1562. this.camera.fov = THREE.MathUtils.clamp( this.camera.fov, this.minFov, this.maxFov );
  1563. this.camera.updateProjectionMatrix();
  1564. }
  1565. const oldRadius = this._tbRadius;
  1566. this._tbRadius = this.calculateTbRadius( this.camera );
  1567. if ( oldRadius < this._tbRadius - EPS || oldRadius > this._tbRadius + EPS ) {
  1568. const scale = ( this._gizmos.scale.x + this._gizmos.scale.y + this._gizmos.scale.z ) / 3;
  1569. const newRadius = this._tbRadius / scale;
  1570. const curve = new THREE.EllipseCurve( 0, 0, newRadius, newRadius );
  1571. const points = curve.getPoints( this._curvePts );
  1572. const curveGeometry = new THREE.BufferGeometry().setFromPoints( points );
  1573. for ( const gizmo in this._gizmos.children ) {
  1574. this._gizmos.children[ gizmo ].geometry = curveGeometry;
  1575. }
  1576. }
  1577. }
  1578. this.camera.lookAt( this._gizmos.position );
  1579. };
  1580. this.setStateFromJSON = json => {
  1581. const state = JSON.parse( json );
  1582. if ( state.arcballState != undefined ) {
  1583. this._cameraMatrixState.fromArray( state.arcballState.cameraMatrix.elements );
  1584. this._cameraMatrixState.decompose( this.camera.position, this.camera.quaternion, this.camera.scale );
  1585. this.camera.up.copy( state.arcballState.cameraUp );
  1586. this.camera.near = state.arcballState.cameraNear;
  1587. this.camera.far = state.arcballState.cameraFar;
  1588. this.camera.zoom = state.arcballState.cameraZoom;
  1589. if ( this.camera.isPerspectiveCamera ) {
  1590. this.camera.fov = state.arcballState.cameraFov;
  1591. }
  1592. this._gizmoMatrixState.fromArray( state.arcballState.gizmoMatrix.elements );
  1593. this._gizmoMatrixState.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1594. this.camera.updateMatrix();
  1595. this.camera.updateProjectionMatrix();
  1596. this._gizmos.updateMatrix();
  1597. this._tbRadius = this.calculateTbRadius( this.camera );
  1598. const gizmoTmp = new THREE.Matrix4().copy( this._gizmoMatrixState0 );
  1599. this.makeGizmos( this._gizmos.position, this._tbRadius );
  1600. this._gizmoMatrixState0.copy( gizmoTmp );
  1601. this.camera.lookAt( this._gizmos.position );
  1602. this.updateTbState( STATE.IDLE, false );
  1603. this.dispatchEvent( _changeEvent );
  1604. }
  1605. };
  1606. this.camera = null;
  1607. this.domElement = domElement;
  1608. this.scene = scene;
  1609. this.target = new THREE.Vector3( 0, 0, 0 );
  1610. this.radiusFactor = 0.67;
  1611. this.mouseActions = [];
  1612. 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)
  1613. this._v2_1 = new THREE.Vector2();
  1614. this._v3_1 = new THREE.Vector3();
  1615. this._v3_2 = new THREE.Vector3();
  1616. this._m4_1 = new THREE.Matrix4();
  1617. this._m4_2 = new THREE.Matrix4();
  1618. this._quat = new THREE.Quaternion(); //transformation matrices
  1619. this._translationMatrix = new THREE.Matrix4(); //matrix for translation operation
  1620. this._rotationMatrix = new THREE.Matrix4(); //matrix for rotation operation
  1621. this._scaleMatrix = new THREE.Matrix4(); //matrix for scaling operation
  1622. this._rotationAxis = new THREE.Vector3(); //axis for rotate operation
  1623. //camera state
  1624. this._cameraMatrixState = new THREE.Matrix4();
  1625. this._cameraProjectionState = new THREE.Matrix4();
  1626. this._fovState = 1;
  1627. this._upState = new THREE.Vector3();
  1628. this._zoomState = 1;
  1629. this._nearPos = 0;
  1630. this._farPos = 0;
  1631. this._gizmoMatrixState = new THREE.Matrix4(); //initial values
  1632. this._up0 = new THREE.Vector3();
  1633. this._zoom0 = 1;
  1634. this._fov0 = 0;
  1635. this._initialNear = 0;
  1636. this._nearPos0 = 0;
  1637. this._initialFar = 0;
  1638. this._farPos0 = 0;
  1639. this._cameraMatrixState0 = new THREE.Matrix4();
  1640. this._gizmoMatrixState0 = new THREE.Matrix4(); //pointers array
  1641. this._button = - 1;
  1642. this._touchStart = [];
  1643. this._touchCurrent = [];
  1644. this._input = INPUT.NONE; //two fingers touch interaction
  1645. this._switchSensibility = 32; //minimum movement to be performed to fire single pan start after the second finger has been released
  1646. this._startFingerDistance = 0; //distance between two fingers
  1647. this._currentFingerDistance = 0;
  1648. this._startFingerRotation = 0; //amount of rotation performed with two fingers
  1649. this._currentFingerRotation = 0; //double tap
  1650. this._devPxRatio = 0;
  1651. this._downValid = true;
  1652. this._nclicks = 0;
  1653. this._downEvents = [];
  1654. this._downStart = 0; //pointerDown time
  1655. this._clickStart = 0; //first click time
  1656. this._maxDownTime = 250;
  1657. this._maxInterval = 300;
  1658. this._posThreshold = 24;
  1659. this._movementThreshold = 24; //cursor positions
  1660. this._currentCursorPosition = new THREE.Vector3();
  1661. this._startCursorPosition = new THREE.Vector3(); //grid
  1662. this._grid = null; //grid to be visualized during pan operation
  1663. this._gridPosition = new THREE.Vector3(); //gizmos
  1664. this._gizmos = new THREE.Group();
  1665. this._curvePts = 128; //animations
  1666. this._timeStart = - 1; //initial time
  1667. this._animationId = - 1; //focus animation
  1668. this.focusAnimationTime = 500; //duration of focus animation in ms
  1669. //rotate animation
  1670. this._timePrev = 0; //time at which previous rotate operation has been detected
  1671. this._timeCurrent = 0; //time at which current rotate operation has been detected
  1672. this._anglePrev = 0; //angle of previous rotation
  1673. this._angleCurrent = 0; //angle of current rotation
  1674. this._cursorPosPrev = new THREE.Vector3(); //cursor position when previous rotate operation has been detected
  1675. this._cursorPosCurr = new THREE.Vector3(); //cursor position when current rotate operation has been detected
  1676. this._wPrev = 0; //angular velocity of the previous rotate operation
  1677. this._wCurr = 0; //angular velocity of the current rotate operation
  1678. //parameters
  1679. this.adjustNearFar = false;
  1680. this.scaleFactor = 1.1; //zoom/distance multiplier
  1681. this.dampingFactor = 25;
  1682. this.wMax = 20; //maximum angular velocity allowed
  1683. this.enableAnimations = true; //if animations should be performed
  1684. this.enableGrid = false; //if grid should be showed during pan operation
  1685. this.cursorZoom = false; //if wheel zoom should be cursor centered
  1686. this.minFov = 5;
  1687. this.maxFov = 90;
  1688. this.enabled = true;
  1689. this.enablePan = true;
  1690. this.enableRotate = true;
  1691. this.enableZoom = true;
  1692. this.enableGizmos = true;
  1693. this.minDistance = 0;
  1694. this.maxDistance = Infinity;
  1695. this.minZoom = 0;
  1696. this.maxZoom = Infinity; //trackball parameters
  1697. this._tbRadius = 1; //FSA
  1698. this._state = STATE.IDLE;
  1699. this.setCamera( _camera );
  1700. if ( this.scene != null ) {
  1701. this.scene.add( this._gizmos );
  1702. }
  1703. this.domElement.style.touchAction = 'none';
  1704. this._devPxRatio = window.devicePixelRatio;
  1705. this.initializeMouseActions();
  1706. this.domElement.addEventListener( 'contextmenu', this.onContextMenu );
  1707. this.domElement.addEventListener( 'wheel', this.onWheel );
  1708. this.domElement.addEventListener( 'pointerdown', this.onPointerDown );
  1709. this.domElement.addEventListener( 'pointercancel', this.onPointerCancel );
  1710. window.addEventListener( 'keydown', this.onKeyDown );
  1711. window.addEventListener( 'resize', this.onWindowResize );
  1712. } //listeners
  1713. /**
  1714. * Apply a transformation matrix, to the camera and gizmos
  1715. * @param {Object} transformation Object containing matrices to apply to camera and gizmos
  1716. */
  1717. applyTransformMatrix( transformation ) {
  1718. if ( transformation.camera != null ) {
  1719. this._m4_1.copy( this._cameraMatrixState ).premultiply( transformation.camera );
  1720. this._m4_1.decompose( this.camera.position, this.camera.quaternion, this.camera.scale );
  1721. this.camera.updateMatrix(); //update camera up vector
  1722. if ( this._state == STATE.ROTATE || this._state == STATE.ZROTATE || this._state == STATE.ANIMATION_ROTATE ) {
  1723. this.camera.up.copy( this._upState ).applyQuaternion( this.camera.quaternion );
  1724. }
  1725. }
  1726. if ( transformation.gizmos != null ) {
  1727. this._m4_1.copy( this._gizmoMatrixState ).premultiply( transformation.gizmos );
  1728. this._m4_1.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1729. this._gizmos.updateMatrix();
  1730. }
  1731. if ( this._state == STATE.SCALE || this._state == STATE.FOCUS || this._state == STATE.ANIMATION_FOCUS ) {
  1732. this._tbRadius = this.calculateTbRadius( this.camera );
  1733. if ( this.adjustNearFar ) {
  1734. const cameraDistance = this.camera.position.distanceTo( this._gizmos.position );
  1735. const bb = new THREE.Box3();
  1736. bb.setFromObject( this._gizmos );
  1737. const sphere = new THREE.Sphere();
  1738. bb.getBoundingSphere( sphere );
  1739. const adjustedNearPosition = Math.max( this._nearPos0, sphere.radius + sphere.center.length() );
  1740. const regularNearPosition = cameraDistance - this._initialNear;
  1741. const minNearPos = Math.min( adjustedNearPosition, regularNearPosition );
  1742. this.camera.near = cameraDistance - minNearPos;
  1743. const adjustedFarPosition = Math.min( this._farPos0, - sphere.radius + sphere.center.length() );
  1744. const regularFarPosition = cameraDistance - this._initialFar;
  1745. const minFarPos = Math.min( adjustedFarPosition, regularFarPosition );
  1746. this.camera.far = cameraDistance - minFarPos;
  1747. this.camera.updateProjectionMatrix();
  1748. } else {
  1749. let update = false;
  1750. if ( this.camera.near != this._initialNear ) {
  1751. this.camera.near = this._initialNear;
  1752. update = true;
  1753. }
  1754. if ( this.camera.far != this._initialFar ) {
  1755. this.camera.far = this._initialFar;
  1756. update = true;
  1757. }
  1758. if ( update ) {
  1759. this.camera.updateProjectionMatrix();
  1760. }
  1761. }
  1762. }
  1763. }
  1764. /**
  1765. * Calculate the angular speed
  1766. * @param {Number} p0 Position at t0
  1767. * @param {Number} p1 Position at t1
  1768. * @param {Number} t0 Initial time in milliseconds
  1769. * @param {Number} t1 Ending time in milliseconds
  1770. */
  1771. /**
  1772. * Set gizmos visibility
  1773. * @param {Boolean} value Value of gizmos visibility
  1774. */
  1775. setGizmosVisible( value ) {
  1776. this._gizmos.visible = value;
  1777. this.dispatchEvent( _changeEvent );
  1778. }
  1779. /**
  1780. * Set gizmos radius factor and redraws gizmos
  1781. * @param {Float} value Value of radius factor
  1782. */
  1783. setTbRadius( value ) {
  1784. this.radiusFactor = value;
  1785. this._tbRadius = this.calculateTbRadius( this.camera );
  1786. const curve = new THREE.EllipseCurve( 0, 0, this._tbRadius, this._tbRadius );
  1787. const points = curve.getPoints( this._curvePts );
  1788. const curveGeometry = new THREE.BufferGeometry().setFromPoints( points );
  1789. for ( const gizmo in this._gizmos.children ) {
  1790. this._gizmos.children[ gizmo ].geometry = curveGeometry;
  1791. }
  1792. this.dispatchEvent( _changeEvent );
  1793. }
  1794. /**
  1795. * Creates the rotation gizmos matching trackball center and radius
  1796. * @param {Vector3} tbCenter The trackball center
  1797. * @param {number} tbRadius The trackball radius
  1798. */
  1799. /**
  1800. * Set values in transformation object
  1801. * @param {Matrix4} camera Transformation to be applied to the camera
  1802. * @param {Matrix4} gizmos Transformation to be applied to gizmos
  1803. */
  1804. setTransformationMatrices( camera = null, gizmos = null ) {
  1805. if ( camera != null ) {
  1806. if ( _transformation.camera != null ) {
  1807. _transformation.camera.copy( camera );
  1808. } else {
  1809. _transformation.camera = camera.clone();
  1810. }
  1811. } else {
  1812. _transformation.camera = null;
  1813. }
  1814. if ( gizmos != null ) {
  1815. if ( _transformation.gizmos != null ) {
  1816. _transformation.gizmos.copy( gizmos );
  1817. } else {
  1818. _transformation.gizmos = gizmos.clone();
  1819. }
  1820. } else {
  1821. _transformation.gizmos = null;
  1822. }
  1823. }
  1824. /**
  1825. * Rotate camera around its direction axis passing by a given point by a given angle
  1826. * @param {Vector3} point The point where the rotation axis is passing trough
  1827. * @param {Number} angle Angle in radians
  1828. * @returns The computed transormation matix
  1829. */
  1830. getRaycaster() {
  1831. return _raycaster;
  1832. }
  1833. /**
  1834. * Unproject the cursor on the 3D object surface
  1835. * @param {Vector2} cursor Cursor coordinates in NDC
  1836. * @param {Camera} camera Virtual camera
  1837. * @returns {Vector3} The point of intersection with the model, if exist, null otherwise
  1838. */
  1839. }
  1840. THREE.ArcballControls = ArcballControls;
  1841. } )();