TransformControls.js 43 KB

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