TransformControls.js 39 KB

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