TransformControls.js 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. import {
  2. BoxGeometry,
  3. BufferGeometry,
  4. Controls,
  5. CylinderGeometry,
  6. DoubleSide,
  7. Euler,
  8. Float32BufferAttribute,
  9. Line,
  10. LineBasicMaterial,
  11. Matrix4,
  12. Mesh,
  13. MeshBasicMaterial,
  14. Object3D,
  15. OctahedronGeometry,
  16. PlaneGeometry,
  17. Quaternion,
  18. Raycaster,
  19. SphereGeometry,
  20. TorusGeometry,
  21. Vector3
  22. } from 'three';
  23. const _raycaster = new Raycaster();
  24. const _tempVector = new Vector3();
  25. const _tempVector2 = new Vector3();
  26. const _tempQuaternion = new Quaternion();
  27. const _unit = {
  28. X: new Vector3( 1, 0, 0 ),
  29. Y: new Vector3( 0, 1, 0 ),
  30. Z: new Vector3( 0, 0, 1 )
  31. };
  32. /**
  33. * Fires if any type of change (object or property change) is performed. Property changes
  34. * are separate events you can add event listeners to. The event type is "propertyname-changed".
  35. *
  36. * @event TransformControls#change
  37. * @type {Object}
  38. */
  39. const _changeEvent = { type: 'change' };
  40. /**
  41. * Fires if a pointer (mouse/touch) becomes active.
  42. *
  43. * @event TransformControls#mouseDown
  44. * @type {Object}
  45. */
  46. const _mouseDownEvent = { type: 'mouseDown', mode: null };
  47. /**
  48. * Fires if a pointer (mouse/touch) is no longer active.
  49. *
  50. * @event TransformControls#mouseUp
  51. * @type {Object}
  52. */
  53. const _mouseUpEvent = { type: 'mouseUp', mode: null };
  54. /**
  55. * Fires if the controlled 3D object is changed.
  56. *
  57. * @event TransformControls#objectChange
  58. * @type {Object}
  59. */
  60. const _objectChangeEvent = { type: 'objectChange' };
  61. /**
  62. * This class can be used to transform objects in 3D space by adapting a similar interaction model
  63. * of DCC tools like Blender. Unlike other controls, it is not intended to transform the scene's camera.
  64. *
  65. * `TransformControls` expects that its attached 3D object is part of the scene graph.
  66. *
  67. * @augments Controls
  68. * @three_import import { TransformControls } from 'three/addons/controls/TransformControls.js';
  69. */
  70. class TransformControls extends Controls {
  71. /**
  72. * Constructs a new controls instance.
  73. *
  74. * @param {Camera} camera - The camera of the rendered scene.
  75. * @param {?HTMLDOMElement} domElement - The HTML element used for event listeners.
  76. */
  77. constructor( camera, domElement = null ) {
  78. super( undefined, domElement );
  79. const root = new TransformControlsRoot( this );
  80. this._root = root;
  81. const gizmo = new TransformControlsGizmo();
  82. this._gizmo = gizmo;
  83. root.add( gizmo );
  84. const plane = new TransformControlsPlane();
  85. this._plane = plane;
  86. root.add( plane );
  87. const scope = this;
  88. // Defined getter, setter and store for a property
  89. function defineProperty( propName, defaultValue ) {
  90. let propValue = defaultValue;
  91. Object.defineProperty( scope, propName, {
  92. get: function () {
  93. return propValue !== undefined ? propValue : defaultValue;
  94. },
  95. set: function ( value ) {
  96. if ( propValue !== value ) {
  97. propValue = value;
  98. plane[ propName ] = value;
  99. gizmo[ propName ] = value;
  100. scope.dispatchEvent( { type: propName + '-changed', value: value } );
  101. scope.dispatchEvent( _changeEvent );
  102. }
  103. }
  104. } );
  105. scope[ propName ] = defaultValue;
  106. plane[ propName ] = defaultValue;
  107. gizmo[ propName ] = defaultValue;
  108. }
  109. // Define properties with getters/setter
  110. // Setting the defined property will automatically trigger change event
  111. // Defined properties are passed down to gizmo and plane
  112. /**
  113. * The camera of the rendered scene.
  114. *
  115. * @name TransformControls#camera
  116. * @type {Camera}
  117. */
  118. defineProperty( 'camera', camera );
  119. defineProperty( 'object', undefined );
  120. defineProperty( 'enabled', true );
  121. /**
  122. * The current transformation axis.
  123. *
  124. * @name TransformControls#axis
  125. * @type {string}
  126. */
  127. defineProperty( 'axis', null );
  128. /**
  129. * The current transformation axis.
  130. *
  131. * @name TransformControls#mode
  132. * @type {('translate'|'rotate'|'scale')}
  133. * @default 'translate'
  134. */
  135. defineProperty( 'mode', 'translate' );
  136. /**
  137. * By default, 3D objects are continuously translated. If you set this property to a numeric
  138. * value (world units), you can define in which steps the 3D object should be translated.
  139. *
  140. * @name TransformControls#translationSnap
  141. * @type {?number}
  142. * @default null
  143. */
  144. defineProperty( 'translationSnap', null );
  145. /**
  146. * By default, 3D objects are continuously rotated. If you set this property to a numeric
  147. * value (radians), you can define in which steps the 3D object should be rotated.
  148. *
  149. * @name TransformControls#rotationSnap
  150. * @type {?number}
  151. * @default null
  152. */
  153. defineProperty( 'rotationSnap', null );
  154. /**
  155. * By default, 3D objects are continuously scaled. If you set this property to a numeric
  156. * value, you can define in which steps the 3D object should be scaled.
  157. *
  158. * @name TransformControls#scaleSnap
  159. * @type {?number}
  160. * @default null
  161. */
  162. defineProperty( 'scaleSnap', null );
  163. /**
  164. * Defines in which coordinate space transformations should be performed.
  165. *
  166. * @name TransformControls#space
  167. * @type {('world'|'local')}
  168. * @default 'world'
  169. */
  170. defineProperty( 'space', 'world' );
  171. /**
  172. * The size of the helper UI (axes/planes).
  173. *
  174. * @name TransformControls#size
  175. * @type {number}
  176. * @default 1
  177. */
  178. defineProperty( 'size', 1 );
  179. /**
  180. * Whether dragging is currently performed or not.
  181. *
  182. * @name TransformControls#dragging
  183. * @type {boolean}
  184. * @readonly
  185. * @default false
  186. */
  187. defineProperty( 'dragging', false );
  188. /**
  189. * Whether the x-axis helper should be visible or not.
  190. *
  191. * @name TransformControls#showX
  192. * @type {boolean}
  193. * @default true
  194. */
  195. defineProperty( 'showX', true );
  196. /**
  197. * Whether the y-axis helper should be visible or not.
  198. *
  199. * @name TransformControls#showY
  200. * @type {boolean}
  201. * @default true
  202. */
  203. defineProperty( 'showY', true );
  204. /**
  205. * Whether the z-axis helper should be visible or not.
  206. *
  207. * @name TransformControls#showZ
  208. * @type {boolean}
  209. * @default true
  210. */
  211. defineProperty( 'showZ', true );
  212. /**
  213. * The minimum allowed X position during translation.
  214. *
  215. * @name TransformControls#minX
  216. * @type {number}
  217. * @default -Infinity
  218. */
  219. defineProperty( 'minX', - Infinity );
  220. /**
  221. * The maximum allowed X position during translation.
  222. *
  223. * @name TransformControls#maxX
  224. * @type {number}
  225. * @default Infinity
  226. */
  227. defineProperty( 'maxX', Infinity );
  228. /**
  229. * The minimum allowed y position during translation.
  230. *
  231. * @name TransformControls#minY
  232. * @type {number}
  233. * @default -Infinity
  234. */
  235. defineProperty( 'minY', - Infinity );
  236. /**
  237. * The maximum allowed Y position during translation.
  238. *
  239. * @name TransformControls#maxY
  240. * @type {number}
  241. * @default Infinity
  242. */
  243. defineProperty( 'maxY', Infinity );
  244. /**
  245. * The minimum allowed z position during translation.
  246. *
  247. * @name TransformControls#minZ
  248. * @type {number}
  249. * @default -Infinity
  250. */
  251. defineProperty( 'minZ', - Infinity );
  252. /**
  253. * The maximum allowed Z position during translation.
  254. *
  255. * @name TransformControls#maxZ
  256. * @type {number}
  257. * @default Infinity
  258. */
  259. defineProperty( 'maxZ', Infinity );
  260. // Reusable utility variables
  261. const worldPosition = new Vector3();
  262. const worldPositionStart = new Vector3();
  263. const worldQuaternion = new Quaternion();
  264. const worldQuaternionStart = new Quaternion();
  265. const cameraPosition = new Vector3();
  266. const cameraQuaternion = new Quaternion();
  267. const pointStart = new Vector3();
  268. const pointEnd = new Vector3();
  269. const rotationAxis = new Vector3();
  270. const rotationAngle = 0;
  271. const eye = new Vector3();
  272. // TODO: remove properties unused in plane and gizmo
  273. defineProperty( 'worldPosition', worldPosition );
  274. defineProperty( 'worldPositionStart', worldPositionStart );
  275. defineProperty( 'worldQuaternion', worldQuaternion );
  276. defineProperty( 'worldQuaternionStart', worldQuaternionStart );
  277. defineProperty( 'cameraPosition', cameraPosition );
  278. defineProperty( 'cameraQuaternion', cameraQuaternion );
  279. defineProperty( 'pointStart', pointStart );
  280. defineProperty( 'pointEnd', pointEnd );
  281. defineProperty( 'rotationAxis', rotationAxis );
  282. defineProperty( 'rotationAngle', rotationAngle );
  283. defineProperty( 'eye', eye );
  284. this._offset = new Vector3();
  285. this._startNorm = new Vector3();
  286. this._endNorm = new Vector3();
  287. this._cameraScale = new Vector3();
  288. this._parentPosition = new Vector3();
  289. this._parentQuaternion = new Quaternion();
  290. this._parentQuaternionInv = new Quaternion();
  291. this._parentScale = new Vector3();
  292. this._worldScaleStart = new Vector3();
  293. this._worldQuaternionInv = new Quaternion();
  294. this._worldScale = new Vector3();
  295. this._positionStart = new Vector3();
  296. this._quaternionStart = new Quaternion();
  297. this._scaleStart = new Vector3();
  298. this._getPointer = getPointer.bind( this );
  299. this._onPointerDown = onPointerDown.bind( this );
  300. this._onPointerHover = onPointerHover.bind( this );
  301. this._onPointerMove = onPointerMove.bind( this );
  302. this._onPointerUp = onPointerUp.bind( this );
  303. if ( domElement !== null ) {
  304. this.connect( domElement );
  305. }
  306. }
  307. connect( element ) {
  308. super.connect( element );
  309. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  310. this.domElement.addEventListener( 'pointermove', this._onPointerHover );
  311. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  312. this.domElement.style.touchAction = 'none'; // disable touch scroll
  313. }
  314. disconnect() {
  315. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  316. this.domElement.removeEventListener( 'pointermove', this._onPointerHover );
  317. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  318. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  319. this.domElement.style.touchAction = 'auto';
  320. }
  321. /**
  322. * Returns the visual representation of the controls. Add the helper to your scene to
  323. * visually transform the attached 3D object.
  324. *
  325. * @return {TransformControlsRoot} The helper.
  326. */
  327. getHelper() {
  328. return this._root;
  329. }
  330. pointerHover( pointer ) {
  331. if ( this.object === undefined || this.dragging === true ) return;
  332. if ( pointer !== null ) _raycaster.setFromCamera( pointer, this.camera );
  333. const intersect = intersectObjectWithRay( this._gizmo.picker[ this.mode ], _raycaster );
  334. if ( intersect ) {
  335. this.axis = intersect.object.name;
  336. } else {
  337. this.axis = null;
  338. }
  339. }
  340. pointerDown( pointer ) {
  341. if ( this.object === undefined || this.dragging === true || ( pointer != null && pointer.button !== 0 ) ) return;
  342. if ( this.axis !== null ) {
  343. if ( pointer !== null ) _raycaster.setFromCamera( pointer, this.camera );
  344. const planeIntersect = intersectObjectWithRay( this._plane, _raycaster, true );
  345. if ( planeIntersect ) {
  346. this.object.updateMatrixWorld();
  347. this.object.parent.updateMatrixWorld();
  348. this._positionStart.copy( this.object.position );
  349. this._quaternionStart.copy( this.object.quaternion );
  350. this._scaleStart.copy( this.object.scale );
  351. this.object.matrixWorld.decompose( this.worldPositionStart, this.worldQuaternionStart, this._worldScaleStart );
  352. this.pointStart.copy( planeIntersect.point ).sub( this.worldPositionStart );
  353. }
  354. this.dragging = true;
  355. _mouseDownEvent.mode = this.mode;
  356. this.dispatchEvent( _mouseDownEvent );
  357. }
  358. }
  359. pointerMove( pointer ) {
  360. const axis = this.axis;
  361. const mode = this.mode;
  362. const object = this.object;
  363. let space = this.space;
  364. if ( mode === 'scale' ) {
  365. space = 'local';
  366. } else if ( axis === 'E' || axis === 'XYZE' || axis === 'XYZ' ) {
  367. space = 'world';
  368. }
  369. if ( object === undefined || axis === null || this.dragging === false || ( pointer !== null && pointer.button !== - 1 ) ) return;
  370. if ( pointer !== null ) _raycaster.setFromCamera( pointer, this.camera );
  371. const planeIntersect = intersectObjectWithRay( this._plane, _raycaster, true );
  372. if ( ! planeIntersect ) return;
  373. this.pointEnd.copy( planeIntersect.point ).sub( this.worldPositionStart );
  374. if ( mode === 'translate' ) {
  375. // Apply translate
  376. this._offset.copy( this.pointEnd ).sub( this.pointStart );
  377. if ( space === 'local' && axis !== 'XYZ' ) {
  378. this._offset.applyQuaternion( this._worldQuaternionInv );
  379. }
  380. if ( axis.indexOf( 'X' ) === - 1 ) this._offset.x = 0;
  381. if ( axis.indexOf( 'Y' ) === - 1 ) this._offset.y = 0;
  382. if ( axis.indexOf( 'Z' ) === - 1 ) this._offset.z = 0;
  383. if ( space === 'local' && axis !== 'XYZ' ) {
  384. this._offset.applyQuaternion( this._quaternionStart ).divide( this._parentScale );
  385. } else {
  386. this._offset.applyQuaternion( this._parentQuaternionInv ).divide( this._parentScale );
  387. }
  388. object.position.copy( this._offset ).add( this._positionStart );
  389. // Apply translation snap
  390. if ( this.translationSnap ) {
  391. if ( space === 'local' ) {
  392. object.position.applyQuaternion( _tempQuaternion.copy( this._quaternionStart ).invert() );
  393. if ( axis.search( 'X' ) !== - 1 ) {
  394. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  395. }
  396. if ( axis.search( 'Y' ) !== - 1 ) {
  397. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  398. }
  399. if ( axis.search( 'Z' ) !== - 1 ) {
  400. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  401. }
  402. object.position.applyQuaternion( this._quaternionStart );
  403. }
  404. if ( space === 'world' ) {
  405. if ( object.parent ) {
  406. object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  407. }
  408. if ( axis.search( 'X' ) !== - 1 ) {
  409. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  410. }
  411. if ( axis.search( 'Y' ) !== - 1 ) {
  412. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  413. }
  414. if ( axis.search( 'Z' ) !== - 1 ) {
  415. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  416. }
  417. if ( object.parent ) {
  418. object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  419. }
  420. }
  421. }
  422. object.position.x = Math.max( this.minX, Math.min( this.maxX, object.position.x ) );
  423. object.position.y = Math.max( this.minY, Math.min( this.maxY, object.position.y ) );
  424. object.position.z = Math.max( this.minZ, Math.min( this.maxZ, object.position.z ) );
  425. } else if ( mode === 'scale' ) {
  426. if ( axis.search( 'XYZ' ) !== - 1 ) {
  427. let d = this.pointEnd.length() / this.pointStart.length();
  428. if ( this.pointEnd.dot( this.pointStart ) < 0 ) d *= - 1;
  429. _tempVector2.set( d, d, d );
  430. } else {
  431. _tempVector.copy( this.pointStart );
  432. _tempVector2.copy( this.pointEnd );
  433. _tempVector.applyQuaternion( this._worldQuaternionInv );
  434. _tempVector2.applyQuaternion( this._worldQuaternionInv );
  435. _tempVector2.divide( _tempVector );
  436. if ( axis.search( 'X' ) === - 1 ) {
  437. _tempVector2.x = 1;
  438. }
  439. if ( axis.search( 'Y' ) === - 1 ) {
  440. _tempVector2.y = 1;
  441. }
  442. if ( axis.search( 'Z' ) === - 1 ) {
  443. _tempVector2.z = 1;
  444. }
  445. }
  446. // Apply scale
  447. object.scale.copy( this._scaleStart ).multiply( _tempVector2 );
  448. if ( this.scaleSnap ) {
  449. if ( axis.search( 'X' ) !== - 1 ) {
  450. object.scale.x = Math.round( object.scale.x / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  451. }
  452. if ( axis.search( 'Y' ) !== - 1 ) {
  453. object.scale.y = Math.round( object.scale.y / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  454. }
  455. if ( axis.search( 'Z' ) !== - 1 ) {
  456. object.scale.z = Math.round( object.scale.z / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  457. }
  458. }
  459. } else if ( mode === 'rotate' ) {
  460. this._offset.copy( this.pointEnd ).sub( this.pointStart );
  461. const ROTATION_SPEED = 20 / this.worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
  462. let _inPlaneRotation = false;
  463. if ( axis === 'XYZE' ) {
  464. this.rotationAxis.copy( this._offset ).cross( this.eye ).normalize();
  465. this.rotationAngle = this._offset.dot( _tempVector.copy( this.rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
  466. } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
  467. this.rotationAxis.copy( _unit[ axis ] );
  468. _tempVector.copy( _unit[ axis ] );
  469. if ( space === 'local' ) {
  470. _tempVector.applyQuaternion( this.worldQuaternion );
  471. }
  472. _tempVector.cross( this.eye );
  473. // When _tempVector is 0 after cross with this.eye the vectors are parallel and should use in-plane rotation logic.
  474. if ( _tempVector.length() === 0 ) {
  475. _inPlaneRotation = true;
  476. } else {
  477. this.rotationAngle = this._offset.dot( _tempVector.normalize() ) * ROTATION_SPEED;
  478. }
  479. }
  480. if ( axis === 'E' || _inPlaneRotation ) {
  481. this.rotationAxis.copy( this.eye );
  482. this.rotationAngle = this.pointEnd.angleTo( this.pointStart );
  483. this._startNorm.copy( this.pointStart ).normalize();
  484. this._endNorm.copy( this.pointEnd ).normalize();
  485. this.rotationAngle *= ( this._endNorm.cross( this._startNorm ).dot( this.eye ) < 0 ? 1 : - 1 );
  486. }
  487. // Apply rotation snap
  488. if ( this.rotationSnap ) this.rotationAngle = Math.round( this.rotationAngle / this.rotationSnap ) * this.rotationSnap;
  489. // Apply rotate
  490. if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) {
  491. object.quaternion.copy( this._quaternionStart );
  492. object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( this.rotationAxis, this.rotationAngle ) ).normalize();
  493. } else {
  494. this.rotationAxis.applyQuaternion( this._parentQuaternionInv );
  495. object.quaternion.copy( _tempQuaternion.setFromAxisAngle( this.rotationAxis, this.rotationAngle ) );
  496. object.quaternion.multiply( this._quaternionStart ).normalize();
  497. }
  498. }
  499. this.dispatchEvent( _changeEvent );
  500. this.dispatchEvent( _objectChangeEvent );
  501. }
  502. pointerUp( pointer ) {
  503. if ( pointer !== null && pointer.button !== 0 ) return;
  504. if ( this.dragging && ( this.axis !== null ) ) {
  505. _mouseUpEvent.mode = this.mode;
  506. this.dispatchEvent( _mouseUpEvent );
  507. }
  508. this.dragging = false;
  509. this.axis = null;
  510. }
  511. dispose() {
  512. this.disconnect();
  513. this._root.dispose();
  514. }
  515. /**
  516. * Sets the 3D object that should be transformed and ensures the controls UI is visible.
  517. *
  518. * @param {Object3D} object - The 3D object that should be transformed.
  519. * @return {TransformControls} A reference to this controls.
  520. */
  521. attach( object ) {
  522. this.object = object;
  523. this._root.visible = true;
  524. return this;
  525. }
  526. /**
  527. * Removes the current 3D object from the controls and makes the helper UI invisible.
  528. *
  529. * @return {TransformControls} A reference to this controls.
  530. */
  531. detach() {
  532. this.object = undefined;
  533. this.axis = null;
  534. this._root.visible = false;
  535. return this;
  536. }
  537. /**
  538. * Resets the object's position, rotation and scale to when the current transform began.
  539. */
  540. reset() {
  541. if ( ! this.enabled ) return;
  542. if ( this.dragging ) {
  543. this.object.position.copy( this._positionStart );
  544. this.object.quaternion.copy( this._quaternionStart );
  545. this.object.scale.copy( this._scaleStart );
  546. this.dispatchEvent( _changeEvent );
  547. this.dispatchEvent( _objectChangeEvent );
  548. this.pointStart.copy( this.pointEnd );
  549. }
  550. }
  551. /**
  552. * Returns the raycaster that is used for user interaction. This object is shared between all
  553. * instances of `TransformControls`.
  554. *
  555. * @returns {Raycaster} The internal raycaster.
  556. */
  557. getRaycaster() {
  558. return _raycaster;
  559. }
  560. /**
  561. * Returns the transformation mode.
  562. *
  563. * @returns {'translate'|'rotate'|'scale'} The transformation mode.
  564. */
  565. getMode() {
  566. return this.mode;
  567. }
  568. /**
  569. * Sets the given transformation mode.
  570. *
  571. * @param {'translate'|'rotate'|'scale'} mode - The transformation mode to set.
  572. */
  573. setMode( mode ) {
  574. this.mode = mode;
  575. }
  576. /**
  577. * Sets the translation snap.
  578. *
  579. * @param {?number} translationSnap - The translation snap to set.
  580. */
  581. setTranslationSnap( translationSnap ) {
  582. this.translationSnap = translationSnap;
  583. }
  584. /**
  585. * Sets the rotation snap.
  586. *
  587. * @param {?number} rotationSnap - The rotation snap to set.
  588. */
  589. setRotationSnap( rotationSnap ) {
  590. this.rotationSnap = rotationSnap;
  591. }
  592. /**
  593. * Sets the scale snap.
  594. *
  595. * @param {?number} scaleSnap - The scale snap to set.
  596. */
  597. setScaleSnap( scaleSnap ) {
  598. this.scaleSnap = scaleSnap;
  599. }
  600. /**
  601. * Sets the size of the helper UI.
  602. *
  603. * @param {number} size - The size to set.
  604. */
  605. setSize( size ) {
  606. this.size = size;
  607. }
  608. /**
  609. * Sets the coordinate space in which transformations are applied.
  610. *
  611. * @param {'world'|'local'} space - The space to set.
  612. */
  613. setSpace( space ) {
  614. this.space = space;
  615. }
  616. /**
  617. * Sets the colors of the control's gizmo.
  618. *
  619. * @param {number|Color|string} xAxis - The x-axis color.
  620. * @param {number|Color|string} yAxis - The y-axis color.
  621. * @param {number|Color|string} zAxis - The z-axis color.
  622. * @param {number|Color|string} active - The color for active elements.
  623. */
  624. setColors( xAxis, yAxis, zAxis, active ) {
  625. const materialLib = this._gizmo.materialLib;
  626. materialLib.xAxis.color.set( xAxis );
  627. materialLib.yAxis.color.set( yAxis );
  628. materialLib.zAxis.color.set( zAxis );
  629. materialLib.active.color.set( active );
  630. materialLib.xAxisTransparent.color.set( xAxis );
  631. materialLib.yAxisTransparent.color.set( yAxis );
  632. materialLib.zAxisTransparent.color.set( zAxis );
  633. materialLib.activeTransparent.color.set( active );
  634. // update color caches
  635. if ( materialLib.xAxis._color ) materialLib.xAxis._color.set( xAxis );
  636. if ( materialLib.yAxis._color ) materialLib.yAxis._color.set( yAxis );
  637. if ( materialLib.zAxis._color ) materialLib.zAxis._color.set( zAxis );
  638. if ( materialLib.active._color ) materialLib.active._color.set( active );
  639. if ( materialLib.xAxisTransparent._color ) materialLib.xAxisTransparent._color.set( xAxis );
  640. if ( materialLib.yAxisTransparent._color ) materialLib.yAxisTransparent._color.set( yAxis );
  641. if ( materialLib.zAxisTransparent._color ) materialLib.zAxisTransparent._color.set( zAxis );
  642. if ( materialLib.activeTransparent._color ) materialLib.activeTransparent._color.set( active );
  643. }
  644. }
  645. // mouse / touch event handlers
  646. function getPointer( event ) {
  647. if ( this.domElement.ownerDocument.pointerLockElement ) {
  648. return {
  649. x: 0,
  650. y: 0,
  651. button: event.button
  652. };
  653. } else {
  654. const rect = this.domElement.getBoundingClientRect();
  655. return {
  656. x: ( event.clientX - rect.left ) / rect.width * 2 - 1,
  657. y: - ( event.clientY - rect.top ) / rect.height * 2 + 1,
  658. button: event.button
  659. };
  660. }
  661. }
  662. function onPointerHover( event ) {
  663. if ( ! this.enabled ) return;
  664. switch ( event.pointerType ) {
  665. case 'mouse':
  666. case 'pen':
  667. this.pointerHover( this._getPointer( event ) );
  668. break;
  669. }
  670. }
  671. function onPointerDown( event ) {
  672. if ( ! this.enabled ) return;
  673. if ( ! document.pointerLockElement ) {
  674. this.domElement.setPointerCapture( event.pointerId );
  675. }
  676. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  677. this.pointerHover( this._getPointer( event ) );
  678. this.pointerDown( this._getPointer( event ) );
  679. }
  680. function onPointerMove( event ) {
  681. if ( ! this.enabled ) return;
  682. this.pointerMove( this._getPointer( event ) );
  683. }
  684. function onPointerUp( event ) {
  685. if ( ! this.enabled ) return;
  686. this.domElement.releasePointerCapture( event.pointerId );
  687. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  688. this.pointerUp( this._getPointer( event ) );
  689. }
  690. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  691. const allIntersections = raycaster.intersectObject( object, true );
  692. for ( let i = 0; i < allIntersections.length; i ++ ) {
  693. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  694. return allIntersections[ i ];
  695. }
  696. }
  697. return false;
  698. }
  699. //
  700. // Reusable utility variables
  701. const _tempEuler = new Euler();
  702. const _alignVector = new Vector3( 0, 1, 0 );
  703. const _zeroVector = new Vector3( 0, 0, 0 );
  704. const _lookAtMatrix = new Matrix4();
  705. const _tempQuaternion2 = new Quaternion();
  706. const _identityQuaternion = new Quaternion();
  707. const _dirVector = new Vector3();
  708. const _tempMatrix = new Matrix4();
  709. const _unitX = new Vector3( 1, 0, 0 );
  710. const _unitY = new Vector3( 0, 1, 0 );
  711. const _unitZ = new Vector3( 0, 0, 1 );
  712. const _v1 = new Vector3();
  713. const _v2 = new Vector3();
  714. const _v3 = new Vector3();
  715. class TransformControlsRoot extends Object3D {
  716. constructor( controls ) {
  717. super();
  718. this.isTransformControlsRoot = true;
  719. this.controls = controls;
  720. this.visible = false;
  721. }
  722. // updateMatrixWorld updates key transformation variables
  723. updateMatrixWorld( force ) {
  724. const controls = this.controls;
  725. if ( controls.object !== undefined ) {
  726. controls.object.updateMatrixWorld();
  727. if ( controls.object.parent === null ) {
  728. console.error( 'TransformControls: The attached 3D object must be a part of the scene graph.' );
  729. } else {
  730. controls.object.parent.matrixWorld.decompose( controls._parentPosition, controls._parentQuaternion, controls._parentScale );
  731. }
  732. controls.object.matrixWorld.decompose( controls.worldPosition, controls.worldQuaternion, controls._worldScale );
  733. controls._parentQuaternionInv.copy( controls._parentQuaternion ).invert();
  734. controls._worldQuaternionInv.copy( controls.worldQuaternion ).invert();
  735. }
  736. controls.camera.updateMatrixWorld();
  737. controls.camera.matrixWorld.decompose( controls.cameraPosition, controls.cameraQuaternion, controls._cameraScale );
  738. if ( controls.camera.isOrthographicCamera ) {
  739. controls.camera.getWorldDirection( controls.eye ).negate();
  740. } else {
  741. controls.eye.copy( controls.cameraPosition ).sub( controls.worldPosition ).normalize();
  742. }
  743. super.updateMatrixWorld( force );
  744. }
  745. dispose() {
  746. this.traverse( function ( child ) {
  747. if ( child.geometry ) child.geometry.dispose();
  748. if ( child.material ) child.material.dispose();
  749. } );
  750. }
  751. }
  752. class TransformControlsGizmo extends Object3D {
  753. constructor() {
  754. super();
  755. this.isTransformControlsGizmo = true;
  756. this.type = 'TransformControlsGizmo';
  757. // shared materials
  758. const gizmoMaterial = new MeshBasicMaterial( {
  759. depthTest: false,
  760. depthWrite: false,
  761. fog: false,
  762. toneMapped: false,
  763. transparent: true
  764. } );
  765. const gizmoLineMaterial = new LineBasicMaterial( {
  766. depthTest: false,
  767. depthWrite: false,
  768. fog: false,
  769. toneMapped: false,
  770. transparent: true
  771. } );
  772. // Make unique material for each axis/color
  773. const matInvisible = gizmoMaterial.clone();
  774. matInvisible.opacity = 0.15;
  775. const matHelper = gizmoLineMaterial.clone();
  776. matHelper.opacity = 0.5;
  777. const matRed = gizmoMaterial.clone();
  778. matRed.color.setHex( 0xff0000 );
  779. const matGreen = gizmoMaterial.clone();
  780. matGreen.color.setHex( 0x00ff00 );
  781. const matBlue = gizmoMaterial.clone();
  782. matBlue.color.setHex( 0x0000ff );
  783. const matRedTransparent = gizmoMaterial.clone();
  784. matRedTransparent.color.setHex( 0xff0000 );
  785. matRedTransparent.opacity = 0.5;
  786. const matGreenTransparent = gizmoMaterial.clone();
  787. matGreenTransparent.color.setHex( 0x00ff00 );
  788. matGreenTransparent.opacity = 0.5;
  789. const matBlueTransparent = gizmoMaterial.clone();
  790. matBlueTransparent.color.setHex( 0x0000ff );
  791. matBlueTransparent.opacity = 0.5;
  792. const matWhiteTransparent = gizmoMaterial.clone();
  793. matWhiteTransparent.opacity = 0.25;
  794. const matYellowTransparent = gizmoMaterial.clone();
  795. matYellowTransparent.color.setHex( 0xffff00 );
  796. matYellowTransparent.opacity = 0.25;
  797. const matYellow = gizmoMaterial.clone();
  798. matYellow.color.setHex( 0xffff00 );
  799. const matGray = gizmoMaterial.clone();
  800. matGray.color.setHex( 0x787878 );
  801. // materials in the below property are configurable via setColors()
  802. this.materialLib = {
  803. xAxis: matRed,
  804. yAxis: matGreen,
  805. zAxis: matBlue,
  806. active: matYellow,
  807. xAxisTransparent: matRedTransparent,
  808. yAxisTransparent: matGreenTransparent,
  809. zAxisTransparent: matBlueTransparent,
  810. activeTransparent: matYellowTransparent
  811. };
  812. // reusable geometry
  813. const arrowGeometry = new CylinderGeometry( 0, 0.04, 0.1, 12 );
  814. arrowGeometry.translate( 0, 0.05, 0 );
  815. const scaleHandleGeometry = new BoxGeometry( 0.08, 0.08, 0.08 );
  816. scaleHandleGeometry.translate( 0, 0.04, 0 );
  817. const lineGeometry = new BufferGeometry();
  818. lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  819. const lineGeometry2 = new CylinderGeometry( 0.0075, 0.0075, 0.5, 3 );
  820. lineGeometry2.translate( 0, 0.25, 0 );
  821. function CircleGeometry( radius, arc ) {
  822. const geometry = new TorusGeometry( radius, 0.0075, 3, 64, arc * Math.PI * 2 );
  823. geometry.rotateY( Math.PI / 2 );
  824. geometry.rotateX( Math.PI / 2 );
  825. return geometry;
  826. }
  827. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  828. function TranslateHelperGeometry() {
  829. const geometry = new BufferGeometry();
  830. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  831. return geometry;
  832. }
  833. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  834. const gizmoTranslate = {
  835. X: [
  836. [ new Mesh( arrowGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  837. [ new Mesh( arrowGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  838. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  839. ],
  840. Y: [
  841. [ new Mesh( arrowGeometry, matGreen ), [ 0, 0.5, 0 ]],
  842. [ new Mesh( arrowGeometry, matGreen ), [ 0, - 0.5, 0 ], [ Math.PI, 0, 0 ]],
  843. [ new Mesh( lineGeometry2, matGreen ) ]
  844. ],
  845. Z: [
  846. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  847. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]],
  848. [ new Mesh( lineGeometry2, matBlue ), null, [ Math.PI / 2, 0, 0 ]]
  849. ],
  850. XYZ: [
  851. [ new Mesh( new OctahedronGeometry( 0.1, 0 ), matWhiteTransparent ), [ 0, 0, 0 ]]
  852. ],
  853. XY: [
  854. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent ), [ 0.15, 0.15, 0 ]]
  855. ],
  856. YZ: [
  857. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  858. ],
  859. XZ: [
  860. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  861. ]
  862. };
  863. const pickerTranslate = {
  864. X: [
  865. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  866. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  867. ],
  868. Y: [
  869. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  870. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  871. ],
  872. Z: [
  873. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  874. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  875. ],
  876. XYZ: [
  877. [ new Mesh( new OctahedronGeometry( 0.2, 0 ), matInvisible ) ]
  878. ],
  879. XY: [
  880. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]]
  881. ],
  882. YZ: [
  883. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  884. ],
  885. XZ: [
  886. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  887. ]
  888. };
  889. const helperTranslate = {
  890. START: [
  891. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  892. ],
  893. END: [
  894. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  895. ],
  896. DELTA: [
  897. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  898. ],
  899. X: [
  900. [ new Line( lineGeometry, matHelper ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  901. ],
  902. Y: [
  903. [ new Line( lineGeometry, matHelper ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  904. ],
  905. Z: [
  906. [ new Line( lineGeometry, matHelper ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  907. ]
  908. };
  909. const gizmoRotate = {
  910. XYZE: [
  911. [ new Mesh( CircleGeometry( 0.5, 1 ), matGray ), null, [ 0, Math.PI / 2, 0 ]]
  912. ],
  913. X: [
  914. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matRed ) ]
  915. ],
  916. Y: [
  917. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matGreen ), null, [ 0, 0, - Math.PI / 2 ]]
  918. ],
  919. Z: [
  920. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matBlue ), null, [ 0, Math.PI / 2, 0 ]]
  921. ],
  922. E: [
  923. [ new Mesh( CircleGeometry( 0.75, 1 ), matYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]]
  924. ]
  925. };
  926. const helperRotate = {
  927. AXIS: [
  928. [ new Line( lineGeometry, matHelper ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  929. ]
  930. };
  931. const pickerRotate = {
  932. XYZE: [
  933. [ new Mesh( new SphereGeometry( 0.25, 10, 8 ), matInvisible ) ]
  934. ],
  935. X: [
  936. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  937. ],
  938. Y: [
  939. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  940. ],
  941. Z: [
  942. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  943. ],
  944. E: [
  945. [ new Mesh( new TorusGeometry( 0.75, 0.1, 2, 24 ), matInvisible ) ]
  946. ]
  947. };
  948. const gizmoScale = {
  949. X: [
  950. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  951. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  952. [ new Mesh( scaleHandleGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  953. ],
  954. Y: [
  955. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.5, 0 ]],
  956. [ new Mesh( lineGeometry2, matGreen ) ],
  957. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, - 0.5, 0 ], [ 0, 0, Math.PI ]],
  958. ],
  959. Z: [
  960. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  961. [ new Mesh( lineGeometry2, matBlue ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  962. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]]
  963. ],
  964. XY: [
  965. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent ), [ 0.15, 0.15, 0 ]]
  966. ],
  967. YZ: [
  968. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  969. ],
  970. XZ: [
  971. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  972. ],
  973. XYZ: [
  974. [ new Mesh( new BoxGeometry( 0.1, 0.1, 0.1 ), matWhiteTransparent ) ],
  975. ]
  976. };
  977. const pickerScale = {
  978. X: [
  979. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  980. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  981. ],
  982. Y: [
  983. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  984. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  985. ],
  986. Z: [
  987. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  988. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  989. ],
  990. XY: [
  991. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]],
  992. ],
  993. YZ: [
  994. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  995. ],
  996. XZ: [
  997. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  998. ],
  999. XYZ: [
  1000. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 0 ]],
  1001. ]
  1002. };
  1003. const helperScale = {
  1004. X: [
  1005. [ new Line( lineGeometry, matHelper ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  1006. ],
  1007. Y: [
  1008. [ new Line( lineGeometry, matHelper ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  1009. ],
  1010. Z: [
  1011. [ new Line( lineGeometry, matHelper ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  1012. ]
  1013. };
  1014. // Creates an Object3D with gizmos described in custom hierarchy definition.
  1015. function setupGizmo( gizmoMap ) {
  1016. const gizmo = new Object3D();
  1017. for ( const name in gizmoMap ) {
  1018. for ( let i = gizmoMap[ name ].length; i --; ) {
  1019. const object = gizmoMap[ name ][ i ][ 0 ].clone();
  1020. const position = gizmoMap[ name ][ i ][ 1 ];
  1021. const rotation = gizmoMap[ name ][ i ][ 2 ];
  1022. const scale = gizmoMap[ name ][ i ][ 3 ];
  1023. const tag = gizmoMap[ name ][ i ][ 4 ];
  1024. // name and tag properties are essential for picking and updating logic.
  1025. object.name = name;
  1026. object.tag = tag;
  1027. if ( position ) {
  1028. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  1029. }
  1030. if ( rotation ) {
  1031. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  1032. }
  1033. if ( scale ) {
  1034. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  1035. }
  1036. object.updateMatrix();
  1037. const tempGeometry = object.geometry.clone();
  1038. tempGeometry.applyMatrix4( object.matrix );
  1039. object.geometry = tempGeometry;
  1040. object.renderOrder = Infinity;
  1041. object.position.set( 0, 0, 0 );
  1042. object.rotation.set( 0, 0, 0 );
  1043. object.scale.set( 1, 1, 1 );
  1044. gizmo.add( object );
  1045. }
  1046. }
  1047. return gizmo;
  1048. }
  1049. // Gizmo creation
  1050. this.gizmo = {};
  1051. this.picker = {};
  1052. this.helper = {};
  1053. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  1054. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  1055. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  1056. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  1057. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  1058. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  1059. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  1060. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  1061. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) );
  1062. // Pickers should be hidden always
  1063. this.picker[ 'translate' ].visible = false;
  1064. this.picker[ 'rotate' ].visible = false;
  1065. this.picker[ 'scale' ].visible = false;
  1066. }
  1067. // updateMatrixWorld will update transformations and appearance of individual handles
  1068. updateMatrixWorld( force ) {
  1069. const space = ( this.mode === 'scale' ) ? 'local' : this.space; // scale always oriented to local rotation
  1070. const quaternion = ( space === 'local' ) ? this.worldQuaternion : _identityQuaternion;
  1071. // Show only gizmos for current transform mode
  1072. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  1073. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  1074. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  1075. this.helper[ 'translate' ].visible = this.mode === 'translate';
  1076. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  1077. this.helper[ 'scale' ].visible = this.mode === 'scale';
  1078. let handles = [];
  1079. handles = handles.concat( this.picker[ this.mode ].children );
  1080. handles = handles.concat( this.gizmo[ this.mode ].children );
  1081. handles = handles.concat( this.helper[ this.mode ].children );
  1082. for ( let i = 0; i < handles.length; i ++ ) {
  1083. const handle = handles[ i ];
  1084. // hide aligned to camera
  1085. handle.visible = true;
  1086. handle.rotation.set( 0, 0, 0 );
  1087. handle.position.copy( this.worldPosition );
  1088. let factor;
  1089. if ( this.camera.isOrthographicCamera ) {
  1090. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  1091. } else {
  1092. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  1093. }
  1094. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 4 );
  1095. // TODO: simplify helpers and consider decoupling from gizmo
  1096. if ( handle.tag === 'helper' ) {
  1097. handle.visible = false;
  1098. if ( handle.name === 'AXIS' ) {
  1099. handle.visible = !! this.axis;
  1100. if ( this.axis === 'X' ) {
  1101. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, 0 ) );
  1102. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  1103. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1104. handle.visible = false;
  1105. }
  1106. }
  1107. if ( this.axis === 'Y' ) {
  1108. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, Math.PI / 2 ) );
  1109. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  1110. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1111. handle.visible = false;
  1112. }
  1113. }
  1114. if ( this.axis === 'Z' ) {
  1115. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  1116. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  1117. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1118. handle.visible = false;
  1119. }
  1120. }
  1121. if ( this.axis === 'XYZE' ) {
  1122. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  1123. _alignVector.copy( this.rotationAxis );
  1124. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( _zeroVector, _alignVector, _unitY ) );
  1125. handle.quaternion.multiply( _tempQuaternion );
  1126. handle.visible = this.dragging;
  1127. }
  1128. if ( this.axis === 'E' ) {
  1129. handle.visible = false;
  1130. }
  1131. } else if ( handle.name === 'START' ) {
  1132. handle.position.copy( this.worldPositionStart );
  1133. handle.visible = this.dragging;
  1134. } else if ( handle.name === 'END' ) {
  1135. handle.position.copy( this.worldPosition );
  1136. handle.visible = this.dragging;
  1137. } else if ( handle.name === 'DELTA' ) {
  1138. handle.position.copy( this.worldPositionStart );
  1139. handle.quaternion.copy( this.worldQuaternionStart );
  1140. _tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  1141. _tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  1142. handle.scale.copy( _tempVector );
  1143. handle.visible = this.dragging;
  1144. } else {
  1145. handle.quaternion.copy( quaternion );
  1146. if ( this.dragging ) {
  1147. handle.position.copy( this.worldPositionStart );
  1148. } else {
  1149. handle.position.copy( this.worldPosition );
  1150. }
  1151. if ( this.axis ) {
  1152. handle.visible = this.axis.search( handle.name ) !== - 1;
  1153. }
  1154. }
  1155. // If updating helper, skip rest of the loop
  1156. continue;
  1157. }
  1158. // Align handles to current local or world rotation
  1159. handle.quaternion.copy( quaternion );
  1160. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  1161. // Hide translate and scale axis facing the camera
  1162. const AXIS_HIDE_THRESHOLD = 0.99;
  1163. const PLANE_HIDE_THRESHOLD = 0.2;
  1164. if ( handle.name === 'X' ) {
  1165. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  1166. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1167. handle.visible = false;
  1168. }
  1169. }
  1170. if ( handle.name === 'Y' ) {
  1171. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  1172. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1173. handle.visible = false;
  1174. }
  1175. }
  1176. if ( handle.name === 'Z' ) {
  1177. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  1178. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1179. handle.visible = false;
  1180. }
  1181. }
  1182. if ( handle.name === 'XY' ) {
  1183. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  1184. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1185. handle.visible = false;
  1186. }
  1187. }
  1188. if ( handle.name === 'YZ' ) {
  1189. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  1190. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1191. handle.visible = false;
  1192. }
  1193. }
  1194. if ( handle.name === 'XZ' ) {
  1195. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  1196. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1197. handle.visible = false;
  1198. }
  1199. }
  1200. } else if ( this.mode === 'rotate' ) {
  1201. // Align handles to current local or world rotation
  1202. _tempQuaternion2.copy( quaternion );
  1203. _alignVector.copy( this.eye ).applyQuaternion( _tempQuaternion.copy( quaternion ).invert() );
  1204. if ( handle.name.search( 'E' ) !== - 1 ) {
  1205. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( this.eye, _zeroVector, _unitY ) );
  1206. }
  1207. if ( handle.name === 'X' ) {
  1208. _tempQuaternion.setFromAxisAngle( _unitX, Math.atan2( - _alignVector.y, _alignVector.z ) );
  1209. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  1210. handle.quaternion.copy( _tempQuaternion );
  1211. }
  1212. if ( handle.name === 'Y' ) {
  1213. _tempQuaternion.setFromAxisAngle( _unitY, Math.atan2( _alignVector.x, _alignVector.z ) );
  1214. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  1215. handle.quaternion.copy( _tempQuaternion );
  1216. }
  1217. if ( handle.name === 'Z' ) {
  1218. _tempQuaternion.setFromAxisAngle( _unitZ, Math.atan2( _alignVector.y, _alignVector.x ) );
  1219. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  1220. handle.quaternion.copy( _tempQuaternion );
  1221. }
  1222. }
  1223. // Hide disabled axes
  1224. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  1225. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  1226. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  1227. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  1228. // highlight selected axis
  1229. handle.material._color = handle.material._color || handle.material.color.clone();
  1230. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  1231. handle.material.color.copy( handle.material._color );
  1232. handle.material.opacity = handle.material._opacity;
  1233. if ( this.enabled && this.axis ) {
  1234. if ( handle.name === this.axis ) {
  1235. handle.material.color.copy( this.materialLib.active.color );
  1236. handle.material.opacity = 1.0;
  1237. } else if ( this.axis.split( '' ).some( function ( a ) {
  1238. return handle.name === a;
  1239. } ) ) {
  1240. handle.material.color.copy( this.materialLib.active.color );
  1241. handle.material.opacity = 1.0;
  1242. }
  1243. }
  1244. }
  1245. super.updateMatrixWorld( force );
  1246. }
  1247. }
  1248. //
  1249. class TransformControlsPlane extends Mesh {
  1250. constructor() {
  1251. super(
  1252. new PlaneGeometry( 100000, 100000, 2, 2 ),
  1253. new MeshBasicMaterial( { visible: false, wireframe: true, side: DoubleSide, transparent: true, opacity: 0.1, toneMapped: false } )
  1254. );
  1255. this.isTransformControlsPlane = true;
  1256. this.type = 'TransformControlsPlane';
  1257. }
  1258. updateMatrixWorld( force ) {
  1259. let space = this.space;
  1260. this.position.copy( this.worldPosition );
  1261. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  1262. _v1.copy( _unitX ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1263. _v2.copy( _unitY ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1264. _v3.copy( _unitZ ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1265. // Align the plane for current transform mode, axis and space.
  1266. _alignVector.copy( _v2 );
  1267. switch ( this.mode ) {
  1268. case 'translate':
  1269. case 'scale':
  1270. switch ( this.axis ) {
  1271. case 'X':
  1272. _alignVector.copy( this.eye ).cross( _v1 );
  1273. _dirVector.copy( _v1 ).cross( _alignVector );
  1274. break;
  1275. case 'Y':
  1276. _alignVector.copy( this.eye ).cross( _v2 );
  1277. _dirVector.copy( _v2 ).cross( _alignVector );
  1278. break;
  1279. case 'Z':
  1280. _alignVector.copy( this.eye ).cross( _v3 );
  1281. _dirVector.copy( _v3 ).cross( _alignVector );
  1282. break;
  1283. case 'XY':
  1284. _dirVector.copy( _v3 );
  1285. break;
  1286. case 'YZ':
  1287. _dirVector.copy( _v1 );
  1288. break;
  1289. case 'XZ':
  1290. _alignVector.copy( _v3 );
  1291. _dirVector.copy( _v2 );
  1292. break;
  1293. case 'XYZ':
  1294. case 'E':
  1295. _dirVector.set( 0, 0, 0 );
  1296. break;
  1297. }
  1298. break;
  1299. case 'rotate':
  1300. default:
  1301. // special case for rotate
  1302. _dirVector.set( 0, 0, 0 );
  1303. }
  1304. if ( _dirVector.length() === 0 ) {
  1305. // If in rotate mode, make the plane parallel to camera
  1306. this.quaternion.copy( this.cameraQuaternion );
  1307. } else {
  1308. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  1309. this.quaternion.setFromRotationMatrix( _tempMatrix );
  1310. }
  1311. super.updateMatrixWorld( force );
  1312. }
  1313. }
  1314. export { TransformControls, TransformControlsGizmo, TransformControlsPlane };