TransformControls.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  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. // intersection planes
  58. {
  59. var planes = new THREE.Object3D();
  60. this.gizmo.add(planes);
  61. for ( var i in intersectionPlaneList ){
  62. intersectionPlanes[intersectionPlaneList[i]] = new THREE.Mesh( new THREE.PlaneGeometry( 500, 500 ) );
  63. intersectionPlanes[intersectionPlaneList[i]].material.side = THREE.DoubleSide;
  64. intersectionPlanes[intersectionPlaneList[i]].visible = false;
  65. planes.add(intersectionPlanes[intersectionPlaneList[i]]);
  66. }
  67. intersectionPlanes['YZ'].rotation.set( 0, Math.PI/2, 0 );
  68. intersectionPlanes['XZ'].rotation.set( -Math.PI/2, 0, 0 );
  69. bakeTransformations(intersectionPlanes['YZ']);
  70. bakeTransformations(intersectionPlanes['XZ']);
  71. }
  72. // gizmo geometry
  73. {
  74. displayAxes["translate"] = new THREE.Object3D();
  75. displayAxes["rotate"] = new THREE.Object3D();
  76. displayAxes["scale"] = new THREE.Object3D();
  77. this.gizmo.add( displayAxes["translate"] );
  78. this.gizmo.add( displayAxes["rotate"] );
  79. this.gizmo.add( displayAxes["scale"] );
  80. pickerAxes["translate"] = new THREE.Object3D();
  81. pickerAxes["rotate"] = new THREE.Object3D();
  82. pickerAxes["scale"] = new THREE.Object3D();
  83. this.gizmo.add( pickerAxes["translate"] );
  84. this.gizmo.add( pickerAxes["rotate"] );
  85. this.gizmo.add( pickerAxes["scale"] );
  86. var HandleMaterial = function ( color, opacity ) {
  87. var material = new THREE.MeshBasicMaterial();
  88. material.color = color;
  89. material.side = THREE.DoubleSide;
  90. material.depthTest = false;
  91. material.depthWrite = false;
  92. material.opacity = opacity !== undefined ? opacity : 1;
  93. material.transparent = true;
  94. return material;
  95. }
  96. var LineMaterial = function ( color, opacity ) {
  97. var material = new THREE.LineBasicMaterial();
  98. material.color = color;
  99. material.depthTest = false;
  100. material.depthWrite = false;
  101. material.opacity = opacity !== undefined ? opacity : 1;
  102. material.transparent = true;
  103. return material;
  104. }
  105. // materials by color
  106. var white = new THREE.Color( 0xffffff );
  107. var gray = new THREE.Color( 0x808080 );
  108. var red = new THREE.Color( 0xff0000 );
  109. var green = new THREE.Color( 0x00ff00 );
  110. var blue = new THREE.Color( 0x0000ff );
  111. var cyan = new THREE.Color( 0x00ffff );
  112. var magenta = new THREE.Color( 0xff00ff );
  113. var yellow = new THREE.Color( 0xffff00 );
  114. var geometry, mesh;
  115. // Line axes
  116. geometry = new THREE.Geometry();
  117. geometry.vertices.push(
  118. new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 1, 0, 0 ),
  119. new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ),
  120. new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, 1 )
  121. );
  122. geometry.colors.push(
  123. red, red, green, green, blue, blue
  124. );
  125. material = new THREE.LineBasicMaterial( {
  126. vertexColors: THREE.VertexColors,
  127. depthTest: false,
  128. depthWrite: false,
  129. transparent: true
  130. } );
  131. mesh = new THREE.Line( geometry, material, THREE.LinePieces );
  132. displayAxes['translate'].add( mesh );
  133. displayAxes['scale'].add( mesh.clone() );
  134. // Translate handles
  135. mesh = new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), HandleMaterial( white, 0.25 ) );
  136. mesh.name = 'TXYZ';
  137. displayAxes['translate'].add( mesh );
  138. pickerAxes['translate'].add( mesh.clone() );
  139. geometry = new THREE.PlaneGeometry( 0.3, 0.3 );
  140. mesh = new THREE.Mesh( geometry, HandleMaterial( yellow, 0.25 ) );
  141. mesh.position.set( 0.15, 0.15, 0 );
  142. bakeTransformations( mesh );
  143. mesh.name = 'TXY';
  144. displayAxes['translate'].add( mesh );
  145. pickerAxes['translate'].add( mesh.clone() );
  146. mesh = new THREE.Mesh( geometry, HandleMaterial( cyan, 0.25 ) );
  147. mesh.position.set( 0, 0.15, 0.15 );
  148. mesh.rotation.y = Math.PI/2;
  149. bakeTransformations( mesh );
  150. mesh.name = 'TYZ';
  151. displayAxes['translate'].add( mesh );
  152. pickerAxes['translate'].add( mesh.clone() );
  153. mesh = new THREE.Mesh( geometry, HandleMaterial( magenta, 0.25 ) );
  154. mesh.position.set( 0.15, 0, 0.15 );
  155. mesh.rotation.x = Math.PI/2;
  156. bakeTransformations( mesh );
  157. mesh.name = 'TXZ';
  158. displayAxes['translate'].add( mesh );
  159. pickerAxes['translate'].add( mesh.clone() );
  160. geometry = new THREE.CylinderGeometry( 0, 0.05, 0.2, 4, 1, true );
  161. mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
  162. mesh.position.x = 1.1;
  163. mesh.rotation.z = -Math.PI/2;
  164. bakeTransformations( mesh );
  165. mesh.name = 'TX';
  166. displayAxes['translate'].add( mesh );
  167. mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
  168. mesh.position.y = 1.1;
  169. bakeTransformations( mesh );
  170. mesh.name = 'TY';
  171. displayAxes['translate'].add( mesh );
  172. mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
  173. mesh.position.z = 1.1;
  174. mesh.rotation.x = Math.PI/2;
  175. bakeTransformations( mesh );
  176. mesh.name = 'TZ';
  177. displayAxes['translate'].add( mesh );
  178. geometry = new THREE.CylinderGeometry( 0.2, 0.1, 0.8, 4, 1, false );
  179. mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
  180. mesh.position.x = 0.7;
  181. mesh.rotation.z = -Math.PI/2;
  182. bakeTransformations( mesh );
  183. mesh.name = 'TX';
  184. pickerAxes['translate'].add( mesh );
  185. mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
  186. mesh.position.y = 0.7;
  187. bakeTransformations( mesh );
  188. mesh.name = 'TY';
  189. pickerAxes['translate'].add( mesh );
  190. mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
  191. mesh.position.z = 0.7;
  192. mesh.rotation.x = Math.PI/2;
  193. bakeTransformations( mesh );
  194. mesh.name = 'TZ';
  195. pickerAxes['translate'].add( mesh );
  196. // scale manipulators
  197. geometry = new THREE.CubeGeometry( 0.125, 0.125, 0.125 );
  198. mesh = new THREE.Mesh( geometry, HandleMaterial( white, 0.25 ) );
  199. mesh.name = 'SXYZ';
  200. displayAxes['scale'].add( mesh );
  201. pickerAxes['scale'].add( mesh.clone() );
  202. mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
  203. mesh.position.set( 1.05, 0, 0 );
  204. bakeTransformations( mesh );
  205. mesh.name = 'SX';
  206. displayAxes['scale'].add( mesh );
  207. pickerAxes['scale'].add( mesh.clone() );
  208. mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
  209. mesh.position.set( 0, 1.05, 0 );
  210. bakeTransformations( mesh );
  211. mesh.name = 'SY';
  212. displayAxes['scale'].add( mesh );
  213. pickerAxes['scale'].add( mesh.clone() );
  214. mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
  215. mesh.position.set( 0, 0, 1.05 );
  216. bakeTransformations( mesh );
  217. mesh.name = 'SZ';
  218. displayAxes['scale'].add( mesh );
  219. pickerAxes['scale'].add( mesh.clone() );
  220. // rotate manipulators
  221. var Circle = function ( radius, facing, arc ) {
  222. geometry = new THREE.Geometry();
  223. arc = arc ? arc : 1;
  224. for ( var i = 0; i <= 64 * arc; ++i ) {
  225. if ( facing == 'x' ) geometry.vertices.push( new THREE.Vector3( 0, Math.cos( i / 32 * Math.PI ), Math.sin( i / 32 * Math.PI ) ).multiplyScalar(radius) );
  226. if ( facing == 'y' ) geometry.vertices.push( new THREE.Vector3( Math.cos( i / 32 * Math.PI ), 0, Math.sin( i / 32 * Math.PI ) ).multiplyScalar(radius) );
  227. if ( facing == 'z' ) geometry.vertices.push( new THREE.Vector3( Math.sin( i / 32 * Math.PI ), Math.cos( i / 32 * Math.PI ), 0 ).multiplyScalar(radius) );
  228. }
  229. return geometry;
  230. }
  231. mesh = new THREE.Line( Circle( 1, 'x', 0.5 ), LineMaterial( red ) );
  232. mesh.name = 'RX';
  233. displayAxes['rotate'].add( mesh );
  234. mesh = new THREE.Line( Circle( 1, 'y', 0.5 ), LineMaterial( green ) );
  235. mesh.name = 'RY';
  236. displayAxes['rotate'].add( mesh );
  237. mesh = new THREE.Line( Circle( 1, 'z', 0.5 ), LineMaterial( blue ) );
  238. mesh.name = 'RZ';
  239. displayAxes['rotate'].add( mesh );
  240. mesh = new THREE.Line( Circle( 1, 'z' ), LineMaterial( gray ) );
  241. mesh.name = 'RXYZE';
  242. displayAxes['rotate'].add( mesh );
  243. mesh = new THREE.Line( Circle( 1.25, 'z' ), LineMaterial( yellow, 0.25 ) );
  244. mesh.name = 'RE';
  245. displayAxes['rotate'].add( mesh );
  246. geometry = new THREE.TorusGeometry( 1, 0.15, 4, 6, Math.PI );
  247. mesh = new THREE.Mesh( geometry, HandleMaterial( red ) );
  248. mesh.rotation.z = -Math.PI/2;
  249. mesh.rotation.y = -Math.PI/2;
  250. bakeTransformations( mesh );
  251. mesh.name = 'RX';
  252. pickerAxes['rotate'].add( mesh );
  253. mesh = new THREE.Mesh( geometry, HandleMaterial( green ) );
  254. mesh.rotation.z = Math.PI;
  255. mesh.rotation.x = -Math.PI/2;
  256. bakeTransformations( mesh );
  257. mesh.name = 'RY';
  258. pickerAxes['rotate'].add( mesh );
  259. mesh = new THREE.Mesh( geometry, HandleMaterial( blue ) );
  260. mesh.rotation.z = -Math.PI/2;
  261. bakeTransformations( mesh );
  262. mesh.name = 'RZ';
  263. pickerAxes['rotate'].add( mesh );
  264. mesh = new THREE.Mesh( new THREE.SphereGeometry( 0.95, 12, 12 ), HandleMaterial( white, 0.25 ) );
  265. mesh.name = 'RXYZE';
  266. pickerAxes['rotate'].add( mesh );
  267. intersectionPlanes['SPHERE'] = new THREE.Mesh( new THREE.SphereGeometry( 0.95, 12, 12 ) );
  268. intersectionPlanes['SPHERE'].visible = false;
  269. planes.add(intersectionPlanes['SPHERE']);
  270. mesh = new THREE.Mesh( new THREE.TorusGeometry( 1.30, 0.15, 4, 12 ), HandleMaterial( yellow, 0.25 ) );
  271. mesh.name = 'RE';
  272. pickerAxes['rotate'].add( mesh );
  273. mesh = null;
  274. }
  275. this.attatch = function ( object ) {
  276. this.object = object;
  277. this.setMode( scope.mode );
  278. this.domElement.addEventListener( 'mousedown', onMouseDown, false );
  279. this.domElement.addEventListener( 'mousemove', onMouseHover, false );
  280. this.document.addEventListener( 'keydown', onKeyDown, false );
  281. }
  282. this.detatch = function ( object ) {
  283. this.hide();
  284. this.domElement.removeEventListener( 'mousedown', onMouseDown, false );
  285. this.domElement.removeEventListener( 'mousemove', onMouseHover, false );
  286. this.document.removeEventListener( 'keydown', onKeyDown, false );
  287. }
  288. this.update = function () {
  289. this.object.updateMatrixWorld();
  290. worldPosition.getPositionFromMatrix( this.object.matrixWorld );
  291. worldRotation.setEulerFromRotationMatrix( tempMatrix.extractRotation(this.object.matrixWorld ));
  292. this.camera.updateMatrixWorld();
  293. camPosition.getPositionFromMatrix( this.camera.matrixWorld );
  294. camRotation.setEulerFromRotationMatrix( tempMatrix.extractRotation( this.camera.matrixWorld ));
  295. scale = worldPosition.distanceTo( camPosition ) / 6 * this.scale;
  296. this.gizmo.position.copy( worldPosition )
  297. this.gizmo.scale.set( scale, scale, scale );
  298. for ( var i in this.gizmo.children ) {
  299. for ( var j in this.gizmo.children[i].children ) {
  300. var object = this.gizmo.children[i].children[j];
  301. var name = object.name;
  302. if ( name.search('E') != -1 ){
  303. lookAtMatrix.lookAt( camPosition, worldPosition, tempVector.set( 0, 1, 0 ));
  304. object.rotation.setEulerFromRotationMatrix( lookAtMatrix );
  305. } else {
  306. eye.copy( camPosition ).sub( worldPosition ).normalize();
  307. if ( this.space == 'local' ) {
  308. tempQuaternion.setFromEuler( worldRotation );
  309. if ( name.search('R') != -1 ){
  310. tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
  311. eye.applyProjection( tempMatrix );
  312. if ( name == 'RX' ) {
  313. quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
  314. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  315. }
  316. if ( name == 'RY' ) {
  317. quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
  318. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  319. }
  320. if ( name == 'RZ' ) {
  321. quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
  322. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  323. }
  324. }
  325. object.rotation.setEulerFromQuaternion( tempQuaternion );
  326. } else if ( this.space == 'world' ) {
  327. object.rotation.set( 0, 0, 0 );
  328. if ( name == 'RX' ) {
  329. object.rotation.setX( Math.atan2( -eye.y, eye.z ) );
  330. }
  331. if ( name == 'RY' ) {
  332. object.rotation.setY( Math.atan2( eye.x, eye.z ) );
  333. }
  334. if ( name == 'RZ' ) {
  335. object.rotation.setZ( Math.atan2( eye.y, eye.x ) );
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. this.hide = function () {
  343. for ( var i in displayAxes ) {
  344. for ( var j in displayAxes[i].children ) {
  345. displayAxes[i].children[j].visible = false;
  346. }
  347. }
  348. for ( var i in pickerAxes ) {
  349. for ( var j in pickerAxes[i].children ) {
  350. pickerAxes[i].children[j].visible = false;
  351. }
  352. }
  353. }
  354. this.setMode = function ( value ) {
  355. scope.mode = value;
  356. this.hide();
  357. if ( scope.mode == 'scale' ) scope.space = 'local';
  358. for ( var i in displayAxes[this.mode].children ) {
  359. displayAxes[this.mode].children[i].visible = true;
  360. }
  361. for ( var i in pickerAxes[this.mode].children ) {
  362. pickerAxes[this.mode].children[i].visible = showPickers;
  363. }
  364. scope.update();
  365. }
  366. this.setIntersectionPlane = function () {
  367. eye.copy( camPosition ).sub( worldPosition ).normalize();
  368. if ( this.space == 'local' ) {
  369. eye.applyMatrix4( tempMatrix.getInverse( scope.object.matrixWorld ) );
  370. }
  371. if ( isActive("X") ) {
  372. if ( eye.y > eye.z ) currentPlane = 'XZ';
  373. else currentPlane = 'XY';
  374. }
  375. if ( isActive("Y") ) {
  376. if ( eye.x > eye.z ) currentPlane = 'YZ';
  377. else currentPlane = 'XY';
  378. }
  379. if ( isActive("Z") ) {
  380. if ( eye.x > eye.y ) currentPlane = 'YZ';
  381. else currentPlane = 'XZ';
  382. }
  383. if ( isActive("XY") ) {
  384. currentPlane = 'XY';
  385. }
  386. if ( isActive("YZ") ) {
  387. currentPlane = 'YZ';
  388. }
  389. if ( isActive("XZ") ) {
  390. currentPlane = 'XZ';
  391. }
  392. if ( isActive("XYZ") || isActive("E") ) {
  393. currentPlane = 'XYZE';
  394. }
  395. if ( isActive("RX") ) {
  396. currentPlane = 'YZ';
  397. }
  398. if ( isActive("RY") ) {
  399. currentPlane = 'XZ';
  400. }
  401. if ( isActive("RZ") ) {
  402. currentPlane = 'XY';
  403. }
  404. if ( isActive("RXYZ") ) {
  405. currentPlane = 'SPHERE';
  406. }
  407. }
  408. var hovered = null;
  409. var hoveredColor = new THREE.Color();
  410. var hoveredOpacity = 1;
  411. function onMouseHover( event ) {
  412. event.preventDefault();
  413. if ( event.button === 0 && scope.active === false ) {
  414. var intersect = intersectObjects( event, pickerAxes[scope.mode].children );
  415. if ( intersect ) {
  416. if ( hovered !== intersect.object ) {
  417. if ( hovered !== null ) {
  418. hovered.material.color.copy( hoveredColor );
  419. hovered.material.opacity = hoveredOpacity;
  420. }
  421. hovered = intersect.object;
  422. hoveredColor.copy( hovered.material.color );
  423. hoveredOpacity = hovered.material.opacity;
  424. hovered.material.color.setRGB( 1, 1, 0 );
  425. hovered.material.opacity = 1;
  426. scope.dispatchEvent( changeEvent );
  427. }
  428. } else if ( hovered !== null ) {
  429. hovered.material.color.copy( hoveredColor );
  430. hovered.material.opacity = hoveredOpacity;
  431. hovered = null;
  432. scope.dispatchEvent( changeEvent );
  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.rotation.setEulerFromQuaternion( 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.rotation.setEulerFromQuaternion( 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.rotation.setEulerFromQuaternion( 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.rotation.setEulerFromQuaternion( 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.setGeometry( 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 );