TransformControls.js 32 KB

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