/src/away3d/materials/utils/MipmapGenerator.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 60 lines · 40 code · 10 blank · 10 comment · 6 complexity · 8c7e200c3e9039e65b3cc929154b1dd0 MD5 · raw file

  1. package away3d.materials.utils
  2. {
  3. import flash.display.*;
  4. import flash.display3D.textures.CubeTexture;
  5. import flash.display3D.textures.Texture;
  6. import flash.display3D.textures.TextureBase;
  7. import flash.geom.*;
  8. /**
  9. * MipmapGenerator is a helper class that uploads BitmapData to a Texture including mipmap levels.
  10. */
  11. public class MipmapGenerator
  12. {
  13. private static var _matrix:Matrix = new Matrix();
  14. private static var _rect:Rectangle = new Rectangle();
  15. /**
  16. * Uploads a BitmapData with mip maps to a target Texture object.
  17. * @param source The source BitmapData to upload.
  18. * @param target The target Texture to upload to.
  19. * @param mipmap An optional mip map holder to avoids creating new instances for fe animated materials.
  20. * @param alpha Indicate whether or not the uploaded bitmapData is transparent.
  21. */
  22. public static function generateMipMaps(source:BitmapData, target:TextureBase, mipmap:BitmapData = null, alpha:Boolean = false, side:int = -1):void
  23. {
  24. var w:uint = source.width,
  25. h:uint = source.height;
  26. var i:uint;
  27. var regen:Boolean = mipmap != null;
  28. mipmap ||= new BitmapData(w, h, alpha);
  29. _rect.width = w;
  30. _rect.height = h;
  31. while (w >= 1 || h >= 1) {
  32. if (alpha)
  33. mipmap.fillRect(_rect, 0);
  34. _matrix.a = _rect.width/source.width;
  35. _matrix.d = _rect.height/source.height;
  36. mipmap.draw(source, _matrix, null, null, null, true);
  37. if (target is Texture)
  38. Texture(target).uploadFromBitmapData(mipmap, i++);
  39. else
  40. CubeTexture(target).uploadFromBitmapData(mipmap, side, i++);
  41. w >>= 1;
  42. h >>= 1;
  43. _rect.width = w > 1? w : 1;
  44. _rect.height = h > 1? h : 1;
  45. }
  46. if (!regen)
  47. mipmap.dispose();
  48. }
  49. }
  50. }