TransformControls.js 31 KB

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