TransformControls.js 50 KB

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