TransformControls.js 32 KB

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