TransformControls.js 43 KB

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