ArcballControls.js 74 KB

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