TransformControls.js 40 KB

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