TransformControls.js 44 KB

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