TransformControls.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. /**
  2. * @author arodic / https://github.com/arodic
  3. */
  4. // "use strict";
  5. THREE.TransformControls = function ( camera, domElement ) {
  6. // TODO: Choose a better fitting intersection plane when looking at grazing angles
  7. // TODO: Make better mapping for scale
  8. // TODO: ADD RXYZ contol
  9. // TODO: fix flickering
  10. // TODO: make everything work with hierarchies
  11. this.camera = camera;
  12. this.domElement = ( domElement !== undefined ) ? domElement : document;
  13. this.active = false;
  14. this.mode = 'rotate';
  15. this.space = 'local';
  16. this.snapDist = null;
  17. this.modifierAxis = new THREE.Vector3( 1, 1, 1 );
  18. var scope = this;
  19. var showPickers = false; // debug
  20. var ray = new THREE.Raycaster();
  21. var projector = new THREE.Projector();
  22. var pointerVector = new THREE.Vector3();
  23. var intersect, planeIntersect;
  24. var offset = new THREE.Vector3();
  25. var localOffset = new THREE.Vector3();
  26. var point = new THREE.Vector3();
  27. var localPoint = new THREE.Vector3();
  28. var rotation = new THREE.Vector3();
  29. var scale = 1;
  30. var offsetRotation = new THREE.Vector3();
  31. var rotationMatrix = new THREE.Matrix4();
  32. var lookAtMatrix = new THREE.Matrix4();
  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 oldMatrix = new THREE.Matrix4();
  44. var oldPosition = new THREE.Vector3();
  45. var oldRotation = new THREE.Vector3();
  46. var oldScale = new THREE.Vector3();
  47. var objPosition = new THREE.Vector3();
  48. var objRotation = new THREE.Vector3();
  49. var objScale = new THREE.Vector3();
  50. var camPosition = new THREE.Vector3();
  51. var camRotation = new THREE.Vector3();
  52. var camDistance;
  53. var displayAxes = {};
  54. var pickerAxes = {};
  55. var intersectionPlanes = {};
  56. var intersectionPlaneList = ['XY','YZ','XZ','XYZE'];
  57. var currentPlane = 'XY';
  58. var object, name;
  59. // gizmo geometry
  60. {
  61. this.gizmo = new THREE.Object3D();
  62. displayAxes["translate"] = new THREE.Object3D();
  63. displayAxes["rotate"] = new THREE.Object3D();
  64. displayAxes["scale"] = new THREE.Object3D();
  65. this.gizmo.add( displayAxes["translate"] );
  66. this.gizmo.add( displayAxes["rotate"] );
  67. this.gizmo.add( displayAxes["scale"] );
  68. pickerAxes["translate"] = new THREE.Object3D();
  69. pickerAxes["rotate"] = new THREE.Object3D();
  70. pickerAxes["scale"] = new THREE.Object3D();
  71. this.gizmo.add( pickerAxes["translate"] );
  72. this.gizmo.add( pickerAxes["rotate"] );
  73. this.gizmo.add( pickerAxes["scale"] );
  74. var HandleMaterial = function ( color ) {
  75. var material = new THREE.MeshBasicMaterial();
  76. material.side = THREE.DoubleSide;
  77. material.transparent = true;
  78. material.depthTest = false;
  79. material.depthWrite = false;
  80. material.color.setRGB( color[0], color[1], color[2] );
  81. material.opacity = color[3];
  82. return material;
  83. }
  84. var LineMaterial = function ( color ) {
  85. var material = new THREE.LineBasicMaterial();
  86. material.side = THREE.DoubleSide;
  87. material.transparent = true;
  88. material.depthTest = false;
  89. material.depthWrite = false;
  90. material.color.setRGB( color[0], color[1], color[2] );
  91. material.opacity = color[3];
  92. return material;
  93. }
  94. // var CutoffMaterial = function () {
  95. // var material = new THREE.MeshBasicMaterial();
  96. // material.side = THREE.DoubleSide;
  97. // material.transparent = true;
  98. // material.depthTest = false;
  99. // material.depthWrite = true;
  100. // material.color.setRGB( 0 ,0 ,0 );
  101. // material.opacity = 0.1;
  102. // return material;
  103. // }
  104. // materials by color
  105. var white = [1,1,1,0.2];
  106. var gray = [0.5,0.5,0.5,1];
  107. var red = [1,0,0,1];
  108. var green = [0,1,0,1];
  109. var blue = [0,0,1,1];
  110. var cyan = [0,1,1,0.2];
  111. var magenta = [1,0,1,0.2];
  112. var yellow = [1,1,0,0.2];
  113. var mesh;
  114. // line axes
  115. mesh = new THREE.Line( new THREE.Geometry(), LineMaterial( red ) );
  116. mesh.geometry.vertices = [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0 ) ];
  117. displayAxes['translate'].add( mesh );
  118. displayAxes['scale'].add( mesh.clone() );
  119. mesh = new THREE.Line( new THREE.Geometry(), LineMaterial( green ) );
  120. mesh.geometry.vertices = [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ) ];
  121. displayAxes['translate'].add( mesh );
  122. displayAxes['scale'].add( mesh.clone() );
  123. mesh = new THREE.Line( new THREE.Geometry(), LineMaterial( blue ) );
  124. mesh.geometry.vertices = [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, 1 ) ];
  125. displayAxes['translate'].add( mesh );
  126. displayAxes['scale'].add( mesh.clone() );
  127. // Translate handles
  128. mesh = new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), HandleMaterial( white ) );
  129. mesh.name = 'TXYZ';
  130. displayAxes['translate'].add( mesh );
  131. pickerAxes['translate'].add( mesh.clone() );
  132. mesh = new THREE.Mesh( new THREE.PlaneGeometry( 0.2, 0.2 ), HandleMaterial( yellow ) );
  133. mesh.position.set( 0.1, 0.1, 0 );
  134. bakeTransformations( mesh );
  135. mesh.name = 'TXY';
  136. displayAxes['translate'].add( mesh );
  137. pickerAxes['translate'].add( mesh.clone() );
  138. mesh = new THREE.Mesh( new THREE.PlaneGeometry( 0.2, 0.2 ), HandleMaterial( cyan ) );
  139. mesh.position.set( 0, 0.1, 0.1 );
  140. mesh.rotation.y = Math.PI/2;
  141. bakeTransformations( mesh );
  142. mesh.name = 'TYZ';
  143. displayAxes['translate'].add( mesh );
  144. pickerAxes['translate'].add( mesh.clone() );
  145. mesh = new THREE.Mesh( new THREE.PlaneGeometry( 0.2, 0.2 ), HandleMaterial( magenta ) );
  146. mesh.position.set( 0.1, 0, 0.1 );
  147. mesh.rotation.x = Math.PI/2;
  148. bakeTransformations( mesh );
  149. mesh.name = 'TXZ';
  150. displayAxes['translate'].add( mesh );
  151. pickerAxes['translate'].add( mesh.clone() );
  152. mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 4, 1, true ), HandleMaterial( red ) );
  153. mesh.position.x = 0.9;
  154. mesh.rotation.z = -Math.PI/2;
  155. bakeTransformations( mesh );
  156. mesh.name = 'TX';
  157. displayAxes['translate'].add( mesh );
  158. mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 4, 1, true ), HandleMaterial( green ) );
  159. mesh.position.y = 0.9;
  160. bakeTransformations( mesh );
  161. mesh.name = 'TY';
  162. displayAxes['translate'].add( mesh );
  163. mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 4, 1, true ), HandleMaterial( blue ) );
  164. mesh.position.z = 0.9;
  165. mesh.rotation.x = Math.PI/2;
  166. bakeTransformations( mesh );
  167. mesh.name = 'TZ';
  168. displayAxes['translate'].add( mesh );
  169. mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0.04, 0.04, 0.8, 4, 1, false ), HandleMaterial( red ) );
  170. mesh.position.x = 0.6;
  171. mesh.rotation.z = -Math.PI/2;
  172. bakeTransformations( mesh );
  173. mesh.name = 'TX';
  174. pickerAxes['translate'].add( mesh );
  175. mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0.04, 0.04, 0.8, 4, 1, false ), HandleMaterial( green ) );
  176. mesh.position.y = 0.6;
  177. bakeTransformations( mesh );
  178. mesh.name = 'TY';
  179. pickerAxes['translate'].add( mesh );
  180. mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0.04, 0.04, 0.8, 4, 1, false ), HandleMaterial( blue ) );
  181. mesh.position.z = 0.6;
  182. mesh.rotation.x = Math.PI/2;
  183. bakeTransformations( mesh );
  184. mesh.name = 'TZ';
  185. pickerAxes['translate'].add( mesh );
  186. // scale manipulators
  187. mesh = new THREE.Mesh( new THREE.CubeGeometry( 0.1, 0.1, 0.1 ), HandleMaterial( white ) );
  188. mesh.name = 'SXYZ';
  189. displayAxes['scale'].add( mesh );
  190. pickerAxes['scale'].add( mesh.clone() );
  191. mesh = new THREE.Mesh( new THREE.CubeGeometry( 0.1, 0.1, 0.1 ), HandleMaterial( red ) );
  192. mesh.position.set( 1, 0, 0 );
  193. bakeTransformations( mesh );
  194. mesh.name = 'SX';
  195. displayAxes['scale'].add( mesh );
  196. pickerAxes['scale'].add( mesh.clone() );
  197. mesh = new THREE.Mesh( new THREE.CubeGeometry( 0.1, 0.1, 0.1 ), HandleMaterial( green ) );
  198. mesh.position.set( 0, 1, 0 );
  199. bakeTransformations( mesh );
  200. mesh.name = 'SY';
  201. displayAxes['scale'].add( mesh );
  202. pickerAxes['scale'].add( mesh.clone() );
  203. mesh = new THREE.Mesh( new THREE.CubeGeometry( 0.1, 0.1, 0.1 ), HandleMaterial( blue ) );
  204. mesh.position.set( 0, 0, 1 );
  205. bakeTransformations( mesh );
  206. mesh.name = 'SZ';
  207. displayAxes['scale'].add( mesh );
  208. pickerAxes['scale'].add( mesh.clone() );
  209. // rotate manipulators
  210. // mesh = new THREE.Mesh( new THREE.PlaneGeometry( 2.3, 2.3, 5, 5 ), CutoffMaterial() );
  211. // mesh.name = 'CUTOFFE';
  212. // displayAxes['rotate'].add( mesh );
  213. var Circle = function( radius, facing, arc ) {
  214. var geometry = new THREE.Geometry();
  215. arc = arc ? arc : 1;
  216. for ( var i = 0; i <= 64 * arc; ++i ) {
  217. if ( facing == 'x' ) geometry.vertices.push( new THREE.Vector3( 0, Math.cos( i / 32 * Math.PI ), Math.sin( i / 32 * Math.PI ) ).multiplyScalar(radius) );
  218. if ( facing == 'y' ) geometry.vertices.push( new THREE.Vector3( Math.cos( i / 32 * Math.PI ), 0, Math.sin( i / 32 * Math.PI ) ).multiplyScalar(radius) );
  219. if ( facing == 'z' ) geometry.vertices.push( new THREE.Vector3( Math.sin( i / 32 * Math.PI ), Math.cos( i / 32 * Math.PI ), 0 ).multiplyScalar(radius) );
  220. }
  221. return geometry;
  222. }
  223. mesh = new THREE.Line( Circle( 1, 'x', 0.5 ), LineMaterial( red ) );
  224. mesh.name = 'RX';
  225. displayAxes['rotate'].add( mesh );
  226. mesh = new THREE.Line( Circle( 1, 'y', 0.5 ), LineMaterial( green ) );
  227. mesh.name = 'RY';
  228. displayAxes['rotate'].add( mesh );
  229. mesh = new THREE.Line( Circle( 1, 'z', 0.5 ), LineMaterial( blue ) );
  230. mesh.name = 'RZ';
  231. displayAxes['rotate'].add( mesh );
  232. mesh = new THREE.Line( Circle( 1, 'z' ), LineMaterial( gray ) );
  233. mesh.name = 'RXYZE';
  234. displayAxes['rotate'].add( mesh );
  235. mesh = new THREE.Line( Circle( 1.1, 'z' ), LineMaterial( [1,1,0,1] ) );
  236. mesh.name = 'RE';
  237. displayAxes['rotate'].add( mesh );
  238. mesh = new THREE.Mesh( new THREE.TorusGeometry( 1, 0.05, 4, 12 ), HandleMaterial( cyan ) );
  239. mesh.rotation.y = -Math.PI/2;
  240. bakeTransformations( mesh );
  241. mesh.name = 'RX';
  242. pickerAxes['rotate'].add( mesh );
  243. mesh = new THREE.Mesh( new THREE.TorusGeometry( 1, 0.05, 4, 12 ), HandleMaterial( magenta ) );
  244. mesh.rotation.x = -Math.PI/2;
  245. bakeTransformations( mesh );
  246. mesh.name = 'RY';
  247. pickerAxes['rotate'].add( mesh );
  248. mesh = new THREE.Mesh( new THREE.TorusGeometry( 1, 0.05, 4, 12 ), HandleMaterial( yellow ) );
  249. mesh.name = 'RZ';
  250. pickerAxes['rotate'].add( mesh );
  251. mesh = new THREE.Mesh( new THREE.SphereGeometry( 0.95, 12, 12 ), HandleMaterial( white ) );
  252. mesh.name = 'RXYZ';
  253. pickerAxes['rotate'].add( mesh );
  254. mesh = new THREE.Mesh( new THREE.TorusGeometry( 1.12, 0.07, 4, 12 ), HandleMaterial( yellow ) );
  255. mesh.name = 'RE';
  256. pickerAxes['rotate'].add( mesh );
  257. mesh = null;
  258. }
  259. // intersection planes
  260. {
  261. var planes = new THREE.Object3D();
  262. this.gizmo.add(planes);
  263. for ( var i in intersectionPlaneList ){
  264. intersectionPlanes[intersectionPlaneList[i]] = new THREE.Mesh( new THREE.PlaneGeometry( 500, 500, 50, 50 ), new THREE.MeshBasicMaterial( { wireframe: true } ) );
  265. intersectionPlanes[intersectionPlaneList[i]].material.side = THREE.DoubleSide;
  266. intersectionPlanes[intersectionPlaneList[i]].name = intersectionPlaneList[i];
  267. intersectionPlanes[intersectionPlaneList[i]].visible = false;
  268. planes.add(intersectionPlanes[intersectionPlaneList[i]]);
  269. }
  270. intersectionPlanes['YZ'].rotation.set( 0, Math.PI/2, 0 );
  271. intersectionPlanes['XZ'].rotation.set( -Math.PI/2, 0, 0 );
  272. bakeTransformations(intersectionPlanes['YZ']);
  273. bakeTransformations(intersectionPlanes['XZ']);
  274. }
  275. this.attatch = function ( object ) {
  276. this.object = object;
  277. this.updateGizmo();
  278. this.updateMode();
  279. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  280. document.addEventListener( 'keydown', onKeyDown, false );
  281. }
  282. this.detatch = function ( object ) {
  283. this.hide();
  284. this.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  285. document.removeEventListener( 'keydown', onKeyDown, false );
  286. }
  287. this.updateGizmo = function() {
  288. objPosition.getPositionFromMatrix(this.object.matrixWorld);
  289. objRotation.setEulerFromRotationMatrix(tempMatrix.extractRotation(this.object.matrixWorld));
  290. objScale.getScaleFromMatrix(this.object.matrixWorld);
  291. camPosition.getPositionFromMatrix(this.camera.matrixWorld);
  292. camRotation.setEulerFromRotationMatrix(tempMatrix.extractRotation(this.camera.matrixWorld));
  293. camDistance = objPosition.distanceTo( camPosition );
  294. this.gizmo.position.copy(objPosition)
  295. this.gizmo.scale.set( camDistance/6, camDistance/6, camDistance/6 );
  296. for ( i in this.gizmo.children ) {
  297. for ( j in this.gizmo.children[i].children ) {
  298. object = this.gizmo.children[i].children[j];
  299. name = object.name;
  300. if ( name.search('E') != -1 ){
  301. lookAtMatrix.lookAt( camPosition, objPosition, tempVector.set( 0, 1, 0 ));
  302. object.rotation.setEulerFromRotationMatrix( lookAtMatrix );
  303. } else {
  304. var eye = new THREE.Vector3().copy(camPosition).sub(objPosition).normalize();
  305. if ( this.space == 'local' ) {
  306. tempQuaternion = new THREE.Quaternion().setFromEuler(objRotation);
  307. tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
  308. eye.applyProjection( tempMatrix );
  309. if ( name == 'RX' ) {
  310. quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
  311. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  312. }
  313. if ( name == 'RY' ) {
  314. quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
  315. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  316. }
  317. if ( name == 'RZ' ) {
  318. quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
  319. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  320. }
  321. object.rotation.setEulerFromQuaternion( tempQuaternion );
  322. } else if ( this.space == 'world' ) {
  323. object.rotation.set( 0, 0, 0 );
  324. if ( name == 'RX' ) {
  325. object.rotation.setX( Math.atan2( -eye.y, eye.z ) );
  326. }
  327. if ( name == 'RY' ) {
  328. object.rotation.setY( Math.atan2( eye.x, eye.z ) );
  329. }
  330. if ( name == 'RZ' ) {
  331. object.rotation.setZ( Math.atan2( eye.y, eye.x ) );
  332. }
  333. }
  334. }
  335. }
  336. }
  337. signals.objectChanged.dispatch( this.object );
  338. }
  339. this.hide = function () {
  340. for ( i in displayAxes ) {
  341. for ( j in displayAxes[i].children ) {
  342. displayAxes[i].children[j].visible = false;
  343. }
  344. }
  345. for ( i in pickerAxes ) {
  346. for ( j in pickerAxes[i].children ) {
  347. pickerAxes[i].children[j].visible = false;
  348. }
  349. }
  350. }
  351. this.updateMode = function() {
  352. this.hide();
  353. if ( scope.mode == 'scale' ) scope.space = 'local';
  354. for ( i in displayAxes[this.mode].children ) {
  355. displayAxes[this.mode].children[i].visible = true;
  356. }
  357. for ( i in pickerAxes[this.mode].children ) {
  358. pickerAxes[this.mode].children[i].visible = showPickers;
  359. }
  360. scope.updateGizmo();
  361. }
  362. this.setIntersectionPlane = function () {
  363. if ( this.active.search("X") != -1 || this.active.search("Y") != -1 ) {
  364. currentPlane = 'XY';
  365. }
  366. if ( this.active.search("Z") != -1 ) {
  367. currentPlane = 'YZ';
  368. }
  369. if ( this.active.search("XZ") != -1 ) {
  370. currentPlane = 'XZ';
  371. }
  372. if ( this.active.search("XYZ") != -1 ) {
  373. currentPlane = 'XYZE';
  374. }
  375. if ( this.active.search("RX") != -1 ) {
  376. currentPlane = 'YZ';
  377. }
  378. if ( this.active.search("RY") != -1 ) {
  379. currentPlane = 'XZ';
  380. }
  381. if ( this.active.search("RZ") != -1 ) {
  382. currentPlane = 'XY';
  383. }
  384. // intersectionPlanes[currentPlane].visible = true;
  385. scope.updateGizmo();
  386. }
  387. function onMouseDown( event ) {
  388. event.preventDefault();
  389. scope.domElement.focus();
  390. scope.updateGizmo();
  391. if ( event.button === 0 ) {
  392. scope.updateGizmo();
  393. intersect = intersectObjects( pickerAxes[scope.mode].children );
  394. if ( intersect[ 0 ] ) scope.active = intersect[ 0 ].object.name;
  395. if ( scope.active ) {
  396. scope.setIntersectionPlane();
  397. oldMatrix.copy( scope.object.matrixWorld );
  398. rotationMatrix.extractRotation( oldMatrix );
  399. planeIntersect = intersectObjects( [intersectionPlanes[currentPlane]] );
  400. offset.copy( planeIntersect[ 0 ].point );
  401. oldPosition.copy(scope.object.position);
  402. oldRotation.copy(objRotation);
  403. oldScale.copy(objScale);
  404. if ( scope.mode == 'rotate' && scope.space == 'world' ) offset.sub( objPosition );
  405. }
  406. }
  407. document.addEventListener( 'mousemove', onMouseMove, false );
  408. document.addEventListener( 'mouseup', onMouseUp, false );
  409. document.addEventListener( 'mouseout', onMouseUp, false );
  410. };
  411. function onMouseMove( event ) {
  412. if ( scope.active ) {
  413. planeIntersect = intersectObjects( [intersectionPlanes[currentPlane]] );
  414. if ( planeIntersect[ 0 ] ) point.copy( planeIntersect[ 0 ].point );
  415. if ( point ) {
  416. localPoint = worldToLocal( point, oldMatrix );
  417. localOffset = worldToLocal( offset, oldMatrix );
  418. if ( ( scope.mode == 'translate' ) && scope.active.search("T") != -1 ) {
  419. if ( scope.space == 'local' ) {
  420. localPoint.multiply(objScale);
  421. localOffset.multiply(objScale);
  422. localPoint.sub( localOffset );
  423. if ( scope.active.search("X") == -1 || scope.modifierAxis.x != 1 ) localPoint.x = 0;
  424. if ( scope.active.search("Y") == -1 || scope.modifierAxis.y != 1 ) localPoint.y = 0;
  425. if ( scope.active.search("Z") == -1 || scope.modifierAxis.z != 1 ) localPoint.z = 0;
  426. if ( scope.active.search("XYZ") != -1 ) localPoint.set( 0, 0, 0 );
  427. localPoint.applyMatrix4( rotationMatrix );
  428. scope.object.position.copy( oldPosition );
  429. scope.object.position.add( localPoint );
  430. }
  431. if ( scope.space == 'world' || scope.active.search("XYZ") != -1 ) {
  432. point.sub( offset );
  433. if ( scope.active.search("X") == -1 || scope.modifierAxis.x != 1 ) point.x = 0;
  434. if ( scope.active.search("Y") == -1 || scope.modifierAxis.y != 1 ) point.y = 0;
  435. if ( scope.active.search("Z") == -1 || scope.modifierAxis.z != 1 ) point.z = 0;
  436. scope.object.position.copy( oldPosition );
  437. scope.object.position.add( point );
  438. if ( scope.snapDist ) {
  439. if ( scope.active.search("X") != -1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.snapDist ) * scope.snapDist;
  440. if ( scope.active.search("Y") != -1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.snapDist ) * scope.snapDist;
  441. if ( scope.active.search("Z") != -1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.snapDist ) * scope.snapDist;
  442. }
  443. }
  444. }
  445. if ( ( scope.mode == 'scale') && scope.active.search("S") != -1 ) {
  446. if ( scope.space == 'local' ) {
  447. point.sub( offset );
  448. if ( scope.active.search("X") == -1 || scope.modifierAxis.x != 1 ) point.x = 0;
  449. if ( scope.active.search("Y") == -1 || scope.modifierAxis.y != 1 ) point.y = 0;
  450. if ( scope.active.search("Z") == -1 || scope.modifierAxis.z != 1 ) point.z = 0;
  451. localPoint.sub( localOffset );
  452. if ( scope.active.search("X") == -1 || scope.modifierAxis.x != 1 ) localPoint.x = 0;
  453. if ( scope.active.search("Y") == -1 || scope.modifierAxis.y != 1 ) localPoint.y = 0;
  454. if ( scope.active.search("Z") == -1 || scope.modifierAxis.z != 1 ) localPoint.z = 0;
  455. if ( scope.active.search("XYZ") != -1) {
  456. scale = 1 + ( ( point.x + point.y ) / 10 );
  457. scope.object.scale.x = oldScale.x * scale;
  458. scope.object.scale.y = oldScale.y * scale;
  459. scope.object.scale.z = oldScale.z * scale;
  460. } else {
  461. // TODO: add more intuitive mapping
  462. scope.object.scale.x = oldScale.x + localPoint.x/30;
  463. scope.object.scale.y = oldScale.y + localPoint.y/30;
  464. scope.object.scale.z = oldScale.z + localPoint.z/30;
  465. }
  466. } else if ( scope.space == 'world' ) {
  467. // Cannot scale in world space. This would require geometry manipulation or another transformation matrix.
  468. }
  469. }
  470. if ( ( scope.mode == 'rotate' ) && scope.active.search("R") != -1 ) {
  471. if ( scope.space == 'local' ) {
  472. offsetRotation.set( Math.atan2( localOffset.z, localOffset.y ), Math.atan2( localOffset.x, localOffset.z ), Math.atan2( localOffset.y, localOffset.x ) );
  473. rotation.set( Math.atan2( localPoint.z, localPoint.y ), Math.atan2( localPoint.x, localPoint.z ), Math.atan2( localPoint.y, localPoint.x ) );
  474. quaternionXYZ.setFromRotationMatrix( rotationMatrix );
  475. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  476. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  477. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  478. if ( scope.active.search("X") != -1 && scope.modifierAxis.x === 1 ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
  479. if ( scope.active.search("Y") != -1 && scope.modifierAxis.y === 1 ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
  480. if ( scope.active.search("Z") != -1 && scope.modifierAxis.z === 1 ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
  481. scope.object.rotation.setEulerFromQuaternion( quaternionXYZ );
  482. } else if ( scope.space == 'world' ) {
  483. point.sub( objPosition );
  484. offsetRotation.set( Math.atan2( offset.z, offset.y ), Math.atan2( offset.x, offset.z ), Math.atan2( offset.y, offset.x ) );
  485. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  486. quaternionXYZ.setFromRotationMatrix( rotationMatrix );
  487. tempQuaternion = new THREE.Quaternion().setFromEuler( new THREE.Vector3( 0, 1, 0 ), 0 );
  488. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  489. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  490. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  491. if ( scope.active.search("X") != -1 && scope.modifierAxis.x === 1 ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  492. if ( scope.active.search("Y") != -1 && scope.modifierAxis.y === 1 ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  493. if ( scope.active.search("Z") != -1 && scope.modifierAxis.z === 1 ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  494. tempQuaternion.multiplyQuaternions( tempQuaternion,quaternionXYZ);
  495. scope.object.rotation.setEulerFromQuaternion( tempQuaternion);
  496. }
  497. }
  498. }
  499. }
  500. signals.objectChanged.dispatch( scope.object );
  501. scope.updateGizmo();
  502. }
  503. function onMouseUp( event ) {
  504. scope.active = false;
  505. document.removeEventListener( 'mousemove', onMouseMove, false );
  506. document.removeEventListener( 'mouseup', onMouseUp, false );
  507. }
  508. function onKeyDown( event ) {
  509. if ( event.keyCode == 87 ) { // W
  510. if ( scope.mode == 'translate' ) scope.space = ( scope.space == 'world' ) ? 'local' : 'world';
  511. scope.mode = 'translate';
  512. }
  513. if ( event.keyCode == 69 ) { // E
  514. if ( scope.mode == 'rotate' ) scope.space = ( scope.space == 'world' ) ? 'local' : 'world';
  515. scope.mode = 'rotate';
  516. }
  517. if ( event.keyCode == 82 ) { // R
  518. scope.mode = 'scale';
  519. scope.space = 'local';
  520. }
  521. scope.updateMode();
  522. }
  523. function intersectObjects( objects ) {
  524. pointerVector.set(
  525. ( event.layerX / scope.domElement.offsetWidth ) * 2 - 1,
  526. - ( event.layerY / scope.domElement.offsetHeight ) * 2 + 1,
  527. 0.5
  528. );
  529. projector.unprojectVector( pointerVector, scope.camera );
  530. ray.set( camPosition, pointerVector.sub( camPosition ).normalize() );
  531. return ray.intersectObjects( objects, true );
  532. }
  533. function worldToLocal( point, objectMatrix ) {
  534. tempMatrix.getInverse( objectMatrix );
  535. return point.clone().applyMatrix4( tempMatrix );
  536. }
  537. function bakeTransformations( object ) {
  538. var tempGeometry = new THREE.Geometry();
  539. var tempMatrix = new THREE.Matrix4().identity();
  540. THREE.GeometryUtils.merge( tempGeometry, object );
  541. object.setGeometry( tempGeometry );
  542. object.position.set( 0, 0, 0 );
  543. object.rotation.set( 0, 0, 0 );
  544. object.scale.set( 1, 1, 1 );
  545. }
  546. };
  547. THREE.TransformControls.prototype = Object.create( THREE.EventDispatcher.prototype );