TransformControls.js 28 KB

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