TransformControls.js 30 KB

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