TransformControls.js 24 KB

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