TransformControls.js 33 KB

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