TransformControls.js 44 KB

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