TransformControls.js 40 KB

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