diff --git a/starling/src/starling/textures/ConcreteTexture.as b/starling/src/starling/textures/ConcreteTexture.as index b033f7327..26ac8e863 100644 --- a/starling/src/starling/textures/ConcreteTexture.as +++ b/starling/src/starling/textures/ConcreteTexture.as @@ -88,9 +88,21 @@ package starling.textures * If the size of the bitmap does not match the size of the texture, the bitmap will be * cropped or filled up with transparent pixels */ public function uploadBitmapData(data:BitmapData):void + { + uploadBitmapDatas([data]); + } + + /** Uploads a list of bitmap datas to the texture. The existing contents will be replaced. + * If the size of the bitmap does not match the size of the texture, the bitmap will be + * cropped or filled up with transparent pixels. The first item in the list is the main + * texture bitmap data; subsequent items are mip map levels. Missing mip map levels are + * automatically calculated from the previous bitmap data */ + public function uploadBitmapDatas(datas:Array):void { var potData:BitmapData; + var data:BitmapData = datas[0] as BitmapData; + if (data.width != mWidth || data.height != mHeight) { potData = new BitmapData(mWidth, mHeight, true, 0); @@ -117,10 +129,20 @@ package starling.textures while (currentWidth >= 1 || currentHeight >= 1) { bounds.width = currentWidth; bounds.height = currentHeight; - canvas.fillRect(bounds, 0); - canvas.draw(data, transform, null, null, null, true); + if (datas.length > level) + { + // Use existing bitmap data + canvas.fillRect(bounds, 0); + canvas.draw(datas[level] as BitmapData, null, null, null, null, true); + } + else + { + // Resize previous bitmap data + canvas.fillRect(bounds, 0); + canvas.draw(datas[datas.length-1] as BitmapData, transform, null, null, null, true); + transform.scale(0.5, 0.5); + } potTexture.uploadFromBitmapData(canvas, level++); - transform.scale(0.5, 0.5); currentWidth = currentWidth >> 1; currentHeight = currentHeight >> 1; } diff --git a/starling/src/starling/textures/Texture.as b/starling/src/starling/textures/Texture.as index ddfa244cd..3652d2791 100644 --- a/starling/src/starling/textures/Texture.as +++ b/starling/src/starling/textures/Texture.as @@ -260,6 +260,43 @@ package starling.textures return texture; } + + /** Creates a texture object from an array of bitmap datas, where each bitmap data represents + * one mipmap level. + * Beware: you must not dispose 'data' if Starling should handle a lost device context; + * alternatively, you can handle restoration yourself via "texture.root.onRestore". + * + * @param bitmap: an array of BitmapData instances. the texture will be created with these + * instances. Each BitmapData should have half the dimensions of the previous + * BitmapData in the list. Missing BitmapData instances at the end of the list + * will be automatically created as needed. + * @param optimizeForRenderToTexture: indicates if this texture will be used as + * render target + * @param scale: the scale factor of the created texture. This affects the reported + * width and height of the texture object. + * @param format: the context3D texture format to use. Pass one of the packed or + * compressed formats to save memory (at the price of reduced image + * quality). + * @param repeat: the repeat value of the texture. Only useful for power-of-two textures. + */ + public static function fromBitmapDatas(datas:Array, + optimizeForRenderToTexture:Boolean=false, + scale:Number=1, format:String="bgra", + repeat:Boolean=false):Texture + { + var data:BitmapData = datas[0] as BitmapData; + var texture:Texture = Texture.empty(data.width / scale, data.height / scale, true, + true, optimizeForRenderToTexture, scale, + format, repeat); + + texture.root.uploadBitmapDatas(datas); + texture.root.onRestore = function():void + { + texture.root.uploadBitmapDatas(datas); + }; + + return texture; + } /** Creates a texture from the compressed ATF format. If you don't want to use any embedded * mipmaps, you can disable them by setting "useMipMaps" to false.