ArcballControls.js 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023
  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.zRotate = ( point, angle ) => {
  1356. this._rotationMatrix.makeRotationAxis( this._rotationAxis, angle );
  1357. this._translationMatrix.makeTranslation( - point.x, - point.y, - point.z );
  1358. this._m4_1.makeTranslation( point.x, point.y, point.z );
  1359. this._m4_1.multiply( this._rotationMatrix );
  1360. this._m4_1.multiply( this._translationMatrix );
  1361. this._v3_1.setFromMatrixPosition( this._gizmoMatrixState ).sub( point ); //vector from rotation center to gizmos position
  1362. this._v3_2.copy( this._v3_1 ).applyAxisAngle( this._rotationAxis, angle ); //apply rotation
  1363. this._v3_2.sub( this._v3_1 );
  1364. this._m4_2.makeTranslation( this._v3_2.x, this._v3_2.y, this._v3_2.z );
  1365. this.setTransformationMatrices( this._m4_1, this._m4_2 );
  1366. return _transformation;
  1367. };
  1368. this.unprojectOnObj = ( cursor, camera ) => {
  1369. const raycaster = this.getRaycaster();
  1370. raycaster.near = camera.near;
  1371. raycaster.far = camera.far;
  1372. raycaster.setFromCamera( cursor, camera );
  1373. const intersect = raycaster.intersectObjects( this.scene.children, true );
  1374. for ( let i = 0; i < intersect.length; i ++ ) {
  1375. if ( intersect[ i ].object.uuid != this._gizmos.uuid && intersect[ i ].face != null ) {
  1376. return intersect[ i ].point.clone();
  1377. }
  1378. }
  1379. return null;
  1380. };
  1381. this.unprojectOnTbSurface = ( camera, cursorX, cursorY, canvas, tbRadius ) => {
  1382. if ( camera.type == 'OrthographicCamera' ) {
  1383. this._v2_1.copy( this.getCursorPosition( cursorX, cursorY, canvas ) );
  1384. this._v3_1.set( this._v2_1.x, this._v2_1.y, 0 );
  1385. const x2 = Math.pow( this._v2_1.x, 2 );
  1386. const y2 = Math.pow( this._v2_1.y, 2 );
  1387. const r2 = Math.pow( this._tbRadius, 2 );
  1388. if ( x2 + y2 <= r2 * 0.5 ) {
  1389. //intersection with sphere
  1390. this._v3_1.setZ( Math.sqrt( r2 - ( x2 + y2 ) ) );
  1391. } else {
  1392. //intersection with hyperboloid
  1393. this._v3_1.setZ( r2 * 0.5 / Math.sqrt( x2 + y2 ) );
  1394. }
  1395. return this._v3_1;
  1396. } else if ( camera.type == 'PerspectiveCamera' ) {
  1397. //unproject cursor on the near plane
  1398. this._v2_1.copy( this.getCursorNDC( cursorX, cursorY, canvas ) );
  1399. this._v3_1.set( this._v2_1.x, this._v2_1.y, - 1 );
  1400. this._v3_1.applyMatrix4( camera.projectionMatrixInverse );
  1401. const rayDir = this._v3_1.clone().normalize(); //unprojected ray direction
  1402. const cameraGizmoDistance = camera.position.distanceTo( this._gizmos.position );
  1403. const radius2 = Math.pow( tbRadius, 2 ); // camera
  1404. // |\
  1405. // | \
  1406. // | \
  1407. // h | \
  1408. // | \
  1409. // | \
  1410. // _ _ | _ _ _\ _ _ near plane
  1411. // l
  1412. const h = this._v3_1.z;
  1413. const l = Math.sqrt( Math.pow( this._v3_1.x, 2 ) + Math.pow( this._v3_1.y, 2 ) );
  1414. if ( l == 0 ) {
  1415. //ray aligned with camera
  1416. rayDir.set( this._v3_1.x, this._v3_1.y, tbRadius );
  1417. return rayDir;
  1418. }
  1419. const m = h / l;
  1420. const q = cameraGizmoDistance;
  1421. /*
  1422. * calculate intersection point between unprojected ray and trackball surface
  1423. *|y = m * x + q
  1424. *|x^2 + y^2 = r^2
  1425. *
  1426. * (m^2 + 1) * x^2 + (2 * m * q) * x + q^2 - r^2 = 0
  1427. */
  1428. let a = Math.pow( m, 2 ) + 1;
  1429. let b = 2 * m * q;
  1430. let c = Math.pow( q, 2 ) - radius2;
  1431. let delta = Math.pow( b, 2 ) - 4 * a * c;
  1432. if ( delta >= 0 ) {
  1433. //intersection with sphere
  1434. this._v2_1.setX( ( - b - Math.sqrt( delta ) ) / ( 2 * a ) );
  1435. this._v2_1.setY( m * this._v2_1.x + q );
  1436. const angle = THREE.MathUtils.RAD2DEG * this._v2_1.angle();
  1437. if ( angle >= 45 ) {
  1438. //if angle between intersection point and X' axis is >= 45°, return that point
  1439. //otherwise, calculate intersection point with hyperboloid
  1440. const rayLength = Math.sqrt( Math.pow( this._v2_1.x, 2 ) + Math.pow( cameraGizmoDistance - this._v2_1.y, 2 ) );
  1441. rayDir.multiplyScalar( rayLength );
  1442. rayDir.z += cameraGizmoDistance;
  1443. return rayDir;
  1444. }
  1445. } //intersection with hyperboloid
  1446. /*
  1447. *|y = m * x + q
  1448. *|y = (1 / x) * (r^2 / 2)
  1449. *
  1450. * m * x^2 + q * x - r^2 / 2 = 0
  1451. */
  1452. a = m;
  1453. b = q;
  1454. c = - radius2 * 0.5;
  1455. delta = Math.pow( b, 2 ) - 4 * a * c;
  1456. this._v2_1.setX( ( - b - Math.sqrt( delta ) ) / ( 2 * a ) );
  1457. this._v2_1.setY( m * this._v2_1.x + q );
  1458. const rayLength = Math.sqrt( Math.pow( this._v2_1.x, 2 ) + Math.pow( cameraGizmoDistance - this._v2_1.y, 2 ) );
  1459. rayDir.multiplyScalar( rayLength );
  1460. rayDir.z += cameraGizmoDistance;
  1461. return rayDir;
  1462. }
  1463. };
  1464. this.unprojectOnTbPlane = ( camera, cursorX, cursorY, canvas, initialDistance = false ) => {
  1465. if ( camera.type == 'OrthographicCamera' ) {
  1466. this._v2_1.copy( this.getCursorPosition( cursorX, cursorY, canvas ) );
  1467. this._v3_1.set( this._v2_1.x, this._v2_1.y, 0 );
  1468. return this._v3_1.clone();
  1469. } else if ( camera.type == 'PerspectiveCamera' ) {
  1470. this._v2_1.copy( this.getCursorNDC( cursorX, cursorY, canvas ) ); //unproject cursor on the near plane
  1471. this._v3_1.set( this._v2_1.x, this._v2_1.y, - 1 );
  1472. this._v3_1.applyMatrix4( camera.projectionMatrixInverse );
  1473. const rayDir = this._v3_1.clone().normalize(); //unprojected ray direction
  1474. // camera
  1475. // |\
  1476. // | \
  1477. // | \
  1478. // h | \
  1479. // | \
  1480. // | \
  1481. // _ _ | _ _ _\ _ _ near plane
  1482. // l
  1483. const h = this._v3_1.z;
  1484. const l = Math.sqrt( Math.pow( this._v3_1.x, 2 ) + Math.pow( this._v3_1.y, 2 ) );
  1485. let cameraGizmoDistance;
  1486. if ( initialDistance ) {
  1487. cameraGizmoDistance = this._v3_1.setFromMatrixPosition( this._cameraMatrixState0 ).distanceTo( this._v3_2.setFromMatrixPosition( this._gizmoMatrixState0 ) );
  1488. } else {
  1489. cameraGizmoDistance = camera.position.distanceTo( this._gizmos.position );
  1490. }
  1491. /*
  1492. * calculate intersection point between unprojected ray and the plane
  1493. *|y = mx + q
  1494. *|y = 0
  1495. *
  1496. * x = -q/m
  1497. */
  1498. if ( l == 0 ) {
  1499. //ray aligned with camera
  1500. rayDir.set( 0, 0, 0 );
  1501. return rayDir;
  1502. }
  1503. const m = h / l;
  1504. const q = cameraGizmoDistance;
  1505. const x = - q / m;
  1506. const rayLength = Math.sqrt( Math.pow( q, 2 ) + Math.pow( x, 2 ) );
  1507. rayDir.multiplyScalar( rayLength );
  1508. rayDir.z = 0;
  1509. return rayDir;
  1510. }
  1511. };
  1512. this.updateMatrixState = () => {
  1513. //update camera and gizmos state
  1514. this._cameraMatrixState.copy( this.camera.matrix );
  1515. this._gizmoMatrixState.copy( this._gizmos.matrix );
  1516. if ( this.camera.isOrthographicCamera ) {
  1517. this._cameraProjectionState.copy( this.camera.projectionMatrix );
  1518. this.camera.updateProjectionMatrix();
  1519. this._zoomState = this.camera.zoom;
  1520. } else if ( this.camera.isPerspectiveCamera ) {
  1521. this._fovState = this.camera.fov;
  1522. }
  1523. };
  1524. this.updateTbState = ( newState, updateMatrices ) => {
  1525. this._state = newState;
  1526. if ( updateMatrices ) {
  1527. this.updateMatrixState();
  1528. }
  1529. };
  1530. this.update = () => {
  1531. const EPS = 0.000001;
  1532. if ( this.target.equals( this._currentTarget ) === false ) {
  1533. this._gizmos.position.copy( this.target ); //for correct radius calculation
  1534. this._tbRadius = this.calculateTbRadius( this.camera );
  1535. this.makeGizmos( this.target, this._tbRadius );
  1536. this._currentTarget.copy( this.target );
  1537. } //check min/max parameters
  1538. if ( this.camera.isOrthographicCamera ) {
  1539. //check zoom
  1540. if ( this.camera.zoom > this.maxZoom || this.camera.zoom < this.minZoom ) {
  1541. const newZoom = THREE.MathUtils.clamp( this.camera.zoom, this.minZoom, this.maxZoom );
  1542. this.applyTransformMatrix( this.scale( newZoom / this.camera.zoom, this._gizmos.position, true ) );
  1543. }
  1544. } else if ( this.camera.isPerspectiveCamera ) {
  1545. //check distance
  1546. const distance = this.camera.position.distanceTo( this._gizmos.position );
  1547. if ( distance > this.maxDistance + EPS || distance < this.minDistance - EPS ) {
  1548. const newDistance = THREE.MathUtils.clamp( distance, this.minDistance, this.maxDistance );
  1549. this.applyTransformMatrix( this.scale( newDistance / distance, this._gizmos.position ) );
  1550. this.updateMatrixState();
  1551. } //check fov
  1552. if ( this.camera.fov < this.minFov || this.camera.fov > this.maxFov ) {
  1553. this.camera.fov = THREE.MathUtils.clamp( this.camera.fov, this.minFov, this.maxFov );
  1554. this.camera.updateProjectionMatrix();
  1555. }
  1556. const oldRadius = this._tbRadius;
  1557. this._tbRadius = this.calculateTbRadius( this.camera );
  1558. if ( oldRadius < this._tbRadius - EPS || oldRadius > this._tbRadius + EPS ) {
  1559. const scale = ( this._gizmos.scale.x + this._gizmos.scale.y + this._gizmos.scale.z ) / 3;
  1560. const newRadius = this._tbRadius / scale;
  1561. const curve = new THREE.EllipseCurve( 0, 0, newRadius, newRadius );
  1562. const points = curve.getPoints( this._curvePts );
  1563. const curveGeometry = new THREE.BufferGeometry().setFromPoints( points );
  1564. for ( const gizmo in this._gizmos.children ) {
  1565. this._gizmos.children[ gizmo ].geometry = curveGeometry;
  1566. }
  1567. }
  1568. }
  1569. this.camera.lookAt( this._gizmos.position );
  1570. };
  1571. this.setStateFromJSON = json => {
  1572. const state = JSON.parse( json );
  1573. if ( state.arcballState != undefined ) {
  1574. this._cameraMatrixState.fromArray( state.arcballState.cameraMatrix.elements );
  1575. this._cameraMatrixState.decompose( this.camera.position, this.camera.quaternion, this.camera.scale );
  1576. this.camera.up.copy( state.arcballState.cameraUp );
  1577. this.camera.near = state.arcballState.cameraNear;
  1578. this.camera.far = state.arcballState.cameraFar;
  1579. this.camera.zoom = state.arcballState.cameraZoom;
  1580. if ( this.camera.isPerspectiveCamera ) {
  1581. this.camera.fov = state.arcballState.cameraFov;
  1582. }
  1583. this._gizmoMatrixState.fromArray( state.arcballState.gizmoMatrix.elements );
  1584. this._gizmoMatrixState.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1585. this.camera.updateMatrix();
  1586. this.camera.updateProjectionMatrix();
  1587. this._gizmos.updateMatrix();
  1588. this._tbRadius = this.calculateTbRadius( this.camera );
  1589. const gizmoTmp = new THREE.Matrix4().copy( this._gizmoMatrixState0 );
  1590. this.makeGizmos( this._gizmos.position, this._tbRadius );
  1591. this._gizmoMatrixState0.copy( gizmoTmp );
  1592. this.camera.lookAt( this._gizmos.position );
  1593. this.updateTbState( STATE.IDLE, false );
  1594. this.dispatchEvent( _changeEvent );
  1595. }
  1596. };
  1597. this.camera = null;
  1598. this.domElement = domElement;
  1599. this.scene = scene;
  1600. this.target = new THREE.Vector3();
  1601. this._currentTarget = new THREE.Vector3();
  1602. this.radiusFactor = 0.67;
  1603. this.mouseActions = [];
  1604. this._mouseOp = null; //global vectors and matrices that are used in some operations to avoid creating new objects every time (e.g. every time cursor moves)
  1605. this._v2_1 = new THREE.Vector2();
  1606. this._v3_1 = new THREE.Vector3();
  1607. this._v3_2 = new THREE.Vector3();
  1608. this._m4_1 = new THREE.Matrix4();
  1609. this._m4_2 = new THREE.Matrix4();
  1610. this._quat = new THREE.Quaternion(); //transformation matrices
  1611. this._translationMatrix = new THREE.Matrix4(); //matrix for translation operation
  1612. this._rotationMatrix = new THREE.Matrix4(); //matrix for rotation operation
  1613. this._scaleMatrix = new THREE.Matrix4(); //matrix for scaling operation
  1614. this._rotationAxis = new THREE.Vector3(); //axis for rotate operation
  1615. //camera state
  1616. this._cameraMatrixState = new THREE.Matrix4();
  1617. this._cameraProjectionState = new THREE.Matrix4();
  1618. this._fovState = 1;
  1619. this._upState = new THREE.Vector3();
  1620. this._zoomState = 1;
  1621. this._nearPos = 0;
  1622. this._farPos = 0;
  1623. this._gizmoMatrixState = new THREE.Matrix4(); //initial values
  1624. this._up0 = new THREE.Vector3();
  1625. this._zoom0 = 1;
  1626. this._fov0 = 0;
  1627. this._initialNear = 0;
  1628. this._nearPos0 = 0;
  1629. this._initialFar = 0;
  1630. this._farPos0 = 0;
  1631. this._cameraMatrixState0 = new THREE.Matrix4();
  1632. this._gizmoMatrixState0 = new THREE.Matrix4(); //pointers array
  1633. this._button = - 1;
  1634. this._touchStart = [];
  1635. this._touchCurrent = [];
  1636. this._input = INPUT.NONE; //two fingers touch interaction
  1637. this._switchSensibility = 32; //minimum movement to be performed to fire single pan start after the second finger has been released
  1638. this._startFingerDistance = 0; //distance between two fingers
  1639. this._currentFingerDistance = 0;
  1640. this._startFingerRotation = 0; //amount of rotation performed with two fingers
  1641. this._currentFingerRotation = 0; //double tap
  1642. this._devPxRatio = 0;
  1643. this._downValid = true;
  1644. this._nclicks = 0;
  1645. this._downEvents = [];
  1646. this._downStart = 0; //pointerDown time
  1647. this._clickStart = 0; //first click time
  1648. this._maxDownTime = 250;
  1649. this._maxInterval = 300;
  1650. this._posThreshold = 24;
  1651. this._movementThreshold = 24; //cursor positions
  1652. this._currentCursorPosition = new THREE.Vector3();
  1653. this._startCursorPosition = new THREE.Vector3(); //grid
  1654. this._grid = null; //grid to be visualized during pan operation
  1655. this._gridPosition = new THREE.Vector3(); //gizmos
  1656. this._gizmos = new THREE.Group();
  1657. this._curvePts = 128; //animations
  1658. this._timeStart = - 1; //initial time
  1659. this._animationId = - 1; //focus animation
  1660. this.focusAnimationTime = 500; //duration of focus animation in ms
  1661. //rotate animation
  1662. this._timePrev = 0; //time at which previous rotate operation has been detected
  1663. this._timeCurrent = 0; //time at which current rotate operation has been detected
  1664. this._anglePrev = 0; //angle of previous rotation
  1665. this._angleCurrent = 0; //angle of current rotation
  1666. this._cursorPosPrev = new THREE.Vector3(); //cursor position when previous rotate operation has been detected
  1667. this._cursorPosCurr = new THREE.Vector3(); //cursor position when current rotate operation has been detected
  1668. this._wPrev = 0; //angular velocity of the previous rotate operation
  1669. this._wCurr = 0; //angular velocity of the current rotate operation
  1670. //parameters
  1671. this.adjustNearFar = false;
  1672. this.scaleFactor = 1.1; //zoom/distance multiplier
  1673. this.dampingFactor = 25;
  1674. this.wMax = 20; //maximum angular velocity allowed
  1675. this.enableAnimations = true; //if animations should be performed
  1676. this.enableGrid = false; //if grid should be showed during pan operation
  1677. this.cursorZoom = false; //if wheel zoom should be cursor centered
  1678. this.minFov = 5;
  1679. this.maxFov = 90;
  1680. this.enabled = true;
  1681. this.enablePan = true;
  1682. this.enableRotate = true;
  1683. this.enableZoom = true;
  1684. this.enableGizmos = true;
  1685. this.minDistance = 0;
  1686. this.maxDistance = Infinity;
  1687. this.minZoom = 0;
  1688. this.maxZoom = Infinity; //trackball parameters
  1689. this._tbRadius = 1; //FSA
  1690. this._state = STATE.IDLE;
  1691. this.setCamera( _camera );
  1692. if ( this.scene != null ) {
  1693. this.scene.add( this._gizmos );
  1694. }
  1695. this.domElement.style.touchAction = 'none';
  1696. this._devPxRatio = window.devicePixelRatio;
  1697. this.initializeMouseActions();
  1698. this.domElement.addEventListener( 'contextmenu', this.onContextMenu );
  1699. this.domElement.addEventListener( 'wheel', this.onWheel );
  1700. this.domElement.addEventListener( 'pointerdown', this.onPointerDown );
  1701. this.domElement.addEventListener( 'pointercancel', this.onPointerCancel );
  1702. window.addEventListener( 'resize', this.onWindowResize );
  1703. } //listeners
  1704. /**
  1705. * Apply a transformation matrix, to the camera and gizmos
  1706. * @param {Object} transformation Object containing matrices to apply to camera and gizmos
  1707. */
  1708. applyTransformMatrix( transformation ) {
  1709. if ( transformation.camera != null ) {
  1710. this._m4_1.copy( this._cameraMatrixState ).premultiply( transformation.camera );
  1711. this._m4_1.decompose( this.camera.position, this.camera.quaternion, this.camera.scale );
  1712. this.camera.updateMatrix(); //update camera up vector
  1713. if ( this._state == STATE.ROTATE || this._state == STATE.ZROTATE || this._state == STATE.ANIMATION_ROTATE ) {
  1714. this.camera.up.copy( this._upState ).applyQuaternion( this.camera.quaternion );
  1715. }
  1716. }
  1717. if ( transformation.gizmos != null ) {
  1718. this._m4_1.copy( this._gizmoMatrixState ).premultiply( transformation.gizmos );
  1719. this._m4_1.decompose( this._gizmos.position, this._gizmos.quaternion, this._gizmos.scale );
  1720. this._gizmos.updateMatrix();
  1721. }
  1722. if ( this._state == STATE.SCALE || this._state == STATE.FOCUS || this._state == STATE.ANIMATION_FOCUS ) {
  1723. this._tbRadius = this.calculateTbRadius( this.camera );
  1724. if ( this.adjustNearFar ) {
  1725. const cameraDistance = this.camera.position.distanceTo( this._gizmos.position );
  1726. const bb = new THREE.Box3();
  1727. bb.setFromObject( this._gizmos );
  1728. const sphere = new THREE.Sphere();
  1729. bb.getBoundingSphere( sphere );
  1730. const adjustedNearPosition = Math.max( this._nearPos0, sphere.radius + sphere.center.length() );
  1731. const regularNearPosition = cameraDistance - this._initialNear;
  1732. const minNearPos = Math.min( adjustedNearPosition, regularNearPosition );
  1733. this.camera.near = cameraDistance - minNearPos;
  1734. const adjustedFarPosition = Math.min( this._farPos0, - sphere.radius + sphere.center.length() );
  1735. const regularFarPosition = cameraDistance - this._initialFar;
  1736. const minFarPos = Math.min( adjustedFarPosition, regularFarPosition );
  1737. this.camera.far = cameraDistance - minFarPos;
  1738. this.camera.updateProjectionMatrix();
  1739. } else {
  1740. let update = false;
  1741. if ( this.camera.near != this._initialNear ) {
  1742. this.camera.near = this._initialNear;
  1743. update = true;
  1744. }
  1745. if ( this.camera.far != this._initialFar ) {
  1746. this.camera.far = this._initialFar;
  1747. update = true;
  1748. }
  1749. if ( update ) {
  1750. this.camera.updateProjectionMatrix();
  1751. }
  1752. }
  1753. }
  1754. }
  1755. /**
  1756. * Calculate the angular speed
  1757. * @param {Number} p0 Position at t0
  1758. * @param {Number} p1 Position at t1
  1759. * @param {Number} t0 Initial time in milliseconds
  1760. * @param {Number} t1 Ending time in milliseconds
  1761. */
  1762. /**
  1763. * Set gizmos visibility
  1764. * @param {Boolean} value Value of gizmos visibility
  1765. */
  1766. setGizmosVisible( value ) {
  1767. this._gizmos.visible = value;
  1768. this.dispatchEvent( _changeEvent );
  1769. }
  1770. /**
  1771. * Set gizmos radius factor and redraws gizmos
  1772. * @param {Float} value Value of radius factor
  1773. */
  1774. setTbRadius( value ) {
  1775. this.radiusFactor = value;
  1776. this._tbRadius = this.calculateTbRadius( this.camera );
  1777. const curve = new THREE.EllipseCurve( 0, 0, this._tbRadius, this._tbRadius );
  1778. const points = curve.getPoints( this._curvePts );
  1779. const curveGeometry = new THREE.BufferGeometry().setFromPoints( points );
  1780. for ( const gizmo in this._gizmos.children ) {
  1781. this._gizmos.children[ gizmo ].geometry = curveGeometry;
  1782. }
  1783. this.dispatchEvent( _changeEvent );
  1784. }
  1785. /**
  1786. * Creates the rotation gizmos matching trackball center and radius
  1787. * @param {Vector3} tbCenter The trackball center
  1788. * @param {number} tbRadius The trackball radius
  1789. */
  1790. /**
  1791. * Set values in transformation object
  1792. * @param {Matrix4} camera Transformation to be applied to the camera
  1793. * @param {Matrix4} gizmos Transformation to be applied to gizmos
  1794. */
  1795. setTransformationMatrices( camera = null, gizmos = null ) {
  1796. if ( camera != null ) {
  1797. if ( _transformation.camera != null ) {
  1798. _transformation.camera.copy( camera );
  1799. } else {
  1800. _transformation.camera = camera.clone();
  1801. }
  1802. } else {
  1803. _transformation.camera = null;
  1804. }
  1805. if ( gizmos != null ) {
  1806. if ( _transformation.gizmos != null ) {
  1807. _transformation.gizmos.copy( gizmos );
  1808. } else {
  1809. _transformation.gizmos = gizmos.clone();
  1810. }
  1811. } else {
  1812. _transformation.gizmos = null;
  1813. }
  1814. }
  1815. /**
  1816. * Rotate camera around its direction axis passing by a given point by a given angle
  1817. * @param {Vector3} point The point where the rotation axis is passing trough
  1818. * @param {Number} angle Angle in radians
  1819. * @returns The computed transormation matix
  1820. */
  1821. getRaycaster() {
  1822. return _raycaster;
  1823. }
  1824. /**
  1825. * Unproject the cursor on the 3D object surface
  1826. * @param {Vector2} cursor Cursor coordinates in NDC
  1827. * @param {Camera} camera Virtual camera
  1828. * @returns {Vector3} The point of intersection with the model, if exist, null otherwise
  1829. */
  1830. }
  1831. THREE.ArcballControls = ArcballControls;
  1832. } )();