TransformControls.js 45 KB

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