TransformControls.js 44 KB

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