Taking advantage of Flash hardware acceleration - actionscript-3

My goal is to test Flash 3D environment performance by generating lots of 3D cubes, rotating them and reading FPS.
I know that I can rotate 2D objects in 3D space. For example, I can construct a cube with 6 movie clips, rotating them and putting them together accordingly. Then I can generate lots of these cubes in random x,y,z locations (predefined area in front of viewport), rotate them and read fps.
But then I read about this hardware acceleration and it's unclear to me, when it is activated/used. Certain conditions must be met. I know that it must be allowed by the user (right click->settings->enable hardware acceleration) or if it is embedded through object tag, wmode=direct must be set.
That's from viewers side, but what from developers?
If I draw a simple red rectangle on stage, and user has enabled hardware acceleration, does that mean that graphic information will get rendered on GPU?
I'm reading various sources and "Adobe Flash 11 Stage3D Game Programming" book, and from what I gather, in order to render graphical information on GPU, I have to explicitly call Stage3D class in AS3, then I can draw my 2d/3d objects there.
Which is it - if I want my app to be run in hardware acceleration mode (and its enabled from viewers side), does it happen automatically no matter what the content in my flash file? Or do I have to add the Stage3D class there.
Thank you.
Will be waiting for the answer.

First off: Don't mix up 2 distinct concepts in Flash:
General Hardware Acceleration
This was introduced mainly for video playback in Flash Player 10. I am not sure if it used for graphics at all. It has nothing to do with rendering 3D Graphics directly on the GPU.
GPU Support
With Flash Player 11, Adobe introduced Stage3D. This is an interface through which Flash can render graphics utilizing the GPU, if available. On Windows it uses Direct3D, on Mac OpenGl. The classic Flash DisplayList Graphics API does not support this. You have to go through the Stage3D Api, as explained in the book you have mentioned. To use GPU Support in the browser the embed tag needs the attribute wmode set to "direct". Working directly with the Stage3D Api is not easy and you have to learn the concepts of 3D Programming, Shader Programming, etc. for doing this. There are some libraries which help working with Stage3D. For 3D there is i.e Away3D. If you just need 2D graphics with gpu support, Starling is a very popular framework these days.

Here's a function that converts a movieclip into a bitmap. I use normal flash to create what is going to be shown on screen, then convert the movieclips to bitmaps and delete the movieclips:
private function fConvertClip(pClip: MovieClip, pWidth: int, pHeight: int): Bitmap {
var bd: BitmapData = new BitmapData(pWidth, pHeight, true, 0x00000000);
bd.draw(pClip, null, null, null, null, true);
aBitmaps.push(bd);
var bmp: Bitmap = new Bitmap(bd, "auto", false);
bmp.smoothing = true;
return bmp;
}

Don't turn on the hardware acceleration setting. In general, you control what's on the gpu with code. Bitmapdata is on the gpu so you convert everything that goes on the screen to bitmaps.
gotoandlearn.com has great tutorial videos on this.
You can create anything in normal flash - shapes, gradients, dropshadows, dynamic text, programmed coloring, etc. - and convert it all to bitmaps with programming while your game is running. You don't need to import images/spritesheets/animations. Have artists give you everything in vector. So when the user wants to change the color of a car, you simply convert the code instead of loading/importing a bitmap of the car in red.

Related

Can I use the effects made in Adobe After Effects in the game developed with Adobe Flash Pro and ActionScript 3? If so how?

I am developing a 2D game using Adobe Flash Pro CS 6 and ActionScript3. I am having some trouble in developing some effects e. g. bomb blast, particle effects etc. I want to know, whether I can use After Effects for those and then use them the game. Please suggest me.
There's no problem of using any graphics data from any program in Flash as long as flash can support the file format.
In case of After Effects graphics, you'll probably have to export them as a PNG file sequence, so they'll most probably be pretty large in file size.
To import:
You can either make a movie clip and just import the first image of a sequence with CRTL/CMD + R and Flash with be "smart" enough to suggest to load the other files of that sequence.
If you want, you can load them during run-time, add them to the array/vector, create a Bitmap object and change it's bitmapData property on enter frame/timer to animate it.
If you are decided on using the actual After Effects graphics rather than emulating them using an Actionscript 3 particle effect, I'd actually recommend exporting them as video on an alpha channel and then converting that to flv and then hosting them individually and streaming them in via Netstream and placing them on the proper layer via an addchild. If bandwidth isn't a huge concern here, this would be a good method for achieving this effect.
Here is some documentation on Netstream. http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d4e.html
If this seems like it might achieve the desired result, I can explain further if needed.

ActionScript 3: How to move a TextField smoothly?

In my program I have a feature of text scrolling across the screen. This works fine, except for the unbelievable laggy movement. I'm simply adding the speed to the x-position of the textfield, and the movement animation works fine for all other objects (movieclips, bitmaps etc).
EDIT:
I now tried to convert the text to a BitMap, and then move it. Unfortunately this resulted in the same "laggy" movement with lots of sudden "jumps".
bmd = new BitmapData (event_field.width, event_field.height, true, 0);
bmd.draw (event_field);
bm = new Bitmap (bmd);
bm.x = event_field.x;
bm.y = event_field.y;
bm.cacheAsBitmap = true;
bm.smoothing = true;
this.addChild(bm);
In my enter-frame-function:
bm.x-=3
Does anyone have a solution for this?
Not smoothing the textfield may improve performance, but using the native TextField class in my experience, has performance limitations that can't really be overcome. Real-time rendering of text is expensive. Real-time rendering that text to bitmapData, can also be expensive and is really only beneficial if the text doesn't change much. In any case that's something cacheAsBitmap should already be doing automatically.
To overcome both of these issues, you should consider bitmap fonts. These are pre-rendered, solving both of the above issues. However, the native API doesn't support it.
The two options that I'm aware of:
BMFontRenderer. Some classes that supposedly provide support
for bitmap font rendering. I've not tried this so I can't vouch for
it.
Use Starling for your whole project, which includes
excellent support for bitmap fonts and distance field fonts
(scalable bitmap fonts). It's a 2D hardware accelerated framework,
so this is just the tip of the iceberg in terms of what it can
offer. I use it for everything now, but for your project it may be too late to be switching from the native framework to another.
To generate your bitmap fonts, these are some tools recommended by the Starling Manual. I personally use Littera and BMFont:
Littera
BMFont
Glyph Designer
bmGlyph
You can try embedding font or animate bitmap not textfield
create snapshot of textfield before animation starts,
hide textfield
animate snapshot
at animation finish move invisible textfield into destination, destroy snapshot and show textfield

Is it possible to warp a Youtube Player Object (Flash/ActionScript 3)

I'd like to be able to warp a YouTube Player object in Flash around a sphere shape.
Is this possible?
No. Youtube player or similar objects use StageVideo, this thing is in a separate layer than everything else, including Stage3D. You can try grabbing that video each frame and making a texture out of that to draw over that sphere, I expect it'll be very slow if at all possible.
Also, "wrap", not "warp".
If instead of using the Youtube player you got a video URL you could try to use a VideoTexture with Away3D. Truth be told, depending on the resolution of the video that will probably be a resource hog.
Here's a tutorial with a plane instead of a sphere, but it should be pretty similar.
http://www.ilike2flash.com/2012/11/videotexture-with-away3d-4.html
I've had more success with projecting video on 3d meshes using C++ frameworks like Cinder or OpenFrameworks.

What does Chrome use for accelerated 2D vector graphics in its HTML5 canvas draw functions?

So i would like to do things like what is possible with a HW accelerated HTML5 canvas for animated 2D vector graphics drawing, but on top of my OpenGL (4.x) rendered 3D scene (for complex HUD and GUI displays). I need this to be able to work on Win7+, MacOs, and Linux, mobile platform support is not needed.
BTW I am working with C++.
I was wondering if anyone knew what for example Chrome uses for accelerated 2D vector graphics in its HTML5 canvas draw functions? I was under the impression it was accelerated using ANGLE (which wraps OpenGL or DX9). Or am I wrong and its only SVG rendering that is accelerated, not the javascript canvas draw functions.
Doing HTML5 canvas style animated 2D vector graphics with OpenGL is highly non-trivial, is Google using an available library for that or is it just in-house code?
I have been looking into OpenVG and have had a hard time finding the right implementation to use for that, so far the only thing i can actually get examples compiled for is ShivaVG (but there seems to be shimmering artifacts for the tiger demo and other issues for the latest release 7 years ago). Also i think ShivaVG is using fixed function and my team decide to lock down our OpenGL usage to 4.x core profile, so that won't work. I would love to use NV_Path_Rendering but its not portable (to anything other than a nvidia accelerated device).
I also thought using OpenVG would be useful since I might be able to hide NV_Path underneath, or a new OpenVG library that might come out in the future. But I am wondering if OpenVG's future might be in peril.
They apparently use the Skia library for all 2D rendering.

Native Flash rendering vs Starling Framework rendering for bejeweled style web game

I am creating a bejeweled style game using flash builder for the web and I am wondering if it would be best (higher framerates, less render intensive, fast on all types of computers) to use the native rendering of actionscript 3 (display lists) or the gpu rendering of the Starling 2D Framework? My reasoning for wanting to use the native flash rendering is because from my research I understand that flash will utilize redraw regions and only redraw portions of the stage that need to be updated whereas starling will re-render the entire stage every frame. In a bejeweled style game there are many static objects and fewer dynamic objects so generally the entire stage does not need to be redrawn every frame, only small portions of the stage. Would it be better for performance to stick with the native rendering calls of actionscript 3 or would it be wiser to use the starling framework to utilize the gpu to accelerate the rendering? Any perspective would be much appreciated. Thanks.
Starling will be faster if you are using images, particularly on mobile.
The static regions can either be rendered as a single Sprite, or you can flatten it at runtime to improve the performance.
If you are drawing, in Starling you'll need to draw to a bitmap and then use that as a Sprite. You'll get better performance if everything is in the same sprite sheet, however.
While it's true that Starling draws every frame, it's a different sort of operation. Here's what actually happens:
1) The textures (images) being used by the app are uploaded to the GPU. This is done once.
2) Each frame, Starling tells the GPU "render this texture at this position with this scale and rotation". This is called a "draw call" but it's extremely fast. The slick thing with Starling is that it can batch these draw calls under certain circumstances to get serious speed improvements.
So yes, everything is "redrawn" each frame, but the GPU's hardware acceleration is used.
On mobile, Starling (or similar) is really the only way to do something at 60fps.