TransformControls.js 44 KB

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