2
0

TransformControls.js 45 KB

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