TransformControls.js 50 KB

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