TransformControls.js 44 KB

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