TransformControls.js 40 KB

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