TransformControls.js 44 KB

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