TransformControls.js 32 KB

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