TransformControls.js 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  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. // mouse / touch event handlers
  618. function getPointer( event ) {
  619. if ( this.domElement.ownerDocument.pointerLockElement ) {
  620. return {
  621. x: 0,
  622. y: 0,
  623. button: event.button
  624. };
  625. } else {
  626. const rect = this.domElement.getBoundingClientRect();
  627. return {
  628. x: ( event.clientX - rect.left ) / rect.width * 2 - 1,
  629. y: - ( event.clientY - rect.top ) / rect.height * 2 + 1,
  630. button: event.button
  631. };
  632. }
  633. }
  634. function onPointerHover( event ) {
  635. if ( ! this.enabled ) return;
  636. switch ( event.pointerType ) {
  637. case 'mouse':
  638. case 'pen':
  639. this.pointerHover( this._getPointer( event ) );
  640. break;
  641. }
  642. }
  643. function onPointerDown( event ) {
  644. if ( ! this.enabled ) return;
  645. if ( ! document.pointerLockElement ) {
  646. this.domElement.setPointerCapture( event.pointerId );
  647. }
  648. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  649. this.pointerHover( this._getPointer( event ) );
  650. this.pointerDown( this._getPointer( event ) );
  651. }
  652. function onPointerMove( event ) {
  653. if ( ! this.enabled ) return;
  654. this.pointerMove( this._getPointer( event ) );
  655. }
  656. function onPointerUp( event ) {
  657. if ( ! this.enabled ) return;
  658. this.domElement.releasePointerCapture( event.pointerId );
  659. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  660. this.pointerUp( this._getPointer( event ) );
  661. }
  662. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  663. const allIntersections = raycaster.intersectObject( object, true );
  664. for ( let i = 0; i < allIntersections.length; i ++ ) {
  665. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  666. return allIntersections[ i ];
  667. }
  668. }
  669. return false;
  670. }
  671. //
  672. // Reusable utility variables
  673. const _tempEuler = new Euler();
  674. const _alignVector = new Vector3( 0, 1, 0 );
  675. const _zeroVector = new Vector3( 0, 0, 0 );
  676. const _lookAtMatrix = new Matrix4();
  677. const _tempQuaternion2 = new Quaternion();
  678. const _identityQuaternion = new Quaternion();
  679. const _dirVector = new Vector3();
  680. const _tempMatrix = new Matrix4();
  681. const _unitX = new Vector3( 1, 0, 0 );
  682. const _unitY = new Vector3( 0, 1, 0 );
  683. const _unitZ = new Vector3( 0, 0, 1 );
  684. const _v1 = new Vector3();
  685. const _v2 = new Vector3();
  686. const _v3 = new Vector3();
  687. class TransformControlsRoot extends Object3D {
  688. constructor( controls ) {
  689. super();
  690. this.isTransformControlsRoot = true;
  691. this.controls = controls;
  692. this.visible = false;
  693. }
  694. // updateMatrixWorld updates key transformation variables
  695. updateMatrixWorld( force ) {
  696. const controls = this.controls;
  697. if ( controls.object !== undefined ) {
  698. controls.object.updateMatrixWorld();
  699. if ( controls.object.parent === null ) {
  700. console.error( 'TransformControls: The attached 3D object must be a part of the scene graph.' );
  701. } else {
  702. controls.object.parent.matrixWorld.decompose( controls._parentPosition, controls._parentQuaternion, controls._parentScale );
  703. }
  704. controls.object.matrixWorld.decompose( controls.worldPosition, controls.worldQuaternion, controls._worldScale );
  705. controls._parentQuaternionInv.copy( controls._parentQuaternion ).invert();
  706. controls._worldQuaternionInv.copy( controls.worldQuaternion ).invert();
  707. }
  708. controls.camera.updateMatrixWorld();
  709. controls.camera.matrixWorld.decompose( controls.cameraPosition, controls.cameraQuaternion, controls._cameraScale );
  710. if ( controls.camera.isOrthographicCamera ) {
  711. controls.camera.getWorldDirection( controls.eye ).negate();
  712. } else {
  713. controls.eye.copy( controls.cameraPosition ).sub( controls.worldPosition ).normalize();
  714. }
  715. super.updateMatrixWorld( force );
  716. }
  717. dispose() {
  718. this.traverse( function ( child ) {
  719. if ( child.geometry ) child.geometry.dispose();
  720. if ( child.material ) child.material.dispose();
  721. } );
  722. }
  723. }
  724. class TransformControlsGizmo extends Object3D {
  725. constructor() {
  726. super();
  727. this.isTransformControlsGizmo = true;
  728. this.type = 'TransformControlsGizmo';
  729. // shared materials
  730. const gizmoMaterial = new MeshBasicMaterial( {
  731. depthTest: false,
  732. depthWrite: false,
  733. fog: false,
  734. toneMapped: false,
  735. transparent: true
  736. } );
  737. const gizmoLineMaterial = new LineBasicMaterial( {
  738. depthTest: false,
  739. depthWrite: false,
  740. fog: false,
  741. toneMapped: false,
  742. transparent: true
  743. } );
  744. // Make unique material for each axis/color
  745. const matInvisible = gizmoMaterial.clone();
  746. matInvisible.opacity = 0.15;
  747. const matHelper = gizmoLineMaterial.clone();
  748. matHelper.opacity = 0.5;
  749. const matRed = gizmoMaterial.clone();
  750. matRed.color.setHex( 0xff0000 );
  751. const matGreen = gizmoMaterial.clone();
  752. matGreen.color.setHex( 0x00ff00 );
  753. const matBlue = gizmoMaterial.clone();
  754. matBlue.color.setHex( 0x0000ff );
  755. const matRedTransparent = gizmoMaterial.clone();
  756. matRedTransparent.color.setHex( 0xff0000 );
  757. matRedTransparent.opacity = 0.5;
  758. const matGreenTransparent = gizmoMaterial.clone();
  759. matGreenTransparent.color.setHex( 0x00ff00 );
  760. matGreenTransparent.opacity = 0.5;
  761. const matBlueTransparent = gizmoMaterial.clone();
  762. matBlueTransparent.color.setHex( 0x0000ff );
  763. matBlueTransparent.opacity = 0.5;
  764. const matWhiteTransparent = gizmoMaterial.clone();
  765. matWhiteTransparent.opacity = 0.25;
  766. const matYellowTransparent = gizmoMaterial.clone();
  767. matYellowTransparent.color.setHex( 0xffff00 );
  768. matYellowTransparent.opacity = 0.25;
  769. const matYellow = gizmoMaterial.clone();
  770. matYellow.color.setHex( 0xffff00 );
  771. const matGray = gizmoMaterial.clone();
  772. matGray.color.setHex( 0x787878 );
  773. // reusable geometry
  774. const arrowGeometry = new CylinderGeometry( 0, 0.04, 0.1, 12 );
  775. arrowGeometry.translate( 0, 0.05, 0 );
  776. const scaleHandleGeometry = new BoxGeometry( 0.08, 0.08, 0.08 );
  777. scaleHandleGeometry.translate( 0, 0.04, 0 );
  778. const lineGeometry = new BufferGeometry();
  779. lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  780. const lineGeometry2 = new CylinderGeometry( 0.0075, 0.0075, 0.5, 3 );
  781. lineGeometry2.translate( 0, 0.25, 0 );
  782. function CircleGeometry( radius, arc ) {
  783. const geometry = new TorusGeometry( radius, 0.0075, 3, 64, arc * Math.PI * 2 );
  784. geometry.rotateY( Math.PI / 2 );
  785. geometry.rotateX( Math.PI / 2 );
  786. return geometry;
  787. }
  788. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  789. function TranslateHelperGeometry() {
  790. const geometry = new BufferGeometry();
  791. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  792. return geometry;
  793. }
  794. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  795. const gizmoTranslate = {
  796. X: [
  797. [ new Mesh( arrowGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  798. [ new Mesh( arrowGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  799. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  800. ],
  801. Y: [
  802. [ new Mesh( arrowGeometry, matGreen ), [ 0, 0.5, 0 ]],
  803. [ new Mesh( arrowGeometry, matGreen ), [ 0, - 0.5, 0 ], [ Math.PI, 0, 0 ]],
  804. [ new Mesh( lineGeometry2, matGreen ) ]
  805. ],
  806. Z: [
  807. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  808. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]],
  809. [ new Mesh( lineGeometry2, matBlue ), null, [ Math.PI / 2, 0, 0 ]]
  810. ],
  811. XYZ: [
  812. [ new Mesh( new OctahedronGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ]]
  813. ],
  814. XY: [
  815. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent.clone() ), [ 0.15, 0.15, 0 ]]
  816. ],
  817. YZ: [
  818. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  819. ],
  820. XZ: [
  821. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  822. ]
  823. };
  824. const pickerTranslate = {
  825. X: [
  826. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  827. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  828. ],
  829. Y: [
  830. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  831. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  832. ],
  833. Z: [
  834. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  835. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  836. ],
  837. XYZ: [
  838. [ new Mesh( new OctahedronGeometry( 0.2, 0 ), matInvisible ) ]
  839. ],
  840. XY: [
  841. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]]
  842. ],
  843. YZ: [
  844. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  845. ],
  846. XZ: [
  847. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  848. ]
  849. };
  850. const helperTranslate = {
  851. START: [
  852. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  853. ],
  854. END: [
  855. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  856. ],
  857. DELTA: [
  858. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  859. ],
  860. X: [
  861. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  862. ],
  863. Y: [
  864. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  865. ],
  866. Z: [
  867. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  868. ]
  869. };
  870. const gizmoRotate = {
  871. XYZE: [
  872. [ new Mesh( CircleGeometry( 0.5, 1 ), matGray ), null, [ 0, Math.PI / 2, 0 ]]
  873. ],
  874. X: [
  875. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matRed ) ]
  876. ],
  877. Y: [
  878. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matGreen ), null, [ 0, 0, - Math.PI / 2 ]]
  879. ],
  880. Z: [
  881. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matBlue ), null, [ 0, Math.PI / 2, 0 ]]
  882. ],
  883. E: [
  884. [ new Mesh( CircleGeometry( 0.75, 1 ), matYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]]
  885. ]
  886. };
  887. const helperRotate = {
  888. AXIS: [
  889. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  890. ]
  891. };
  892. const pickerRotate = {
  893. XYZE: [
  894. [ new Mesh( new SphereGeometry( 0.25, 10, 8 ), matInvisible ) ]
  895. ],
  896. X: [
  897. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  898. ],
  899. Y: [
  900. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  901. ],
  902. Z: [
  903. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  904. ],
  905. E: [
  906. [ new Mesh( new TorusGeometry( 0.75, 0.1, 2, 24 ), matInvisible ) ]
  907. ]
  908. };
  909. const gizmoScale = {
  910. X: [
  911. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  912. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  913. [ new Mesh( scaleHandleGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  914. ],
  915. Y: [
  916. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.5, 0 ]],
  917. [ new Mesh( lineGeometry2, matGreen ) ],
  918. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, - 0.5, 0 ], [ 0, 0, Math.PI ]],
  919. ],
  920. Z: [
  921. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  922. [ new Mesh( lineGeometry2, matBlue ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  923. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]]
  924. ],
  925. XY: [
  926. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent ), [ 0.15, 0.15, 0 ]]
  927. ],
  928. YZ: [
  929. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  930. ],
  931. XZ: [
  932. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  933. ],
  934. XYZ: [
  935. [ new Mesh( new BoxGeometry( 0.1, 0.1, 0.1 ), matWhiteTransparent.clone() ) ],
  936. ]
  937. };
  938. const pickerScale = {
  939. X: [
  940. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  941. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  942. ],
  943. Y: [
  944. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  945. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  946. ],
  947. Z: [
  948. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  949. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  950. ],
  951. XY: [
  952. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]],
  953. ],
  954. YZ: [
  955. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  956. ],
  957. XZ: [
  958. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  959. ],
  960. XYZ: [
  961. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 0 ]],
  962. ]
  963. };
  964. const helperScale = {
  965. X: [
  966. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  967. ],
  968. Y: [
  969. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  970. ],
  971. Z: [
  972. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  973. ]
  974. };
  975. // Creates an Object3D with gizmos described in custom hierarchy definition.
  976. function setupGizmo( gizmoMap ) {
  977. const gizmo = new Object3D();
  978. for ( const name in gizmoMap ) {
  979. for ( let i = gizmoMap[ name ].length; i --; ) {
  980. const object = gizmoMap[ name ][ i ][ 0 ].clone();
  981. const position = gizmoMap[ name ][ i ][ 1 ];
  982. const rotation = gizmoMap[ name ][ i ][ 2 ];
  983. const scale = gizmoMap[ name ][ i ][ 3 ];
  984. const tag = gizmoMap[ name ][ i ][ 4 ];
  985. // name and tag properties are essential for picking and updating logic.
  986. object.name = name;
  987. object.tag = tag;
  988. if ( position ) {
  989. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  990. }
  991. if ( rotation ) {
  992. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  993. }
  994. if ( scale ) {
  995. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  996. }
  997. object.updateMatrix();
  998. const tempGeometry = object.geometry.clone();
  999. tempGeometry.applyMatrix4( object.matrix );
  1000. object.geometry = tempGeometry;
  1001. object.renderOrder = Infinity;
  1002. object.position.set( 0, 0, 0 );
  1003. object.rotation.set( 0, 0, 0 );
  1004. object.scale.set( 1, 1, 1 );
  1005. gizmo.add( object );
  1006. }
  1007. }
  1008. return gizmo;
  1009. }
  1010. // Gizmo creation
  1011. this.gizmo = {};
  1012. this.picker = {};
  1013. this.helper = {};
  1014. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  1015. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  1016. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  1017. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  1018. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  1019. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  1020. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  1021. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  1022. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) );
  1023. // Pickers should be hidden always
  1024. this.picker[ 'translate' ].visible = false;
  1025. this.picker[ 'rotate' ].visible = false;
  1026. this.picker[ 'scale' ].visible = false;
  1027. }
  1028. // updateMatrixWorld will update transformations and appearance of individual handles
  1029. updateMatrixWorld( force ) {
  1030. const space = ( this.mode === 'scale' ) ? 'local' : this.space; // scale always oriented to local rotation
  1031. const quaternion = ( space === 'local' ) ? this.worldQuaternion : _identityQuaternion;
  1032. // Show only gizmos for current transform mode
  1033. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  1034. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  1035. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  1036. this.helper[ 'translate' ].visible = this.mode === 'translate';
  1037. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  1038. this.helper[ 'scale' ].visible = this.mode === 'scale';
  1039. let handles = [];
  1040. handles = handles.concat( this.picker[ this.mode ].children );
  1041. handles = handles.concat( this.gizmo[ this.mode ].children );
  1042. handles = handles.concat( this.helper[ this.mode ].children );
  1043. for ( let i = 0; i < handles.length; i ++ ) {
  1044. const handle = handles[ i ];
  1045. // hide aligned to camera
  1046. handle.visible = true;
  1047. handle.rotation.set( 0, 0, 0 );
  1048. handle.position.copy( this.worldPosition );
  1049. let factor;
  1050. if ( this.camera.isOrthographicCamera ) {
  1051. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  1052. } else {
  1053. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  1054. }
  1055. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 4 );
  1056. // TODO: simplify helpers and consider decoupling from gizmo
  1057. if ( handle.tag === 'helper' ) {
  1058. handle.visible = false;
  1059. if ( handle.name === 'AXIS' ) {
  1060. handle.visible = !! this.axis;
  1061. if ( this.axis === 'X' ) {
  1062. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, 0 ) );
  1063. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  1064. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1065. handle.visible = false;
  1066. }
  1067. }
  1068. if ( this.axis === 'Y' ) {
  1069. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, Math.PI / 2 ) );
  1070. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  1071. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1072. handle.visible = false;
  1073. }
  1074. }
  1075. if ( this.axis === 'Z' ) {
  1076. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  1077. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  1078. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  1079. handle.visible = false;
  1080. }
  1081. }
  1082. if ( this.axis === 'XYZE' ) {
  1083. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  1084. _alignVector.copy( this.rotationAxis );
  1085. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( _zeroVector, _alignVector, _unitY ) );
  1086. handle.quaternion.multiply( _tempQuaternion );
  1087. handle.visible = this.dragging;
  1088. }
  1089. if ( this.axis === 'E' ) {
  1090. handle.visible = false;
  1091. }
  1092. } else if ( handle.name === 'START' ) {
  1093. handle.position.copy( this.worldPositionStart );
  1094. handle.visible = this.dragging;
  1095. } else if ( handle.name === 'END' ) {
  1096. handle.position.copy( this.worldPosition );
  1097. handle.visible = this.dragging;
  1098. } else if ( handle.name === 'DELTA' ) {
  1099. handle.position.copy( this.worldPositionStart );
  1100. handle.quaternion.copy( this.worldQuaternionStart );
  1101. _tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  1102. _tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  1103. handle.scale.copy( _tempVector );
  1104. handle.visible = this.dragging;
  1105. } else {
  1106. handle.quaternion.copy( quaternion );
  1107. if ( this.dragging ) {
  1108. handle.position.copy( this.worldPositionStart );
  1109. } else {
  1110. handle.position.copy( this.worldPosition );
  1111. }
  1112. if ( this.axis ) {
  1113. handle.visible = this.axis.search( handle.name ) !== - 1;
  1114. }
  1115. }
  1116. // If updating helper, skip rest of the loop
  1117. continue;
  1118. }
  1119. // Align handles to current local or world rotation
  1120. handle.quaternion.copy( quaternion );
  1121. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  1122. // Hide translate and scale axis facing the camera
  1123. const AXIS_HIDE_THRESHOLD = 0.99;
  1124. const PLANE_HIDE_THRESHOLD = 0.2;
  1125. if ( handle.name === 'X' ) {
  1126. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  1127. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1128. handle.visible = false;
  1129. }
  1130. }
  1131. if ( handle.name === 'Y' ) {
  1132. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  1133. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1134. handle.visible = false;
  1135. }
  1136. }
  1137. if ( handle.name === 'Z' ) {
  1138. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  1139. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1140. handle.visible = false;
  1141. }
  1142. }
  1143. if ( handle.name === 'XY' ) {
  1144. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  1145. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1146. handle.visible = false;
  1147. }
  1148. }
  1149. if ( handle.name === 'YZ' ) {
  1150. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  1151. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1152. handle.visible = false;
  1153. }
  1154. }
  1155. if ( handle.name === 'XZ' ) {
  1156. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  1157. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  1158. handle.visible = false;
  1159. }
  1160. }
  1161. } else if ( this.mode === 'rotate' ) {
  1162. // Align handles to current local or world rotation
  1163. _tempQuaternion2.copy( quaternion );
  1164. _alignVector.copy( this.eye ).applyQuaternion( _tempQuaternion.copy( quaternion ).invert() );
  1165. if ( handle.name.search( 'E' ) !== - 1 ) {
  1166. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( this.eye, _zeroVector, _unitY ) );
  1167. }
  1168. if ( handle.name === 'X' ) {
  1169. _tempQuaternion.setFromAxisAngle( _unitX, Math.atan2( - _alignVector.y, _alignVector.z ) );
  1170. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  1171. handle.quaternion.copy( _tempQuaternion );
  1172. }
  1173. if ( handle.name === 'Y' ) {
  1174. _tempQuaternion.setFromAxisAngle( _unitY, Math.atan2( _alignVector.x, _alignVector.z ) );
  1175. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  1176. handle.quaternion.copy( _tempQuaternion );
  1177. }
  1178. if ( handle.name === 'Z' ) {
  1179. _tempQuaternion.setFromAxisAngle( _unitZ, Math.atan2( _alignVector.y, _alignVector.x ) );
  1180. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  1181. handle.quaternion.copy( _tempQuaternion );
  1182. }
  1183. }
  1184. // Hide disabled axes
  1185. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  1186. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  1187. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  1188. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  1189. // highlight selected axis
  1190. handle.material._color = handle.material._color || handle.material.color.clone();
  1191. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  1192. handle.material.color.copy( handle.material._color );
  1193. handle.material.opacity = handle.material._opacity;
  1194. if ( this.enabled && this.axis ) {
  1195. if ( handle.name === this.axis ) {
  1196. handle.material.color.setHex( 0xffff00 );
  1197. handle.material.opacity = 1.0;
  1198. } else if ( this.axis.split( '' ).some( function ( a ) {
  1199. return handle.name === a;
  1200. } ) ) {
  1201. handle.material.color.setHex( 0xffff00 );
  1202. handle.material.opacity = 1.0;
  1203. }
  1204. }
  1205. }
  1206. super.updateMatrixWorld( force );
  1207. }
  1208. }
  1209. //
  1210. class TransformControlsPlane extends Mesh {
  1211. constructor() {
  1212. super(
  1213. new PlaneGeometry( 100000, 100000, 2, 2 ),
  1214. new MeshBasicMaterial( { visible: false, wireframe: true, side: DoubleSide, transparent: true, opacity: 0.1, toneMapped: false } )
  1215. );
  1216. this.isTransformControlsPlane = true;
  1217. this.type = 'TransformControlsPlane';
  1218. }
  1219. updateMatrixWorld( force ) {
  1220. let space = this.space;
  1221. this.position.copy( this.worldPosition );
  1222. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  1223. _v1.copy( _unitX ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1224. _v2.copy( _unitY ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1225. _v3.copy( _unitZ ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  1226. // Align the plane for current transform mode, axis and space.
  1227. _alignVector.copy( _v2 );
  1228. switch ( this.mode ) {
  1229. case 'translate':
  1230. case 'scale':
  1231. switch ( this.axis ) {
  1232. case 'X':
  1233. _alignVector.copy( this.eye ).cross( _v1 );
  1234. _dirVector.copy( _v1 ).cross( _alignVector );
  1235. break;
  1236. case 'Y':
  1237. _alignVector.copy( this.eye ).cross( _v2 );
  1238. _dirVector.copy( _v2 ).cross( _alignVector );
  1239. break;
  1240. case 'Z':
  1241. _alignVector.copy( this.eye ).cross( _v3 );
  1242. _dirVector.copy( _v3 ).cross( _alignVector );
  1243. break;
  1244. case 'XY':
  1245. _dirVector.copy( _v3 );
  1246. break;
  1247. case 'YZ':
  1248. _dirVector.copy( _v1 );
  1249. break;
  1250. case 'XZ':
  1251. _alignVector.copy( _v3 );
  1252. _dirVector.copy( _v2 );
  1253. break;
  1254. case 'XYZ':
  1255. case 'E':
  1256. _dirVector.set( 0, 0, 0 );
  1257. break;
  1258. }
  1259. break;
  1260. case 'rotate':
  1261. default:
  1262. // special case for rotate
  1263. _dirVector.set( 0, 0, 0 );
  1264. }
  1265. if ( _dirVector.length() === 0 ) {
  1266. // If in rotate mode, make the plane parallel to camera
  1267. this.quaternion.copy( this.cameraQuaternion );
  1268. } else {
  1269. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  1270. this.quaternion.setFromRotationMatrix( _tempMatrix );
  1271. }
  1272. super.updateMatrixWorld( force );
  1273. }
  1274. }
  1275. export { TransformControls, TransformControlsGizmo, TransformControlsPlane };