Atlas.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @author Mark Kellogg - http://www.github.com/mkkellogg
  3. */
  4. THREE.Atlas = function( texture, createFirstFullFrame ) {
  5. this.texture = texture;
  6. this.imageCount = 0;
  7. this.imageDescriptors = [];
  8. if ( createFirstFullFrame ) {
  9. this.addImageDescriptor( 0, 1, 1, 0 );
  10. }
  11. };
  12. THREE.Atlas.ImageDescriptor = function( left, top, right, bottom ) {
  13. this.left = left;
  14. this.top = top;
  15. this.right = right;
  16. this.bottom = bottom;
  17. };
  18. THREE.Atlas.prototype.addImageDescriptor = function( left, top, right, bottom ) {
  19. this.imageDescriptors[ this.imageCount ] = new THREE.Atlas.ImageDescriptor( left, top, right, bottom );
  20. this.imageCount ++;
  21. };
  22. THREE.Atlas.prototype.getImageDescriptor = function( index ) {
  23. return this.imageDescriptors[ index ];
  24. };
  25. THREE.Atlas.prototype.getTexture = function() {
  26. return this.texture;
  27. };
  28. THREE.Atlas.createGridAtlas = function( texture, left, top, right, bottom, xCount, yCount, reverseX, reverseY ) {
  29. var atlas = new THREE.Atlas( texture );
  30. var width = right - left;
  31. var height = top - bottom;
  32. var xBlockSize = width / xCount;
  33. var yBlockSize = height / yCount;
  34. var xInc = 1;
  35. var yInc = 1;
  36. var xStart = 0;
  37. var yStart = 0;
  38. var xFinish = xCount;
  39. var yFinish = yCount;
  40. if ( reverseX ) {
  41. xInc = - 1;
  42. xStart = xCount - 1;
  43. xFinish = - 1;
  44. }
  45. if ( reverseY ) {
  46. yInc = - 1;
  47. yStart = yCount - 1;
  48. yFinish = - 1;
  49. }
  50. for ( var y = yStart; y != yFinish; y += yInc ) {
  51. for ( var x = xStart; x != xFinish; x += xInc ) {
  52. var currentLeft = left + xBlockSize * x;
  53. var currentTop = bottom + yBlockSize * ( y + 1 );
  54. var currentRight = currentLeft + xBlockSize;
  55. var currentBottom = currentTop - yBlockSize;
  56. atlas.addImageDescriptor( currentLeft, currentTop, currentRight, currentBottom );
  57. }
  58. }
  59. return atlas;
  60. };