TransformControls.js 30 KB

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