1
0

TransformControls.js 49 KB

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