ArcballControls.js 75 KB

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