ArcballControls.js 74 KB

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