TransformControls.js 44 KB

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