TransformControls.js 40 KB

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