TransformControls.js 31 KB

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