TransformControls.js 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. import {
  2. BoxGeometry,
  3. BufferGeometry,
  4. CylinderGeometry,
  5. DoubleSide,
  6. Euler,
  7. Float32BufferAttribute,
  8. Line,
  9. LineBasicMaterial,
  10. Matrix4,
  11. Mesh,
  12. MeshBasicMaterial,
  13. Object3D,
  14. OctahedronGeometry,
  15. PlaneGeometry,
  16. Quaternion,
  17. Raycaster,
  18. SphereGeometry,
  19. TorusGeometry,
  20. Vector3
  21. } from '../../../build/three.module.js';
  22. const _raycaster = new Raycaster();
  23. const _tempVector = new Vector3();
  24. const _tempVector2 = new Vector3();
  25. const _tempQuaternion = new Quaternion();
  26. const _unit = {
  27. X: new Vector3( 1, 0, 0 ),
  28. Y: new Vector3( 0, 1, 0 ),
  29. Z: new Vector3( 0, 0, 1 )
  30. };
  31. const _changeEvent = { type: 'change' };
  32. const _mouseDownEvent = { type: 'mouseDown' };
  33. const _mouseUpEvent = { type: 'mouseUp', mode: null };
  34. const _objectChangeEvent = { type: 'objectChange' };
  35. class TransformControls extends Object3D {
  36. constructor( camera, domElement ) {
  37. super();
  38. if ( domElement === undefined ) {
  39. console.warn( 'THREE.TransformControls: The second parameter "domElement" is now mandatory.' );
  40. domElement = document;
  41. }
  42. this.visible = false;
  43. this.domElement = domElement;
  44. this.domElement.style.touchAction = 'none'; // disable touch scroll
  45. const _gizmo = new TransformControlsGizmo();
  46. this._gizmo = _gizmo;
  47. this.add( _gizmo );
  48. const _plane = new TransformControlsPlane();
  49. this._plane = _plane;
  50. this.add( _plane );
  51. const scope = this;
  52. // Defined getter, setter and store for a property
  53. function defineProperty( propName, defaultValue ) {
  54. let propValue = defaultValue;
  55. Object.defineProperty( scope, propName, {
  56. get: function () {
  57. return propValue !== undefined ? propValue : defaultValue;
  58. },
  59. set: function ( value ) {
  60. if ( propValue !== value ) {
  61. propValue = value;
  62. _plane[ propName ] = value;
  63. _gizmo[ propName ] = value;
  64. scope.dispatchEvent( { type: propName + '-changed', value: value } );
  65. scope.dispatchEvent( _changeEvent );
  66. }
  67. }
  68. } );
  69. scope[ propName ] = defaultValue;
  70. _plane[ propName ] = defaultValue;
  71. _gizmo[ propName ] = defaultValue;
  72. }
  73. // Define properties with getters/setter
  74. // Setting the defined property will automatically trigger change event
  75. // Defined properties are passed down to gizmo and plane
  76. defineProperty( 'camera', camera );
  77. defineProperty( 'object', undefined );
  78. defineProperty( 'enabled', true );
  79. defineProperty( 'axis', null );
  80. defineProperty( 'mode', 'translate' );
  81. defineProperty( 'translationSnap', null );
  82. defineProperty( 'rotationSnap', null );
  83. defineProperty( 'scaleSnap', null );
  84. defineProperty( 'space', 'world' );
  85. defineProperty( 'size', 1 );
  86. defineProperty( 'dragging', false );
  87. defineProperty( 'showX', true );
  88. defineProperty( 'showY', true );
  89. defineProperty( 'showZ', true );
  90. // Reusable utility variables
  91. const worldPosition = new Vector3();
  92. const worldPositionStart = new Vector3();
  93. const worldQuaternion = new Quaternion();
  94. const worldQuaternionStart = new Quaternion();
  95. const cameraPosition = new Vector3();
  96. const cameraQuaternion = new Quaternion();
  97. const pointStart = new Vector3();
  98. const pointEnd = new Vector3();
  99. const rotationAxis = new Vector3();
  100. const rotationAngle = 0;
  101. const eye = new Vector3();
  102. // TODO: remove properties unused in plane and gizmo
  103. defineProperty( 'worldPosition', worldPosition );
  104. defineProperty( 'worldPositionStart', worldPositionStart );
  105. defineProperty( 'worldQuaternion', worldQuaternion );
  106. defineProperty( 'worldQuaternionStart', worldQuaternionStart );
  107. defineProperty( 'cameraPosition', cameraPosition );
  108. defineProperty( 'cameraQuaternion', cameraQuaternion );
  109. defineProperty( 'pointStart', pointStart );
  110. defineProperty( 'pointEnd', pointEnd );
  111. defineProperty( 'rotationAxis', rotationAxis );
  112. defineProperty( 'rotationAngle', rotationAngle );
  113. defineProperty( 'eye', eye );
  114. this._offset = new Vector3();
  115. this._startNorm = new Vector3();
  116. this._endNorm = new Vector3();
  117. this._cameraScale = new Vector3();
  118. this._parentPosition = new Vector3();
  119. this._parentQuaternion = new Quaternion();
  120. this._parentQuaternionInv = new Quaternion();
  121. this._parentScale = new Vector3();
  122. this._worldScaleStart = new Vector3();
  123. this._worldQuaternionInv = new Quaternion();
  124. this._worldScale = new Vector3();
  125. this._positionStart = new Vector3();
  126. this._quaternionStart = new Quaternion();
  127. this._scaleStart = new Vector3();
  128. this._getPointer = getPointer.bind( this );
  129. this._onPointerDown = onPointerDown.bind( this );
  130. this._onPointerHover = onPointerHover.bind( this );
  131. this._onPointerMove = onPointerMove.bind( this );
  132. this._onPointerUp = onPointerUp.bind( this );
  133. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  134. this.domElement.addEventListener( 'pointermove', this._onPointerHover );
  135. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  136. }
  137. // updateMatrixWorld updates key transformation variables
  138. updateMatrixWorld() {
  139. if ( this.object !== undefined ) {
  140. this.object.updateMatrixWorld();
  141. if ( this.object.parent === null ) {
  142. console.error( 'TransformControls: The attached 3D object must be a part of the scene graph.' );
  143. } else {
  144. this.object.parent.matrixWorld.decompose( this._parentPosition, this._parentQuaternion, this._parentScale );
  145. }
  146. this.object.matrixWorld.decompose( this.worldPosition, this.worldQuaternion, this._worldScale );
  147. this._parentQuaternionInv.copy( this._parentQuaternion ).invert();
  148. this._worldQuaternionInv.copy( this.worldQuaternion ).invert();
  149. }
  150. this.camera.updateMatrixWorld();
  151. this.camera.matrixWorld.decompose( this.cameraPosition, this.cameraQuaternion, this._cameraScale );
  152. this.eye.copy( this.cameraPosition ).sub( this.worldPosition ).normalize();
  153. super.updateMatrixWorld( this );
  154. }
  155. pointerHover( pointer ) {
  156. if ( this.object === undefined || this.dragging === true ) return;
  157. _raycaster.setFromCamera( pointer, this.camera );
  158. const intersect = intersectObjectWithRay( this._gizmo.picker[ this.mode ], _raycaster );
  159. if ( intersect ) {
  160. this.axis = intersect.object.name;
  161. } else {
  162. this.axis = null;
  163. }
  164. }
  165. pointerDown( pointer ) {
  166. if ( this.object === undefined || this.dragging === true || pointer.button !== 0 ) return;
  167. if ( this.axis !== null ) {
  168. _raycaster.setFromCamera( pointer, this.camera );
  169. const planeIntersect = intersectObjectWithRay( this._plane, _raycaster, true );
  170. if ( planeIntersect ) {
  171. this.object.updateMatrixWorld();
  172. this.object.parent.updateMatrixWorld();
  173. this._positionStart.copy( this.object.position );
  174. this._quaternionStart.copy( this.object.quaternion );
  175. this._scaleStart.copy( this.object.scale );
  176. this.object.matrixWorld.decompose( this.worldPositionStart, this.worldQuaternionStart, this._worldScaleStart );
  177. this.pointStart.copy( planeIntersect.point ).sub( this.worldPositionStart );
  178. }
  179. this.dragging = true;
  180. _mouseDownEvent.mode = this.mode;
  181. this.dispatchEvent( _mouseDownEvent );
  182. }
  183. }
  184. pointerMove( pointer ) {
  185. const axis = this.axis;
  186. const mode = this.mode;
  187. const object = this.object;
  188. let space = this.space;
  189. if ( mode === 'scale' ) {
  190. space = 'local';
  191. } else if ( axis === 'E' || axis === 'XYZE' || axis === 'XYZ' ) {
  192. space = 'world';
  193. }
  194. if ( object === undefined || axis === null || this.dragging === false || pointer.button !== - 1 ) return;
  195. _raycaster.setFromCamera( pointer, this.camera );
  196. const planeIntersect = intersectObjectWithRay( this._plane, _raycaster, true );
  197. if ( ! planeIntersect ) return;
  198. this.pointEnd.copy( planeIntersect.point ).sub( this.worldPositionStart );
  199. if ( mode === 'translate' ) {
  200. // Apply translate
  201. this._offset.copy( this.pointEnd ).sub( this.pointStart );
  202. if ( space === 'local' && axis !== 'XYZ' ) {
  203. this._offset.applyQuaternion( this._worldQuaternionInv );
  204. }
  205. if ( axis.indexOf( 'X' ) === - 1 ) this._offset.x = 0;
  206. if ( axis.indexOf( 'Y' ) === - 1 ) this._offset.y = 0;
  207. if ( axis.indexOf( 'Z' ) === - 1 ) this._offset.z = 0;
  208. if ( space === 'local' && axis !== 'XYZ' ) {
  209. this._offset.applyQuaternion( this._quaternionStart ).divide( this._parentScale );
  210. } else {
  211. this._offset.applyQuaternion( this._parentQuaternionInv ).divide( this._parentScale );
  212. }
  213. object.position.copy( this._offset ).add( this._positionStart );
  214. // Apply translation snap
  215. if ( this.translationSnap ) {
  216. if ( space === 'local' ) {
  217. object.position.applyQuaternion( _tempQuaternion.copy( this._quaternionStart ).invert() );
  218. if ( axis.search( 'X' ) !== - 1 ) {
  219. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  220. }
  221. if ( axis.search( 'Y' ) !== - 1 ) {
  222. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  223. }
  224. if ( axis.search( 'Z' ) !== - 1 ) {
  225. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  226. }
  227. object.position.applyQuaternion( this._quaternionStart );
  228. }
  229. if ( space === 'world' ) {
  230. if ( object.parent ) {
  231. object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  232. }
  233. if ( axis.search( 'X' ) !== - 1 ) {
  234. object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
  235. }
  236. if ( axis.search( 'Y' ) !== - 1 ) {
  237. object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
  238. }
  239. if ( axis.search( 'Z' ) !== - 1 ) {
  240. object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
  241. }
  242. if ( object.parent ) {
  243. object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
  244. }
  245. }
  246. }
  247. } else if ( mode === 'scale' ) {
  248. if ( axis.search( 'XYZ' ) !== - 1 ) {
  249. let d = this.pointEnd.length() / this.pointStart.length();
  250. if ( this.pointEnd.dot( this.pointStart ) < 0 ) d *= - 1;
  251. _tempVector2.set( d, d, d );
  252. } else {
  253. _tempVector.copy( this.pointStart );
  254. _tempVector2.copy( this.pointEnd );
  255. _tempVector.applyQuaternion( this._worldQuaternionInv );
  256. _tempVector2.applyQuaternion( this._worldQuaternionInv );
  257. _tempVector2.divide( _tempVector );
  258. if ( axis.search( 'X' ) === - 1 ) {
  259. _tempVector2.x = 1;
  260. }
  261. if ( axis.search( 'Y' ) === - 1 ) {
  262. _tempVector2.y = 1;
  263. }
  264. if ( axis.search( 'Z' ) === - 1 ) {
  265. _tempVector2.z = 1;
  266. }
  267. }
  268. // Apply scale
  269. object.scale.copy( this._scaleStart ).multiply( _tempVector2 );
  270. if ( this.scaleSnap ) {
  271. if ( axis.search( 'X' ) !== - 1 ) {
  272. object.scale.x = Math.round( object.scale.x / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  273. }
  274. if ( axis.search( 'Y' ) !== - 1 ) {
  275. object.scale.y = Math.round( object.scale.y / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  276. }
  277. if ( axis.search( 'Z' ) !== - 1 ) {
  278. object.scale.z = Math.round( object.scale.z / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
  279. }
  280. }
  281. } else if ( mode === 'rotate' ) {
  282. this._offset.copy( this.pointEnd ).sub( this.pointStart );
  283. const ROTATION_SPEED = 20 / this.worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
  284. if ( axis === 'E' ) {
  285. this.rotationAxis.copy( this.eye );
  286. this.rotationAngle = this.pointEnd.angleTo( this.pointStart );
  287. this._startNorm.copy( this.pointStart ).normalize();
  288. this._endNorm.copy( this.pointEnd ).normalize();
  289. this.rotationAngle *= ( this._endNorm.cross( this._startNorm ).dot( this.eye ) < 0 ? 1 : - 1 );
  290. } else if ( axis === 'XYZE' ) {
  291. this.rotationAxis.copy( this._offset ).cross( this.eye ).normalize();
  292. this.rotationAngle = this._offset.dot( _tempVector.copy( this.rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
  293. } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
  294. this.rotationAxis.copy( _unit[ axis ] );
  295. _tempVector.copy( _unit[ axis ] );
  296. if ( space === 'local' ) {
  297. _tempVector.applyQuaternion( this.worldQuaternion );
  298. }
  299. this.rotationAngle = this._offset.dot( _tempVector.cross( this.eye ).normalize() ) * ROTATION_SPEED;
  300. }
  301. // Apply rotation snap
  302. if ( this.rotationSnap ) this.rotationAngle = Math.round( this.rotationAngle / this.rotationSnap ) * this.rotationSnap;
  303. // Apply rotate
  304. if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) {
  305. object.quaternion.copy( this._quaternionStart );
  306. object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( this.rotationAxis, this.rotationAngle ) ).normalize();
  307. } else {
  308. this.rotationAxis.applyQuaternion( this._parentQuaternionInv );
  309. object.quaternion.copy( _tempQuaternion.setFromAxisAngle( this.rotationAxis, this.rotationAngle ) );
  310. object.quaternion.multiply( this._quaternionStart ).normalize();
  311. }
  312. }
  313. this.dispatchEvent( _changeEvent );
  314. this.dispatchEvent( _objectChangeEvent );
  315. }
  316. pointerUp( pointer ) {
  317. if ( pointer.button !== 0 ) return;
  318. if ( this.dragging && ( this.axis !== null ) ) {
  319. _mouseUpEvent.mode = this.mode;
  320. this.dispatchEvent( _mouseUpEvent );
  321. }
  322. this.dragging = false;
  323. this.axis = null;
  324. }
  325. dispose() {
  326. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  327. this.domElement.removeEventListener( 'pointermove', this._onPointerHover );
  328. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  329. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  330. this.traverse( function ( child ) {
  331. if ( child.geometry ) child.geometry.dispose();
  332. if ( child.material ) child.material.dispose();
  333. } );
  334. }
  335. // Set current object
  336. attach( object ) {
  337. this.object = object;
  338. this.visible = true;
  339. return this;
  340. }
  341. // Detatch from object
  342. detach() {
  343. this.object = undefined;
  344. this.visible = false;
  345. this.axis = null;
  346. return this;
  347. }
  348. getRaycaster() {
  349. return _raycaster;
  350. }
  351. // TODO: deprecate
  352. getMode() {
  353. return this.mode;
  354. }
  355. setMode( mode ) {
  356. this.mode = mode;
  357. }
  358. setTranslationSnap( translationSnap ) {
  359. this.translationSnap = translationSnap;
  360. }
  361. setRotationSnap( rotationSnap ) {
  362. this.rotationSnap = rotationSnap;
  363. }
  364. setScaleSnap( scaleSnap ) {
  365. this.scaleSnap = scaleSnap;
  366. }
  367. setSize( size ) {
  368. this.size = size;
  369. }
  370. setSpace( space ) {
  371. this.space = space;
  372. }
  373. update() {
  374. console.warn( 'THREE.TransformControls: update function has no more functionality and therefore has been deprecated.' );
  375. }
  376. }
  377. TransformControls.prototype.isTransformControls = true;
  378. // mouse / touch event handlers
  379. function getPointer( event ) {
  380. if ( this.domElement.ownerDocument.pointerLockElement ) {
  381. return {
  382. x: 0,
  383. y: 0,
  384. button: event.button
  385. };
  386. } else {
  387. const rect = this.domElement.getBoundingClientRect();
  388. return {
  389. x: ( event.clientX - rect.left ) / rect.width * 2 - 1,
  390. y: - ( event.clientY - rect.top ) / rect.height * 2 + 1,
  391. button: event.button
  392. };
  393. }
  394. }
  395. function onPointerHover( event ) {
  396. if ( ! this.enabled ) return;
  397. switch ( event.pointerType ) {
  398. case 'mouse':
  399. case 'pen':
  400. this.pointerHover( this._getPointer( event ) );
  401. break;
  402. }
  403. }
  404. function onPointerDown( event ) {
  405. if ( ! this.enabled ) return;
  406. this.domElement.setPointerCapture( event.pointerId );
  407. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  408. this.pointerHover( this._getPointer( event ) );
  409. this.pointerDown( this._getPointer( event ) );
  410. }
  411. function onPointerMove( event ) {
  412. if ( ! this.enabled ) return;
  413. this.pointerMove( this._getPointer( event ) );
  414. }
  415. function onPointerUp( event ) {
  416. if ( ! this.enabled ) return;
  417. this.domElement.releasePointerCapture( event.pointerId );
  418. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  419. this.pointerUp( this._getPointer( event ) );
  420. }
  421. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  422. const allIntersections = raycaster.intersectObject( object, true );
  423. for ( let i = 0; i < allIntersections.length; i ++ ) {
  424. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  425. return allIntersections[ i ];
  426. }
  427. }
  428. return false;
  429. }
  430. //
  431. // Reusable utility variables
  432. const _tempEuler = new Euler();
  433. const _alignVector = new Vector3( 0, 1, 0 );
  434. const _zeroVector = new Vector3( 0, 0, 0 );
  435. const _lookAtMatrix = new Matrix4();
  436. const _tempQuaternion2 = new Quaternion();
  437. const _identityQuaternion = new Quaternion();
  438. const _dirVector = new Vector3();
  439. const _tempMatrix = new Matrix4();
  440. const _unitX = new Vector3( 1, 0, 0 );
  441. const _unitY = new Vector3( 0, 1, 0 );
  442. const _unitZ = new Vector3( 0, 0, 1 );
  443. const _v1 = new Vector3();
  444. const _v2 = new Vector3();
  445. const _v3 = new Vector3();
  446. class TransformControlsGizmo extends Object3D {
  447. constructor() {
  448. super();
  449. this.type = 'TransformControlsGizmo';
  450. // shared materials
  451. const gizmoMaterial = new MeshBasicMaterial( {
  452. depthTest: false,
  453. depthWrite: false,
  454. fog: false,
  455. toneMapped: false,
  456. transparent: true
  457. } );
  458. const gizmoLineMaterial = new LineBasicMaterial( {
  459. depthTest: false,
  460. depthWrite: false,
  461. fog: false,
  462. toneMapped: false,
  463. transparent: true
  464. } );
  465. // Make unique material for each axis/color
  466. const matInvisible = gizmoMaterial.clone();
  467. matInvisible.opacity = 0.15;
  468. const matHelper = gizmoLineMaterial.clone();
  469. matHelper.opacity = 0.5;
  470. const matRed = gizmoMaterial.clone();
  471. matRed.color.setHex( 0xff0000 );
  472. const matGreen = gizmoMaterial.clone();
  473. matGreen.color.setHex( 0x00ff00 );
  474. const matBlue = gizmoMaterial.clone();
  475. matBlue.color.setHex( 0x0000ff );
  476. const matRedTransparent = gizmoMaterial.clone();
  477. matRedTransparent.color.setHex( 0xff0000 );
  478. matRedTransparent.opacity = 0.5;
  479. const matGreenTransparent = gizmoMaterial.clone();
  480. matGreenTransparent.color.setHex( 0x00ff00 );
  481. matGreenTransparent.opacity = 0.5;
  482. const matBlueTransparent = gizmoMaterial.clone();
  483. matBlueTransparent.color.setHex( 0x0000ff );
  484. matBlueTransparent.opacity = 0.5;
  485. const matWhiteTransparent = gizmoMaterial.clone();
  486. matWhiteTransparent.opacity = 0.25;
  487. const matYellowTransparent = gizmoMaterial.clone();
  488. matYellowTransparent.color.setHex( 0xffff00 );
  489. matYellowTransparent.opacity = 0.25;
  490. const matYellow = gizmoMaterial.clone();
  491. matYellow.color.setHex( 0xffff00 );
  492. const matGray = gizmoMaterial.clone();
  493. matGray.color.setHex( 0x787878 );
  494. // reusable geometry
  495. const arrowGeometry = new CylinderGeometry( 0, 0.04, 0.1, 12 );
  496. arrowGeometry.translate( 0, 0.05, 0 );
  497. const scaleHandleGeometry = new BoxGeometry( 0.08, 0.08, 0.08 );
  498. scaleHandleGeometry.translate( 0, 0.04, 0 );
  499. const lineGeometry = new BufferGeometry();
  500. lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  501. const lineGeometry2 = new CylinderGeometry( 0.0075, 0.0075, 0.5, 3 );
  502. lineGeometry2.translate( 0, 0.25, 0 );
  503. function CircleGeometry( radius, arc ) {
  504. const geometry = new TorusGeometry( radius, 0.0075, 3, 64, arc * Math.PI * 2 );
  505. geometry.rotateY( Math.PI / 2 );
  506. geometry.rotateX( Math.PI / 2 );
  507. return geometry;
  508. }
  509. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  510. function TranslateHelperGeometry() {
  511. const geometry = new BufferGeometry();
  512. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  513. return geometry;
  514. }
  515. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  516. const gizmoTranslate = {
  517. X: [
  518. [ new Mesh( arrowGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  519. [ new Mesh( arrowGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  520. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  521. ],
  522. Y: [
  523. [ new Mesh( arrowGeometry, matGreen ), [ 0, 0.5, 0 ]],
  524. [ new Mesh( arrowGeometry, matGreen ), [ 0, - 0.5, 0 ], [ Math.PI, 0, 0 ]],
  525. [ new Mesh( lineGeometry2, matGreen ) ]
  526. ],
  527. Z: [
  528. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  529. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]],
  530. [ new Mesh( lineGeometry2, matBlue ), null, [ Math.PI / 2, 0, 0 ]]
  531. ],
  532. XYZ: [
  533. [ new Mesh( new OctahedronGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ]]
  534. ],
  535. XY: [
  536. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent.clone() ), [ 0.15, 0.15, 0 ]]
  537. ],
  538. YZ: [
  539. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  540. ],
  541. XZ: [
  542. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  543. ]
  544. };
  545. const pickerTranslate = {
  546. X: [
  547. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  548. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  549. ],
  550. Y: [
  551. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  552. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  553. ],
  554. Z: [
  555. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  556. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  557. ],
  558. XYZ: [
  559. [ new Mesh( new OctahedronGeometry( 0.2, 0 ), matInvisible ) ]
  560. ],
  561. XY: [
  562. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]]
  563. ],
  564. YZ: [
  565. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  566. ],
  567. XZ: [
  568. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  569. ]
  570. };
  571. const helperTranslate = {
  572. START: [
  573. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  574. ],
  575. END: [
  576. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  577. ],
  578. DELTA: [
  579. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  580. ],
  581. X: [
  582. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  583. ],
  584. Y: [
  585. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  586. ],
  587. Z: [
  588. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  589. ]
  590. };
  591. const gizmoRotate = {
  592. XYZE: [
  593. [ new Mesh( CircleGeometry( 0.5, 1 ), matGray ), null, [ 0, Math.PI / 2, 0 ]]
  594. ],
  595. X: [
  596. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matRed ) ]
  597. ],
  598. Y: [
  599. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matGreen ), null, [ 0, 0, - Math.PI / 2 ]]
  600. ],
  601. Z: [
  602. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matBlue ), null, [ 0, Math.PI / 2, 0 ]]
  603. ],
  604. E: [
  605. [ new Mesh( CircleGeometry( 0.75, 1 ), matYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]]
  606. ]
  607. };
  608. const helperRotate = {
  609. AXIS: [
  610. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  611. ]
  612. };
  613. const pickerRotate = {
  614. XYZE: [
  615. [ new Mesh( new SphereGeometry( 0.25, 10, 8 ), matInvisible ) ]
  616. ],
  617. X: [
  618. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  619. ],
  620. Y: [
  621. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  622. ],
  623. Z: [
  624. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  625. ],
  626. E: [
  627. [ new Mesh( new TorusGeometry( 0.75, 0.1, 2, 24 ), matInvisible ) ]
  628. ]
  629. };
  630. const gizmoScale = {
  631. X: [
  632. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  633. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  634. [ new Mesh( scaleHandleGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  635. ],
  636. Y: [
  637. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.5, 0 ]],
  638. [ new Mesh( lineGeometry2, matGreen ) ],
  639. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, - 0.5, 0 ], [ 0, 0, Math.PI ]],
  640. ],
  641. Z: [
  642. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  643. [ new Mesh( lineGeometry2, matBlue ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  644. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]]
  645. ],
  646. XY: [
  647. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent ), [ 0.15, 0.15, 0 ]]
  648. ],
  649. YZ: [
  650. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  651. ],
  652. XZ: [
  653. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  654. ],
  655. XYZ: [
  656. [ new Mesh( new BoxGeometry( 0.1, 0.1, 0.1 ), matWhiteTransparent.clone() ) ],
  657. ]
  658. };
  659. const pickerScale = {
  660. X: [
  661. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  662. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  663. ],
  664. Y: [
  665. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  666. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  667. ],
  668. Z: [
  669. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  670. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  671. ],
  672. XY: [
  673. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]],
  674. ],
  675. YZ: [
  676. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  677. ],
  678. XZ: [
  679. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  680. ],
  681. XYZ: [
  682. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 0 ]],
  683. ]
  684. };
  685. const helperScale = {
  686. X: [
  687. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  688. ],
  689. Y: [
  690. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  691. ],
  692. Z: [
  693. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  694. ]
  695. };
  696. // Creates an Object3D with gizmos described in custom hierarchy definition.
  697. function setupGizmo( gizmoMap ) {
  698. const gizmo = new Object3D();
  699. for ( const name in gizmoMap ) {
  700. for ( let i = gizmoMap[ name ].length; i --; ) {
  701. const object = gizmoMap[ name ][ i ][ 0 ].clone();
  702. const position = gizmoMap[ name ][ i ][ 1 ];
  703. const rotation = gizmoMap[ name ][ i ][ 2 ];
  704. const scale = gizmoMap[ name ][ i ][ 3 ];
  705. const tag = gizmoMap[ name ][ i ][ 4 ];
  706. // name and tag properties are essential for picking and updating logic.
  707. object.name = name;
  708. object.tag = tag;
  709. if ( position ) {
  710. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  711. }
  712. if ( rotation ) {
  713. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  714. }
  715. if ( scale ) {
  716. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  717. }
  718. object.updateMatrix();
  719. const tempGeometry = object.geometry.clone();
  720. tempGeometry.applyMatrix4( object.matrix );
  721. object.geometry = tempGeometry;
  722. object.renderOrder = Infinity;
  723. object.position.set( 0, 0, 0 );
  724. object.rotation.set( 0, 0, 0 );
  725. object.scale.set( 1, 1, 1 );
  726. gizmo.add( object );
  727. }
  728. }
  729. return gizmo;
  730. }
  731. // Gizmo creation
  732. this.gizmo = {};
  733. this.picker = {};
  734. this.helper = {};
  735. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  736. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  737. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  738. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  739. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  740. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  741. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  742. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  743. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) );
  744. // Pickers should be hidden always
  745. this.picker[ 'translate' ].visible = false;
  746. this.picker[ 'rotate' ].visible = false;
  747. this.picker[ 'scale' ].visible = false;
  748. }
  749. // updateMatrixWorld will update transformations and appearance of individual handles
  750. updateMatrixWorld( force ) {
  751. const space = ( this.mode === 'scale' ) ? 'local' : this.space; // scale always oriented to local rotation
  752. const quaternion = ( space === 'local' ) ? this.worldQuaternion : _identityQuaternion;
  753. // Show only gizmos for current transform mode
  754. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  755. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  756. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  757. this.helper[ 'translate' ].visible = this.mode === 'translate';
  758. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  759. this.helper[ 'scale' ].visible = this.mode === 'scale';
  760. let handles = [];
  761. handles = handles.concat( this.picker[ this.mode ].children );
  762. handles = handles.concat( this.gizmo[ this.mode ].children );
  763. handles = handles.concat( this.helper[ this.mode ].children );
  764. for ( let i = 0; i < handles.length; i ++ ) {
  765. const handle = handles[ i ];
  766. // hide aligned to camera
  767. handle.visible = true;
  768. handle.rotation.set( 0, 0, 0 );
  769. handle.position.copy( this.worldPosition );
  770. let factor;
  771. if ( this.camera.isOrthographicCamera ) {
  772. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  773. } else {
  774. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  775. }
  776. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 4 );
  777. // TODO: simplify helpers and consider decoupling from gizmo
  778. if ( handle.tag === 'helper' ) {
  779. handle.visible = false;
  780. if ( handle.name === 'AXIS' ) {
  781. handle.position.copy( this.worldPositionStart );
  782. handle.visible = !! this.axis;
  783. if ( this.axis === 'X' ) {
  784. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, 0 ) );
  785. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  786. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  787. handle.visible = false;
  788. }
  789. }
  790. if ( this.axis === 'Y' ) {
  791. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, Math.PI / 2 ) );
  792. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  793. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  794. handle.visible = false;
  795. }
  796. }
  797. if ( this.axis === 'Z' ) {
  798. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  799. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  800. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  801. handle.visible = false;
  802. }
  803. }
  804. if ( this.axis === 'XYZE' ) {
  805. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  806. _alignVector.copy( this.rotationAxis );
  807. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( _zeroVector, _alignVector, _unitY ) );
  808. handle.quaternion.multiply( _tempQuaternion );
  809. handle.visible = this.dragging;
  810. }
  811. if ( this.axis === 'E' ) {
  812. handle.visible = false;
  813. }
  814. } else if ( handle.name === 'START' ) {
  815. handle.position.copy( this.worldPositionStart );
  816. handle.visible = this.dragging;
  817. } else if ( handle.name === 'END' ) {
  818. handle.position.copy( this.worldPosition );
  819. handle.visible = this.dragging;
  820. } else if ( handle.name === 'DELTA' ) {
  821. handle.position.copy( this.worldPositionStart );
  822. handle.quaternion.copy( this.worldQuaternionStart );
  823. _tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  824. _tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  825. handle.scale.copy( _tempVector );
  826. handle.visible = this.dragging;
  827. } else {
  828. handle.quaternion.copy( quaternion );
  829. if ( this.dragging ) {
  830. handle.position.copy( this.worldPositionStart );
  831. } else {
  832. handle.position.copy( this.worldPosition );
  833. }
  834. if ( this.axis ) {
  835. handle.visible = this.axis.search( handle.name ) !== - 1;
  836. }
  837. }
  838. // If updating helper, skip rest of the loop
  839. continue;
  840. }
  841. // Align handles to current local or world rotation
  842. handle.quaternion.copy( quaternion );
  843. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  844. // Hide translate and scale axis facing the camera
  845. const AXIS_HIDE_TRESHOLD = 0.99;
  846. const PLANE_HIDE_TRESHOLD = 0.2;
  847. if ( handle.name === 'X' ) {
  848. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  849. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  850. handle.visible = false;
  851. }
  852. }
  853. if ( handle.name === 'Y' ) {
  854. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  855. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  856. handle.visible = false;
  857. }
  858. }
  859. if ( handle.name === 'Z' ) {
  860. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  861. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  862. handle.visible = false;
  863. }
  864. }
  865. if ( handle.name === 'XY' ) {
  866. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  867. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  868. handle.visible = false;
  869. }
  870. }
  871. if ( handle.name === 'YZ' ) {
  872. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  873. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  874. handle.visible = false;
  875. }
  876. }
  877. if ( handle.name === 'XZ' ) {
  878. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  879. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  880. handle.visible = false;
  881. }
  882. }
  883. } else if ( this.mode === 'rotate' ) {
  884. // Align handles to current local or world rotation
  885. _tempQuaternion2.copy( quaternion );
  886. _alignVector.copy( this.eye ).applyQuaternion( _tempQuaternion.copy( quaternion ).invert() );
  887. if ( handle.name.search( 'E' ) !== - 1 ) {
  888. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( this.eye, _zeroVector, _unitY ) );
  889. }
  890. if ( handle.name === 'X' ) {
  891. _tempQuaternion.setFromAxisAngle( _unitX, Math.atan2( - _alignVector.y, _alignVector.z ) );
  892. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  893. handle.quaternion.copy( _tempQuaternion );
  894. }
  895. if ( handle.name === 'Y' ) {
  896. _tempQuaternion.setFromAxisAngle( _unitY, Math.atan2( _alignVector.x, _alignVector.z ) );
  897. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  898. handle.quaternion.copy( _tempQuaternion );
  899. }
  900. if ( handle.name === 'Z' ) {
  901. _tempQuaternion.setFromAxisAngle( _unitZ, Math.atan2( _alignVector.y, _alignVector.x ) );
  902. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  903. handle.quaternion.copy( _tempQuaternion );
  904. }
  905. }
  906. // Hide disabled axes
  907. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  908. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  909. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  910. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  911. // highlight selected axis
  912. handle.material._color = handle.material._color || handle.material.color.clone();
  913. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  914. handle.material.color.copy( handle.material._color );
  915. handle.material.opacity = handle.material._opacity;
  916. if ( this.enabled && this.axis ) {
  917. if ( handle.name === this.axis ) {
  918. handle.material.color.setHex( 0xffff00 );
  919. handle.material.opacity = 1.0;
  920. } else if ( this.axis.split( '' ).some( function ( a ) {
  921. return handle.name === a;
  922. } ) ) {
  923. handle.material.color.setHex( 0xffff00 );
  924. handle.material.opacity = 1.0;
  925. }
  926. }
  927. }
  928. super.updateMatrixWorld( force );
  929. }
  930. }
  931. TransformControlsGizmo.prototype.isTransformControlsGizmo = true;
  932. //
  933. class TransformControlsPlane extends Mesh {
  934. constructor() {
  935. super(
  936. new PlaneGeometry( 100000, 100000, 2, 2 ),
  937. new MeshBasicMaterial( { visible: false, wireframe: true, side: DoubleSide, transparent: true, opacity: 0.1, toneMapped: false } )
  938. );
  939. this.type = 'TransformControlsPlane';
  940. }
  941. updateMatrixWorld( force ) {
  942. let space = this.space;
  943. this.position.copy( this.worldPosition );
  944. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  945. _v1.copy( _unitX ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  946. _v2.copy( _unitY ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  947. _v3.copy( _unitZ ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  948. // Align the plane for current transform mode, axis and space.
  949. _alignVector.copy( _v2 );
  950. switch ( this.mode ) {
  951. case 'translate':
  952. case 'scale':
  953. switch ( this.axis ) {
  954. case 'X':
  955. _alignVector.copy( this.eye ).cross( _v1 );
  956. _dirVector.copy( _v1 ).cross( _alignVector );
  957. break;
  958. case 'Y':
  959. _alignVector.copy( this.eye ).cross( _v2 );
  960. _dirVector.copy( _v2 ).cross( _alignVector );
  961. break;
  962. case 'Z':
  963. _alignVector.copy( this.eye ).cross( _v3 );
  964. _dirVector.copy( _v3 ).cross( _alignVector );
  965. break;
  966. case 'XY':
  967. _dirVector.copy( _v3 );
  968. break;
  969. case 'YZ':
  970. _dirVector.copy( _v1 );
  971. break;
  972. case 'XZ':
  973. _alignVector.copy( _v3 );
  974. _dirVector.copy( _v2 );
  975. break;
  976. case 'XYZ':
  977. case 'E':
  978. _dirVector.set( 0, 0, 0 );
  979. break;
  980. }
  981. break;
  982. case 'rotate':
  983. default:
  984. // special case for rotate
  985. _dirVector.set( 0, 0, 0 );
  986. }
  987. if ( _dirVector.length() === 0 ) {
  988. // If in rotate mode, make the plane parallel to camera
  989. this.quaternion.copy( this.cameraQuaternion );
  990. } else {
  991. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  992. this.quaternion.setFromRotationMatrix( _tempMatrix );
  993. }
  994. super.updateMatrixWorld( force );
  995. }
  996. }
  997. TransformControlsPlane.prototype.isTransformControlsPlane = true;
  998. export { TransformControls, TransformControlsGizmo, TransformControlsPlane };