TransformControls.js 44 KB

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