TransformControls.js 26 KB

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