Quickest and best way to swap a bitmap texture

Software: Away3D 4.x

TerryMcG, Newbie
Posted: 24 January 2013 04:16 AM   Total Posts: 7

What’s the best way to swap a Bitmap texture? Currently, I’m changing it by setting the bitmapData property of a bitmap texture object. When I set the property, I get a short performance hit and a frame rate drop? So, is there a more efficient way to do this that would keep my animation running smoothly?

   

bart_the_13th, Newbie
Posted: 24 January 2013 04:27 PM   Total Posts: 14   [ # 1 ]

I believe that when you set/change the bitmapData, the 3d renderer must write the bitmap from memory to graphic card’s memory, maybe that’s the cause of the performance hit.
Maybe, you can try to change the texture/bitmapTexture instead the bitmapData
Or if you are using mesh with UV, you can use a spritesheet as texture and shift the UV to animate it.

   

Monkey on a laptop, Newbie
Posted: 25 January 2013 03:03 PM   Total Posts: 4   [ # 2 ]

I have a similar problem. I understand the idea of shifting the UV of a spritesheet texture to animate it but I can’t figure out how to do it. Could it be possible to have an example?

   

Avatar
Fabrice Closier, Administrator
Posted: 25 January 2013 03:51 PM   Total Posts: 1265   [ # 3 ]

If you use a sprite sheet divided 4 images for instance.
and you use a plane. The plane is mapped 0-1. with corners being 0-1, 1.1, 1.0 and 0.0.
if one of the “cells” is ment to be displayed on entire plane, then it needs to be bigger. in this case 2 times on the u and 2 times on the v. So depending on howmany cells horizontal and vertical you have you adjust the uvscale of the mesh receiving the material. then in most common cases, the cells are all equal. So you need to add the u and v offset. If you look at case of just 4 cells on the sheet, the first image is at 0 u. because we scale to 2, what would be normally 0.5 (the end of the cell on u axis) it becomes 1. no need do more for this one. Now iteration two, assuming your sequence of images goes from right to left. the same as previous cell will work, but we need to move the content back to left side.
so you divide 1 by cell count on u axis. that give you .5. substract this as offset to u values, you get on u axis. .5 to 1. Your second image is displayed. repeat per iteration adding the right offset and your material will animate…

Another way to do it and way more efficient is to precalculate the uvs during the build of the sheet. and runtime simply pass them as uv’s. Because it’s predefined, you can in case of transparent images, push more data, (more images) per spritesheets.
What your material may also need to manage, is the swap of sheets if the full animation doesn’t fit on one source sheet. You must also keep track of empty cells and possibly scale ratio in case of multiple sheets.

An even smarter material would allow reverse anim, cell transforms, sequences within cells etc..

   

Monkey on a laptop, Newbie
Posted: 25 January 2013 04:11 PM   Total Posts: 4   [ # 4 ]

That’s what I was trying to do. The one thing I was missing was that I didn’t set material.animateUVs = true.

   

Avatar
Fabrice Closier, Administrator
Posted: 25 January 2013 04:37 PM   Total Posts: 1265   [ # 5 ]

Here a little example of the hardcoded approach
in your init of an anim
you save the uvs offsets.

_uvs = new Vector.<Number>();
  _uvs.push(0,0);
  _uvs.push(.25,0);
  _uvs.push(.5,0);
  _uvs.push(.75,0);
 
  _uvs.push(0,.5);
  _uvs.push(.25,.5);
  _uvs.push(.5,.5);
  _uvs.push(.75,.5);
  etc etc


var subMesh:SubMesh = myAnimatedMesh.subMeshes[0];
subMesh.scaleUV(.25, .5);
set the scale, here where its a 4x2 cells

in your enterframe you increase _frameCount;
if you reach > than images count you reset to zero.

and in your update code do something like.
var subMesh:SubMesh = myAnimatedMesh.subMeshes[0];
ind = _frameCount*2;
subMesh.offsetU = _uvs[ind];
subMesh.offsetV = _uvs[ind+1];

and yes animateUV must be true

You can also ensure that your playrate fit your anim
to play 20 fps while scenery renders at 60, framecount would then be increased by .33. and you use Math.floor to calculate the index in your offset _uvs buffer.

   

Monkey on a laptop, Newbie
Posted: 25 January 2013 07:17 PM   Total Posts: 4   [ # 6 ]

Thank you for your help.

One last question, when I set animateUV to true, flash starts tracing the same line over and over :

Warning: animateUVs is set to true with an IRenderable without a uvTransform. Identity matrix assumed.


What can I do to get rid of that?

   

Avatar
Fabrice Closier, Administrator
Posted: 25 January 2013 08:34 PM   Total Posts: 1265   [ # 7 ]

Simply ensure that you render with animateUV true when you have applied at least one time. Now your probably set to true, render a few frames then set the transforms. You may also have set it to another mesh and do not animate the uvs on this one.

   

mkfui, Newbie
Posted: 20 October 2016 07:27 AM   Total Posts: 29   [ # 8 ]

sir, it didn’t work

mat = new TextureMaterial(Cast.bitmapTexture(mynum));
mat.alphaBlending = true;
 
mesh = new Mesh(new PlaneGeometry(250, 250, 1, 1, false, true), mat);
_view.scene.addChild(mesh);
mesh.y = 100;
mat.animateUVs = true;
var subMesh:SubMesh = mesh.subMeshes[0];
mesh.geometry.scaleUV(.25, .25);
 
var _uvs:Vector.<Number> = new Vector.<Number>();
_uvs.push(0,0);
_uvs.push(.25,0);
_uvs.push(.5,0);
_uvs.push(.75,0);
 
_uvs.push(0,.25);
_uvs.push(.25,.25);
_uvs.push(.5,.25);
_uvs.push(.75,.25);
 
_uvs.push(0,.5);
_uvs.push(.25,.5);
_uvs.push(.5,.5);
_uvs.push(.75,.5);
 
_uvs.push(0,.75);
_uvs.push(.25,.75);
_uvs.push(.5,.75);
_uvs.push(.75,.75);
 
subMesh.offsetU = _uvs[k];
subMesh.offsetV = _uvs[k + 1];

private function _onEnterFrame(e:Event):void

for (var k:uint=0;k<15;k+=1)
{
trace(k);
}

sir, it goto frame 1 only, why ? from 0 to frame 1, but not continuosly offset uv, anything wrong, can sir demonstrate it in complete code ?

   

mkfui, Newbie
Posted: 21 October 2016 12:41 AM   Total Posts: 29   [ # 9 ]

anyone pls help me, it didn’t animate

   

Avatar
Fabrice Closier, Administrator
Posted: 21 October 2016 09:32 AM   Total Posts: 1265   [ # 10 ]

Because you set animateUV to true, doesn’t mean they actually animate.
It’s a flag for the render pass, to make sure the buffers are updated if required.

To animate uv’s you need an AnimatorBase. Use either UVAnimator or SpriteSheetAnimator.
Look in examples on github or tutorials on this site to see how its done.

   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X