TransformControls.js 43 KB

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