TransformControls.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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', mode: null };
  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( force ) {
  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 ).negate();
  155. } else {
  156. this.eye.copy( this.cameraPosition ).sub( this.worldPosition ).normalize();
  157. }
  158. super.updateMatrixWorld( force );
  159. }
  160. pointerHover( pointer ) {
  161. if ( this.object === undefined || this.dragging === true ) return;
  162. if ( pointer !== null ) _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 != null && pointer.button !== 0 ) ) return;
  172. if ( this.axis !== null ) {
  173. if ( pointer !== null ) _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 !== null && pointer.button !== - 1 ) ) return;
  200. if ( pointer !== null ) _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. let _inPlaneRotation = false;
  290. if ( axis === 'XYZE' ) {
  291. this.rotationAxis.copy( this._offset ).cross( this.eye ).normalize();
  292. this.rotationAngle = this._offset.dot( _tempVector.copy( this.rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
  293. } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
  294. this.rotationAxis.copy( _unit[ axis ] );
  295. _tempVector.copy( _unit[ axis ] );
  296. if ( space === 'local' ) {
  297. _tempVector.applyQuaternion( this.worldQuaternion );
  298. }
  299. _tempVector.cross( this.eye );
  300. // When _tempVector is 0 after cross with this.eye the vectors are parallel and should use in-plane rotation logic.
  301. if ( _tempVector.length() === 0 ) {
  302. _inPlaneRotation = true;
  303. } else {
  304. this.rotationAngle = this._offset.dot( _tempVector.normalize() ) * ROTATION_SPEED;
  305. }
  306. }
  307. if ( axis === 'E' || _inPlaneRotation ) {
  308. this.rotationAxis.copy( this.eye );
  309. this.rotationAngle = this.pointEnd.angleTo( this.pointStart );
  310. this._startNorm.copy( this.pointStart ).normalize();
  311. this._endNorm.copy( this.pointEnd ).normalize();
  312. this.rotationAngle *= ( this._endNorm.cross( this._startNorm ).dot( this.eye ) < 0 ? 1 : - 1 );
  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 !== null && 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.removeEventListener( 'pointermove', this._onPointerMove );
  342. this.domElement.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. // Detach from object
  355. detach() {
  356. this.object = undefined;
  357. this.visible = false;
  358. this.axis = null;
  359. return this;
  360. }
  361. reset() {
  362. if ( ! this.enabled ) return;
  363. if ( this.dragging ) {
  364. this.object.position.copy( this._positionStart );
  365. this.object.quaternion.copy( this._quaternionStart );
  366. this.object.scale.copy( this._scaleStart );
  367. this.dispatchEvent( _changeEvent );
  368. this.dispatchEvent( _objectChangeEvent );
  369. this.pointStart.copy( this.pointEnd );
  370. }
  371. }
  372. getRaycaster() {
  373. return _raycaster;
  374. }
  375. // TODO: deprecate
  376. getMode() {
  377. return this.mode;
  378. }
  379. setMode( mode ) {
  380. this.mode = mode;
  381. }
  382. setTranslationSnap( translationSnap ) {
  383. this.translationSnap = translationSnap;
  384. }
  385. setRotationSnap( rotationSnap ) {
  386. this.rotationSnap = rotationSnap;
  387. }
  388. setScaleSnap( scaleSnap ) {
  389. this.scaleSnap = scaleSnap;
  390. }
  391. setSize( size ) {
  392. this.size = size;
  393. }
  394. setSpace( space ) {
  395. this.space = space;
  396. }
  397. }
  398. // mouse / touch event handlers
  399. function getPointer( event ) {
  400. if ( this.domElement.ownerDocument.pointerLockElement ) {
  401. return {
  402. x: 0,
  403. y: 0,
  404. button: event.button
  405. };
  406. } else {
  407. const rect = this.domElement.getBoundingClientRect();
  408. return {
  409. x: ( event.clientX - rect.left ) / rect.width * 2 - 1,
  410. y: - ( event.clientY - rect.top ) / rect.height * 2 + 1,
  411. button: event.button
  412. };
  413. }
  414. }
  415. function onPointerHover( event ) {
  416. if ( ! this.enabled ) return;
  417. switch ( event.pointerType ) {
  418. case 'mouse':
  419. case 'pen':
  420. this.pointerHover( this._getPointer( event ) );
  421. break;
  422. }
  423. }
  424. function onPointerDown( event ) {
  425. if ( ! this.enabled ) return;
  426. if ( ! document.pointerLockElement ) {
  427. this.domElement.setPointerCapture( event.pointerId );
  428. }
  429. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  430. this.pointerHover( this._getPointer( event ) );
  431. this.pointerDown( this._getPointer( event ) );
  432. }
  433. function onPointerMove( event ) {
  434. if ( ! this.enabled ) return;
  435. this.pointerMove( this._getPointer( event ) );
  436. }
  437. function onPointerUp( event ) {
  438. if ( ! this.enabled ) return;
  439. this.domElement.releasePointerCapture( event.pointerId );
  440. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  441. this.pointerUp( this._getPointer( event ) );
  442. }
  443. function intersectObjectWithRay( object, raycaster, includeInvisible ) {
  444. const allIntersections = raycaster.intersectObject( object, true );
  445. for ( let i = 0; i < allIntersections.length; i ++ ) {
  446. if ( allIntersections[ i ].object.visible || includeInvisible ) {
  447. return allIntersections[ i ];
  448. }
  449. }
  450. return false;
  451. }
  452. //
  453. // Reusable utility variables
  454. const _tempEuler = new Euler();
  455. const _alignVector = new Vector3( 0, 1, 0 );
  456. const _zeroVector = new Vector3( 0, 0, 0 );
  457. const _lookAtMatrix = new Matrix4();
  458. const _tempQuaternion2 = new Quaternion();
  459. const _identityQuaternion = new Quaternion();
  460. const _dirVector = new Vector3();
  461. const _tempMatrix = new Matrix4();
  462. const _unitX = new Vector3( 1, 0, 0 );
  463. const _unitY = new Vector3( 0, 1, 0 );
  464. const _unitZ = new Vector3( 0, 0, 1 );
  465. const _v1 = new Vector3();
  466. const _v2 = new Vector3();
  467. const _v3 = new Vector3();
  468. class TransformControlsGizmo extends Object3D {
  469. constructor() {
  470. super();
  471. this.isTransformControlsGizmo = true;
  472. this.type = 'TransformControlsGizmo';
  473. // shared materials
  474. const gizmoMaterial = new MeshBasicMaterial( {
  475. depthTest: false,
  476. depthWrite: false,
  477. fog: false,
  478. toneMapped: false,
  479. transparent: true
  480. } );
  481. const gizmoLineMaterial = new LineBasicMaterial( {
  482. depthTest: false,
  483. depthWrite: false,
  484. fog: false,
  485. toneMapped: false,
  486. transparent: true
  487. } );
  488. // Make unique material for each axis/color
  489. const matInvisible = gizmoMaterial.clone();
  490. matInvisible.opacity = 0.15;
  491. const matHelper = gizmoLineMaterial.clone();
  492. matHelper.opacity = 0.5;
  493. const matRed = gizmoMaterial.clone();
  494. matRed.color.setHex( 0xff0000 );
  495. const matGreen = gizmoMaterial.clone();
  496. matGreen.color.setHex( 0x00ff00 );
  497. const matBlue = gizmoMaterial.clone();
  498. matBlue.color.setHex( 0x0000ff );
  499. const matRedTransparent = gizmoMaterial.clone();
  500. matRedTransparent.color.setHex( 0xff0000 );
  501. matRedTransparent.opacity = 0.5;
  502. const matGreenTransparent = gizmoMaterial.clone();
  503. matGreenTransparent.color.setHex( 0x00ff00 );
  504. matGreenTransparent.opacity = 0.5;
  505. const matBlueTransparent = gizmoMaterial.clone();
  506. matBlueTransparent.color.setHex( 0x0000ff );
  507. matBlueTransparent.opacity = 0.5;
  508. const matWhiteTransparent = gizmoMaterial.clone();
  509. matWhiteTransparent.opacity = 0.25;
  510. const matYellowTransparent = gizmoMaterial.clone();
  511. matYellowTransparent.color.setHex( 0xffff00 );
  512. matYellowTransparent.opacity = 0.25;
  513. const matYellow = gizmoMaterial.clone();
  514. matYellow.color.setHex( 0xffff00 );
  515. const matGray = gizmoMaterial.clone();
  516. matGray.color.setHex( 0x787878 );
  517. // reusable geometry
  518. const arrowGeometry = new CylinderGeometry( 0, 0.04, 0.1, 12 );
  519. arrowGeometry.translate( 0, 0.05, 0 );
  520. const scaleHandleGeometry = new BoxGeometry( 0.08, 0.08, 0.08 );
  521. scaleHandleGeometry.translate( 0, 0.04, 0 );
  522. const lineGeometry = new BufferGeometry();
  523. lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  524. const lineGeometry2 = new CylinderGeometry( 0.0075, 0.0075, 0.5, 3 );
  525. lineGeometry2.translate( 0, 0.25, 0 );
  526. function CircleGeometry( radius, arc ) {
  527. const geometry = new TorusGeometry( radius, 0.0075, 3, 64, arc * Math.PI * 2 );
  528. geometry.rotateY( Math.PI / 2 );
  529. geometry.rotateX( Math.PI / 2 );
  530. return geometry;
  531. }
  532. // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position
  533. function TranslateHelperGeometry() {
  534. const geometry = new BufferGeometry();
  535. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) );
  536. return geometry;
  537. }
  538. // Gizmo definitions - custom hierarchy definitions for setupGizmo() function
  539. const gizmoTranslate = {
  540. X: [
  541. [ new Mesh( arrowGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  542. [ new Mesh( arrowGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  543. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]]
  544. ],
  545. Y: [
  546. [ new Mesh( arrowGeometry, matGreen ), [ 0, 0.5, 0 ]],
  547. [ new Mesh( arrowGeometry, matGreen ), [ 0, - 0.5, 0 ], [ Math.PI, 0, 0 ]],
  548. [ new Mesh( lineGeometry2, matGreen ) ]
  549. ],
  550. Z: [
  551. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  552. [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]],
  553. [ new Mesh( lineGeometry2, matBlue ), null, [ Math.PI / 2, 0, 0 ]]
  554. ],
  555. XYZ: [
  556. [ new Mesh( new OctahedronGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ]]
  557. ],
  558. XY: [
  559. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent.clone() ), [ 0.15, 0.15, 0 ]]
  560. ],
  561. YZ: [
  562. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  563. ],
  564. XZ: [
  565. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  566. ]
  567. };
  568. const pickerTranslate = {
  569. X: [
  570. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  571. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  572. ],
  573. Y: [
  574. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  575. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  576. ],
  577. Z: [
  578. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  579. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  580. ],
  581. XYZ: [
  582. [ new Mesh( new OctahedronGeometry( 0.2, 0 ), matInvisible ) ]
  583. ],
  584. XY: [
  585. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]]
  586. ],
  587. YZ: [
  588. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  589. ],
  590. XZ: [
  591. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  592. ]
  593. };
  594. const helperTranslate = {
  595. START: [
  596. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  597. ],
  598. END: [
  599. [ new Mesh( new OctahedronGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ]
  600. ],
  601. DELTA: [
  602. [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ]
  603. ],
  604. X: [
  605. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  606. ],
  607. Y: [
  608. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  609. ],
  610. Z: [
  611. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  612. ]
  613. };
  614. const gizmoRotate = {
  615. XYZE: [
  616. [ new Mesh( CircleGeometry( 0.5, 1 ), matGray ), null, [ 0, Math.PI / 2, 0 ]]
  617. ],
  618. X: [
  619. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matRed ) ]
  620. ],
  621. Y: [
  622. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matGreen ), null, [ 0, 0, - Math.PI / 2 ]]
  623. ],
  624. Z: [
  625. [ new Mesh( CircleGeometry( 0.5, 0.5 ), matBlue ), null, [ 0, Math.PI / 2, 0 ]]
  626. ],
  627. E: [
  628. [ new Mesh( CircleGeometry( 0.75, 1 ), matYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]]
  629. ]
  630. };
  631. const helperRotate = {
  632. AXIS: [
  633. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  634. ]
  635. };
  636. const pickerRotate = {
  637. XYZE: [
  638. [ new Mesh( new SphereGeometry( 0.25, 10, 8 ), matInvisible ) ]
  639. ],
  640. X: [
  641. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]],
  642. ],
  643. Y: [
  644. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  645. ],
  646. Z: [
  647. [ new Mesh( new TorusGeometry( 0.5, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  648. ],
  649. E: [
  650. [ new Mesh( new TorusGeometry( 0.75, 0.1, 2, 24 ), matInvisible ) ]
  651. ]
  652. };
  653. const gizmoScale = {
  654. X: [
  655. [ new Mesh( scaleHandleGeometry, matRed ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  656. [ new Mesh( lineGeometry2, matRed ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  657. [ new Mesh( scaleHandleGeometry, matRed ), [ - 0.5, 0, 0 ], [ 0, 0, Math.PI / 2 ]],
  658. ],
  659. Y: [
  660. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.5, 0 ]],
  661. [ new Mesh( lineGeometry2, matGreen ) ],
  662. [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, - 0.5, 0 ], [ 0, 0, Math.PI ]],
  663. ],
  664. Z: [
  665. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]],
  666. [ new Mesh( lineGeometry2, matBlue ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]],
  667. [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, - 0.5 ], [ - Math.PI / 2, 0, 0 ]]
  668. ],
  669. XY: [
  670. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matBlueTransparent ), [ 0.15, 0.15, 0 ]]
  671. ],
  672. YZ: [
  673. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matRedTransparent ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]]
  674. ],
  675. XZ: [
  676. [ new Mesh( new BoxGeometry( 0.15, 0.15, 0.01 ), matGreenTransparent ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]]
  677. ],
  678. XYZ: [
  679. [ new Mesh( new BoxGeometry( 0.1, 0.1, 0.1 ), matWhiteTransparent.clone() ) ],
  680. ]
  681. };
  682. const pickerScale = {
  683. X: [
  684. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0.3, 0, 0 ], [ 0, 0, - Math.PI / 2 ]],
  685. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ - 0.3, 0, 0 ], [ 0, 0, Math.PI / 2 ]]
  686. ],
  687. Y: [
  688. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0.3, 0 ]],
  689. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, - 0.3, 0 ], [ 0, 0, Math.PI ]]
  690. ],
  691. Z: [
  692. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, 0.3 ], [ Math.PI / 2, 0, 0 ]],
  693. [ new Mesh( new CylinderGeometry( 0.2, 0, 0.6, 4 ), matInvisible ), [ 0, 0, - 0.3 ], [ - Math.PI / 2, 0, 0 ]]
  694. ],
  695. XY: [
  696. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0.15, 0 ]],
  697. ],
  698. YZ: [
  699. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]],
  700. ],
  701. XZ: [
  702. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.01 ), matInvisible ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]],
  703. ],
  704. XYZ: [
  705. [ new Mesh( new BoxGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 0 ]],
  706. ]
  707. };
  708. const helperScale = {
  709. X: [
  710. [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ]
  711. ],
  712. Y: [
  713. [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ]
  714. ],
  715. Z: [
  716. [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ]
  717. ]
  718. };
  719. // Creates an Object3D with gizmos described in custom hierarchy definition.
  720. function setupGizmo( gizmoMap ) {
  721. const gizmo = new Object3D();
  722. for ( const name in gizmoMap ) {
  723. for ( let i = gizmoMap[ name ].length; i --; ) {
  724. const object = gizmoMap[ name ][ i ][ 0 ].clone();
  725. const position = gizmoMap[ name ][ i ][ 1 ];
  726. const rotation = gizmoMap[ name ][ i ][ 2 ];
  727. const scale = gizmoMap[ name ][ i ][ 3 ];
  728. const tag = gizmoMap[ name ][ i ][ 4 ];
  729. // name and tag properties are essential for picking and updating logic.
  730. object.name = name;
  731. object.tag = tag;
  732. if ( position ) {
  733. object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] );
  734. }
  735. if ( rotation ) {
  736. object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] );
  737. }
  738. if ( scale ) {
  739. object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] );
  740. }
  741. object.updateMatrix();
  742. const tempGeometry = object.geometry.clone();
  743. tempGeometry.applyMatrix4( object.matrix );
  744. object.geometry = tempGeometry;
  745. object.renderOrder = Infinity;
  746. object.position.set( 0, 0, 0 );
  747. object.rotation.set( 0, 0, 0 );
  748. object.scale.set( 1, 1, 1 );
  749. gizmo.add( object );
  750. }
  751. }
  752. return gizmo;
  753. }
  754. // Gizmo creation
  755. this.gizmo = {};
  756. this.picker = {};
  757. this.helper = {};
  758. this.add( this.gizmo[ 'translate' ] = setupGizmo( gizmoTranslate ) );
  759. this.add( this.gizmo[ 'rotate' ] = setupGizmo( gizmoRotate ) );
  760. this.add( this.gizmo[ 'scale' ] = setupGizmo( gizmoScale ) );
  761. this.add( this.picker[ 'translate' ] = setupGizmo( pickerTranslate ) );
  762. this.add( this.picker[ 'rotate' ] = setupGizmo( pickerRotate ) );
  763. this.add( this.picker[ 'scale' ] = setupGizmo( pickerScale ) );
  764. this.add( this.helper[ 'translate' ] = setupGizmo( helperTranslate ) );
  765. this.add( this.helper[ 'rotate' ] = setupGizmo( helperRotate ) );
  766. this.add( this.helper[ 'scale' ] = setupGizmo( helperScale ) );
  767. // Pickers should be hidden always
  768. this.picker[ 'translate' ].visible = false;
  769. this.picker[ 'rotate' ].visible = false;
  770. this.picker[ 'scale' ].visible = false;
  771. }
  772. // updateMatrixWorld will update transformations and appearance of individual handles
  773. updateMatrixWorld( force ) {
  774. const space = ( this.mode === 'scale' ) ? 'local' : this.space; // scale always oriented to local rotation
  775. const quaternion = ( space === 'local' ) ? this.worldQuaternion : _identityQuaternion;
  776. // Show only gizmos for current transform mode
  777. this.gizmo[ 'translate' ].visible = this.mode === 'translate';
  778. this.gizmo[ 'rotate' ].visible = this.mode === 'rotate';
  779. this.gizmo[ 'scale' ].visible = this.mode === 'scale';
  780. this.helper[ 'translate' ].visible = this.mode === 'translate';
  781. this.helper[ 'rotate' ].visible = this.mode === 'rotate';
  782. this.helper[ 'scale' ].visible = this.mode === 'scale';
  783. let handles = [];
  784. handles = handles.concat( this.picker[ this.mode ].children );
  785. handles = handles.concat( this.gizmo[ this.mode ].children );
  786. handles = handles.concat( this.helper[ this.mode ].children );
  787. for ( let i = 0; i < handles.length; i ++ ) {
  788. const handle = handles[ i ];
  789. // hide aligned to camera
  790. handle.visible = true;
  791. handle.rotation.set( 0, 0, 0 );
  792. handle.position.copy( this.worldPosition );
  793. let factor;
  794. if ( this.camera.isOrthographicCamera ) {
  795. factor = ( this.camera.top - this.camera.bottom ) / this.camera.zoom;
  796. } else {
  797. factor = this.worldPosition.distanceTo( this.cameraPosition ) * Math.min( 1.9 * Math.tan( Math.PI * this.camera.fov / 360 ) / this.camera.zoom, 7 );
  798. }
  799. handle.scale.set( 1, 1, 1 ).multiplyScalar( factor * this.size / 4 );
  800. // TODO: simplify helpers and consider decoupling from gizmo
  801. if ( handle.tag === 'helper' ) {
  802. handle.visible = false;
  803. if ( handle.name === 'AXIS' ) {
  804. handle.visible = !! this.axis;
  805. if ( this.axis === 'X' ) {
  806. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, 0 ) );
  807. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  808. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  809. handle.visible = false;
  810. }
  811. }
  812. if ( this.axis === 'Y' ) {
  813. _tempQuaternion.setFromEuler( _tempEuler.set( 0, 0, Math.PI / 2 ) );
  814. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  815. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  816. handle.visible = false;
  817. }
  818. }
  819. if ( this.axis === 'Z' ) {
  820. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  821. handle.quaternion.copy( quaternion ).multiply( _tempQuaternion );
  822. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) {
  823. handle.visible = false;
  824. }
  825. }
  826. if ( this.axis === 'XYZE' ) {
  827. _tempQuaternion.setFromEuler( _tempEuler.set( 0, Math.PI / 2, 0 ) );
  828. _alignVector.copy( this.rotationAxis );
  829. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( _zeroVector, _alignVector, _unitY ) );
  830. handle.quaternion.multiply( _tempQuaternion );
  831. handle.visible = this.dragging;
  832. }
  833. if ( this.axis === 'E' ) {
  834. handle.visible = false;
  835. }
  836. } else if ( handle.name === 'START' ) {
  837. handle.position.copy( this.worldPositionStart );
  838. handle.visible = this.dragging;
  839. } else if ( handle.name === 'END' ) {
  840. handle.position.copy( this.worldPosition );
  841. handle.visible = this.dragging;
  842. } else if ( handle.name === 'DELTA' ) {
  843. handle.position.copy( this.worldPositionStart );
  844. handle.quaternion.copy( this.worldQuaternionStart );
  845. _tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 );
  846. _tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() );
  847. handle.scale.copy( _tempVector );
  848. handle.visible = this.dragging;
  849. } else {
  850. handle.quaternion.copy( quaternion );
  851. if ( this.dragging ) {
  852. handle.position.copy( this.worldPositionStart );
  853. } else {
  854. handle.position.copy( this.worldPosition );
  855. }
  856. if ( this.axis ) {
  857. handle.visible = this.axis.search( handle.name ) !== - 1;
  858. }
  859. }
  860. // If updating helper, skip rest of the loop
  861. continue;
  862. }
  863. // Align handles to current local or world rotation
  864. handle.quaternion.copy( quaternion );
  865. if ( this.mode === 'translate' || this.mode === 'scale' ) {
  866. // Hide translate and scale axis facing the camera
  867. const AXIS_HIDE_THRESHOLD = 0.99;
  868. const PLANE_HIDE_THRESHOLD = 0.2;
  869. if ( handle.name === 'X' ) {
  870. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  871. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  872. handle.visible = false;
  873. }
  874. }
  875. if ( handle.name === 'Y' ) {
  876. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  877. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  878. handle.visible = false;
  879. }
  880. }
  881. if ( handle.name === 'Z' ) {
  882. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_THRESHOLD ) {
  883. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  884. handle.visible = false;
  885. }
  886. }
  887. if ( handle.name === 'XY' ) {
  888. if ( Math.abs( _alignVector.copy( _unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  889. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  890. handle.visible = false;
  891. }
  892. }
  893. if ( handle.name === 'YZ' ) {
  894. if ( Math.abs( _alignVector.copy( _unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  895. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  896. handle.visible = false;
  897. }
  898. }
  899. if ( handle.name === 'XZ' ) {
  900. if ( Math.abs( _alignVector.copy( _unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_THRESHOLD ) {
  901. handle.scale.set( 1e-10, 1e-10, 1e-10 );
  902. handle.visible = false;
  903. }
  904. }
  905. } else if ( this.mode === 'rotate' ) {
  906. // Align handles to current local or world rotation
  907. _tempQuaternion2.copy( quaternion );
  908. _alignVector.copy( this.eye ).applyQuaternion( _tempQuaternion.copy( quaternion ).invert() );
  909. if ( handle.name.search( 'E' ) !== - 1 ) {
  910. handle.quaternion.setFromRotationMatrix( _lookAtMatrix.lookAt( this.eye, _zeroVector, _unitY ) );
  911. }
  912. if ( handle.name === 'X' ) {
  913. _tempQuaternion.setFromAxisAngle( _unitX, Math.atan2( - _alignVector.y, _alignVector.z ) );
  914. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  915. handle.quaternion.copy( _tempQuaternion );
  916. }
  917. if ( handle.name === 'Y' ) {
  918. _tempQuaternion.setFromAxisAngle( _unitY, Math.atan2( _alignVector.x, _alignVector.z ) );
  919. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  920. handle.quaternion.copy( _tempQuaternion );
  921. }
  922. if ( handle.name === 'Z' ) {
  923. _tempQuaternion.setFromAxisAngle( _unitZ, Math.atan2( _alignVector.y, _alignVector.x ) );
  924. _tempQuaternion.multiplyQuaternions( _tempQuaternion2, _tempQuaternion );
  925. handle.quaternion.copy( _tempQuaternion );
  926. }
  927. }
  928. // Hide disabled axes
  929. handle.visible = handle.visible && ( handle.name.indexOf( 'X' ) === - 1 || this.showX );
  930. handle.visible = handle.visible && ( handle.name.indexOf( 'Y' ) === - 1 || this.showY );
  931. handle.visible = handle.visible && ( handle.name.indexOf( 'Z' ) === - 1 || this.showZ );
  932. handle.visible = handle.visible && ( handle.name.indexOf( 'E' ) === - 1 || ( this.showX && this.showY && this.showZ ) );
  933. // highlight selected axis
  934. handle.material._color = handle.material._color || handle.material.color.clone();
  935. handle.material._opacity = handle.material._opacity || handle.material.opacity;
  936. handle.material.color.copy( handle.material._color );
  937. handle.material.opacity = handle.material._opacity;
  938. if ( this.enabled && this.axis ) {
  939. if ( handle.name === this.axis ) {
  940. handle.material.color.setHex( 0xffff00 );
  941. handle.material.opacity = 1.0;
  942. } else if ( this.axis.split( '' ).some( function ( a ) {
  943. return handle.name === a;
  944. } ) ) {
  945. handle.material.color.setHex( 0xffff00 );
  946. handle.material.opacity = 1.0;
  947. }
  948. }
  949. }
  950. super.updateMatrixWorld( force );
  951. }
  952. }
  953. //
  954. class TransformControlsPlane extends Mesh {
  955. constructor() {
  956. super(
  957. new PlaneGeometry( 100000, 100000, 2, 2 ),
  958. new MeshBasicMaterial( { visible: false, wireframe: true, side: DoubleSide, transparent: true, opacity: 0.1, toneMapped: false } )
  959. );
  960. this.isTransformControlsPlane = true;
  961. this.type = 'TransformControlsPlane';
  962. }
  963. updateMatrixWorld( force ) {
  964. let space = this.space;
  965. this.position.copy( this.worldPosition );
  966. if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation
  967. _v1.copy( _unitX ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  968. _v2.copy( _unitY ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  969. _v3.copy( _unitZ ).applyQuaternion( space === 'local' ? this.worldQuaternion : _identityQuaternion );
  970. // Align the plane for current transform mode, axis and space.
  971. _alignVector.copy( _v2 );
  972. switch ( this.mode ) {
  973. case 'translate':
  974. case 'scale':
  975. switch ( this.axis ) {
  976. case 'X':
  977. _alignVector.copy( this.eye ).cross( _v1 );
  978. _dirVector.copy( _v1 ).cross( _alignVector );
  979. break;
  980. case 'Y':
  981. _alignVector.copy( this.eye ).cross( _v2 );
  982. _dirVector.copy( _v2 ).cross( _alignVector );
  983. break;
  984. case 'Z':
  985. _alignVector.copy( this.eye ).cross( _v3 );
  986. _dirVector.copy( _v3 ).cross( _alignVector );
  987. break;
  988. case 'XY':
  989. _dirVector.copy( _v3 );
  990. break;
  991. case 'YZ':
  992. _dirVector.copy( _v1 );
  993. break;
  994. case 'XZ':
  995. _alignVector.copy( _v3 );
  996. _dirVector.copy( _v2 );
  997. break;
  998. case 'XYZ':
  999. case 'E':
  1000. _dirVector.set( 0, 0, 0 );
  1001. break;
  1002. }
  1003. break;
  1004. case 'rotate':
  1005. default:
  1006. // special case for rotate
  1007. _dirVector.set( 0, 0, 0 );
  1008. }
  1009. if ( _dirVector.length() === 0 ) {
  1010. // If in rotate mode, make the plane parallel to camera
  1011. this.quaternion.copy( this.cameraQuaternion );
  1012. } else {
  1013. _tempMatrix.lookAt( _tempVector.set( 0, 0, 0 ), _dirVector, _alignVector );
  1014. this.quaternion.setFromRotationMatrix( _tempMatrix );
  1015. }
  1016. super.updateMatrixWorld( force );
  1017. }
  1018. }
  1019. export { TransformControls, TransformControlsGizmo, TransformControlsPlane };