TransformControls.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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.TransformGizmo = function () {
  15. this.handleGizmos = {
  16. X: [
  17. new THREE.Mesh( new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0xff0000 } ) ),
  18. new THREE.Vector3( 0.5, 0, 0 ),
  19. new THREE.Vector3( 0, 0, -Math.PI/2 )
  20. ],
  21. Y: [
  22. new THREE.Mesh( new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x00ff00 } ) ),
  23. new THREE.Vector3( 0, 0.5, 0 )
  24. ],
  25. Z: [
  26. new THREE.Mesh( new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x0000ff } ) ),
  27. new THREE.Vector3( 0, 0, 0.5 ),
  28. new THREE.Vector3( Math.PI/2, 0, 0 )
  29. ]
  30. }
  31. var showPickers = false; //debug
  32. var showActivePlane = false; //debug
  33. this.activePlane = undefined;
  34. this.init = function () {
  35. THREE.Object3D.call( this );
  36. this.handles = new THREE.Object3D();
  37. this.pickers = new THREE.Object3D();
  38. this.planes = new THREE.Object3D();
  39. this.add(this.handles);
  40. this.add(this.pickers);
  41. this.add(this.planes);
  42. //// PLANES
  43. var planeGeometry = new THREE.PlaneGeometry( 50, 50, 2, 2 );
  44. var planeMaterial = new THREE.MeshBasicMaterial( { wireframe: true } );
  45. planeMaterial.side = THREE.DoubleSide;
  46. var planes = {
  47. "XY": new THREE.Mesh( planeGeometry, planeMaterial ),
  48. "YZ": new THREE.Mesh( planeGeometry, planeMaterial ),
  49. "XZ": new THREE.Mesh( planeGeometry, planeMaterial ),
  50. "XYZE": new THREE.Mesh( planeGeometry, planeMaterial )
  51. };
  52. planes["YZ"].rotation.set( 0, Math.PI/2, 0 );
  53. planes["XZ"].rotation.set( -Math.PI/2, 0, 0 );
  54. for (var i in planes) {
  55. planes[i].name = i;
  56. this.planes.add(planes[i]);
  57. this.planes[i] = planes[i];
  58. planes[i].visible = false;
  59. }
  60. //// HANDLES AND PICKERS
  61. for ( var i in this.handleGizmos ) {
  62. var handle = this.handleGizmos[i][0];
  63. handle.name = i;
  64. if ( this.handleGizmos[i][1] ) handle.position.set( this.handleGizmos[i][1].x, this.handleGizmos[i][1].y, this.handleGizmos[i][1].z );
  65. if ( this.handleGizmos[i][2] ) handle.rotation.set( this.handleGizmos[i][2].x, this.handleGizmos[i][2].y, this.handleGizmos[i][2].z );
  66. this.handles.add( handle );
  67. if ( this.pickerGizmos && this.pickerGizmos[i] ) {
  68. var picker = this.pickerGizmos[i][0];
  69. if ( this.pickerGizmos[i][1] ) picker.position.set( this.pickerGizmos[i][1].x, this.pickerGizmos[i][1].y, this.pickerGizmos[i][1].z );
  70. if ( this.pickerGizmos[i][2] ) picker.rotation.set( this.pickerGizmos[i][2].x, this.pickerGizmos[i][2].y, this.pickerGizmos[i][2].z );
  71. } else {
  72. var picker = handle.clone();
  73. }
  74. picker.name = i;
  75. this.pickers.add( picker );
  76. }
  77. // reset Transformations
  78. this.traverse(function (child) {
  79. if (child instanceof THREE.Mesh) {
  80. var tempGeometry = new THREE.Geometry();
  81. THREE.GeometryUtils.merge( tempGeometry, child );
  82. child.geometry = tempGeometry;
  83. child.position.set( 0, 0, 0 );
  84. child.rotation.set( 0, 0, 0 );
  85. child.scale.set( 1, 1, 1 );
  86. }
  87. });
  88. }
  89. this.hide = function () {
  90. for ( var j in this.handles.children ) this.handles.children[j].visible = false;
  91. for ( var j in this.pickers.children ) this.pickers.children[j].visible = false;
  92. for ( var j in this.planes.children ) this.planes.children[j].visible = false;
  93. }
  94. this.show = function () {
  95. for ( var i in this.handles.children ) {
  96. this.handles.children[i].visible = true;
  97. }
  98. for ( var i in this.pickers.children ) {
  99. this.pickers.children[i].visible = showPickers;
  100. }
  101. if ( this.activePlane !== undefined ) this.activePlane.visible = showActivePlane;
  102. }
  103. this.highlight = function ( axis ) {
  104. var handle;
  105. for ( var i in this.handleGizmos ) {
  106. handle = this.handleGizmos[ i ][0];
  107. if ( handle.material.oldColor !== undefined ) {
  108. handle.material.color.copy( handle.material.oldColor );
  109. handle.material.opacity = handle.material.oldOpacity;
  110. }
  111. }
  112. if ( this.handleGizmos[ axis ] !== undefined ) {
  113. handle = this.handleGizmos[ axis ][0];
  114. handle.material.oldColor = handle.material.color.clone();
  115. handle.material.oldOpacity = handle.material.opacity;
  116. handle.material.color.setRGB( 1, 1, 0 );
  117. handle.material.opacity = 1;
  118. }
  119. }
  120. this.init();
  121. }
  122. THREE.TransformGizmo.prototype = Object.create( THREE.Object3D.prototype );
  123. THREE.TransformGizmo.prototype.update = function ( rotation, eye ) {
  124. var vec1 = new THREE.Vector3( 0, 0, 0 );
  125. var vec2 = new THREE.Vector3( 0, 1, 0 );
  126. var lookAtMatrix = new THREE.Matrix4();
  127. for ( var i in this.children ) {
  128. for ( var j in this.children[i].children ) {
  129. var object = this.children[i].children[j];
  130. if ( object.name.search("E") != -1 ) {
  131. object.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( eye, vec1, vec2 ) );
  132. } else {
  133. object.quaternion.setFromEuler( rotation );
  134. }
  135. }
  136. }
  137. }
  138. THREE.TransformGizmoTranslate = function () {
  139. THREE.TransformGizmo.call( this );
  140. var arrowGeometry = new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false );
  141. var mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 12, 1, false ) );
  142. mesh.position.y = 0.5;
  143. THREE.GeometryUtils.merge( arrowGeometry, mesh );
  144. this.handleGizmos = {
  145. X: [
  146. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0xff0000 } ) ),
  147. new THREE.Vector3( 0.5, 0, 0 ),
  148. new THREE.Vector3( 0, 0, -Math.PI/2 )
  149. ],
  150. Y: [
  151. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0x00ff00 } ) ),
  152. new THREE.Vector3( 0, 0.5, 0 )
  153. ],
  154. Z: [
  155. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0x0000ff } ) ),
  156. new THREE.Vector3( 0, 0, 0.5 ),
  157. new THREE.Vector3( Math.PI/2, 0, 0 )
  158. ],
  159. XYZ: [
  160. new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), new THREE.TransformGizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) )
  161. ],
  162. XY: [
  163. new THREE.Mesh( new THREE.PlaneGeometry( 0.29, 0.29 ), new THREE.TransformGizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ),
  164. new THREE.Vector3( 0.15, 0.15, 0 )
  165. ],
  166. YZ: [
  167. new THREE.Mesh( new THREE.PlaneGeometry( 0.29, 0.29 ), new THREE.TransformGizmoMaterial( { color: 0x00ffff, opacity: 0.25 } ) ),
  168. new THREE.Vector3( 0, 0.15, 0.15 ),
  169. new THREE.Vector3( 0, Math.PI/2, 0 )
  170. ],
  171. XZ: [
  172. new THREE.Mesh( new THREE.PlaneGeometry( 0.29, 0.29 ), new THREE.TransformGizmoMaterial( { color: 0xff00ff, opacity: 0.25 } ) ),
  173. new THREE.Vector3( 0.15, 0, 0.15 ),
  174. new THREE.Vector3( -Math.PI/2, 0, 0 )
  175. ]
  176. }
  177. this.pickerGizmos = {
  178. X: [
  179. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ),
  180. new THREE.Vector3( 0.6, 0, 0 ),
  181. new THREE.Vector3( 0, 0, -Math.PI/2 )
  182. ],
  183. Y: [
  184. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ),
  185. new THREE.Vector3( 0, 0.6, 0 )
  186. ],
  187. Z: [
  188. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ),
  189. new THREE.Vector3( 0, 0, 0.6 ),
  190. new THREE.Vector3( Math.PI/2, 0, 0 )
  191. ],
  192. XYZ: [
  193. new THREE.Mesh( new THREE.OctahedronGeometry( 0.2, 0 ), new THREE.TransformGizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) )
  194. ],
  195. XY: [
  196. new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), new THREE.TransformGizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ),
  197. new THREE.Vector3( 0.2, 0.2, 0 )
  198. ],
  199. YZ: [
  200. new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), new THREE.TransformGizmoMaterial( { color: 0x00ffff, opacity: 0.25 } ) ),
  201. new THREE.Vector3( 0, 0.2, 0.2 ),
  202. new THREE.Vector3( 0, Math.PI/2, 0 )
  203. ],
  204. XZ: [
  205. new THREE.Mesh( new THREE.PlaneGeometry( 0.4, 0.4 ), new THREE.TransformGizmoMaterial( { color: 0xff00ff, opacity: 0.25 } ) ),
  206. new THREE.Vector3( 0.2, 0, 0.2 ),
  207. new THREE.Vector3( -Math.PI/2, 0, 0 )
  208. ]
  209. }
  210. this.setActivePlane = function ( axis, eye ) {
  211. var tempMatrix = new THREE.Matrix4();
  212. eye.applyProjection( 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.TransformGizmoRotate = function () {
  236. THREE.TransformGizmo.call( this );
  237. this.handleGizmos = {
  238. X: [
  239. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.005, 4, 32, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0xff0000 } ) ),
  240. new THREE.Vector3( 0, 0, 0 ),
  241. new THREE.Vector3( 0, -Math.PI/2, -Math.PI/2 )
  242. ],
  243. Y: [
  244. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.005, 4, 32, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0x00ff00 } ) ),
  245. new THREE.Vector3( 0, 0, 0 ),
  246. new THREE.Vector3( Math.PI/2, 0, 0 )
  247. ],
  248. Z: [
  249. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.005, 4, 32, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0x0000ff } ) ),
  250. new THREE.Vector3( 0, 0, 0 ),
  251. new THREE.Vector3( 0, 0, -Math.PI/2 )
  252. ],
  253. E: [
  254. new THREE.Mesh( new THREE.TorusGeometry( 1.25, 0.005, 4, 64 ), new THREE.TransformGizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) )
  255. ],
  256. XYZE: [
  257. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.005, 4, 64 ), new THREE.TransformGizmoMaterial( { color: 0x787878 } ) )
  258. ]
  259. }
  260. this.pickerGizmos = {
  261. X: [
  262. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ),
  263. new THREE.Vector3( 0, 0, 0 ),
  264. new THREE.Vector3( 0, -Math.PI/2, -Math.PI/2 )
  265. ],
  266. Y: [
  267. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ),
  268. new THREE.Vector3( 0, 0, 0 ),
  269. new THREE.Vector3( Math.PI/2, 0, 0 )
  270. ],
  271. Z: [
  272. new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), new THREE.TransformGizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ),
  273. new THREE.Vector3( 0, 0, 0 ),
  274. new THREE.Vector3( 0, 0, -Math.PI/2 )
  275. ],
  276. E: [
  277. new THREE.Mesh( new THREE.TorusGeometry( 1.25, 0.12, 2, 24 ), new THREE.TransformGizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) )
  278. ],
  279. XYZE: [
  280. new THREE.Mesh( new THREE.Geometry() ) // TODO
  281. ]
  282. }
  283. this.setActivePlane = function ( axis ) {
  284. if ( axis == "E" ) this.activePlane = this.planes[ "XYZE" ];
  285. if ( axis == "X" ) this.activePlane = this.planes[ "YZ" ];
  286. if ( axis == "Y" ) this.activePlane = this.planes[ "XZ" ];
  287. if ( axis == "Z" ) this.activePlane = this.planes[ "XY" ];
  288. this.hide();
  289. this.show();
  290. }
  291. this.update = function ( rotation, eye2 ) {
  292. THREE.TransformGizmo.prototype.update.apply( this, arguments );
  293. var group = {
  294. handles: this["handles"],
  295. pickers: this["pickers"],
  296. }
  297. var tempMatrix = new THREE.Matrix4();
  298. var worldRotation = new THREE.Euler( 0, 0, 1 );
  299. var tempQuaternion = new THREE.Quaternion();
  300. var unitX = new THREE.Vector3( 1, 0, 0 );
  301. var unitY = new THREE.Vector3( 0, 1, 0 );
  302. var unitZ = new THREE.Vector3( 0, 0, 1 );
  303. var quaternionX = new THREE.Quaternion();
  304. var quaternionY = new THREE.Quaternion();
  305. var quaternionZ = new THREE.Quaternion();
  306. var eye = eye2.clone();
  307. worldRotation.copy( this.planes["XY"].rotation );
  308. tempQuaternion.setFromEuler( worldRotation );
  309. tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
  310. eye.applyProjection( tempMatrix );
  311. for ( var i in group ) {
  312. for ( var j in group[i].children ) {
  313. var object = group[i].children[j];
  314. tempQuaternion.setFromEuler( worldRotation );
  315. if ( object.name == "X" ) {
  316. quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
  317. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  318. object.quaternion.copy( tempQuaternion );
  319. }
  320. if ( object.name == "Y" ) {
  321. quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
  322. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  323. object.quaternion.copy( tempQuaternion );
  324. }
  325. if ( object.name == "Z" ) {
  326. quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
  327. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  328. object.quaternion.copy( tempQuaternion );
  329. }
  330. }
  331. }
  332. }
  333. this.init();
  334. }
  335. THREE.TransformGizmoRotate.prototype = Object.create( THREE.TransformGizmo.prototype );
  336. THREE.TransformGizmoScale = function () {
  337. THREE.TransformGizmo.call( this );
  338. var arrowGeometry = new THREE.CylinderGeometry( 0.005, 0.005, 1, 4, 1, false );
  339. var mesh = new THREE.Mesh( new THREE.CubeGeometry( 0.125, 0.125, 0.125 ) );
  340. mesh.position.y = 0.5;
  341. THREE.GeometryUtils.merge( arrowGeometry, mesh );
  342. this.handleGizmos = {
  343. X: [
  344. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0xff0000 } ) ),
  345. new THREE.Vector3( 0.5, 0, 0 ),
  346. new THREE.Vector3( 0, 0, -Math.PI/2 )
  347. ],
  348. Y: [
  349. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0x00ff00 } ) ),
  350. new THREE.Vector3( 0, 0.5, 0 )
  351. ],
  352. Z: [
  353. new THREE.Mesh( arrowGeometry, new THREE.TransformGizmoMaterial( { color: 0x0000ff } ) ),
  354. new THREE.Vector3( 0, 0, 0.5 ),
  355. new THREE.Vector3( Math.PI/2, 0, 0 )
  356. ],
  357. XYZ: [
  358. new THREE.Mesh( new THREE.CubeGeometry( 0.125, 0.125, 0.125 ), new THREE.TransformGizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) )
  359. ]
  360. }
  361. this.pickerGizmos = {
  362. X: [
  363. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0xff0000, opacity: 0.25 } ) ),
  364. new THREE.Vector3( 0.6, 0, 0 ),
  365. new THREE.Vector3( 0, 0, -Math.PI/2 )
  366. ],
  367. Y: [
  368. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x00ff00, opacity: 0.25 } ) ),
  369. new THREE.Vector3( 0, 0.6, 0 )
  370. ],
  371. Z: [
  372. new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), new THREE.TransformGizmoMaterial( { color: 0x0000ff, opacity: 0.25 } ) ),
  373. new THREE.Vector3( 0, 0, 0.6 ),
  374. new THREE.Vector3( Math.PI/2, 0, 0 )
  375. ],
  376. XYZ: [
  377. new THREE.Mesh( new THREE.CubeGeometry( 0.4, 0.4, 0.4 ), new THREE.TransformGizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) )
  378. ]
  379. }
  380. this.setActivePlane = function ( axis, eye ) {
  381. var tempMatrix = new THREE.Matrix4();
  382. eye.applyProjection( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  383. if ( axis == "X" ) {
  384. this.activePlane = this.planes[ "XY" ];
  385. if ( Math.abs(eye.y) > Math.abs(eye.z) ) this.activePlane = this.planes[ "XZ" ];
  386. }
  387. if ( axis == "Y" ){
  388. this.activePlane = this.planes[ "XY" ];
  389. if ( Math.abs(eye.x) > Math.abs(eye.z) ) this.activePlane = this.planes[ "YZ" ];
  390. }
  391. if ( axis == "Z" ){
  392. this.activePlane = this.planes[ "XZ" ];
  393. if ( Math.abs(eye.x) > Math.abs(eye.y) ) this.activePlane = this.planes[ "YZ" ];
  394. }
  395. if ( axis == "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  396. this.hide();
  397. this.show();
  398. }
  399. this.init();
  400. }
  401. THREE.TransformGizmoScale.prototype = Object.create( THREE.TransformGizmo.prototype );
  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 = false;
  419. this.space = "world";
  420. this.size = 1;
  421. this.axis = undefined;
  422. var scope = this;
  423. var _dragging = false;
  424. var _mode = "translate";
  425. var _plane = "XY";
  426. var changeEvent = { type: "change" };
  427. var ray = new THREE.Raycaster();
  428. var projector = new THREE.Projector();
  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 = undefined;
  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.getPositionFromMatrix( scope.object.matrixWorld );
  511. worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( scope.object.matrixWorld ) );
  512. camera.updateMatrixWorld();
  513. camPosition.getPositionFromMatrix( 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.touches? event.touches[0] : event;
  529. var intersect = intersectObjects( pointer, scope.gizmo[_mode].pickers.children );
  530. if ( intersect ) {
  531. scope.axis = intersect.object.name;
  532. scope.update();
  533. scope.dispatchEvent( changeEvent );
  534. } else if ( scope.axis !== undefined ) {
  535. scope.axis = undefined;
  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.touches? event.touches[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.axis = intersect.object.name;
  549. scope.update();
  550. eye.copy( camPosition ).sub( worldPosition ).normalize();
  551. scope.gizmo[_mode].setActivePlane( scope.axis, eye );
  552. var planeIntersect = intersectObjects( pointer, [scope.gizmo[_mode].activePlane] );
  553. oldPosition.copy( scope.object.position );
  554. oldScale.copy( scope.object.scale );
  555. oldRotationMatrix.extractRotation( scope.object.matrix );
  556. worldRotationMatrix.extractRotation( scope.object.matrixWorld );
  557. parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
  558. parentScale.getScaleFromMatrix( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
  559. offset.copy( planeIntersect.point );
  560. }
  561. }
  562. _dragging = true;
  563. };
  564. function onPointerMove( event ) {
  565. if ( scope.object === undefined || scope.axis === undefined || _dragging == false ) return;
  566. event.preventDefault();
  567. event.stopPropagation();
  568. var pointer = event.touches? event.touches[0] : event;
  569. var planeIntersect = intersectObjects( pointer, [scope.gizmo[_mode].activePlane] );
  570. point.copy( planeIntersect.point );
  571. if ( _mode == "translate" ) {
  572. point.sub( offset );
  573. point.multiply(parentScale);
  574. if ( scope.space == "local" ) {
  575. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  576. if ( scope.axis.search("X") == -1 ) point.x = 0;
  577. if ( scope.axis.search("Y") == -1 ) point.y = 0;
  578. if ( scope.axis.search("Z") == -1 ) point.z = 0;
  579. point.applyMatrix4( oldRotationMatrix );
  580. scope.object.position.copy( oldPosition );
  581. scope.object.position.add( point );
  582. }
  583. if ( scope.space == "world" || scope.axis.search("XYZ") != -1 ) {
  584. if ( scope.axis.search("X") == -1 ) point.x = 0;
  585. if ( scope.axis.search("Y") == -1 ) point.y = 0;
  586. if ( scope.axis.search("Z") == -1 ) point.z = 0;
  587. point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
  588. scope.object.position.copy( oldPosition );
  589. scope.object.position.add( point );
  590. if ( scope.snap == true ) {
  591. if ( scope.axis.search("X") != -1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.snap ) * scope.snap;
  592. if ( scope.axis.search("Y") != -1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.snap ) * scope.snap;
  593. if ( scope.axis.search("Z") != -1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.snap ) * scope.snap;
  594. }
  595. }
  596. } else if ( _mode == "scale" ) {
  597. point.sub( offset );
  598. point.multiply(parentScale);
  599. if ( scope.space == "local" ) {
  600. if ( scope.axis == "XYZ") {
  601. scale = 1 + ( ( point.y ) / 50 );
  602. scope.object.scale.x = oldScale.x * scale;
  603. scope.object.scale.y = oldScale.y * scale;
  604. scope.object.scale.z = oldScale.z * scale;
  605. } else {
  606. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  607. if ( scope.axis == "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / 50 );
  608. if ( scope.axis == "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / 50 );
  609. if ( scope.axis == "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / 50 );
  610. }
  611. }
  612. } else if ( _mode == "rotate" ) {
  613. point.sub( worldPosition );
  614. point.multiply(parentScale);
  615. tempVector.copy(offset).sub( worldPosition );
  616. tempVector.multiply(parentScale);
  617. if ( scope.axis == "E" ) {
  618. point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  619. tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  620. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  621. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  622. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  623. quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
  624. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  625. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
  626. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  627. scope.object.quaternion.copy( tempQuaternion );
  628. } else if ( scope.axis == "XYZE" ) {
  629. quaternionE.setFromEuler( point.clone().cross(tempVector).normalize() ); // rotation axis
  630. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  631. quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo(tempVector) );
  632. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  633. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  634. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  635. scope.object.quaternion.copy( tempQuaternion );
  636. } else if ( scope.space == "local" ) {
  637. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  638. tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  639. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  640. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  641. quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
  642. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  643. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  644. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  645. if ( scope.axis == "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
  646. if ( scope.axis == "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
  647. if ( scope.axis == "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
  648. scope.object.quaternion.copy( quaternionXYZ );
  649. } else if ( scope.space == "world" ) {
  650. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  651. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  652. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  653. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  654. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  655. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  656. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  657. if ( scope.axis == "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  658. if ( scope.axis == "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  659. if ( scope.axis == "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  660. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  661. scope.object.quaternion.copy( tempQuaternion );
  662. }
  663. }
  664. scope.update();
  665. scope.dispatchEvent( changeEvent );
  666. }
  667. function onPointerUp( event ) {
  668. scope.axis = undefined;
  669. _dragging = false;
  670. scope.update();
  671. }
  672. function intersectObjects( pointer, objects ) {
  673. var rect = domElement.getBoundingClientRect();
  674. var x = (pointer.clientX - rect.left) / rect.width;
  675. var y = (pointer.clientY - rect.top) / rect.height;
  676. pointerVector.set( ( x ) * 2 - 1, - ( y ) * 2 + 1, 0.5 );
  677. projector.unprojectVector( pointerVector, camera );
  678. ray.set( camPosition, pointerVector.sub( camPosition ).normalize() );
  679. var intersections = ray.intersectObjects( objects, true );
  680. return intersections[0] ? intersections[0] : false;
  681. }
  682. };
  683. THREE.TransformControls.prototype = Object.create( THREE.Object3D.prototype );