2
0

TransformControls.js 44 KB

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