TransformControls.js 45 KB

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