TransformControls.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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( new THREE.TorusBufferGeometry( 1, 0.12, 2, 24 ), pickerMaterial ) ]
  265. ]
  266. };
  267. this.pickerGizmos.XYZE[ 0 ][ 0 ].visible = false; // disable XYZE picker gizmo
  268. this.setActivePlane = function ( axis ) {
  269. if ( axis === "E" ) this.activePlane = this.planes[ "XYZE" ];
  270. if ( axis === "X" ) this.activePlane = this.planes[ "YZ" ];
  271. if ( axis === "Y" ) this.activePlane = this.planes[ "XZ" ];
  272. if ( axis === "Z" ) this.activePlane = this.planes[ "XY" ];
  273. };
  274. this.update = function ( rotation, eye2 ) {
  275. THREE.TransformGizmo.prototype.update.apply( this, arguments );
  276. var tempMatrix = new THREE.Matrix4();
  277. var worldRotation = new THREE.Euler( 0, 0, 1 );
  278. var tempQuaternion = new THREE.Quaternion();
  279. var unitX = new THREE.Vector3( 1, 0, 0 );
  280. var unitY = new THREE.Vector3( 0, 1, 0 );
  281. var unitZ = new THREE.Vector3( 0, 0, 1 );
  282. var quaternionX = new THREE.Quaternion();
  283. var quaternionY = new THREE.Quaternion();
  284. var quaternionZ = new THREE.Quaternion();
  285. var eye = eye2.clone();
  286. worldRotation.copy( this.planes[ "XY" ].rotation );
  287. tempQuaternion.setFromEuler( worldRotation );
  288. tempMatrix.makeRotationFromQuaternion( tempQuaternion ).getInverse( tempMatrix );
  289. eye.applyMatrix4( tempMatrix );
  290. this.traverse( function( child ) {
  291. tempQuaternion.setFromEuler( worldRotation );
  292. if ( child.name === "X" ) {
  293. quaternionX.setFromAxisAngle( unitX, Math.atan2( - eye.y, eye.z ) );
  294. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  295. child.quaternion.copy( tempQuaternion );
  296. }
  297. if ( child.name === "Y" ) {
  298. quaternionY.setFromAxisAngle( unitY, Math.atan2( eye.x, eye.z ) );
  299. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  300. child.quaternion.copy( tempQuaternion );
  301. }
  302. if ( child.name === "Z" ) {
  303. quaternionZ.setFromAxisAngle( unitZ, Math.atan2( eye.y, eye.x ) );
  304. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  305. child.quaternion.copy( tempQuaternion );
  306. }
  307. } );
  308. };
  309. this.init();
  310. };
  311. THREE.TransformGizmoRotate.prototype = Object.create( THREE.TransformGizmo.prototype );
  312. THREE.TransformGizmoRotate.prototype.constructor = THREE.TransformGizmoRotate;
  313. THREE.TransformGizmoScale = function () {
  314. THREE.TransformGizmo.call( this );
  315. var arrowGeometry = new THREE.Geometry();
  316. var mesh = new THREE.Mesh( new THREE.BoxGeometry( 0.125, 0.125, 0.125 ) );
  317. mesh.position.y = 0.5;
  318. mesh.updateMatrix();
  319. arrowGeometry.merge( mesh.geometry, mesh.matrix );
  320. var lineXGeometry = new THREE.BufferGeometry();
  321. lineXGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) );
  322. var lineYGeometry = new THREE.BufferGeometry();
  323. lineYGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  324. var lineZGeometry = new THREE.BufferGeometry();
  325. lineZGeometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  326. this.handleGizmos = {
  327. X: [
  328. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0xff0000 } ) ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ],
  329. [ new THREE.Line( lineXGeometry, new GizmoLineMaterial( { color: 0xff0000 } ) ) ]
  330. ],
  331. Y: [
  332. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x00ff00 } ) ), [ 0, 0.5, 0 ] ],
  333. [ new THREE.Line( lineYGeometry, new GizmoLineMaterial( { color: 0x00ff00 } ) ) ]
  334. ],
  335. Z: [
  336. [ new THREE.Mesh( arrowGeometry, new GizmoMaterial( { color: 0x0000ff } ) ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ] ],
  337. [ new THREE.Line( lineZGeometry, new GizmoLineMaterial( { color: 0x0000ff } ) ) ]
  338. ],
  339. XYZ: [
  340. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.125, 0.125, 0.125 ), new GizmoMaterial( { color: 0xffffff, opacity: 0.25 } ) ) ]
  341. ]
  342. };
  343. this.pickerGizmos = {
  344. X: [
  345. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ] ]
  346. ],
  347. Y: [
  348. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0.6, 0 ] ]
  349. ],
  350. Z: [
  351. [ new THREE.Mesh( new THREE.CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), pickerMaterial ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ] ]
  352. ],
  353. XYZ: [
  354. [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.4, 0.4, 0.4 ), pickerMaterial ) ]
  355. ]
  356. };
  357. this.setActivePlane = function ( axis, eye ) {
  358. var tempMatrix = new THREE.Matrix4();
  359. eye.applyMatrix4( tempMatrix.getInverse( tempMatrix.extractRotation( this.planes[ "XY" ].matrixWorld ) ) );
  360. if ( axis === "X" ) {
  361. this.activePlane = this.planes[ "XY" ];
  362. if ( Math.abs( eye.y ) > Math.abs( eye.z ) ) this.activePlane = this.planes[ "XZ" ];
  363. }
  364. if ( axis === "Y" ) {
  365. this.activePlane = this.planes[ "XY" ];
  366. if ( Math.abs( eye.x ) > Math.abs( eye.z ) ) this.activePlane = this.planes[ "YZ" ];
  367. }
  368. if ( axis === "Z" ) {
  369. this.activePlane = this.planes[ "XZ" ];
  370. if ( Math.abs( eye.x ) > Math.abs( eye.y ) ) this.activePlane = this.planes[ "YZ" ];
  371. }
  372. if ( axis === "XYZ" ) this.activePlane = this.planes[ "XYZE" ];
  373. };
  374. this.init();
  375. };
  376. THREE.TransformGizmoScale.prototype = Object.create( THREE.TransformGizmo.prototype );
  377. THREE.TransformGizmoScale.prototype.constructor = THREE.TransformGizmoScale;
  378. THREE.TransformControls = function ( camera, domElement ) {
  379. // TODO: Make non-uniform scale and rotate play nice in hierarchies
  380. // TODO: ADD RXYZ contol
  381. THREE.Object3D.call( this );
  382. domElement = ( domElement !== undefined ) ? domElement : document;
  383. this.object = undefined;
  384. this.visible = false;
  385. this.translationSnap = null;
  386. this.rotationSnap = null;
  387. this.space = "world";
  388. this.size = 1;
  389. this.axis = null;
  390. var scope = this;
  391. var _mode = "translate";
  392. var _dragging = false;
  393. var _gizmo = {
  394. "translate": new THREE.TransformGizmoTranslate(),
  395. "rotate": new THREE.TransformGizmoRotate(),
  396. "scale": new THREE.TransformGizmoScale()
  397. };
  398. for ( var type in _gizmo ) {
  399. var gizmoObj = _gizmo[ type ];
  400. gizmoObj.visible = ( type === _mode );
  401. this.add( gizmoObj );
  402. }
  403. var changeEvent = { type: "change" };
  404. var mouseDownEvent = { type: "mouseDown" };
  405. var mouseUpEvent = { type: "mouseUp", mode: _mode };
  406. var objectChangeEvent = { type: "objectChange" };
  407. var ray = new THREE.Raycaster();
  408. var pointerVector = new THREE.Vector2();
  409. var point = new THREE.Vector3();
  410. var offset = new THREE.Vector3();
  411. var rotation = new THREE.Vector3();
  412. var offsetRotation = new THREE.Vector3();
  413. var scale = 1;
  414. var lookAtMatrix = new THREE.Matrix4();
  415. var eye = new THREE.Vector3();
  416. var tempMatrix = new THREE.Matrix4();
  417. var tempVector = new THREE.Vector3();
  418. var tempQuaternion = new THREE.Quaternion();
  419. var unitX = new THREE.Vector3( 1, 0, 0 );
  420. var unitY = new THREE.Vector3( 0, 1, 0 );
  421. var unitZ = new THREE.Vector3( 0, 0, 1 );
  422. var quaternionXYZ = new THREE.Quaternion();
  423. var quaternionX = new THREE.Quaternion();
  424. var quaternionY = new THREE.Quaternion();
  425. var quaternionZ = new THREE.Quaternion();
  426. var quaternionE = new THREE.Quaternion();
  427. var oldPosition = new THREE.Vector3();
  428. var oldScale = new THREE.Vector3();
  429. var oldRotationMatrix = new THREE.Matrix4();
  430. var parentRotationMatrix = new THREE.Matrix4();
  431. var parentScale = new THREE.Vector3();
  432. var worldPosition = new THREE.Vector3();
  433. var worldRotation = new THREE.Euler();
  434. var worldRotationMatrix = new THREE.Matrix4();
  435. var camPosition = new THREE.Vector3();
  436. var camRotation = new THREE.Euler();
  437. domElement.addEventListener( "mousedown", onPointerDown, false );
  438. domElement.addEventListener( "touchstart", onPointerDown, false );
  439. domElement.addEventListener( "mousemove", onPointerHover, false );
  440. domElement.addEventListener( "touchmove", onPointerHover, false );
  441. domElement.addEventListener( "mousemove", onPointerMove, false );
  442. domElement.addEventListener( "touchmove", onPointerMove, false );
  443. domElement.addEventListener( "mouseup", onPointerUp, false );
  444. domElement.addEventListener( "mouseout", onPointerUp, false );
  445. domElement.addEventListener( "touchend", onPointerUp, false );
  446. domElement.addEventListener( "touchcancel", onPointerUp, false );
  447. domElement.addEventListener( "touchleave", onPointerUp, false );
  448. this.dispose = function () {
  449. domElement.removeEventListener( "mousedown", onPointerDown );
  450. domElement.removeEventListener( "touchstart", onPointerDown );
  451. domElement.removeEventListener( "mousemove", onPointerHover );
  452. domElement.removeEventListener( "touchmove", onPointerHover );
  453. domElement.removeEventListener( "mousemove", onPointerMove );
  454. domElement.removeEventListener( "touchmove", onPointerMove );
  455. domElement.removeEventListener( "mouseup", onPointerUp );
  456. domElement.removeEventListener( "mouseout", onPointerUp );
  457. domElement.removeEventListener( "touchend", onPointerUp );
  458. domElement.removeEventListener( "touchcancel", onPointerUp );
  459. domElement.removeEventListener( "touchleave", onPointerUp );
  460. };
  461. this.attach = function ( object ) {
  462. this.object = object;
  463. this.visible = true;
  464. this.update();
  465. };
  466. this.detach = function () {
  467. this.object = undefined;
  468. this.visible = false;
  469. this.axis = null;
  470. };
  471. this.getMode = function () {
  472. return _mode;
  473. };
  474. this.setMode = function ( mode ) {
  475. _mode = mode ? mode : _mode;
  476. if ( _mode === "scale" ) scope.space = "local";
  477. for ( var type in _gizmo ) _gizmo[ type ].visible = ( type === _mode );
  478. this.update();
  479. scope.dispatchEvent( changeEvent );
  480. };
  481. this.setTranslationSnap = function ( translationSnap ) {
  482. scope.translationSnap = translationSnap;
  483. };
  484. this.setRotationSnap = function ( rotationSnap ) {
  485. scope.rotationSnap = rotationSnap;
  486. };
  487. this.setSize = function ( size ) {
  488. scope.size = size;
  489. this.update();
  490. scope.dispatchEvent( changeEvent );
  491. };
  492. this.setSpace = function ( space ) {
  493. scope.space = space;
  494. this.update();
  495. scope.dispatchEvent( changeEvent );
  496. };
  497. this.update = function () {
  498. if ( scope.object === undefined ) return;
  499. scope.object.updateMatrixWorld();
  500. worldPosition.setFromMatrixPosition( scope.object.matrixWorld );
  501. worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( scope.object.matrixWorld ) );
  502. camera.updateMatrixWorld();
  503. camPosition.setFromMatrixPosition( camera.matrixWorld );
  504. camRotation.setFromRotationMatrix( tempMatrix.extractRotation( camera.matrixWorld ) );
  505. scale = worldPosition.distanceTo( camPosition ) / 6 * scope.size;
  506. this.position.copy( worldPosition );
  507. this.scale.set( scale, scale, scale );
  508. if ( camera instanceof THREE.PerspectiveCamera ) {
  509. eye.copy( camPosition ).sub( worldPosition ).normalize();
  510. } else if ( camera instanceof THREE.OrthographicCamera ) {
  511. eye.copy( camPosition ).normalize();
  512. }
  513. if ( scope.space === "local" ) {
  514. _gizmo[ _mode ].update( worldRotation, eye );
  515. } else if ( scope.space === "world" ) {
  516. _gizmo[ _mode ].update( new THREE.Euler(), eye );
  517. }
  518. _gizmo[ _mode ].highlight( scope.axis );
  519. };
  520. function onPointerHover( event ) {
  521. if ( scope.object === undefined || _dragging === true || ( event.button !== undefined && event.button !== 0 ) ) return;
  522. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  523. var intersect = intersectObjects( pointer, _gizmo[ _mode ].pickers.children );
  524. var axis = null;
  525. if ( intersect ) {
  526. axis = intersect.object.name;
  527. event.preventDefault();
  528. }
  529. if ( scope.axis !== axis ) {
  530. scope.axis = axis;
  531. scope.update();
  532. scope.dispatchEvent( changeEvent );
  533. }
  534. }
  535. function onPointerDown( event ) {
  536. if ( scope.object === undefined || _dragging === true || ( event.button !== undefined && event.button !== 0 ) ) return;
  537. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  538. if ( pointer.button === 0 || pointer.button === undefined ) {
  539. var intersect = intersectObjects( pointer, _gizmo[ _mode ].pickers.children );
  540. if ( intersect ) {
  541. event.preventDefault();
  542. event.stopPropagation();
  543. scope.axis = intersect.object.name;
  544. scope.dispatchEvent( mouseDownEvent );
  545. scope.update();
  546. eye.copy( camPosition ).sub( worldPosition ).normalize();
  547. _gizmo[ _mode ].setActivePlane( scope.axis, eye );
  548. var planeIntersect = intersectObjects( pointer, [ _gizmo[ _mode ].activePlane ] );
  549. if ( planeIntersect ) {
  550. oldPosition.copy( scope.object.position );
  551. oldScale.copy( scope.object.scale );
  552. oldRotationMatrix.extractRotation( scope.object.matrix );
  553. worldRotationMatrix.extractRotation( scope.object.matrixWorld );
  554. parentRotationMatrix.extractRotation( scope.object.parent.matrixWorld );
  555. parentScale.setFromMatrixScale( tempMatrix.getInverse( scope.object.parent.matrixWorld ) );
  556. offset.copy( planeIntersect.point );
  557. }
  558. }
  559. }
  560. _dragging = true;
  561. }
  562. function onPointerMove( event ) {
  563. if ( scope.object === undefined || scope.axis === null || _dragging === false || ( event.button !== undefined && event.button !== 0 ) ) return;
  564. var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event;
  565. var planeIntersect = intersectObjects( pointer, [ _gizmo[ _mode ].activePlane ] );
  566. if ( planeIntersect === false ) return;
  567. event.preventDefault();
  568. event.stopPropagation();
  569. point.copy( planeIntersect.point );
  570. if ( _mode === "translate" ) {
  571. point.sub( offset );
  572. point.multiply( parentScale );
  573. if ( scope.space === "local" ) {
  574. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  575. if ( scope.axis.search( "X" ) === - 1 ) point.x = 0;
  576. if ( scope.axis.search( "Y" ) === - 1 ) point.y = 0;
  577. if ( scope.axis.search( "Z" ) === - 1 ) point.z = 0;
  578. point.applyMatrix4( oldRotationMatrix );
  579. scope.object.position.copy( oldPosition );
  580. scope.object.position.add( point );
  581. }
  582. if ( scope.space === "world" || scope.axis.search( "XYZ" ) !== - 1 ) {
  583. if ( scope.axis.search( "X" ) === - 1 ) point.x = 0;
  584. if ( scope.axis.search( "Y" ) === - 1 ) point.y = 0;
  585. if ( scope.axis.search( "Z" ) === - 1 ) point.z = 0;
  586. point.applyMatrix4( tempMatrix.getInverse( parentRotationMatrix ) );
  587. scope.object.position.copy( oldPosition );
  588. scope.object.position.add( point );
  589. }
  590. if ( scope.translationSnap !== null ) {
  591. if ( scope.space === "local" ) {
  592. scope.object.position.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  593. }
  594. if ( scope.axis.search( "X" ) !== - 1 ) scope.object.position.x = Math.round( scope.object.position.x / scope.translationSnap ) * scope.translationSnap;
  595. if ( scope.axis.search( "Y" ) !== - 1 ) scope.object.position.y = Math.round( scope.object.position.y / scope.translationSnap ) * scope.translationSnap;
  596. if ( scope.axis.search( "Z" ) !== - 1 ) scope.object.position.z = Math.round( scope.object.position.z / scope.translationSnap ) * scope.translationSnap;
  597. if ( scope.space === "local" ) {
  598. scope.object.position.applyMatrix4( worldRotationMatrix );
  599. }
  600. }
  601. } else if ( _mode === "scale" ) {
  602. point.sub( offset );
  603. point.multiply( parentScale );
  604. if ( scope.space === "local" ) {
  605. if ( scope.axis === "XYZ" ) {
  606. scale = 1 + ( ( point.y ) / Math.max( oldScale.x, oldScale.y, oldScale.z ) );
  607. scope.object.scale.x = oldScale.x * scale;
  608. scope.object.scale.y = oldScale.y * scale;
  609. scope.object.scale.z = oldScale.z * scale;
  610. } else {
  611. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  612. if ( scope.axis === "X" ) scope.object.scale.x = oldScale.x * ( 1 + point.x / oldScale.x );
  613. if ( scope.axis === "Y" ) scope.object.scale.y = oldScale.y * ( 1 + point.y / oldScale.y );
  614. if ( scope.axis === "Z" ) scope.object.scale.z = oldScale.z * ( 1 + point.z / oldScale.z );
  615. }
  616. }
  617. } else if ( _mode === "rotate" ) {
  618. point.sub( worldPosition );
  619. point.multiply( parentScale );
  620. tempVector.copy( offset ).sub( worldPosition );
  621. tempVector.multiply( parentScale );
  622. if ( scope.axis === "E" ) {
  623. point.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  624. tempVector.applyMatrix4( tempMatrix.getInverse( lookAtMatrix ) );
  625. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  626. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  627. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  628. quaternionE.setFromAxisAngle( eye, rotation.z - offsetRotation.z );
  629. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  630. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionE );
  631. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  632. scope.object.quaternion.copy( tempQuaternion );
  633. } else if ( scope.axis === "XYZE" ) {
  634. quaternionE.setFromEuler( point.clone().cross( tempVector ).normalize() ); // rotation axis
  635. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  636. quaternionX.setFromAxisAngle( quaternionE, - point.clone().angleTo( tempVector ) );
  637. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  638. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  639. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  640. scope.object.quaternion.copy( tempQuaternion );
  641. } else if ( scope.space === "local" ) {
  642. point.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  643. tempVector.applyMatrix4( tempMatrix.getInverse( worldRotationMatrix ) );
  644. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  645. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  646. quaternionXYZ.setFromRotationMatrix( oldRotationMatrix );
  647. if ( scope.rotationSnap !== null ) {
  648. quaternionX.setFromAxisAngle( unitX, Math.round( ( rotation.x - offsetRotation.x ) / scope.rotationSnap ) * scope.rotationSnap );
  649. quaternionY.setFromAxisAngle( unitY, Math.round( ( rotation.y - offsetRotation.y ) / scope.rotationSnap ) * scope.rotationSnap );
  650. quaternionZ.setFromAxisAngle( unitZ, Math.round( ( rotation.z - offsetRotation.z ) / scope.rotationSnap ) * scope.rotationSnap );
  651. } else {
  652. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  653. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  654. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  655. }
  656. if ( scope.axis === "X" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionX );
  657. if ( scope.axis === "Y" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionY );
  658. if ( scope.axis === "Z" ) quaternionXYZ.multiplyQuaternions( quaternionXYZ, quaternionZ );
  659. scope.object.quaternion.copy( quaternionXYZ );
  660. } else if ( scope.space === "world" ) {
  661. rotation.set( Math.atan2( point.z, point.y ), Math.atan2( point.x, point.z ), Math.atan2( point.y, point.x ) );
  662. offsetRotation.set( Math.atan2( tempVector.z, tempVector.y ), Math.atan2( tempVector.x, tempVector.z ), Math.atan2( tempVector.y, tempVector.x ) );
  663. tempQuaternion.setFromRotationMatrix( tempMatrix.getInverse( parentRotationMatrix ) );
  664. if ( scope.rotationSnap !== null ) {
  665. quaternionX.setFromAxisAngle( unitX, Math.round( ( rotation.x - offsetRotation.x ) / scope.rotationSnap ) * scope.rotationSnap );
  666. quaternionY.setFromAxisAngle( unitY, Math.round( ( rotation.y - offsetRotation.y ) / scope.rotationSnap ) * scope.rotationSnap );
  667. quaternionZ.setFromAxisAngle( unitZ, Math.round( ( rotation.z - offsetRotation.z ) / scope.rotationSnap ) * scope.rotationSnap );
  668. } else {
  669. quaternionX.setFromAxisAngle( unitX, rotation.x - offsetRotation.x );
  670. quaternionY.setFromAxisAngle( unitY, rotation.y - offsetRotation.y );
  671. quaternionZ.setFromAxisAngle( unitZ, rotation.z - offsetRotation.z );
  672. }
  673. quaternionXYZ.setFromRotationMatrix( worldRotationMatrix );
  674. if ( scope.axis === "X" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionX );
  675. if ( scope.axis === "Y" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionY );
  676. if ( scope.axis === "Z" ) tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionZ );
  677. tempQuaternion.multiplyQuaternions( tempQuaternion, quaternionXYZ );
  678. scope.object.quaternion.copy( tempQuaternion );
  679. }
  680. }
  681. scope.update();
  682. scope.dispatchEvent( changeEvent );
  683. scope.dispatchEvent( objectChangeEvent );
  684. }
  685. function onPointerUp( event ) {
  686. event.preventDefault(); // Prevent MouseEvent on mobile
  687. if ( event.button !== undefined && event.button !== 0 ) return;
  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. }() );