PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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