TransformControls.js 43 KB

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