TransformControls.js 32 KB

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