TransformControls.js 26 KB

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