TransformControls.js 41 KB

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