2
0

TransformControls.js 29 KB

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