TransformControls.js 30 KB

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