TransformControls.js 46 KB

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