TransformControls.js 30 KB

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