Skip to content

Added a way to upload an array of bitmaps for textures with custom mipmap levels #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions starling/src/starling/textures/ConcreteTexture.as
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
37 changes: 37 additions & 0 deletions starling/src/starling/textures/Texture.as
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>false</code>.
Expand Down