TransformControls.js 32 KB

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