2
0

TransformControls.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /**
  2. * @author arodic / https://github.com/arodic
  3. */
  4. //"use strict";
  5. THREE.TransformControls = function ( camera, domElement, doc ) {
  6. // TODO: Make non-uniform scale and rotate play nice in hierarchies
  7. // TODO: ADD RXYZ contol
  8. this.camera = camera;
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. this.document = ( doc !== undefined ) ? doc : document;
  11. this.object = undefined;
  12. this.active = false;
  13. this.hovered = false;
  14. this.mode = 'translate';
  15. this.space = 'world';
  16. this.scale = 1;
  17. this.snapDist = null;
  18. this.modifierAxis = new THREE.Vector3( 1, 1, 1 );
  19. this.gizmo = new THREE.Object3D();
  20. var scope = this;
  21. var changeEvent = { type: 'change' };
  22. var showPickers = false; // debug
  23. var ray = new THREE.Raycaster();
  24. var projector = new THREE.Projector();
  25. var pointerVector = new THREE.Vector3();
  26. var point = new THREE.Vector3();
  27. var offset = new THREE.Vector3();
  28. var rotation = new THREE.Vector3();
  29. var offsetRotation = new THREE.Vector3();
  30. var scale = 1;
  31. var lookAtMatrix = new THREE.Matrix4();
  32. var eye = new THREE.Vector3()
  33. var tempMatrix = new THREE.Matrix4();
  34. var tempVector = new THREE.Vector3();
  35. var tempQuaternion = new THREE.Quaternion();
  36. var unitX = new THREE.Vector3( 1, 0, 0 );
  37. var unitY = new THREE.Vector3( 0, 1, 0 );
  38. var unitZ = new THREE.Vector3( 0, 0, 1 );
  39. var quaternionXYZ = new THREE.Quaternion();
  40. var quaternionX = new THREE.Quaternion();
  41. var quaternionY = new THREE.Quaternion();
  42. var quaternionZ = new THREE.Quaternion();
  43. var quaternionE = new THREE.Quaternion();
  44. var oldPosition = new THREE.Vector3();
  45. var oldScale = new THREE.Vector3();
  46. var oldRotationMatrix = new THREE.Matrix4();
  47. var parentRotationMatrix = new THREE.Matrix4();
  48. var parentScale = new THREE.Vector3();
  49. var worldPosition = new THREE.Vector3();
  50. var worldRotation = new THREE.Euler();
  51. var worldRotationMatrix = new THREE.Matrix4();
  52. var camPosition = new THREE.Vector3();
  53. var camRotation = new THREE.Euler();
  54. var displayAxes = {};
  55. var pickerAxes = {};
  56. var intersectionPlanes = {};
  57. var intersectionPlaneList = ['XY','YZ','XZ','XYZE']; // E
  58. var currentPlane = 'XY';
  59. // intersection planes
  60. {
  61. var planes = new THREE.Object3D();
  62. this.gizmo.add(planes);
  63. for ( var i in intersectionPlaneList ){
  64. intersectionPlanes[intersectionPlaneList[i]] = new THREE.Mesh( new THREE.PlaneGeometry( 500, 500 ) );
  65. intersectionPlanes[intersectionPlaneList[i]].material.side = THREE.DoubleSide;
  66. intersectionPlanes[intersectionPlaneList[i]].visible = false;
  67. intersectionPlanes[intersectionPlaneList[i]].name = intersectionPlaneList[i];
  68. planes.add(intersectionPlanes[intersectionPlaneList[i]]);
  69. }
  70. intersectionPlanes['YZ'].rotation.set( 0, Math.PI/2, 0 );
  71. intersectionPlanes['XZ'].rotation.set( -Math.PI/2, 0, 0 );
  72. bakeTransformations(intersectionPlanes['YZ']);
  73. bakeTransformations(intersectionPlanes['XZ']);
  74. }
  75. // gizmo geometry
  76. {
  77. displayAxes["translate"] = new THREE.Object3D();
  78. displayAxes["rotate"] = new THREE.Object3D();
  79. displayAxes["scale"] = new THREE.Object3D();
  80. this.gizmo.add( displayAxes["translate"] );
  81. this.gizmo.add( displayAxes["rotate"] );
  82. this.gizmo.add( displayAxes["scale"] );
  83. pickerAxes["translate"] = new THREE.Object3D();
  84. pickerAxes["rotate"] = new THREE.Object3D();
  85. pickerAxes["scale"] = new THREE.Object3D();
  86. this.gizmo.add( pickerAxes["translate"] );
  87. this.gizmo.add( pickerAxes["rotate"] );
  88. this.gizmo.add( pickerAxes["scale"] );
  89. var HandleMaterial = function ( color, opacity ) {
  90. var material = new THREE.MeshBasicMaterial();
  91. material.color = color;
  92. material.side = THREE.DoubleSide;
  93. material.depthTest = false;
  94. material.depthWrite = false;
  95. material.opacity = opacity !== undefined ? opacity : 1;
  96. material.transparent = true;
  97. return material;
  98. }
  99. var LineMaterial = function ( color, opacity ) {
  100. var material = new THREE.LineBasicMaterial();
  101. material.color = color;
  102. material.depthTest = false;
  103. material.depthWrite = false;
  104. material.opacity = opacity !== undefined ? opacity : 1;
  105. material.transparent = true;
  106. return material;
  107. }
  108. // materials by color
  109. var white = new THREE.Color( 0xffffff );
  110. var gray = new THREE.Color( 0x808080 );
  111. var red = new THREE.Color( 0xff0000 );
  112. var green = new THREE.Color( 0x00ff00 );
  113. var blue = new THREE.Color( 0x0000ff );
  114. var cyan = new THREE.Color( 0x00ffff );
  115. var magenta = new THREE.Color( 0xff00ff );
  116. var yellow = new THREE.Color( 0xffff00 );
  117. var geometry, mesh;
  118. // Line axes
  119. geometry = new THREE.Geometry();
  120. geometry.vertices.push(
  121. new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0 ),
  122. new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ),
  123. new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, 1 )
  124. );
  125. geometry.colors.push(
  126. red, red, green, green, blue, blue
  127. );
  128. material = new THREE.LineBasicMaterial( {
  129. vertexColors: THREE.VertexColors,
  130. depthTest: false,
  131. depthWrite: false,
  132. transparent: true
  133. } );
  134. mesh = new THREE.Line( geometry, material, THREE.LinePieces );
  135. displayAxes['translate'].add( mesh );
  136. displayAxes['scale'].add( mesh.clone() );
  137. // Translate handles
  138. mesh = new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), HandleMaterial( white, 0.25 ) );
  139. mesh.name = 'TXYZ';
  140. displayAxes['translate'].add( mesh );
  141. pickerAxes['translate'].add( mesh.clone() );
  142. geometry = new THREE.PlaneGeometry( 0.3, 0.3 );
  143. mesh = new THREE.Mesh( geometry, HandleMaterial( yellow, 0.25 ) );
  144. mesh.position.set( 0.15, 0.15, 0 );
  145. bakeTransformations( mesh );
  146. mesh.name = 'TXY';
  147. displayAxes['translate'].add( mesh );
  148. pickerAxes['translate'].add( mesh.clone() );
  149. mesh = new THREE.Mesh( geometry, HandleMaterial( cyan, 0.25 ) );
  150. mesh.position.set( 0, 0.15, 0.15 );
  151. mesh.rotation.y = Math.PI/2;
  152. bakeTransformations( mesh );
  153. mesh.name = 'TYZ';
  154. displayAxes['translate'].add( mesh );
  155. pickerAxes['translate'].add( mesh.clone() );
  156. mesh = new THREE.Mesh( geometry, HandleMaterial( magenta, 0.25 ) );
  157. mesh.position.set( 0.15, 0, 0.15 );
  158. mesh.rotation.x = Math.PI/2;
  159. bakeTransformations( mesh );
  160. mesh.name = 'TXZ';
  161. displayAxes['translate'].add( mesh );
  162. pickerAxes['translate'].add( mesh.clone() );
  163. geometry = new THREE.CylinderGeometry( 0, 0.05, 0.2, 4, 1, true );
  164. mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
  165. mesh.position.x = 1.1;
  166. mesh.rotation.z = -Math.PI/2;
  167. bakeTransformations( mesh );
  168. mesh.name = 'TX';
  169. displayAxes['translate'].add( mesh );
  170. mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
  171. mesh.position.y = 1.1;
  172. bakeTransformations( mesh );
  173. mesh.name = 'TY';
  174. displayAxes['translate'].add( mesh );
  175. mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
  176. mesh.position.z = 1.1;
  177. mesh.rotation.x = Math.PI/2;
  178. bakeTransformations( mesh );
  179. mesh.name = 'TZ';
  180. displayAxes['translate'].add( mesh );
  181. geometry = new THREE.CylinderGeometry( 0.2, 0.1, 0.8, 4, 1, false );
  182. mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
  183. mesh.position.x = 0.7;
  184. mesh.rotation.z = -Math.PI/2;
  185. bakeTransformations( mesh );
  186. mesh.name = 'TX';
  187. pickerAxes['translate'].add( mesh );
  188. mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
  189. mesh.position.y = 0.7;
  190. bakeTransformations( mesh );
  191. mesh.name = 'TY';
  192. pickerAxes['translate'].add( mesh );
  193. mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
  194. mesh.position.z = 0.7;
  195. mesh.rotation.x = Math.PI/2;
  196. bakeTransformations( mesh );
  197. mesh.name = 'TZ';
  198. pickerAxes['translate'].add( mesh );
  199. // scale manipulators
  200. geometry = new THREE.CubeGeometry( 0.125, 0.125, 0.125 );
  201. mesh = new THREE.Mesh( geometry, HandleMaterial( white, 0.25 ) );
  202. mesh.name = 'SXYZ';
  203. displayAxes['scale'].add( mesh );
  204. pickerAxes['scale'].add( mesh.clone() );
  205. mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
  206. mesh.position.set( 1.05, 0, 0 );
  207. bakeTransformations( mesh );
  208. mesh.name = 'SX';
  209. displayAxes['scale'].add( mesh );
  210. pickerAxes['scale'].add( mesh.clone() );
  211. mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
  212. mesh.position.set( 0, 1.05, 0 );
  213. bakeTransformations( mesh );
  214. mesh.name = 'SY';
  215. displayAxes['scale'].add( mesh );
  216. pickerAxes['scale'].add( mesh.clone() );
  217. mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
  218. mesh.position.set( 0, 0, 1.05 );
  219. bakeTransformations( mesh );
  220. mesh.name = 'SZ';
  221. displayAxes['scale'].add( mesh );
  222. pickerAxes['scale'].add( mesh.clone() );
  223. // rotate manipulators
  224. var Circle = function ( radius, facing, arc ) {
  225. geometry = new THREE.Geometry();
  226. arc = arc ? arc : 1;
  227. for ( var i = 0; i <= 64 * arc; ++i ) {
  228. if ( facing == 'x' ) geometry.vertices.push( new THREE.Vector3( 0, Math.cos( i / 32 * Math.PI ), Math.sin( i / 32 * Math.PI ) ).multiplyScalar(radius) );
  229. if ( facing == 'y' ) geometry.vertices.push( new THREE.Vector3( Math.cos( i / 32 * Math.PI ), 0, Math.sin( i / 32 * Math.PI ) ).multiplyScalar(radius) );
  230. if ( facing == 'z' ) geometry.vertices.push( new THREE.Vector3( Math.sin( i / 32 * Math.PI ), Math.cos( i / 32 * Math.PI ), 0 ).multiplyScalar(radius) );
  231. }
  232. return geometry;
  233. }
  234. mesh = new THREE.Line( Circle( 1, 'x', 0.5 ), LineMaterial( red ) );
  235. mesh.name = 'RX';
  236. displayAxes['rotate'].add( mesh );
  237. mesh = new THREE.Line( Circle( 1, 'y', 0.5 ), LineMaterial( green ) );
  238. mesh.name = 'RY';
  239. displayAxes['rotate'].add( mesh );
  240. mesh = new THREE.Line( Circle( 1, 'z', 0.5 ), LineMaterial( blue ) );
  241. mesh.name = 'RZ';
  242. displayAxes['rotate'].add( mesh );
  243. mesh = new THREE.Line( Circle( 1, 'z' ), LineMaterial( gray ) );
  244. mesh.name = 'RXYZE';
  245. displayAxes['rotate'].add( mesh );
  246. mesh = new THREE.Line( Circle( 1.25, 'z' ), LineMaterial( yellow, 0.25 ) );
  247. mesh.name = 'RE';
  248. displayAxes['rotate'].add( mesh );
  249. geometry = new THREE.TorusGeometry( 1, 0.15, 4, 6, Math.PI );
  250. mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
  251. mesh.rotation.z = -Math.PI/2;
  252. mesh.rotation.y = -Math.PI/2;
  253. bakeTransformations( mesh );
  254. mesh.name = 'RX';
  255. pickerAxes['rotate'].add( mesh );
  256. mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
  257. mesh.rotation.z = Math.PI;
  258. mesh.rotation.x = -Math.PI/2;
  259. bakeTransformations( mesh );
  260. mesh.name = 'RY';
  261. pickerAxes['rotate'].add( mesh );
  262. mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
  263. mesh.rotation.z = -Math.PI/2;
  264. bakeTransformations( mesh );
  265. mesh.name = 'RZ';
  266. pickerAxes['rotate'].add( mesh );
  267. mesh = new THREE.Mesh( new THREE.SphereGeometry( 0.95, 12, 12 ), HandleMaterial( white, 0.25 ) );
  268. mesh.name = 'RXYZ';
  269. pickerAxes['rotate'].add( mesh );
  270. intersectionPlanes['SPHERE'] = new THREE.Mesh( new THREE.SphereGeometry( 0.95, 12, 12 ) );
  271. intersectionPlanes['SPHERE'].visible = false;
  272. planes.add(intersectionPlanes['SPHERE']);
  273. mesh = new THREE.Mesh( new THREE.TorusGeometry( 1.30, 0.15, 4, 12 ), HandleMaterial( yellow, 0.25 ) );
  274. mesh.name = 'RE';
  275. pickerAxes['rotate'].add( mesh );
  276. mesh = null;
  277. }
  278. this.attach = function ( object ) {
  279. this.object = object;
  280. this.setMode( scope.mode );
  281. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  282. this.domElement.addEventListener( 'mousemove', onMouseHover, false );
  283. this.document.addEventListener( 'keydown', onKeyDown, false );
  284. }
  285. this.detach = function ( object ) {
  286. this.object = undefined;
  287. this.hovered = false;
  288. this.hide();
  289. this.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  290. this.domElement.removeEventListener( 'mousemove', onMouseHover, false );
  291. this.document.removeEventListener( 'keydown', onKeyDown, false );
  292. }
  293. this.update = function () {
  294. if ( this.object === undefined ) return;
  295. this.object.updateMatrixWorld();
  296. worldPosition.getPositionFromMatrix( this.object.matrixWorld );
  297. worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( this.object.matrixWorld ) );
  298. this.camera.updateMatrixWorld();
  299. camPosition.getPositionFromMatrix( this.camera.matrixWorld );
  300. camRotation.setFromRotationMatrix( tempMatrix.extractRotation( this.camera.matrixWorld ) );
  301. scale = worldPosition.distanceTo( camPosition ) / 6 * this.scale;
  302. this.gizmo.position.copy( worldPosition )
  303. this.gizmo.scale.set( scale, scale, scale );
  304. lookAtMatrix.lookAt( camPosition, worldPosition, tempVector.set( 0, 1, 0 ));
  305. for ( var i in this.gizmo.children ) {
  306. for ( var j in this.gizmo.children[i].children ) {
  307. var object = this.gizmo.children[i].children[j];
  308. var name = object.name;
  309. if ( name.search('E') != -1 ){
  310. lookAtMatrix.lookAt( camPosition, worldPosition, tempVector.set( 0, 1, 0 ));
  311. object.quaternion.setFromRotationMatrix( lookAtMatrix );
  312. } else {
  313. eye.copy( camPosition ).sub( worldPosition ).normalize();
  314. if ( this.space == 'local' ) {
  315. tempQuaternion.setFromEuler( worldRotation );
  316. if ( name.search('R') != -1 ){
  317. tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
  318. eye.applyProjection( tempMatrix );
  319. if ( name == 'RX' ) {
  320. quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
  321. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  322. }
  323. if ( name == 'RY' ) {
  324. quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
  325. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  326. }
  327. if ( name == 'RZ' ) {
  328. quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
  329. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  330. }
  331. }
  332. object.quaternion.copy( tempQuaternion );
  333. } else if ( this.space == 'world' ) {
  334. object.rotation.set( 0, 0, 0 );
  335. if ( name == 'RX' ) object.rotation.x = Math.atan2( -eye.y, eye.z );
  336. if ( name == 'RY' ) object.rotation.y = Math.atan2( eye.x, eye.z );
  337. if ( name == 'RZ' ) object.rotation.z = Math.atan2( eye.y, eye.x );
  338. }
  339. }
  340. }
  341. }
  342. }
  343. this.hide = function () {
  344. for ( var i in displayAxes ) {
  345. for ( var j in displayAxes[i].children ) {
  346. displayAxes[i].children[j].visible = false;
  347. }
  348. }
  349. for ( var i in pickerAxes ) {
  350. for ( var j in pickerAxes[i].children ) {
  351. pickerAxes[i].children[j].visible = false;
  352. }
  353. }
  354. }
  355. this.setMode = function ( value ) {
  356. scope.mode = value;
  357. this.hide();
  358. if ( scope.mode == 'scale' ) scope.space = 'local';
  359. for ( var i in displayAxes[this.mode].children ) {
  360. displayAxes[this.mode].children[i].visible = true;
  361. }
  362. for ( var i in pickerAxes[this.mode].children ) {
  363. pickerAxes[this.mode].children[i].visible = showPickers;
  364. }
  365. scope.update();
  366. }
  367. this.setIntersectionPlane = function () {
  368. eye.copy( camPosition ).sub( worldPosition ).normalize();
  369. if ( this.space == 'local' ) {
  370. eye.applyMatrix4( tempMatrix.getInverse( scope.object.matrixWorld ) );
  371. }
  372. if ( isActive("X") ) {
  373. if ( eye.y > eye.z ) currentPlane = 'XZ';
  374. else currentPlane = 'XY';
  375. }
  376. if ( isActive("Y") ) {
  377. if ( eye.x > eye.z ) currentPlane = 'YZ';
  378. else currentPlane = 'XY';
  379. }
  380. if ( isActive("Z") ) {
  381. if ( eye.x > eye.y ) currentPlane = 'YZ';
  382. else currentPlane = 'XZ';
  383. }
  384. if ( isActive("XY") ) {
  385. currentPlane = 'XY';
  386. }
  387. if ( isActive("YZ") ) {
  388. currentPlane = 'YZ';
  389. }
  390. if ( isActive("XZ") ) {
  391. currentPlane = 'XZ';
  392. }
  393. if ( isActive("XYZ") || isActive("E") ) {
  394. currentPlane = 'XYZE';
  395. }
  396. if ( isActive("RX") ) {
  397. currentPlane = 'YZ';
  398. }
  399. if ( isActive("RY") ) {
  400. currentPlane = 'XZ';
  401. }
  402. if ( isActive("RZ") ) {
  403. currentPlane = 'XY';
  404. }
  405. if ( isActive("RXYZ") ) {
  406. currentPlane = 'SPHERE';
  407. }
  408. // console.log(currentPlane);
  409. }
  410. var hovered = null;
  411. var hoveredColor = new THREE.Color();
  412. var hoveredOpacity = 1;
  413. function onMouseHover( event ) {
  414. event.preventDefault();
  415. if ( event.button === 0 && scope.active === false ) {
  416. var intersect = intersectObjects( event, pickerAxes[scope.mode].children );
  417. if ( intersect ) {
  418. if ( hovered !== intersect.object ) {
  419. if ( hovered !== null ) {
  420. hovered.material.color.copy( hoveredColor );
  421. hovered.material.opacity = hoveredOpacity;
  422. }
  423. hovered = intersect.object;
  424. hoveredColor.copy( hovered.material.color );
  425. hoveredOpacity = hovered.material.opacity;
  426. hovered.material.color.setRGB( 1, 1, 0 );
  427. hovered.material.opacity = 1;
  428. scope.dispatchEvent( changeEvent );
  429. }
  430. scope.hovered = true;
  431. } else if ( hovered !== null ) {
  432. hovered.material.color.copy( hoveredColor );
  433. hovered.material.opacity = hoveredOpacity;
  434. hovered = null;
  435. scope.dispatchEvent( changeEvent );
  436. scope.hovered = false;
  437. }
  438. }
  439. scope.domElement.addEventListener( 'mousemove', onMouseMove, false );
  440. scope.domElement.addEventListener( 'mouseup', onMouseUp, false );
  441. };
  442. function onMouseDown( event ) {
  443. event.preventDefault();
  444. if ( event.button === 0 ) {
  445. var intersect = intersectObjects( event, pickerAxes[scope.mode].children );
  446. if ( intersect ) {
  447. scope.active = intersect.object.name;
  448. scope.update();
  449. scope.setIntersectionPlane();
  450. var planeIntersect = intersectObjects( event, [intersectionPlanes[currentPlane]] );
  451. if ( planeIntersect ) {
  452. oldPosition.copy( scope.object.position );
  453. oldScale.copy( scope.object.scale );
  454. oldRotationMatrix.extractRotation( scope.object.matrix );
  455. worldRotationMatrix.extractRotation( scope.object.matrixWorld );
  456. parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
  457. parentScale.getScaleFromMatrix( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
  458. offset.copy( planeIntersect.point );
  459. }
  460. }
  461. }
  462. scope.domElement.addEventListener( 'mousemove', onMouseMove, false );
  463. scope.domElement.addEventListener( 'mouseup', onMouseUp, false );
  464. scope.domElement.addEventListener( 'mouseout', onMouseUp, false );
  465. };
  466. function onMouseMove( event ) {
  467. if ( scope.active ) {
  468. var planeIntersect = intersectObjects( event, [intersectionPlanes[currentPlane]] );
  469. if ( planeIntersect ) {
  470. point.copy( planeIntersect.point );
  471. if ( ( scope.mode == 'translate' ) && isActive("T") ) {
  472. point.sub( offset );
  473. point.multiply(parentScale);
  474. if ( scope.space == 'local' ) {
  475. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  476. if ( !(isActive("X")) || scope.modifierAxis.x != 1 ) point.x = 0;
  477. if ( !(isActive("Y")) || scope.modifierAxis.y != 1 ) point.y = 0;
  478. if ( !(isActive("Z")) || scope.modifierAxis.z != 1 ) point.z = 0;
  479. point.applyMatrix4( oldRotationMatrix );
  480. scope.object.position.copy( oldPosition );
  481. scope.object.position.add( point );
  482. }
  483. if ( scope.space == 'world' || isActive("XYZ") ) {
  484. if ( !(isActive("X")) || scope.modifierAxis.x != 1 ) point.x = 0;
  485. if ( !(isActive("Y")) || scope.modifierAxis.y != 1 ) point.y = 0;
  486. if ( !(isActive("Z")) || scope.modifierAxis.z != 1 ) point.z = 0;
  487. point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
  488. scope.object.position.copy( oldPosition );
  489. scope.object.position.add( point );
  490. if ( scope.snapDist ) {
  491. if ( isActive("X") ) scope.object.position.x = Math.round( scope.object.position.x / scope.snapDist ) * scope.snapDist;
  492. if ( isActive("Y") ) scope.object.position.y = Math.round( scope.object.position.y / scope.snapDist ) * scope.snapDist;
  493. if ( isActive("Z") ) scope.object.position.z = Math.round( scope.object.position.z / scope.snapDist ) * scope.snapDist;
  494. }
  495. }
  496. } else if ( ( scope.mode == 'scale') && isActive("S") ) {
  497. point.sub( offset );
  498. point.multiply(parentScale);
  499. if ( scope.space == 'local' ) {
  500. if ( isActive("XYZ")) {
  501. scale = 1 + ( ( point.y ) / 50 );
  502. scope.object.scale.x = oldScale.x * scale;
  503. scope.object.scale.y = oldScale.y * scale;
  504. scope.object.scale.z = oldScale.z * scale;
  505. } else {
  506. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  507. if ( !(isActive("X")) || scope.modifierAxis.x != 1 ) point.x = 0;
  508. if ( !(isActive("Y")) || scope.modifierAxis.y != 1 ) point.y = 0;
  509. if ( !(isActive("Z")) || scope.modifierAxis.z != 1 ) point.z = 0;
  510. if ( isActive("X") ) scope.object.scale.x = oldScale.x * ( 1 + point.x / 50 );
  511. if ( isActive("Y") ) scope.object.scale.y = oldScale.y * ( 1 + point.y / 50 );
  512. if ( isActive("Z") ) scope.object.scale.z = oldScale.z * ( 1 + point.z / 50 );
  513. }
  514. }
  515. } else if ( ( scope.mode == 'rotate' ) && isActive("R") ) {
  516. point.sub( worldPosition );
  517. point.multiply(parentScale);
  518. tempVector.copy(offset).sub( worldPosition );
  519. tempVector.multiply(parentScale);
  520. if ( scope.active == "RE" ) {
  521. point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  522. tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  523. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  524. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  525. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  526. quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
  527. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  528. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
  529. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  530. scope.object.quaternion.copy( tempQuaternion );
  531. } else if ( scope.active == "RXYZE" ) {
  532. quaternionE.setFromEuler( point.clone().cross(tempVector).normalize() ); // rotation axis
  533. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  534. quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo(tempVector) );
  535. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  536. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  537. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  538. scope.object.quaternion.copy( tempQuaternion );
  539. } else if ( scope.space == 'local' ) {
  540. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  541. tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  542. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  543. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  544. quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
  545. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  546. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  547. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  548. if ( scope.active == "RX" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
  549. if ( scope.active == "RY" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
  550. if ( scope.active == "RZ" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
  551. scope.object.quaternion.copy( quaternionXYZ );
  552. } else if ( scope.space == 'world' ) {
  553. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  554. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  555. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  556. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  557. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  558. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  559. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  560. if ( scope.active == "RX" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  561. if ( scope.active == "RY" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  562. if ( scope.active == "RZ" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  563. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  564. scope.object.quaternion.copy( tempQuaternion );
  565. }
  566. }
  567. }
  568. scope.update();
  569. scope.dispatchEvent( changeEvent );
  570. }
  571. }
  572. function onMouseUp( event ) {
  573. scope.active = false;
  574. scope.domElement.removeEventListener( 'mousemove', onMouseMove, false );
  575. scope.domElement.removeEventListener( 'mouseup', onMouseUp, false );
  576. scope.domElement.removeEventListener( 'mouseout', onMouseUp, false );
  577. }
  578. function onKeyDown( event ) {
  579. var currentMode = scope.mode;
  580. var currentSpace = scope.space;
  581. var currentScale = scope.scale;
  582. if ( event.keyCode == 87 ) { // W
  583. if ( scope.mode == 'translate' ) scope.space = ( scope.space == 'world' ) ? 'local' : 'world';
  584. scope.mode = 'translate';
  585. }
  586. if ( event.keyCode == 69 ) { // E
  587. if ( scope.mode == 'rotate' ) scope.space = ( scope.space == 'world' ) ? 'local' : 'world';
  588. scope.mode = 'rotate';
  589. }
  590. if ( event.keyCode == 82 ) { // R
  591. scope.mode = 'scale';
  592. scope.space = 'local';
  593. }
  594. if ( event.keyCode == 187 || event.keyCode == 107 ) { // +,=,num+
  595. scope.scale += 0.1
  596. }
  597. if ( event.keyCode == 189 || event.keyCode == 109) { // -,_,num-
  598. scope.scale -= 0.1
  599. scope.scale = Math.max( scope.scale, 0.1 );
  600. }
  601. if ( scope.mode !== currentMode || scope.space !== currentSpace || scope.scale !== currentScale ) {
  602. scope.setMode( scope.mode );
  603. scope.dispatchEvent( changeEvent );
  604. }
  605. }
  606. function intersectObjects( event, objects ) {
  607. var rect = scope.domElement.getBoundingClientRect();
  608. x = (event.clientX - rect.left) / rect.width;
  609. y = (event.clientY - rect.top) / rect.height;
  610. pointerVector.set( ( x ) * 2 - 1, - ( y ) * 2 + 1, 0.5 );
  611. projector.unprojectVector( pointerVector, scope.camera );
  612. ray.set( camPosition, pointerVector.sub( camPosition ).normalize() );
  613. var intersections = ray.intersectObjects( objects, true );
  614. return intersections[0] ? intersections[0] : false;
  615. }
  616. function isActive( name ) {
  617. if ( scope.active.search( name ) != -1 ) return true;
  618. else return false;
  619. }
  620. function bakeTransformations( object ) {
  621. var tempGeometry = new THREE.Geometry();
  622. THREE.GeometryUtils.merge( tempGeometry, object );
  623. object.geometry = tempGeometry;
  624. object.position.set( 0, 0, 0 );
  625. object.rotation.set( 0, 0, 0 );
  626. object.scale.set( 1, 1, 1 );
  627. }
  628. };
  629. THREE.TransformControls.prototype = Object.create( THREE.EventDispatcher.prototype );