TransformControls.js 26 KB

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