i try generate my sprite and save it for use in app (i wont generate it every render method call). But, if i have black details on my generated sprite it takes transparency. But i wont it...
How i need draw texture on generated sprite?
Related
Im building an iOS AIR app using AS3/Flash builder.
The app grabs the camera stream into a video object and then draws that to a BitmapData. The camera stream is 1280x720px and the container is 2208x1242px (iPhone 7+) so I need to scale the footage. I also need to rotate it around the center point:
mat.identity();
mat.translate(-video.width/2,-video.height/2);
mat.rotate(Math.PI/2);
mat.translate(video.height/2,video.width/2);
mat.scale(deviceSize.height/cam.width,deviceSize.width/cam.height);
I have timed the drawing operation with matrix:
videoBitmapData.draw(video,mat); //9-11ms
And without matrix:
videoBitmapData.draw(video); //2-3ms
Clearly the transformation is slowing me down.
Is there a faster way to do this? Maybe drawing first, then applying the matrix somehow?
Pre-rotating something?
Leveraging native scaling/rotation?
Can I skip using the video object somehow? Accessing the camera data in a more raw form?
I see no difference using render mode GPU vs CPU.
Thanks!
Edit:
I managed to get down to about half by doing this:
1. Put the video object inside a Sprite
2. Transform the Sprite.
3. Draw the Sprite to the BitmapData.
(5-6ms)
Suprisingly, this was more consistent than this:
1. Put the video object inside a Sprite
2. Transform the video object inside the Sprite
3. Draw the sprite to the BitmapData
(5-12ms)
Bonus:
I only needed part of the pixels from the camera (full width x 100px of the height). When realizing I could use clipRect with the .draw command I managed to get down to 0-1ms.
Thanks to Organis
I want to use animated sprite characters in my LibGdx Project.
I have sprite sheets and also individual images for the sprite animations.
While creating texture atlas,do I have to use sprite sheet itself or individual images for packing?
Which one is the best approach?
Currently I'm creating separate atlas for each sprite animations using individual images,and adding the pack& png files to assets folder. It is working. But I hope there is some way to efficiently use one pack file and one png file for the game.
Also I want to know about naming those individual sprite sheet images.
I'm naming it in such a way that "name_01,name_02.....etc".
Please suggest the better way of creating single texture atlas for the entire game,that contain sprite sheets and non-sprite images.
You would have to use a texture packer. https://code.google.com/archive/p/libgdx-texturepacker-gui/
Create the sprite sheet as one texture for animation and pack them.
Pack all sprite animations images into one pack file.
Get TextureAtlas from AssetManager if you want to use AssetManager or you can create object of TextureAtlas.
Then you can call getArray(gameAtlas.findRegion("resourceName"),x,y); method. That returns Array of TextureRegion which can be used as argument of Animation class.
public TextureRegion[] getArray(TextureRegion textureRegion,int column,int row){
TextureRegion[][] arrayTextureRegion= textureRegion.split(textureRegion.getRegionWidth()/column,textureRegion.getRegionHeight()/row);
TextureRegion textureRegion1[]=new TextureRegion[arrayTextureRegion[0].length*arrayTextureRegion.length];
for (int i=0;i<arrayTextureRegion.length;i++)
for (int j=0;j<arrayTextureRegion[i].length;j++)
textureRegion1[i*arrayTextureRegion[i].length+j]=arrayTextureRegion[i][j];
return textureRegion1;
}
I am making a game in AS3 for a school project (using the AIR api). I have about a year's worth of experience in AS3, so i would say i'm proficient but not an expert. Anyway, i have never attempted to do sprite animations in AS3 and i'm not quite sure how to approach it.
If i create a list of bitmaps and call addChild() and removeChild() to display each frame of the animation, it would affect the framerate as these functions are not very efficient (i have tried this before and it tanked my framerate if i had too many animations going at once). I have also tried creating a list of BitmapData objects, adding a bitmap to the display list, and then pointing it to a different BitmapData each frame, but this does not seem to work at all.
So what is the best way to do this? In XNA, for example, i would create a sprite class that draws to the screen using a sprite batch, then i would create a list of sprite objects and cycle through them to create the animation. Is there any way to achieve a similar result in actionscript?
First (simple) method: you can just use multi-frame MovieClip for animation. Put an image in each frame, put a MovieClip on the stage and that's all. You can control this animation using play(), stop(), gotoAndPlay(), gotoAndStop(). It will work without much problems for a generic platform game (I did that myself long ago).
Second (advanced) method: use bitmap blitting. For each animation, create a bitmap image that holds each frame of the animation. Use this image as a source for copying pixels into your current animated object. You just need to copy a particular rectangle area inside a source bitmap that corresponds to the current frame.
The actual blitting happens here
destinationBitmapData.copyPixels(sourceBitmapData, areaRectangle, destinationPoint);
Where destinationBitmapData is your "canvas" that you're blitting to; sourceBitmapData is the source image holding all animation frames; areaRectangle is the Rectangle inside the source image defining which area to copy; destinationPoint is left-top coordinate of the copy area in your canvas.
The destination canvas can be just one small object (like your game character that is moving around) or the entire game screen with all objects. I.e. instead of blitting and adding each object separately, you can just have one big canvas and blit any necessary parts directly to it.
That said, there is already a number of various ready-made engines that use blitting and even advanced techniques like 3D acceleration for 2D sprites.
One of them is Starling.
In my flash project I have 3 layers.
back layer contain Flvplayback video
middle layer contain webcam stream
front layer contain flvplayback video(with Transparent background).
webcam stream video is also transparent.
I add the stream from following code.
webCam=Camera.getCamera();
webCam.setMode(appWidth/scale,appHeight/scale,60);
video=new Video(appWidth/scale,appHeight/scale);
video.smoothing=true;
video.attachCamera(webCam);
bmpdata=new BitmapData(appWidth/scale,appHeight/scale);
bmpdata_bg=new BitmapData(appWidth/scale,appHeight/scale);
bmp=new Bitmap(bmpdata);
bmp.smoothing=true;
bmp.x = 256;
bmp.y = 374;
addChild(bmp);
and I update that webcam stream using following function
function onFrameEnter(evt:Event):void {
bmpdata.draw(video);
}
I want to know how to add this webcam stream to a middle layer and play it.
Is there any way to add this webcam stream to flvplayback in the middle layer?
As far as I know it's impossible to refer to Flash IDE layers from Actionscript. There are a couple of workarounds you can do, though.
One is to have an empty instance movieclip in the desired layer with an instance name mcStream. Then, you can use mcStream.addChild(bmp);.
Another way is to refer to the item you want it to be under (we'll call it mcOverlay) and add it at its index. addChildAt(bmp, getChildIndex(mcOverlay)) This will push add it where the overlay is and push the overlay on top of it.
I definitely recommend having a look at http://www.adobe.com/devnet/flash/quickstart/display_list_programming_as3.html
I'm making an animation of a 2D character that can walk, run, jump, bend,...
Would it be better to load one big 'spritesheet' with all the animations and just use a mask, or would loading separate files (walk, run,...) be better because you're not using a mask on such a big image every frame?
I'm not using the Stage3D features with a framework like Starling because I think the normal flash display API is fast enough and has much less bugs than the relatively new GPU frameworks.
Blitting just the character (using lock(),copyPixels(),unlock()) works pretty well.
private function updatePixels():void{
//update sprite sheet copy position based on the frame placements ons prite sheet
position.x = spriteSourceData[currentFrame].x + offset.x;
position.y = spriteSourceData[currentFrame].y + offset.y;
//draw into the bitmap displayed
displayData.lock();
displayData.fillRect(displayData.rect, 0x00FFFFFF);//clear
displayData.copyPixels(sourceData, spriteData[currentFrame], position);//copy new frame pixels
displayData.unlock();
}
//a bit about vars:
position:Point
spriteSourceData:Vector.<Rectangle> - from parsed Texture Packer data
offset:Point - front view and side view animations weren't always centred, so an offset was needed
displayData:BitmapData - pluging into a Bitmap object displayed
sourceData:BitmapData - the large sprite sheet
currentFrame:int - image index on the sprite sheet
I've done this on an older project writing a custom class loosely following what I've learned from Lee Brimelow's tutorial series Sprite Sheets and Blitting (Part 1,Part 2, Part 3)
In short, you'd use two BitmapData objects:
a large sprite sheet
a small image to display just the character (size of the largest character bounding box) to copy pixels into
In my project I had a character with front and side animations and for the sides I've used one set of animations and used the Matrix class to flip(scale and translate) the side animation accordingly. I've used TexturePacker to export the image sequence as a sprite sheet and the frame data as well as a JSON object. There is native JSON support now, so that's handy. Texture Packer isn't free but it's really worth the money (affordable, fast and does the job perfectly). I haven't used Flash CS6 yet but I imagine it's also possible to import your image sequence and export a spritesheet with the new feature.
In my experience the rule "the simpler the display list the better the performance" generally applies. Which means you should use the most specific display object that will do the job (don't use a Sprite when a Shape would be sufficient or favor Bitmaps over vectors where it makes sense).
The most extreme version of this is to only have one Bitmap display object on the stage and use copyPixels to draw all game objects into it every time you want to update the screen. It doesn't really matter what the source in the copyPixel call is, it could either be a large BitmapData acting as a sprite sheet or a small BitmapData objects representing a single frame in an animation. This method is really fast and you could easily have many hundreds of objects on screen at the same time. But using copyPixels means you can't scale or rotate a game object, so for those cases you would have to fall back to the much slower draw() method. Of course this single Bitmap method is not suitable for games where you need to attach mouse events to specfic objects in the game, but works well for shoot'em ups or platform games.
To answer your question, I think you will get better performance by using a single Bitmap display object to represent the player and a collection of BitmapData objects for all the animation frames. Then you can just change the Bitmap's bitmapData property to the frame you want to display. You can still load a large spritesheet png and then plit it up into a series of BitmapData objects during the initialization of the game.