TransformControls.js 46 KB

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