TransformControls.js 44 KB

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