ArcballControls.js 75 KB

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