TransformControls.js 44 KB

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