Dynamicly painting bitmap textures

Software: Away3D 4.x

maneatingsheep, Newbie
Posted: 01 December 2011 11:50 AM   Total Posts: 12

Hey there
In my project i load a bunch of models, place and remove them from the scene according to the player actions.
the thing is, the textures are mostly transparent, and i actually create a new texture each time by painting the transparent parts to different colors
then i crate a new bitmap material from it and apply it to the mesh.

eventually i get the “Resource limit for this resource type exceeded” error.

what am i doing wrong? what should i reuse or dispose of?

here’s the code that runs each time a place a mesh on the scene:

public function applyMaterial(model:ObjectContainer3DoriginalBitmap:BitmapDatacolor:uint):void {
 
var mat:BitmapMaterial;
   
 var 
coloredBitmapData:BitmapData = new BitmapData(originalBitmap.widthoriginalBitmap.heighttrue,  color);
 
coloredBitmapData.draw(originalBitmap);
 
mat = new BitmapMaterial(coloredBitmapData);
 
mat.lights = (ghost)?[]:[light1light2];
   
 
mat.ambientColor 0xFFFFFF;
   
   
 for (var 
i:int 0ObjectContainer3D(model).numChildreni++) //for multiple meshes like wheels
  
Mesh(model.getChildAt(i)).material mat;
 
}

cheers smile

   

Avatar
Baush, Sr. Member
Posted: 01 December 2011 01:34 PM   Total Posts: 135   [ # 1 ]

You could simply update the lights attribute

Mesh(model.getChildAt(i)).material.lights = (ghost)?[]:[light1,light2];

And ssme for bitmap

.material.bitmapData = coloredBitmapData

   

maneatingsheep, Newbie
Posted: 01 December 2011 02:18 PM   Total Posts: 12   [ # 2 ]

Thanks alot, that helped.
Thought about it some more, and figured out there’s no reason not to use the original material.
My addition, is to dispose of the previous bitmapdata (unless it’s your trmplate)

it also helped to fix the memory leak it was causing

   

Avatar
Baush, Sr. Member
Posted: 01 December 2011 02:24 PM   Total Posts: 135   [ # 3 ]

You can also paint over the material’s existing bitmapData using the .copyPixel() method if you do not want to share the bitmapData accross all your meshes.

Mesh(model.getChildAt(i)).material.bitmapData.copyPixels(coloredBitmapData, coloredBitmapData.rect, new Point());

copyPixel is much faster than draw() unless you need to resize your source bitmap

   

maneatingsheep, Newbie
Posted: 01 December 2011 02:49 PM   Total Posts: 12   [ # 4 ]

yep, tried that and it works great also

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X