TransformControls.js 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. /**
  2. * @author arodic / https://github.com/arodic
  3. */
  4. /*jshint sub:true*/
  5. (function () {
  6. 'use strict';
  7. var GizmoMaterial = function ( parameters ) {
  8. THREE.MeshBasicMaterial.call( this );
  9. this.depthTest = false;
  10. this.depthWrite = false;
  11. this.side = THREE.FrontSide;
  12. this.transparent = true;
  13. this.setValues( parameters );
  14. this.oldColor = this.color.clone();
  15. this.oldOpacity = this.opacity;
  16. this.highlight = function( highlighted ) {
  17. if ( highlighted ) {
  18. this.color.setRGB( 1, 1, 0 );
  19. this.opacity = 1;
  20. } else {
  21. this.color.copy( this.oldColor );
  22. this.opacity = this.oldOpacity;
  23. }
  24. };
  25. };
  26. GizmoMaterial.prototype = Object.create( THREE.MeshBasicMaterial.prototype );
  27. GizmoMaterial.prototype.constructor = GizmoMaterial;
  28. var GizmoLineMaterial = function ( parameters ) {
  29. THREE.LineBasicMaterial.call( this );
  30. this.depthTest = false;
  31. this.depthWrite = false;
  32. this.transparent = true;
  33. this.linewidth = 1;
  34. this.setValues( parameters );
  35. this.oldColor = this.color.clone();
  36. this.oldOpacity = this.opacity;
  37. this.highlight = function( highlighted ) {
  38. if ( highlighted ) {
  39. this.color.setRGB( 1, 1, 0 );
  40. this.opacity = 1;
  41. } else {
  42. this.color.copy( this.oldColor );
  43. this.opacity = this.oldOpacity;
  44. }
  45. };
  46. };
  47. GizmoLineMaterial.prototype = Object.create( THREE.LineBasicMaterial.prototype );
  48. GizmoLineMaterial.prototype.constructor = GizmoLineMaterial;
  49. var pickerMaterial = new GizmoMaterial( { visible: false, transparent: false } );
  50. THREE.TransformGizmo = function () {
  51. var scope = this;
  52. this.init = function () {
  53. THREE.Object3D.call( this );
  54. this.handles = new THREE.Object3D();
  55. this.pickers = new THREE.Object3D();
  56. this.planes = new THREE.Object3D();
  57. this.add(this.handles);
  58. this.add(this.pickers);
  59. this.add(this.planes);
  60. //// PLANES
  61. var planeGeometry = new THREE.PlaneBufferGeometry( 50, 50, 2, 2 );
  62. var planeMaterial = new THREE.MeshBasicMaterial( { visible: false } );
  63. var planes = {
  64. "XY": new THREE.Mesh( planeGeometry, planeMaterial ),
  65. "YZ": new THREE.Mesh( planeGeometry, planeMaterial ),
  66. "XZ": new THREE.Mesh( planeGeometry, planeMaterial ),
  67. "XYZE": new THREE.Mesh( planeGeometry, planeMaterial )
  68. };
  69. this.activePlane = planes["XYZE"];
  70. planes["YZ"].rotation.set( 0, Math.PI / 2, 0 );
  71. planes["XZ"].rotation.set( -Math.PI / 2, 0, 0 );
  72. for (var i in planes) {
  73. planes[i].name = i;
  74. this.planes.add(planes[i]);
  75. this.planes[i] = planes[i];
  76. }
  77. //// HANDLES AND PICKERS
  78. var setupGizmos = function( gizmoMap, parent ) {
  79. for ( var name in gizmoMap ) {
  80. for ( i = gizmoMap[name].length; i --;) {
  81. var object = gizmoMap[name][i][0];
  82. var position = gizmoMap[name][i][1];
  83. var rotation = gizmoMap[name][i][2];
  84. object.name = name;
  85. if ( position ) object.position.set( position[0], position[1], position[2] );
  86. if ( rotation ) object.rotation.set( rotation[0], rotation[1], rotation[2] );
  87. parent.add( object );
  88. }
  89. }
  90. };
  91. setupGizmos(this.handleGizmos, this.handles);
  92. setupGizmos(this.pickerGizmos, this.pickers);
  93. // reset Transformations
  94. this.traverse(function ( child ) {
  95. if (child instanceof THREE.Mesh) {
  96. child.updateMatrix();
  97. var tempGeometry = child.geometry.clone();
  98. tempGeometry.applyMatrix( child.matrix );
  99. child.geometry = tempGeometry;
  100. child.position.set( 0, 0, 0 );
  101. child.rotation.set( 0, 0, 0 );
  102. child.scale.set( 1, 1, 1 );
  103. }
  104. });
  105. };
  106. this.hide = function () {
  107. this.traverse( function( child ) { child.visible = false; } );
  108. };
  109. this.show = function () {
  110. this.traverse( function( child ) { child.visible = true; } );
  111. };
  112. this.highlight = function ( axis ) {
  113. this.traverse( function( child ) {
  114. if ( child.material && child.material.highlight ) {
  115. if ( child.name === axis ) {
  116. child.material.highlight( true );
  117. } else {
  118. child.material.highlight( false );
  119. }
  120. }
  121. } );
  122. };
  123. };
  124. THREE.TransformGizmo.prototype = Object.create( THREE.Object3D.prototype );
  125. THREE.TransformGizmo.prototype.constructor = THREE.TransformGizmo;
  126. THREE.TransformGizmo.prototype.update = function ( rotation, eye ) {
  127. var vec1 = new THREE.Vector3( 0, 0, 0 );
  128. var vec2 = new THREE.Vector3( 0, 1, 0 );
  129. var lookAtMatrix = new THREE.Matrix4();
  130. this.traverse( function(child) {
  131. if ( child.name.search("E") !== -1 ) {
  132. child.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( eye, vec1, vec2 ) );
  133. } else if ( child.name.search("X") !== -1 || child.name.search("Y") !== -1 || child.name.search("Z") !== -1 ) {
  134. child.quaternion.setFromEuler( rotation );
  135. }
  136. } );
  137. };
  138. THREE.TransformGizmoTranslate = function () {
  139. THREE.TransformGizmo.call( this );
  140. var arrowGeometry = new THREE.Geometry();
  141. var mesh = new THREE.Mesh( new THREE.CylinderGeometry( 0, 0.05, 0.2, 12, 1, false ) );
  142. mesh.position.y = 0.5;
  143. mesh.updateMatrix();
  144. arrowGeometry.merge( mesh.geometry, mesh.matrix );
  145. var lineXGeometry = new THREE.BufferGeometry();
  146. lineXGeometry.addAttribute( 'position', new THREE.Float32Attribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  147. var lineYGeometry = new THREE.BufferGeometry();
  148. lineYGeometry.addAttribute( 'position', new THREE.Float32Attribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  149. var lineZGeometry = new THREE.BufferGeometry();
  150. lineZGeometry.addAttribute( 'position', new THREE.Float32Attribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  151. this.handleGizmos = {
  152. X: [
  153. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0xff0000 } ) ), [ 0.5, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ],
  154. [ new THREE.Line( lineXGeometry, new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  155. ],
  156. Y: [
  157. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x00ff00 } ) ), [ 0, 0.5, 0 ] ],
  158. [ new THREE.Line( lineYGeometry, new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  159. ],
  160. Z: [
  161. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x0000ff } ) ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ],
  162. [ new THREE.Line( lineZGeometry, new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  163. ],
  164. XYZ: [
  165. [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.1, 0 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ), [ 0, 0, 0 ], [ 0, 0, 0 ] ]
  166. ],
  167. XY: [
  168. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0xffff00, opacity: 0.25 } ) ), [ 0.15, 0.15, 0 ] ]
  169. ],
  170. YZ: [
  171. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0x00ffff, opacity: 0.25 } ) ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ] ]
  172. ],
  173. XZ: [
  174. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.29, 0.29 ), new GizmoMaterial( { color: 0xff00ff, opacity: 0.25 } ) ), [ 0.15, 0, 0.15 ], [ -Math.PI / 2, 0, 0 ] ]
  175. ]
  176. };
  177. this.pickerGizmos = {
  178. X: [
  179. [ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0.6, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ]
  180. ],
  181. Y: [
  182. [ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0.6, 0 ] ]
  183. ],
  184. Z: [
  185. [ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
  186. ],
  187. XYZ: [
  188. [ new THREE.Mesh( new THREE.OctahedronGeometry( 0.2, 0 ), pickerMaterial ) ]
  189. ],
  190. XY: [
  191. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0.2, 0.2, 0 ] ]
  192. ],
  193. YZ: [
  194. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ] ]
  195. ],
  196. XZ: [
  197. [ new THREE.Mesh( new THREE.PlaneBufferGeometry( 0.4, 0.4 ), pickerMaterial ), [ 0.2, 0, 0.2 ], [ -Math.PI / 2, 0, 0 ] ]
  198. ]
  199. };
  200. this.setActivePlane = function ( axis, eye ) {
  201. var tempMatrix = new THREE.Matrix4();
  202. eye.applyMatrix4( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  203. if ( axis === "X" ) {
  204. this.activePlane = this.planes[ "XY" ];
  205. if ( Math.abs(eye.y) > Math.abs(eye.z) ) this.activePlane = this.planes[ "XZ" ];
  206. }
  207. if ( axis === "Y" ) {
  208. this.activePlane = this.planes[ "XY" ];
  209. if ( Math.abs(eye.x) > Math.abs(eye.z) ) this.activePlane = this.planes[ "YZ" ];
  210. }
  211. if ( axis === "Z" ) {
  212. this.activePlane = this.planes[ "XZ" ];
  213. if ( Math.abs(eye.x) > Math.abs(eye.y) ) this.activePlane = this.planes[ "YZ" ];
  214. }
  215. if ( axis === "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  216. if ( axis === "XY" ) this.activePlane = this.planes[ "XY" ];
  217. if ( axis === "YZ" ) this.activePlane = this.planes[ "YZ" ];
  218. if ( axis === "XZ" ) this.activePlane = this.planes[ "XZ" ];
  219. };
  220. this.init();
  221. };
  222. THREE.TransformGizmoTranslate.prototype = Object.create( THREE.TransformGizmo.prototype );
  223. THREE.TransformGizmoTranslate.prototype.constructor = THREE.TransformGizmoTranslate;
  224. THREE.TransformGizmoRotate = function () {
  225. THREE.TransformGizmo.call( this );
  226. var CircleGeometry = function ( radius, facing, arc ) {
  227. var geometry = new THREE.BufferGeometry();
  228. var vertices = [];
  229. arc = arc ? arc : 1;
  230. for ( var i = 0; i <= 64 * arc; ++ i ) {
  231. if ( facing === 'x' ) vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius );
  232. if ( facing === 'y' ) vertices.push( Math.cos( i / 32 * Math.PI ) * radius, 0, Math.sin( i / 32 * Math.PI ) * radius );
  233. if ( facing === 'z' ) vertices.push( Math.sin( i / 32 * Math.PI ) * radius, Math.cos( i / 32 * Math.PI ) * radius, 0 );
  234. }
  235. geometry.addAttribute( 'position', new THREE.Float32Attribute( vertices, 3 ) );
  236. return geometry;
  237. };
  238. this.handleGizmos = {
  239. X: [
  240. [ new THREE.Line( new CircleGeometry(1,'x',0.5), new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  241. ],
  242. Y: [
  243. [ new THREE.Line( new CircleGeometry(1,'y',0.5), new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  244. ],
  245. Z: [
  246. [ new THREE.Line( new CircleGeometry(1,'z',0.5), new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  247. ],
  248. E: [
  249. [ new THREE.Line( new CircleGeometry(1.25,'z',1), new GizmoLineMaterial( { color: 0xcccc00 } ) ) ]
  250. ],
  251. XYZE: [
  252. [ new THREE.Line( new CircleGeometry(1,'z',1), new GizmoLineMaterial( { color: 0x787878 } ) ) ]
  253. ]
  254. };
  255. this.pickerGizmos = {
  256. X: [
  257. [ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ 0, -Math.PI / 2, -Math.PI / 2 ] ]
  258. ],
  259. Y: [
  260. [ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ] ]
  261. ],
  262. Z: [
  263. [ new THREE.Mesh( new THREE.TorusGeometry( 1, 0.12, 4, 12, Math.PI ), pickerMaterial ), [ 0, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ]
  264. ],
  265. E: [
  266. [ new THREE.Mesh( new THREE.TorusGeometry( 1.25, 0.12, 2, 24 ), pickerMaterial ) ]
  267. ],
  268. XYZE: [
  269. [ new THREE.Mesh( new THREE.Geometry() ) ]// TODO
  270. ]
  271. };
  272. this.setActivePlane = function ( axis ) {
  273. if ( axis === "E" ) this.activePlane = this.planes[ "XYZE" ];
  274. if ( axis === "X" ) this.activePlane = this.planes[ "YZ" ];
  275. if ( axis === "Y" ) this.activePlane = this.planes[ "XZ" ];
  276. if ( axis === "Z" ) this.activePlane = this.planes[ "XY" ];
  277. };
  278. this.update = function ( rotation, eye2 ) {
  279. THREE.TransformGizmo.prototype.update.apply( this, arguments );
  280. var group = {
  281. handles: this["handles"],
  282. pickers: this["pickers"],
  283. };
  284. var tempMatrix = new THREE.Matrix4();
  285. var worldRotation = new THREE.Euler( 0, 0, 1 );
  286. var tempQuaternion = new THREE.Quaternion();
  287. var unitX = new THREE.Vector3( 1, 0, 0 );
  288. var unitY = new THREE.Vector3( 0, 1, 0 );
  289. var unitZ = new THREE.Vector3( 0, 0, 1 );
  290. var quaternionX = new THREE.Quaternion();
  291. var quaternionY = new THREE.Quaternion();
  292. var quaternionZ = new THREE.Quaternion();
  293. var eye = eye2.clone();
  294. worldRotation.copy( this.planes["XY"].rotation );
  295. tempQuaternion.setFromEuler( worldRotation );
  296. tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
  297. eye.applyMatrix4( tempMatrix );
  298. this.traverse(function(child) {
  299. tempQuaternion.setFromEuler( worldRotation );
  300. if ( child.name === "X" ) {
  301. quaternionX.setFromAxisAngle( unitX, Math.atan2( -eye.y, eye.z ) );
  302. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  303. child.quaternion.copy( tempQuaternion );
  304. }
  305. if ( child.name === "Y" ) {
  306. quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
  307. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  308. child.quaternion.copy( tempQuaternion );
  309. }
  310. if ( child.name === "Z" ) {
  311. quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
  312. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  313. child.quaternion.copy( tempQuaternion );
  314. }
  315. });
  316. };
  317. this.init();
  318. };
  319. THREE.TransformGizmoRotate.prototype = Object.create( THREE.TransformGizmo.prototype );
  320. THREE.TransformGizmoRotate.prototype.constructor = THREE.TransformGizmoRotate;
  321. THREE.TransformGizmoScale = function () {
  322. THREE.TransformGizmo.call( this );
  323. var arrowGeometry = new THREE.Geometry();
  324. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ) );
  325. mesh.position.y = 0.5;
  326. mesh.updateMatrix();
  327. arrowGeometry.merge( mesh.geometry, mesh.matrix );
  328. var lineXGeometry = new THREE.BufferGeometry();
  329. lineXGeometry.addAttribute( 'position', new THREE.Float32Attribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  330. var lineYGeometry = new THREE.BufferGeometry();
  331. lineYGeometry.addAttribute( 'position', new THREE.Float32Attribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  332. var lineZGeometry = new THREE.BufferGeometry();
  333. lineZGeometry.addAttribute( 'position', new THREE.Float32Attribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  334. this.handleGizmos = {
  335. X: [
  336. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0xff0000 } ) ), [ 0.5, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ],
  337. [ new THREE.Line( lineXGeometry, new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  338. ],
  339. Y: [
  340. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x00ff00 } ) ), [ 0, 0.5, 0 ] ],
  341. [ new THREE.Line( lineYGeometry, new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  342. ],
  343. Z: [
  344. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x0000ff } ) ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ],
  345. [ new THREE.Line( lineZGeometry, new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  346. ],
  347. XYZ: [
  348. [ new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ) ]
  349. ]
  350. };
  351. this.pickerGizmos = {
  352. X: [
  353. [ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0.6, 0, 0 ], [ 0, 0, -Math.PI / 2 ] ]
  354. ],
  355. Y: [
  356. [ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0.6, 0 ] ]
  357. ],
  358. Z: [
  359. [ new THREE.Mesh( new THREE.CylinderGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
  360. ],
  361. XYZ: [
  362. [ new THREE.Mesh( new THREE.BoxGeometry( 0.4, 0.4, 0.4 ), pickerMaterial ) ]
  363. ]
  364. };
  365. this.setActivePlane = function ( axis, eye ) {
  366. var tempMatrix = new THREE.Matrix4();
  367. eye.applyMatrix4( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  368. if ( axis === "X" ) {
  369. this.activePlane = this.planes[ "XY" ];
  370. if ( Math.abs(eye.y) > Math.abs(eye.z) ) this.activePlane = this.planes[ "XZ" ];
  371. }
  372. if ( axis === "Y" ) {
  373. this.activePlane = this.planes[ "XY" ];
  374. if ( Math.abs(eye.x) > Math.abs(eye.z) ) this.activePlane = this.planes[ "YZ" ];
  375. }
  376. if ( axis === "Z" ) {
  377. this.activePlane = this.planes[ "XZ" ];
  378. if ( Math.abs(eye.x) > Math.abs(eye.y) ) this.activePlane = this.planes[ "YZ" ];
  379. }
  380. if ( axis === "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  381. };
  382. this.init();
  383. };
  384. THREE.TransformGizmoScale.prototype = Object.create( THREE.TransformGizmo.prototype );
  385. THREE.TransformGizmoScale.prototype.constructor = THREE.TransformGizmoScale;
  386. THREE.TransformControls = function ( camera, domElement ) {
  387. // TODO: Make non-uniform scale and rotate play nice in hierarchies
  388. // TODO: ADD RXYZ contol
  389. THREE.Object3D.call( this );
  390. domElement = ( domElement !== undefined ) ? domElement : document;
  391. this.gizmo = {};
  392. this.gizmo["translate"] = new THREE.TransformGizmoTranslate();
  393. this.gizmo["rotate"] = new THREE.TransformGizmoRotate();
  394. this.gizmo["scale"] = new THREE.TransformGizmoScale();
  395. this.add(this.gizmo["translate"]);
  396. this.add(this.gizmo["rotate"]);
  397. this.add(this.gizmo["scale"]);
  398. this.gizmo["translate"].hide();
  399. this.gizmo["rotate"].hide();
  400. this.gizmo["scale"].hide();
  401. this.object = undefined;
  402. this.snap = null;
  403. this.space = "world";
  404. this.size = 1;
  405. this.axis = null;
  406. var scope = this;
  407. var _dragging = false;
  408. var _mode = "translate";
  409. var _plane = "XY";
  410. var changeEvent = { type: "change" };
  411. var mouseDownEvent = { type: "mouseDown" };
  412. var mouseUpEvent = { type: "mouseUp", mode: _mode };
  413. var objectChangeEvent = { type: "objectChange" };
  414. var ray = new THREE.Raycaster();
  415. var pointerVector = new THREE.Vector3();
  416. var point = new THREE.Vector3();
  417. var offset = new THREE.Vector3();
  418. var rotation = new THREE.Vector3();
  419. var offsetRotation = new THREE.Vector3();
  420. var scale = 1;
  421. var lookAtMatrix = new THREE.Matrix4();
  422. var eye = new THREE.Vector3();
  423. var tempMatrix = new THREE.Matrix4();
  424. var tempVector = new THREE.Vector3();
  425. var tempQuaternion = new THREE.Quaternion();
  426. var unitX = new THREE.Vector3( 1, 0, 0 );
  427. var unitY = new THREE.Vector3( 0, 1, 0 );
  428. var unitZ = new THREE.Vector3( 0, 0, 1 );
  429. var quaternionXYZ = new THREE.Quaternion();
  430. var quaternionX = new THREE.Quaternion();
  431. var quaternionY = new THREE.Quaternion();
  432. var quaternionZ = new THREE.Quaternion();
  433. var quaternionE = new THREE.Quaternion();
  434. var oldPosition = new THREE.Vector3();
  435. var oldScale = new THREE.Vector3();
  436. var oldRotationMatrix = new THREE.Matrix4();
  437. var parentRotationMatrix = new THREE.Matrix4();
  438. var parentScale = new THREE.Vector3();
  439. var worldPosition = new THREE.Vector3();
  440. var worldRotation = new THREE.Euler();
  441. var worldRotationMatrix = new THREE.Matrix4();
  442. var camPosition = new THREE.Vector3();
  443. var camRotation = new THREE.Euler();
  444. domElement.addEventListener( "mousedown", onPointerDown, false );
  445. domElement.addEventListener( "touchstart", onPointerDown, false );
  446. domElement.addEventListener( "mousemove", onPointerHover, false );
  447. domElement.addEventListener( "touchmove", onPointerHover, false );
  448. domElement.addEventListener( "mousemove", onPointerMove, false );
  449. domElement.addEventListener( "touchmove", onPointerMove, false );
  450. domElement.addEventListener( "mouseup", onPointerUp, false );
  451. domElement.addEventListener( "mouseout", onPointerUp, false );
  452. domElement.addEventListener( "touchend", onPointerUp, false );
  453. domElement.addEventListener( "touchcancel", onPointerUp, false );
  454. domElement.addEventListener( "touchleave", onPointerUp, false );
  455. this.dispose = function () {
  456. domElement.removeEventListener( "mousedown", onPointerDown );
  457. domElement.removeEventListener( "touchstart", onPointerDown );
  458. domElement.removeEventListener( "mousemove", onPointerHover );
  459. domElement.removeEventListener( "touchmove", onPointerHover );
  460. domElement.removeEventListener( "mousemove", onPointerMove );
  461. domElement.removeEventListener( "touchmove", onPointerMove );
  462. domElement.removeEventListener( "mouseup", onPointerUp );
  463. domElement.removeEventListener( "mouseout", onPointerUp );
  464. domElement.removeEventListener( "touchend", onPointerUp );
  465. domElement.removeEventListener( "touchcancel", onPointerUp );
  466. domElement.removeEventListener( "touchleave", onPointerUp );
  467. };
  468. this.attach = function ( object ) {
  469. scope.object = object;
  470. this.gizmo["translate"].hide();
  471. this.gizmo["rotate"].hide();
  472. this.gizmo["scale"].hide();
  473. this.gizmo[_mode].show();
  474. scope.update();
  475. };
  476. this.detach = function () {
  477. scope.object = undefined;
  478. this.axis = null;
  479. this.gizmo["translate"].hide();
  480. this.gizmo["rotate"].hide();
  481. this.gizmo["scale"].hide();
  482. };
  483. this.setMode = function ( mode ) {
  484. _mode = mode ? mode : _mode;
  485. if ( _mode === "scale" ) scope.space = "local";
  486. this.gizmo["translate"].hide();
  487. this.gizmo["rotate"].hide();
  488. this.gizmo["scale"].hide();
  489. this.gizmo[_mode].show();
  490. this.update();
  491. scope.dispatchEvent( changeEvent );
  492. };
  493. this.setSnap = function ( snap ) {
  494. scope.snap = snap;
  495. };
  496. this.setSize = function ( size ) {
  497. scope.size = size;
  498. this.update();
  499. scope.dispatchEvent( changeEvent );
  500. };
  501. this.setSpace = function ( space ) {
  502. scope.space = space;
  503. this.update();
  504. scope.dispatchEvent( changeEvent );
  505. };
  506. this.update = function () {
  507. if ( scope.object === undefined ) return;
  508. scope.object.updateMatrixWorld();
  509. worldPosition.setFromMatrixPosition( scope.object.matrixWorld );
  510. worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( scope.object.matrixWorld ) );
  511. camera.updateMatrixWorld();
  512. camPosition.setFromMatrixPosition( camera.matrixWorld );
  513. camRotation.setFromRotationMatrix( tempMatrix.extractRotation( camera.matrixWorld ) );
  514. scale = worldPosition.distanceTo( camPosition ) / 6 * scope.size;
  515. this.position.copy( worldPosition );
  516. this.scale.set( scale, scale, scale );
  517. eye.copy( camPosition ).sub( worldPosition ).normalize();
  518. if ( scope.space === "local" ) {
  519. this.gizmo[_mode].update( worldRotation, eye );
  520. } else if ( scope.space === "world" ) {
  521. this.gizmo[_mode].update( new THREE.Euler(), eye );
  522. }
  523. this.gizmo[_mode].highlight( scope.axis );
  524. };
  525. function onPointerHover( event ) {
  526. if ( scope.object === undefined || _dragging === true ) return;
  527. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  528. var intersect = intersectObjects( pointer, scope.gizmo[_mode].pickers.children );
  529. var axis = null;
  530. if ( intersect ) {
  531. axis = intersect.object.name;
  532. event.preventDefault();
  533. }
  534. if ( scope.axis !== axis ) {
  535. scope.axis = axis;
  536. scope.update();
  537. scope.dispatchEvent( changeEvent );
  538. }
  539. }
  540. function onPointerDown( event ) {
  541. if ( scope.object === undefined || _dragging === true ) return;
  542. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  543. if ( pointer.button === 0 || pointer.button === undefined ) {
  544. var intersect = intersectObjects( pointer, scope.gizmo[_mode].pickers.children );
  545. if ( intersect ) {
  546. event.preventDefault();
  547. event.stopPropagation();
  548. scope.dispatchEvent( mouseDownEvent );
  549. scope.axis = intersect.object.name;
  550. scope.update();
  551. eye.copy( camPosition ).sub( worldPosition ).normalize();
  552. scope.gizmo[_mode].setActivePlane( scope.axis, eye );
  553. var planeIntersect = intersectObjects( pointer, [ scope.gizmo[_mode].activePlane ] );
  554. if ( planeIntersect ) {
  555. oldPosition.copy( scope.object.position );
  556. oldScale.copy( scope.object.scale );
  557. oldRotationMatrix.extractRotation( scope.object.matrix );
  558. worldRotationMatrix.extractRotation( scope.object.matrixWorld );
  559. parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
  560. parentScale.setFromMatrixScale( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
  561. offset.copy( planeIntersect.point );
  562. }
  563. }
  564. }
  565. _dragging = true;
  566. }
  567. function onPointerMove( event ) {
  568. if ( scope.object === undefined || scope.axis === null || _dragging === false ) return;
  569. var pointer = event.changedTouches ? event.changedTouches[0] : event;
  570. var planeIntersect = intersectObjects( pointer, [ scope.gizmo[_mode].activePlane ] );
  571. if ( planeIntersect === false ) return;
  572. event.preventDefault();
  573. event.stopPropagation();
  574. point.copy( planeIntersect.point );
  575. if ( _mode === "translate" ) {
  576. point.sub( offset );
  577. point.multiply(parentScale);
  578. if ( scope.space === "local" ) {
  579. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  580. if ( scope.axis.search("X") === -1 ) point.x = 0;
  581. if ( scope.axis.search("Y") === -1 ) point.y = 0;
  582. if ( scope.axis.search("Z") === -1 ) point.z = 0;
  583. point.applyMatrix4( oldRotationMatrix );
  584. scope.object.position.copy( oldPosition );
  585. scope.object.position.add( point );
  586. }
  587. if ( scope.space === "world" || scope.axis.search("XYZ") !== -1 ) {
  588. if ( scope.axis.search("X") === -1 ) point.x = 0;
  589. if ( scope.axis.search("Y") === -1 ) point.y = 0;
  590. if ( scope.axis.search("Z") === -1 ) point.z = 0;
  591. point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
  592. scope.object.position.copy( oldPosition );
  593. scope.object.position.add( point );
  594. }
  595. if ( scope.snap !== null ) {
  596. if ( scope.axis.search("X") !== -1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.snap ) * scope.snap;
  597. if ( scope.axis.search("Y") !== -1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.snap ) * scope.snap;
  598. if ( scope.axis.search("Z") !== -1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.snap ) * scope.snap;
  599. }
  600. } else if ( _mode === "scale" ) {
  601. point.sub( offset );
  602. point.multiply(parentScale);
  603. if ( scope.space === "local" ) {
  604. if ( scope.axis === "XYZ") {
  605. scale = 1 + ( ( point.y ) / 50 );
  606. scope.object.scale.x = oldScale.x * scale;
  607. scope.object.scale.y = oldScale.y * scale;
  608. scope.object.scale.z = oldScale.z * scale;
  609. } else {
  610. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  611. if ( scope.axis === "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / 50 );
  612. if ( scope.axis === "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / 50 );
  613. if ( scope.axis === "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / 50 );
  614. }
  615. }
  616. } else if ( _mode === "rotate" ) {
  617. point.sub( worldPosition );
  618. point.multiply( parentScale );
  619. tempVector.copy( offset ).sub( worldPosition );
  620. tempVector.multiply( parentScale );
  621. if ( scope.axis === "E" ) {
  622. point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  623. tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  624. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  625. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  626. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  627. quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
  628. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  629. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
  630. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  631. scope.object.quaternion.copy( tempQuaternion );
  632. } else if ( scope.axis === "XYZE" ) {
  633. quaternionE.setFromEuler( point.clone().cross(tempVector).normalize() ); // rotation axis
  634. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  635. quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo(tempVector) );
  636. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  637. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  638. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  639. scope.object.quaternion.copy( tempQuaternion );
  640. } else if ( scope.space === "local" ) {
  641. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  642. tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  643. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  644. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  645. quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
  646. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  647. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  648. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  649. if ( scope.axis === "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
  650. if ( scope.axis === "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
  651. if ( scope.axis === "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
  652. scope.object.quaternion.copy( quaternionXYZ );
  653. } else if ( scope.space === "world" ) {
  654. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  655. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  656. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  657. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  658. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  659. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  660. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  661. if ( scope.axis === "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  662. if ( scope.axis === "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  663. if ( scope.axis === "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  664. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  665. scope.object.quaternion.copy( tempQuaternion );
  666. }
  667. }
  668. scope.update();
  669. scope.dispatchEvent( changeEvent );
  670. scope.dispatchEvent( objectChangeEvent );
  671. }
  672. function onPointerUp( event ) {
  673. if ( _dragging && ( scope.axis !== null ) ) {
  674. mouseUpEvent.mode = _mode;
  675. scope.dispatchEvent( mouseUpEvent )
  676. }
  677. _dragging = false;
  678. onPointerHover( event );
  679. }
  680. function intersectObjects( pointer, objects ) {
  681. var rect = domElement.getBoundingClientRect();
  682. var x = ( pointer.clientX - rect.left ) / rect.width;
  683. var y = ( pointer.clientY - rect.top ) / rect.height;
  684. pointerVector.set( ( x * 2 ) - 1, - ( y * 2 ) + 1, 0.5 );
  685. pointerVector.unproject( camera );
  686. ray.set( camPosition, pointerVector.sub( camPosition ).normalize() );
  687. var intersections = ray.intersectObjects( objects, true );
  688. return intersections[0] ? intersections[0] : false;
  689. }
  690. };
  691. THREE.TransformControls.prototype = Object.create( THREE.Object3D.prototype );
  692. THREE.TransformControls.prototype.constructor = THREE.TransformControls;
  693. }());