2
0

TransformControls.js 24 KB

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