TransformControls.js 44 KB

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