TransformControls.js 41 KB

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