TransformControls.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /**
  2. * @author arodic / https://github.com/arodic
  3. */
  4. "use strict";
  5. THREE.TransformGizmoMaterial = function ( parameters ) {
  6. THREE.MeshBasicMaterial.call( this );
  7. this.depthTest = false;
  8. this.depthWrite = false;
  9. this.side = THREE.DoubleSide;
  10. this.transparent = true;
  11. this.setValues( parameters );
  12. }
  13. THREE.TransformGizmoMaterial.prototype = Object.create( THREE.MeshBasicMaterial.prototype );
  14. THREE.TransformGizmo = function () {
  15. this.handleGizmos = {
  16. X: [
  17. new THREE.Mesh( new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0xff0000 } ) ),
  18. new THREE.Vector3( 0.5, 0, 0 ),
  19. new THREE.Vector3( 0, 0, -Math.PI/2 )
  20. ],
  21. Y: [
  22. new THREE.Mesh( new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x00ff00 } ) ),
  23. new THREE.Vector3( 0, 0.5, 0 )
  24. ],
  25. Z: [
  26. new THREE.Mesh( new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x0000ff } ) ),
  27. new THREE.Vector3( 0, 0, 0.5 ),
  28. new THREE.Vector3( Math.PI/2, 0, 0 )
  29. ]
  30. }
  31. var showPickers = false; //debug
  32. var showActivePlane = false; //debug
  33. this.activePlane = undefined;
  34. this.init = function () {
  35. THREE.Object3D.call( this );
  36. this.handles = new THREE.Object3D();
  37. this.pickers = new THREE.Object3D();
  38. this.planes = new THREE.Object3D();
  39. this.add(this.handles);
  40. this.add(this.pickers);
  41. this.add(this.planes);
  42. //// PLANES
  43. var planeGeometry = new THREE.PlaneGeometry( 50, 50, 2, 2 );
  44. var planeMaterial = new THREE.MeshBasicMaterial( { wireframe: true } );
  45. planeMaterial.side = THREE.DoubleSide;
  46. var planes = {
  47. "XY": new THREE.Mesh( planeGeometry, planeMaterial ),
  48. "YZ": new THREE.Mesh( planeGeometry, planeMaterial ),
  49. "XZ": new THREE.Mesh( planeGeometry, planeMaterial ),
  50. "XYZE": new THREE.Mesh( planeGeometry, planeMaterial )
  51. };
  52. planes["YZ"].rotation.set( 0, Math.PI/2, 0 );
  53. planes["XZ"].rotation.set( -Math.PI/2, 0, 0 );
  54. for (var i in planes) {
  55. planes[i].name = i;
  56. this.planes.add(planes[i]);
  57. this.planes[i] = planes[i];
  58. planes[i].visible = false;
  59. }
  60. //// HANDLES AND PICKERS
  61. for ( var i in this.handleGizmos ) {
  62. var handle = this.handleGizmos[i][0];
  63. handle.name = i;
  64. if ( this.handleGizmos[i][1] ) handle.position.set( this.handleGizmos[i][1].x, this.handleGizmos[i][1].y, this.handleGizmos[i][1].z );
  65. if ( this.handleGizmos[i][2] ) handle.rotation.set( this.handleGizmos[i][2].x, this.handleGizmos[i][2].y, this.handleGizmos[i][2].z );
  66. this.handles.add( handle );
  67. if ( this.pickerGizmos && this.pickerGizmos[i] ) {
  68. var picker = this.pickerGizmos[i][0];
  69. if ( this.pickerGizmos[i][1] ) picker.position.set( this.pickerGizmos[i][1].x, this.pickerGizmos[i][1].y, this.pickerGizmos[i][1].z );
  70. if ( this.pickerGizmos[i][2] ) picker.rotation.set( this.pickerGizmos[i][2].x, this.pickerGizmos[i][2].y, this.pickerGizmos[i][2].z );
  71. } else {
  72. var picker = handle.clone();
  73. }
  74. picker.name = i;
  75. this.pickers.add( picker );
  76. }
  77. // reset Transformations
  78. this.traverse(function (child) {
  79. if (child instanceof THREE.Mesh) {
  80. var tempGeometry = new THREE.Geometry();
  81. THREE.GeometryUtils.merge( tempGeometry, child );
  82. child.geometry = tempGeometry;
  83. child.position.set( 0, 0, 0 );
  84. child.rotation.set( 0, 0, 0 );
  85. child.scale.set( 1, 1, 1 );
  86. }
  87. });
  88. }
  89. this.hide = function () {
  90. for ( var j in this.handles.children ) this.handles.children[j].visible = false;
  91. for ( var j in this.pickers.children ) this.pickers.children[j].visible = false;
  92. for ( var j in this.planes.children ) this.planes.children[j].visible = false;
  93. }
  94. this.show = function () {
  95. for ( var i in this.handles.children ) {
  96. this.handles.children[i].visible = true;
  97. }
  98. for ( var i in this.pickers.children ) {
  99. this.pickers.children[i].visible = showPickers;
  100. }
  101. if ( this.activePlane !== undefined ) this.activePlane.visible = showActivePlane;
  102. }
  103. this.highlight = function ( axis ) {
  104. var handle;
  105. for ( var i in this.handleGizmos ) {
  106. handle = this.handleGizmos[ i ][0];
  107. if ( handle.material.oldColor !== undefined ) {
  108. handle.material.color.copy( handle.material.oldColor );
  109. handle.material.opacity = handle.material.oldOpacity;
  110. }
  111. }
  112. if ( this.handleGizmos[ axis ] !== undefined ) {
  113. handle = this.handleGizmos[ axis ][0];
  114. handle.material.oldColor = handle.material.color.clone();
  115. handle.material.oldOpacity = handle.material.opacity;
  116. handle.material.color.setRGB( 1, 1, 0 );
  117. handle.material.opacity = 1;
  118. }
  119. }
  120. this.init();
  121. }
  122. THREE.TransformGizmo.prototype = Object.create( THREE.Object3D.prototype );
  123. THREE.TransformGizmo.prototype.update = function ( rotation, eye ) {
  124. var vec1 = new THREE.Vector3( 0, 0, 0 );
  125. var vec2 = new THREE.Vector3( 0, 1, 0 );
  126. var lookAtMatrix = new THREE.Matrix4();
  127. for ( var i in this.children ) {
  128. for ( var j in this.children[i].children ) {
  129. var object = this.children[i].children[j];
  130. if ( object.name.search("E") != -1 ) {
  131. object.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( eye, vec1, vec2 ) );
  132. } else {
  133. object.quaternion.setFromEuler( rotation );
  134. }
  135. }
  136. }
  137. }
  138. THREE.TransformGizmoTranslate = function () {
  139. THREE.TransformGizmo.call( this );
  140. var arrowGeometry = new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false );
  141. var mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 12, 1, false ) );
  142. mesh.position.y = 0.5;
  143. THREE.GeometryUtils.merge( arrowGeometry, mesh );
  144. this.handleGizmos = {
  145. X: [
  146. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0xff0000 } ) ),
  147. new THREE.Vector3( 0.5, 0, 0 ),
  148. new THREE.Vector3( 0, 0, -Math.PI/2 )
  149. ],
  150. Y: [
  151. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0x00ff00 } ) ),
  152. new THREE.Vector3( 0, 0.5, 0 )
  153. ],
  154. Z: [
  155. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0x0000ff } ) ),
  156. new THREE.Vector3( 0, 0, 0.5 ),
  157. new THREE.Vector3( Math.PI/2, 0, 0 )
  158. ],
  159. XYZ: [
  160. new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), new THREE.TransformGizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) )
  161. ],
  162. XY: [
  163. new THREE.Mesh( new THREE.PlaneGeometry( 0.29, 0.29 ), new THREE.TransformGizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ),
  164. new THREE.Vector3( 0.15, 0.15, 0 )
  165. ],
  166. YZ: [
  167. new THREE.Mesh( new THREE.PlaneGeometry( 0.29, 0.29 ), new THREE.TransformGizmoMaterial( { color: 0x00ffff, opacity: 0.25 } ) ),
  168. new THREE.Vector3( 0, 0.15, 0.15 ),
  169. new THREE.Vector3( 0, Math.PI/2, 0 )
  170. ],
  171. XZ: [
  172. new THREE.Mesh( new THREE.PlaneGeometry( 0.29, 0.29 ), new THREE.TransformGizmoMaterial( { color: 0xff00ff, opacity: 0.25 } ) ),
  173. new THREE.Vector3( 0.15, 0, 0.15 ),
  174. new THREE.Vector3( -Math.PI/2, 0, 0 )
  175. ]
  176. }
  177. this.pickerGizmos = {
  178. X: [
  179. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ),
  180. new THREE.Vector3( 0.6, 0, 0 ),
  181. new THREE.Vector3( 0, 0, -Math.PI/2 )
  182. ],
  183. Y: [
  184. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ),
  185. new THREE.Vector3( 0, 0.6, 0 )
  186. ],
  187. Z: [
  188. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ),
  189. new THREE.Vector3( 0, 0, 0.6 ),
  190. new THREE.Vector3( Math.PI/2, 0, 0 )
  191. ],
  192. XYZ: [
  193. new THREE.Mesh( new THREE.OctahedronGeometry( 0.2, 0 ), new THREE.TransformGizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) )
  194. ],
  195. XY: [
  196. new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), new THREE.TransformGizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ),
  197. new THREE.Vector3( 0.2, 0.2, 0 )
  198. ],
  199. YZ: [
  200. new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), new THREE.TransformGizmoMaterial( { color: 0x00ffff, opacity: 0.25 } ) ),
  201. new THREE.Vector3( 0, 0.2, 0.2 ),
  202. new THREE.Vector3( 0, Math.PI/2, 0 )
  203. ],
  204. XZ: [
  205. new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), new THREE.TransformGizmoMaterial( { color: 0xff00ff, opacity: 0.25 } ) ),
  206. new THREE.Vector3( 0.2, 0, 0.2 ),
  207. new THREE.Vector3( -Math.PI/2, 0, 0 )
  208. ]
  209. }
  210. this.setActivePlane = function ( axis, eye ) {
  211. var tempMatrix = new THREE.Matrix4();
  212. eye.applyProjection( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  213. if ( axis == "X" ) {
  214. this.activePlane = this.planes[ "XY" ];
  215. if ( Math.abs(eye.y) > Math.abs(eye.z) ) this.activePlane = this.planes[ "XZ" ];
  216. }
  217. if ( axis == "Y" ){
  218. this.activePlane = this.planes[ "XY" ];
  219. if ( Math.abs(eye.x) > Math.abs(eye.z) ) this.activePlane = this.planes[ "YZ" ];
  220. }
  221. if ( axis == "Z" ){
  222. this.activePlane = this.planes[ "XZ" ];
  223. if ( Math.abs(eye.x) > Math.abs(eye.y) ) this.activePlane = this.planes[ "YZ" ];
  224. }
  225. if ( axis == "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  226. if ( axis == "XY" ) this.activePlane = this.planes[ "XY" ];
  227. if ( axis == "YZ" ) this.activePlane = this.planes[ "YZ" ];
  228. if ( axis == "XZ" ) this.activePlane = this.planes[ "XZ" ];
  229. this.hide();
  230. this.show();
  231. }
  232. this.init();
  233. }
  234. THREE.TransformGizmoTranslate.prototype = Object.create( THREE.TransformGizmo.prototype );
  235. THREE.TransformGizmoRotate = function () {
  236. THREE.TransformGizmo.call( this );
  237. this.handleGizmos = {
  238. X: [
  239. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.005, 4, 32, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0xff0000 } ) ),
  240. new THREE.Vector3( 0, 0, 0 ),
  241. new THREE.Vector3( 0, -Math.PI/2, -Math.PI/2 )
  242. ],
  243. Y: [
  244. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.005, 4, 32, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0x00ff00 } ) ),
  245. new THREE.Vector3( 0, 0, 0 ),
  246. new THREE.Vector3( Math.PI/2, 0, 0 )
  247. ],
  248. Z: [
  249. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.005, 4, 32, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0x0000ff } ) ),
  250. new THREE.Vector3( 0, 0, 0 ),
  251. new THREE.Vector3( 0, 0, -Math.PI/2 )
  252. ],
  253. E: [
  254. new THREE.Mesh( new THREE.TorusGeometry( 1.25, 0.005, 4, 64 ), new THREE.TransformGizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) )
  255. ],
  256. XYZE: [
  257. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.005, 4, 64 ), new THREE.TransformGizmoMaterial( { color: 0x787878 } ) )
  258. ]
  259. }
  260. this.pickerGizmos = {
  261. X: [
  262. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ),
  263. new THREE.Vector3( 0, 0, 0 ),
  264. new THREE.Vector3( 0, -Math.PI/2, -Math.PI/2 )
  265. ],
  266. Y: [
  267. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ),
  268. new THREE.Vector3( 0, 0, 0 ),
  269. new THREE.Vector3( Math.PI/2, 0, 0 )
  270. ],
  271. Z: [
  272. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ),
  273. new THREE.Vector3( 0, 0, 0 ),
  274. new THREE.Vector3( 0, 0, -Math.PI/2 )
  275. ],
  276. E: [
  277. new THREE.Mesh( new THREE.TorusGeometry( 1.25, 0.12, 2, 24 ), new THREE.TransformGizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) )
  278. ],
  279. XYZE: [
  280. new THREE.Mesh( new THREE.Geometry() ) // TODO
  281. ]
  282. }
  283. this.setActivePlane = function ( axis ) {
  284. if ( axis == "E" ) this.activePlane = this.planes[ "XYZE" ];
  285. if ( axis == "X" ) this.activePlane = this.planes[ "YZ" ];
  286. if ( axis == "Y" ) this.activePlane = this.planes[ "XZ" ];
  287. if ( axis == "Z" ) this.activePlane = this.planes[ "XY" ];
  288. this.hide();
  289. this.show();
  290. }
  291. this.update = function ( rotation, eye2 ) {
  292. THREE.TransformGizmo.prototype.update.apply( this, arguments );
  293. var group = {
  294. handles: this["handles"],
  295. pickers: this["pickers"],
  296. }
  297. var tempMatrix = new THREE.Matrix4();
  298. var worldRotation = new THREE.Euler( 0, 0, 1 );
  299. var tempQuaternion = new THREE.Quaternion();
  300. var unitX = new THREE.Vector3( 1, 0, 0 );
  301. var unitY = new THREE.Vector3( 0, 1, 0 );
  302. var unitZ = new THREE.Vector3( 0, 0, 1 );
  303. var quaternionX = new THREE.Quaternion();
  304. var quaternionY = new THREE.Quaternion();
  305. var quaternionZ = new THREE.Quaternion();
  306. var eye = eye2.clone();
  307. worldRotation.copy( this.planes["XY"].rotation );
  308. tempQuaternion.setFromEuler( worldRotation );
  309. tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
  310. eye.applyProjection( tempMatrix );
  311. for ( var i in group ) {
  312. for ( var j in group[i].children ) {
  313. var object = group[i].children[j];
  314. tempQuaternion.setFromEuler( worldRotation );
  315. if ( object.name == "X" ) {
  316. quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
  317. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  318. object.quaternion.copy( tempQuaternion );
  319. }
  320. if ( object.name == "Y" ) {
  321. quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
  322. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  323. object.quaternion.copy( tempQuaternion );
  324. }
  325. if ( object.name == "Z" ) {
  326. quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
  327. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  328. object.quaternion.copy( tempQuaternion );
  329. }
  330. }
  331. }
  332. }
  333. this.init();
  334. }
  335. THREE.TransformGizmoRotate.prototype = Object.create( THREE.TransformGizmo.prototype );
  336. THREE.TransformGizmoScale = function () {
  337. THREE.TransformGizmo.call( this );
  338. var arrowGeometry = new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false );
  339. var mesh = new THREE.Mesh( new THREE.CubeGeometry( 0.125, 0.125, 0.125 ) );
  340. mesh.position.y = 0.5;
  341. THREE.GeometryUtils.merge( arrowGeometry, mesh );
  342. this.handleGizmos = {
  343. X: [
  344. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0xff0000 } ) ),
  345. new THREE.Vector3( 0.5, 0, 0 ),
  346. new THREE.Vector3( 0, 0, -Math.PI/2 )
  347. ],
  348. Y: [
  349. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0x00ff00 } ) ),
  350. new THREE.Vector3( 0, 0.5, 0 )
  351. ],
  352. Z: [
  353. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0x0000ff } ) ),
  354. new THREE.Vector3( 0, 0, 0.5 ),
  355. new THREE.Vector3( Math.PI/2, 0, 0 )
  356. ],
  357. XYZ: [
  358. new THREE.Mesh( new THREE.CubeGeometry( 0.125, 0.125, 0.125 ), new THREE.TransformGizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) )
  359. ]
  360. }
  361. this.pickerGizmos = {
  362. X: [
  363. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ),
  364. new THREE.Vector3( 0.6, 0, 0 ),
  365. new THREE.Vector3( 0, 0, -Math.PI/2 )
  366. ],
  367. Y: [
  368. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ),
  369. new THREE.Vector3( 0, 0.6, 0 )
  370. ],
  371. Z: [
  372. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ),
  373. new THREE.Vector3( 0, 0, 0.6 ),
  374. new THREE.Vector3( Math.PI/2, 0, 0 )
  375. ],
  376. XYZ: [
  377. new THREE.Mesh( new THREE.CubeGeometry( 0.4, 0.4, 0.4 ), new THREE.TransformGizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) )
  378. ]
  379. }
  380. this.setActivePlane = function ( axis, eye ) {
  381. var tempMatrix = new THREE.Matrix4();
  382. eye.applyProjection( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  383. if ( axis == "X" ) {
  384. this.activePlane = this.planes[ "XY" ];
  385. if ( Math.abs(eye.y) > Math.abs(eye.z) ) this.activePlane = this.planes[ "XZ" ];
  386. }
  387. if ( axis == "Y" ){
  388. this.activePlane = this.planes[ "XY" ];
  389. if ( Math.abs(eye.x) > Math.abs(eye.z) ) this.activePlane = this.planes[ "YZ" ];
  390. }
  391. if ( axis == "Z" ){
  392. this.activePlane = this.planes[ "XZ" ];
  393. if ( Math.abs(eye.x) > Math.abs(eye.y) ) this.activePlane = this.planes[ "YZ" ];
  394. }
  395. if ( axis == "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  396. this.hide();
  397. this.show();
  398. }
  399. this.init();
  400. }
  401. THREE.TransformGizmoScale.prototype = Object.create( THREE.TransformGizmo.prototype );
  402. THREE.TransformControls = function ( camera, domElement ) {
  403. // TODO: Make non-uniform scale and rotate play nice in hierarchies
  404. // TODO: ADD RXYZ contol
  405. THREE.Object3D.call( this );
  406. domElement = ( domElement !== undefined ) ? domElement : document;
  407. this.gizmo = {}
  408. this.gizmo["translate"] = new THREE.TransformGizmoTranslate();
  409. this.gizmo["rotate"] = new THREE.TransformGizmoRotate();
  410. this.gizmo["scale"] = new THREE.TransformGizmoScale();
  411. this.add(this.gizmo["translate"]);
  412. this.add(this.gizmo["rotate"]);
  413. this.add(this.gizmo["scale"]);
  414. this.gizmo["translate"].hide();
  415. this.gizmo["rotate"].hide();
  416. this.gizmo["scale"].hide();
  417. this.object = undefined;
  418. this.snap = false;
  419. this.space = "world";
  420. this.size = 1;
  421. this.axis = undefined;
  422. var scope = this;
  423. var _dragging = false;
  424. var _mode = "translate";
  425. var _plane = "XY";
  426. var changeEvent = { type: "change" };
  427. var ray = new THREE.Raycaster();
  428. var projector = new THREE.Projector();
  429. var pointerVector = new THREE.Vector3();
  430. var point = new THREE.Vector3();
  431. var offset = new THREE.Vector3();
  432. var rotation = new THREE.Vector3();
  433. var offsetRotation = new THREE.Vector3();
  434. var scale = 1;
  435. var lookAtMatrix = new THREE.Matrix4();
  436. var eye = new THREE.Vector3()
  437. var tempMatrix = new THREE.Matrix4();
  438. var tempVector = new THREE.Vector3();
  439. var tempQuaternion = new THREE.Quaternion();
  440. var unitX = new THREE.Vector3( 1, 0, 0 );
  441. var unitY = new THREE.Vector3( 0, 1, 0 );
  442. var unitZ = new THREE.Vector3( 0, 0, 1 );
  443. var quaternionXYZ = new THREE.Quaternion();
  444. var quaternionX = new THREE.Quaternion();
  445. var quaternionY = new THREE.Quaternion();
  446. var quaternionZ = new THREE.Quaternion();
  447. var quaternionE = new THREE.Quaternion();
  448. var oldPosition = new THREE.Vector3();
  449. var oldScale = new THREE.Vector3();
  450. var oldRotationMatrix = new THREE.Matrix4();
  451. var parentRotationMatrix = new THREE.Matrix4();
  452. var parentScale = new THREE.Vector3();
  453. var worldPosition = new THREE.Vector3();
  454. var worldRotation = new THREE.Euler();
  455. var worldRotationMatrix = new THREE.Matrix4();
  456. var camPosition = new THREE.Vector3();
  457. var camRotation = new THREE.Euler();
  458. domElement.addEventListener( "mousedown", onPointerDown, false );
  459. domElement.addEventListener( "touchstart", onPointerDown, false );
  460. domElement.addEventListener( "mousemove", onPointerHover, false );
  461. domElement.addEventListener( "touchmove", onPointerHover, false );
  462. domElement.addEventListener( "mousemove", onPointerMove, false );
  463. domElement.addEventListener( "touchmove", onPointerMove, false );
  464. domElement.addEventListener( "mouseup", onPointerUp, false );
  465. domElement.addEventListener( "mouseout", onPointerUp, false );
  466. domElement.addEventListener( "touchend", onPointerUp, false );
  467. domElement.addEventListener( "touchcancel", onPointerUp, false );
  468. domElement.addEventListener( "touchleave", onPointerUp, false );
  469. this.attach = function ( object ) {
  470. scope.object = object;
  471. this.gizmo["translate"].hide();
  472. this.gizmo["rotate"].hide();
  473. this.gizmo["scale"].hide();
  474. this.gizmo[_mode].show();
  475. scope.update();
  476. }
  477. this.detach = function ( object ) {
  478. scope.object = undefined;
  479. this.axis = undefined;
  480. this.gizmo["translate"].hide();
  481. this.gizmo["rotate"].hide();
  482. this.gizmo["scale"].hide();
  483. }
  484. this.setMode = function ( mode ) {
  485. _mode = mode ? mode : _mode;
  486. if ( _mode == "scale" ) scope.space = "local";
  487. this.gizmo["translate"].hide();
  488. this.gizmo["rotate"].hide();
  489. this.gizmo["scale"].hide();
  490. this.gizmo[_mode].show();
  491. this.update();
  492. scope.dispatchEvent( changeEvent );
  493. }
  494. this.setSnap = function ( snap ) {
  495. scope.snap = snap;
  496. }
  497. this.setSize = function ( size ) {
  498. scope.size = size;
  499. this.update();
  500. scope.dispatchEvent( changeEvent );
  501. }
  502. this.setSpace = function ( space ) {
  503. scope.space = space;
  504. this.update();
  505. scope.dispatchEvent( changeEvent );
  506. }
  507. this.update = function () {
  508. if ( scope.object === undefined ) return;
  509. scope.object.updateMatrixWorld();
  510. worldPosition.getPositionFromMatrix( scope.object.matrixWorld );
  511. worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( scope.object.matrixWorld ) );
  512. camera.updateMatrixWorld();
  513. camPosition.getPositionFromMatrix( camera.matrixWorld );
  514. camRotation.setFromRotationMatrix( tempMatrix.extractRotation( camera.matrixWorld ) );
  515. scale = worldPosition.distanceTo( camPosition ) / 6 * scope.size;
  516. this.position.copy( worldPosition )
  517. this.scale.set( scale, scale, scale );
  518. eye.copy( camPosition ).sub( worldPosition ).normalize();
  519. if ( scope.space == "local" )
  520. this.gizmo[_mode].update( worldRotation, eye );
  521. else if ( scope.space == "world" )
  522. this.gizmo[_mode].update( new THREE.Euler(), eye );
  523. this.gizmo[_mode].highlight( scope.axis );
  524. }
  525. function onPointerHover( event ) {
  526. if ( scope.object === undefined || _dragging == true ) return;
  527. event.preventDefault();
  528. var pointer = event.touches ? event.touches[ 0 ] : event;
  529. var intersect = intersectObjects( pointer, scope.gizmo[_mode].pickers.children );
  530. if ( intersect ) {
  531. scope.axis = intersect.object.name;
  532. scope.update();
  533. scope.dispatchEvent( changeEvent );
  534. } else if ( scope.axis !== undefined ) {
  535. scope.axis = undefined;
  536. scope.update();
  537. scope.dispatchEvent( changeEvent );
  538. }
  539. };
  540. function onPointerDown( event ) {
  541. if ( scope.object === undefined || _dragging == true ) return;
  542. event.preventDefault();
  543. event.stopPropagation();
  544. var pointer = event.touches ? event.touches[ 0 ] : event;
  545. if ( pointer.button === 0 || pointer.button === undefined ) {
  546. var intersect = intersectObjects( pointer, scope.gizmo[_mode].pickers.children );
  547. if ( intersect ) {
  548. scope.axis = intersect.object.name;
  549. scope.update();
  550. eye.copy( camPosition ).sub( worldPosition ).normalize();
  551. scope.gizmo[_mode].setActivePlane( scope.axis, eye );
  552. var planeIntersect = intersectObjects( pointer, [scope.gizmo[_mode].activePlane] );
  553. oldPosition.copy( scope.object.position );
  554. oldScale.copy( scope.object.scale );
  555. oldRotationMatrix.extractRotation( scope.object.matrix );
  556. worldRotationMatrix.extractRotation( scope.object.matrixWorld );
  557. parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
  558. parentScale.getScaleFromMatrix( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
  559. offset.copy( planeIntersect.point );
  560. }
  561. }
  562. _dragging = true;
  563. };
  564. function onPointerMove( event ) {
  565. if ( scope.object === undefined || scope.axis === undefined || _dragging == false ) return;
  566. event.preventDefault();
  567. event.stopPropagation();
  568. var pointer = event.touches? event.touches[0] : event;
  569. var planeIntersect = intersectObjects( pointer, [scope.gizmo[_mode].activePlane] );
  570. point.copy( planeIntersect.point );
  571. if ( _mode == "translate" ) {
  572. point.sub( offset );
  573. point.multiply(parentScale);
  574. if ( scope.space == "local" ) {
  575. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  576. if ( scope.axis.search("X") == -1 ) point.x = 0;
  577. if ( scope.axis.search("Y") == -1 ) point.y = 0;
  578. if ( scope.axis.search("Z") == -1 ) point.z = 0;
  579. point.applyMatrix4( oldRotationMatrix );
  580. scope.object.position.copy( oldPosition );
  581. scope.object.position.add( point );
  582. }
  583. if ( scope.space == "world" || scope.axis.search("XYZ") != -1 ) {
  584. if ( scope.axis.search("X") == -1 ) point.x = 0;
  585. if ( scope.axis.search("Y") == -1 ) point.y = 0;
  586. if ( scope.axis.search("Z") == -1 ) point.z = 0;
  587. point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
  588. scope.object.position.copy( oldPosition );
  589. scope.object.position.add( point );
  590. if ( scope.snap == true ) {
  591. if ( scope.axis.search("X") != -1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.snap ) * scope.snap;
  592. if ( scope.axis.search("Y") != -1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.snap ) * scope.snap;
  593. if ( scope.axis.search("Z") != -1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.snap ) * scope.snap;
  594. }
  595. }
  596. } else if ( _mode == "scale" ) {
  597. point.sub( offset );
  598. point.multiply(parentScale);
  599. if ( scope.space == "local" ) {
  600. if ( scope.axis == "XYZ") {
  601. scale = 1 + ( ( point.y ) / 50 );
  602. scope.object.scale.x = oldScale.x * scale;
  603. scope.object.scale.y = oldScale.y * scale;
  604. scope.object.scale.z = oldScale.z * scale;
  605. } else {
  606. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  607. if ( scope.axis == "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / 50 );
  608. if ( scope.axis == "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / 50 );
  609. if ( scope.axis == "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / 50 );
  610. }
  611. }
  612. } else if ( _mode == "rotate" ) {
  613. point.sub( worldPosition );
  614. point.multiply(parentScale);
  615. tempVector.copy(offset).sub( worldPosition );
  616. tempVector.multiply(parentScale);
  617. if ( scope.axis == "E" ) {
  618. point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  619. tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  620. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  621. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  622. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  623. quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
  624. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  625. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
  626. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  627. scope.object.quaternion.copy( tempQuaternion );
  628. } else if ( scope.axis == "XYZE" ) {
  629. quaternionE.setFromEuler( point.clone().cross(tempVector).normalize() ); // rotation axis
  630. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  631. quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo(tempVector) );
  632. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  633. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  634. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  635. scope.object.quaternion.copy( tempQuaternion );
  636. } else if ( scope.space == "local" ) {
  637. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  638. tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  639. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  640. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  641. quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
  642. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  643. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  644. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  645. if ( scope.axis == "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
  646. if ( scope.axis == "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
  647. if ( scope.axis == "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
  648. scope.object.quaternion.copy( quaternionXYZ );
  649. } else if ( scope.space == "world" ) {
  650. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  651. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  652. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  653. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  654. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  655. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  656. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  657. if ( scope.axis == "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  658. if ( scope.axis == "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  659. if ( scope.axis == "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  660. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  661. scope.object.quaternion.copy( tempQuaternion );
  662. }
  663. }
  664. scope.update();
  665. scope.dispatchEvent( changeEvent );
  666. }
  667. function onPointerUp( event ) {
  668. _dragging = false;
  669. onPointerHover( event );
  670. }
  671. function intersectObjects( pointer, objects ) {
  672. var rect = domElement.getBoundingClientRect();
  673. var x = (pointer.clientX - rect.left) / rect.width;
  674. var y = (pointer.clientY - rect.top) / rect.height;
  675. pointerVector.set( ( x ) * 2 - 1, - ( y ) * 2 + 1, 0.5 );
  676. projector.unprojectVector( pointerVector, camera );
  677. ray.set( camPosition, pointerVector.sub( camPosition ).normalize() );
  678. var intersections = ray.intersectObjects( objects, true );
  679. return intersections[0] ? intersections[0] : false;
  680. }
  681. };
  682. THREE.TransformControls.prototype = Object.create( THREE.Object3D.prototype );