TransformControls.js 43 KB

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