TransformControls.js 40 KB

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