TransformControls.js 41 KB

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