TransformControls.js 32 KB

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