ArcballControls.js 75 KB

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