TransformControls.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545
  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. // TODO: deprecate
  361. getMode() {
  362. return this.mode;
  363. }
  364. setMode( mode ) {
  365. this.mode = mode;
  366. }
  367. setTranslationSnap( translationSnap ) {
  368. this.translationSnap = translationSnap;
  369. }
  370. setRotationSnap( rotationSnap ) {
  371. this.rotationSnap = rotationSnap;
  372. }
  373. setScaleSnap( scaleSnap ) {
  374. this.scaleSnap = scaleSnap;
  375. }
  376. setSize( size ) {
  377. this.size = size;
  378. }
  379. setSpace( space ) {
  380. this.space = space;
  381. }
  382. update() {
  383. console.warn( 'THREE.TransformControls: update function has no more functionality and therefore has been deprecated.' );
  384. }
  385. }
  386. TransformControls.prototype.isTransformControls = true;
  387. // mouse / touch event handlers
  388. function getPointer( event ) {
  389. if ( this.domElement.ownerDocument.pointerLockElement ) {
  390. return {
  391. x: 0,
  392. y: 0,
  393. button: event.button
  394. };
  395. } else {
  396. const rect = this.domElement.getBoundingClientRect();
  397. return {
  398. x: ( event.clientX - rect.left ) / rect.width * 2 - 1,
  399. y: - ( event.clientY - rect.top ) / rect.height * 2 + 1,
  400. button: event.button
  401. };
  402. }
  403. }
  404. function onPointerHover( event ) {
  405. if ( ! this.enabled ) return;
  406. switch ( event.pointerType ) {
  407. case 'mouse':
  408. case 'pen':
  409. this.pointerHover( this._getPointer( event ) );
  410. break;
  411. }
  412. }
  413. function onPointerDown( event ) {
  414. if ( ! this.enabled ) return;
  415. this.domElement.ownerDocument.addEventListener( 'pointermove', this._onPointerMove );
  416. this.pointerHover( this._getPointer( event ) );
  417. this.pointerDown( this._getPointer( event ) );
  418. }
  419. function onPointerMove( event ) {
  420. if ( ! this.enabled ) return;
  421. this.pointerMove( this._getPointer( event ) );
  422. }
  423. function onPointerUp( event ) {
  424. if ( ! this.enabled ) return;
  425. this.domElement.ownerDocument.removeEventListener( 'pointermove', this._onPointerMove );
  426. this.pointerUp( this._getPointer( event ) );
  427. }
  428. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  429. const allIntersections = raycaster.intersectObject( object, true );
  430. for ( let i = 0; i < allIntersections.length; i ++ ) {
  431. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  432. return allIntersections[ i ];
  433. }
  434. }
  435. return false;
  436. }
  437. //
  438. // Reusable utility variables
  439. const _tempEuler = new Euler();
  440. const _alignVector = new Vector3( 0, 1, 0 );
  441. const _zeroVector = new Vector3( 0, 0, 0 );
  442. const _lookAtMatrix = new Matrix4();
  443. const _tempQuaternion2 = new Quaternion();
  444. const _identityQuaternion = new Quaternion();
  445. const _dirVector = new Vector3();
  446. const _tempMatrix = new Matrix4();
  447. const _unitX = new Vector3( 1, 0, 0 );
  448. const _unitY = new Vector3( 0, 1, 0 );
  449. const _unitZ = new Vector3( 0, 0, 1 );
  450. const _v1 = new Vector3();
  451. const _v2 = new Vector3();
  452. const _v3 = new Vector3();
  453. class TransformControlsGizmo extends Object3D {
  454. constructor() {
  455. super();
  456. this.type = 'TransformControlsGizmo';
  457. // shared materials
  458. const gizmoMaterial = new MeshBasicMaterial( {
  459. depthTest: false,
  460. depthWrite: false,
  461. fog: false,
  462. toneMapped: false,
  463. transparent: true
  464. } );
  465. const gizmoLineMaterial = new LineBasicMaterial( {
  466. depthTest: false,
  467. depthWrite: false,
  468. fog: false,
  469. toneMapped: false,
  470. transparent: true
  471. } );
  472. // Make unique material for each axis/color
  473. const matInvisible = gizmoMaterial.clone();
  474. matInvisible.opacity = 0.15;
  475. const matHelper = gizmoLineMaterial.clone();
  476. matHelper.opacity = 0.5;
  477. const matRed = gizmoMaterial.clone();
  478. matRed.color.setHex( 0xff0000 );
  479. const matGreen = gizmoMaterial.clone();
  480. matGreen.color.setHex( 0x00ff00 );
  481. const matBlue = gizmoMaterial.clone();
  482. matBlue.color.setHex( 0x0000ff );
  483. const matRedTransparent = gizmoMaterial.clone();
  484. matRedTransparent.color.setHex( 0xff0000 );
  485. matRedTransparent.opacity = 0.5;
  486. const matGreenTransparent = gizmoMaterial.clone();
  487. matGreenTransparent.color.setHex( 0x00ff00 );
  488. matGreenTransparent.opacity = 0.5;
  489. const matBlueTransparent = gizmoMaterial.clone();
  490. matBlueTransparent.color.setHex( 0x0000ff );
  491. matBlueTransparent.opacity = 0.5;
  492. const matWhiteTransparent = gizmoMaterial.clone();
  493. matWhiteTransparent.opacity = 0.25;
  494. const matYellowTransparent = gizmoMaterial.clone();
  495. matYellowTransparent.color.setHex( 0xffff00 );
  496. matYellowTransparent.opacity = 0.25;
  497. const matYellow = gizmoMaterial.clone();
  498. matYellow.color.setHex( 0xffff00 );
  499. const matGray = gizmoMaterial.clone();
  500. matGray.color.setHex( 0x787878 );
  501. // reusable geometry
  502. const arrowGeometry = new CylinderGeometry( 0, 0.04, 0.1, 12 );
  503. arrowGeometry.translate( 0, 0.05, 0 );
  504. const scaleHandleGeometry = new BoxGeometry( 0.08, 0.08, 0.08 );
  505. scaleHandleGeometry.translate( 0, 0.04, 0 );
  506. const lineGeometry = new BufferGeometry();
  507. lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  508. const lineGeometry2 = new CylinderGeometry( 0.0075, 0.0075, 0.5, 3 );
  509. lineGeometry2.translate( 0, 0.25, 0 );
  510. function CircleGeometry( radius, arc ) {
  511. const geometry = new TorusGeometry( radius, 0.0075, 3, 64, arc * Math.PI * 2 );
  512. geometry.rotateY( Math.PI / 2 );
  513. geometry.rotateX( Math.PI / 2 );
  514. return geometry;
  515. }
  516. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  517. function TranslateHelperGeometry() {
  518. const geometry = new BufferGeometry();
  519. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  520. return geometry;
  521. }
  522. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  523. const gizmoTranslate = {
  524. X: [
  525. [ new Mesh( arrowGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  526. [ new Mesh( arrowGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  527. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  528. ],
  529. Y: [
  530. [ new Mesh( arrowGeometry, matGreen ), [ 0, 0.5, 0 ]],
  531. [ new Mesh( arrowGeometry, matGreen ), [ 0, - 0.5, 0 ], [ Math.PI, 0, 0 ]],
  532. [ new Mesh( lineGeometry2, matGreen ) ]
  533. ],
  534. Z: [
  535. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  536. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]],
  537. [ new Mesh( lineGeometry2, matBlue ), null, [ Math.PI / 2, 0, 0 ]]
  538. ],
  539. XYZ: [
  540. [ new Mesh( new OctahedronGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ]]
  541. ],
  542. XY: [
  543. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent.clone() ), [ 0.15, 0.15, 0 ]]
  544. ],
  545. YZ: [
  546. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  547. ],
  548. XZ: [
  549. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  550. ]
  551. };
  552. const pickerTranslate = {
  553. X: [
  554. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  555. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  556. ],
  557. Y: [
  558. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  559. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  560. ],
  561. Z: [
  562. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  563. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  564. ],
  565. XYZ: [
  566. [ new Mesh( new OctahedronGeometry( 0.2, 0 ), matInvisible ) ]
  567. ],
  568. XY: [
  569. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]]
  570. ],
  571. YZ: [
  572. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  573. ],
  574. XZ: [
  575. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  576. ]
  577. };
  578. const helperTranslate = {
  579. START: [
  580. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  581. ],
  582. END: [
  583. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  584. ],
  585. DELTA: [
  586. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  587. ],
  588. X: [
  589. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  590. ],
  591. Y: [
  592. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  593. ],
  594. Z: [
  595. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  596. ]
  597. };
  598. const gizmoRotate = {
  599. XYZE: [
  600. [ new Mesh( CircleGeometry( 0.5, 1 ), matGray ), null, [ 0, Math.PI / 2, 0 ]]
  601. ],
  602. X: [
  603. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matRed ) ]
  604. ],
  605. Y: [
  606. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matGreen ), null, [ 0, 0, - Math.PI / 2 ]]
  607. ],
  608. Z: [
  609. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matBlue ), null, [ 0, Math.PI / 2, 0 ]]
  610. ],
  611. E: [
  612. [ new Mesh( CircleGeometry( 0.75, 1 ), matYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]]
  613. ]
  614. };
  615. const helperRotate = {
  616. AXIS: [
  617. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  618. ]
  619. };
  620. const pickerRotate = {
  621. XYZE: [
  622. [ new Mesh( new SphereGeometry( 0.25, 10, 8 ), matInvisible ) ]
  623. ],
  624. X: [
  625. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  626. ],
  627. Y: [
  628. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  629. ],
  630. Z: [
  631. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  632. ],
  633. E: [
  634. [ new Mesh( new TorusGeometry( 0.75, 0.1, 2, 24 ), matInvisible ) ]
  635. ]
  636. };
  637. const gizmoScale = {
  638. X: [
  639. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  640. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  641. [ new Mesh( scaleHandleGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  642. ],
  643. Y: [
  644. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.5, 0 ]],
  645. [ new Mesh( lineGeometry2, matGreen ) ],
  646. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, - 0.5, 0 ], [ 0, 0, Math.PI ]],
  647. ],
  648. Z: [
  649. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  650. [ new Mesh( lineGeometry2, matBlue ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  651. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]]
  652. ],
  653. XY: [
  654. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent ), [ 0.15, 0.15, 0 ]]
  655. ],
  656. YZ: [
  657. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  658. ],
  659. XZ: [
  660. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  661. ],
  662. XYZ: [
  663. [ new Mesh( new BoxGeometry( 0.1, 0.1, 0.1 ), matWhiteTransparent.clone() ) ],
  664. ]
  665. };
  666. const pickerScale = {
  667. X: [
  668. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  669. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  670. ],
  671. Y: [
  672. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  673. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  674. ],
  675. Z: [
  676. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  677. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  678. ],
  679. XY: [
  680. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]],
  681. ],
  682. YZ: [
  683. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  684. ],
  685. XZ: [
  686. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  687. ],
  688. XYZ: [
  689. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 0 ]],
  690. ]
  691. };
  692. const helperScale = {
  693. X: [
  694. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  695. ],
  696. Y: [
  697. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  698. ],
  699. Z: [
  700. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  701. ]
  702. };
  703. // Creates an Object3D with gizmos described in custom hierarchy definition.
  704. function setupGizmo( gizmoMap ) {
  705. const gizmo = new Object3D();
  706. for ( const name in gizmoMap ) {
  707. for ( let i = gizmoMap[ name ].length; i --; ) {
  708. const object = gizmoMap[ name ][ i ][ 0 ].clone();
  709. const position = gizmoMap[ name ][ i ][ 1 ];
  710. const rotation = gizmoMap[ name ][ i ][ 2 ];
  711. const scale = gizmoMap[ name ][ i ][ 3 ];
  712. const tag = gizmoMap[ name ][ i ][ 4 ];
  713. // name and tag properties are essential for picking and updating logic.
  714. object.name = name;
  715. object.tag = tag;
  716. if ( position ) {
  717. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  718. }
  719. if ( rotation ) {
  720. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  721. }
  722. if ( scale ) {
  723. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  724. }
  725. object.updateMatrix();
  726. const tempGeometry = object.geometry.clone();
  727. tempGeometry.applyMatrix4( object.matrix );
  728. object.geometry = tempGeometry;
  729. object.renderOrder = Infinity;
  730. object.position.set( 0, 0, 0 );
  731. object.rotation.set( 0, 0, 0 );
  732. object.scale.set( 1, 1, 1 );
  733. gizmo.add( object );
  734. }
  735. }
  736. return gizmo;
  737. }
  738. // Gizmo creation
  739. this.gizmo = {};
  740. this.picker = {};
  741. this.helper = {};
  742. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  743. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  744. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  745. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  746. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  747. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  748. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  749. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  750. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) );
  751. // Pickers should be hidden always
  752. this.picker[ 'translate' ].visible = false;
  753. this.picker[ 'rotate' ].visible = false;
  754. this.picker[ 'scale' ].visible = false;
  755. }
  756. // updateMatrixWorld will update transformations and appearance of individual handles
  757. updateMatrixWorld( force ) {
  758. const space = ( this.mode === 'scale' ) ? 'local' : this.space; // scale always oriented to local rotation
  759. const quaternion = ( space === 'local' ) ? this.worldQuaternion : _identityQuaternion;
  760. // Show only gizmos for current transform mode
  761. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  762. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  763. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  764. this.helper[ 'translate' ].visible = this.mode === 'translate';
  765. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  766. this.helper[ 'scale' ].visible = this.mode === 'scale';
  767. let handles = [];
  768. handles = handles.concat( this.picker[ this.mode ].children );
  769. handles = handles.concat( this.gizmo[ this.mode ].children );
  770. handles = handles.concat( this.helper[ this.mode ].children );
  771. for ( let i = 0; i < handles.length; i ++ ) {
  772. const handle = handles[ i ];
  773. // hide aligned to camera
  774. handle.visible = true;
  775. handle.rotation.set( 0, 0, 0 );
  776. handle.position.copy( this.worldPosition );
  777. let factor;
  778. if ( this.camera.isOrthographicCamera ) {
  779. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  780. } else {
  781. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  782. }
  783. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 4 );
  784. // TODO: simplify helpers and consider decoupling from gizmo
  785. if ( handle.tag === 'helper' ) {
  786. handle.visible = false;
  787. if ( handle.name === 'AXIS' ) {
  788. handle.position.copy( this.worldPositionStart );
  789. handle.visible = !! this.axis;
  790. if ( this.axis === 'X' ) {
  791. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, 0 ) );
  792. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  793. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  794. handle.visible = false;
  795. }
  796. }
  797. if ( this.axis === 'Y' ) {
  798. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, Math.PI / 2 ) );
  799. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  800. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  801. handle.visible = false;
  802. }
  803. }
  804. if ( this.axis === 'Z' ) {
  805. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  806. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  807. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  808. handle.visible = false;
  809. }
  810. }
  811. if ( this.axis === 'XYZE' ) {
  812. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  813. _alignVector.copy( this.rotationAxis );
  814. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( _zeroVector, _alignVector, _unitY ) );
  815. handle.quaternion.multiply( _tempQuaternion );
  816. handle.visible = this.dragging;
  817. }
  818. if ( this.axis === 'E' ) {
  819. handle.visible = false;
  820. }
  821. } else if ( handle.name === 'START' ) {
  822. handle.position.copy( this.worldPositionStart );
  823. handle.visible = this.dragging;
  824. } else if ( handle.name === 'END' ) {
  825. handle.position.copy( this.worldPosition );
  826. handle.visible = this.dragging;
  827. } else if ( handle.name === 'DELTA' ) {
  828. handle.position.copy( this.worldPositionStart );
  829. handle.quaternion.copy( this.worldQuaternionStart );
  830. _tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  831. _tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  832. handle.scale.copy( _tempVector );
  833. handle.visible = this.dragging;
  834. } else {
  835. handle.quaternion.copy( quaternion );
  836. if ( this.dragging ) {
  837. handle.position.copy( this.worldPositionStart );
  838. } else {
  839. handle.position.copy( this.worldPosition );
  840. }
  841. if ( this.axis ) {
  842. handle.visible = this.axis.search( handle.name ) !== - 1;
  843. }
  844. }
  845. // If updating helper, skip rest of the loop
  846. continue;
  847. }
  848. // Align handles to current local or world rotation
  849. handle.quaternion.copy( quaternion );
  850. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  851. // Hide translate and scale axis facing the camera
  852. const AXIS_HIDE_TRESHOLD = 0.99;
  853. const PLANE_HIDE_TRESHOLD = 0.2;
  854. if ( handle.name === 'X' ) {
  855. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  856. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  857. handle.visible = false;
  858. }
  859. }
  860. if ( handle.name === 'Y' ) {
  861. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  862. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  863. handle.visible = false;
  864. }
  865. }
  866. if ( handle.name === 'Z' ) {
  867. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) {
  868. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  869. handle.visible = false;
  870. }
  871. }
  872. if ( handle.name === 'XY' ) {
  873. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  874. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  875. handle.visible = false;
  876. }
  877. }
  878. if ( handle.name === 'YZ' ) {
  879. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  880. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  881. handle.visible = false;
  882. }
  883. }
  884. if ( handle.name === 'XZ' ) {
  885. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) {
  886. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  887. handle.visible = false;
  888. }
  889. }
  890. } else if ( this.mode === 'rotate' ) {
  891. // Align handles to current local or world rotation
  892. _tempQuaternion2.copy( quaternion );
  893. _alignVector.copy( this.eye ).applyQuaternion( _tempQuaternion.copy( quaternion ).invert() );
  894. if ( handle.name.search( 'E' ) !== - 1 ) {
  895. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( this.eye, _zeroVector, _unitY ) );
  896. }
  897. if ( handle.name === 'X' ) {
  898. _tempQuaternion.setFromAxisAngle( _unitX, Math.atan2( - _alignVector.y, _alignVector.z ) );
  899. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  900. handle.quaternion.copy( _tempQuaternion );
  901. }
  902. if ( handle.name === 'Y' ) {
  903. _tempQuaternion.setFromAxisAngle( _unitY, Math.atan2( _alignVector.x, _alignVector.z ) );
  904. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  905. handle.quaternion.copy( _tempQuaternion );
  906. }
  907. if ( handle.name === 'Z' ) {
  908. _tempQuaternion.setFromAxisAngle( _unitZ, Math.atan2( _alignVector.y, _alignVector.x ) );
  909. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  910. handle.quaternion.copy( _tempQuaternion );
  911. }
  912. }
  913. // Hide disabled axes
  914. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  915. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  916. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  917. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  918. // highlight selected axis
  919. handle.material._color = handle.material._color || handle.material.color.clone();
  920. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  921. handle.material.color.copy( handle.material._color );
  922. handle.material.opacity = handle.material._opacity;
  923. if ( this.enabled && this.axis ) {
  924. if ( handle.name === this.axis ) {
  925. handle.material.color.setHex( 0xffff00 );
  926. handle.material.opacity = 1.0;
  927. } else if ( this.axis.split( '' ).some( function ( a ) {
  928. return handle.name === a;
  929. } ) ) {
  930. handle.material.color.setHex( 0xffff00 );
  931. handle.material.opacity = 1.0;
  932. }
  933. }
  934. }
  935. super.updateMatrixWorld( force );
  936. }
  937. }
  938. TransformControlsGizmo.prototype.isTransformControlsGizmo = true;
  939. //
  940. class TransformControlsPlane extends Mesh {
  941. constructor() {
  942. super(
  943. new PlaneGeometry( 100000, 100000, 2, 2 ),
  944. new MeshBasicMaterial( { visible: false, wireframe: true, side: DoubleSide, transparent: true, opacity: 0.1, toneMapped: false } )
  945. );
  946. this.type = 'TransformControlsPlane';
  947. }
  948. updateMatrixWorld( force ) {
  949. let space = this.space;
  950. this.position.copy( this.worldPosition );
  951. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  952. _v1.copy( _unitX ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  953. _v2.copy( _unitY ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  954. _v3.copy( _unitZ ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  955. // Align the plane for current transform mode, axis and space.
  956. _alignVector.copy( _v2 );
  957. switch ( this.mode ) {
  958. case 'translate':
  959. case 'scale':
  960. switch ( this.axis ) {
  961. case 'X':
  962. _alignVector.copy( this.eye ).cross( _v1 );
  963. _dirVector.copy( _v1 ).cross( _alignVector );
  964. break;
  965. case 'Y':
  966. _alignVector.copy( this.eye ).cross( _v2 );
  967. _dirVector.copy( _v2 ).cross( _alignVector );
  968. break;
  969. case 'Z':
  970. _alignVector.copy( this.eye ).cross( _v3 );
  971. _dirVector.copy( _v3 ).cross( _alignVector );
  972. break;
  973. case 'XY':
  974. _dirVector.copy( _v3 );
  975. break;
  976. case 'YZ':
  977. _dirVector.copy( _v1 );
  978. break;
  979. case 'XZ':
  980. _alignVector.copy( _v3 );
  981. _dirVector.copy( _v2 );
  982. break;
  983. case 'XYZ':
  984. case 'E':
  985. _dirVector.set( 0, 0, 0 );
  986. break;
  987. }
  988. break;
  989. case 'rotate':
  990. default:
  991. // special case for rotate
  992. _dirVector.set( 0, 0, 0 );
  993. }
  994. if ( _dirVector.length() === 0 ) {
  995. // If in rotate mode, make the plane parallel to camera
  996. this.quaternion.copy( this.cameraQuaternion );
  997. } else {
  998. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  999. this.quaternion.setFromRotationMatrix( _tempMatrix );
  1000. }
  1001. super.updateMatrixWorld( force );
  1002. }
  1003. }
  1004. TransformControlsPlane.prototype.isTransformControlsPlane = true;
  1005. export { TransformControls, TransformControlsGizmo, TransformControlsPlane };