TransformControls.js 39 KB

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