TransformControls.js 44 KB

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