TransformControls.js 32 KB

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