TransformControls.js 40 KB

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