AS3 Stage3D: how to render all triangles with an overall alpha? - actionscript-3

I am working on a 2D AS3 project, where the different layers are rendered via Stage3D, in a single drawTriangles() call, as a single mesh.
(if you are familiar with the BunnyMark GPUSprite mini render engine, that will give you an idea: http://www.bytearray.org/?p=4074)
What I would like is to draw one of these entire 'render layers', with an overall alpha transparency value, that would apply to ALL triangles drawn, adding to their own alpha values.
IE I am not looking to change the alpha by using textures with alpha transparency or via having to go through setting each triangle separately to have the same alpha: I want a master switch that would affect the alpha value of everything that is drawn? ( something computationally cheap)
Is this possible? Perhaps via a Shader, or a setProgramConstantsFromVector command?

Well you could render your layer to a texture, then you could put this texture on the stage with the alpha you want, this way your whole layer would have the same alpha!
You can do this in two ways, the first way is to render it to a texture, and display that texture with Stage3D and create a shader that adjusts the alpha.
The second way is rendering it to a bitmapData, and displaying it on the normal stage. I'll give an example for this way:
var bmd:BitmapData = new BitmapData(renderWidth, renderHeight, true, 0);
var bit:Bitmap = new Bitmap(bmd);
bit.alpha = 0.5;
addChild(bit);
//In your render loop at the beginning
context3D.renderToBitmapData(bmd);

Related

Drawing Rectangle images-LibGdx

In my first LibGdx Project,I want to draw some rectangles.
I am not looking for shape rendering purpose.I am aiming to implement a function like what fillRect() in j2me do.I have to draw filled rectangles and need to manipulate it(changing size,rotating.. etc).
When I google about it, always getting shapeRenderer related things only.
Please mention how can I draw and manipulate my own images.
Draw Rectangle by using Pixmap.
Texture texture=getPixmapTexture(Color.WHITE);
Sprite sprite=new Sprite(texture); //Used for drawing 2D sprites.
//or
Image image=new Image(texture); //2D scene graph node.
public static Texture getPixmapTexture(Color color){
return new Texture(PixmapBuilder.getPixmapRectangle(1, 1, color));
}
public static Pixmap getPixmapRectangle(int width, int height, Color color){
Pixmap pixmap=new Pixmap(width, height, Pixmap.Format.RGBA8888);
pixmap.setColor(color);
pixmap.fillRectangle(0,0, pixmap.getWidth(), pixmap.getHeight());
return pixmap;
}
The answer by Abhishek is correct.
However, if you have just started game developement with LibGDX, I would check whether you need at all to perform such operation (draw a rectangle).
In libGDX you can use Scene2D which allow you to create a Stage, Actors and direct them on your stage.
So instead of drawing a rectangle, you create an actor, such as an image, to which you can associate a texture, a button or a TextBox and place it on your screen.
Scene2D allows you to then use things like Action or rotation, scaling..
There are some good visual demos about that on Libgdx.info
I am mentioning this because moving to Scene2D later may be more complicated than if you make that decision early on.

Removing Color from Sprite ActionScript

My project loads SWF files using the Loader class, and then loads Sprite objects from those child files. Each sprite has a specific green color, and I want to substitute all pixels of one color for transparent ones.
I can re-compile these Sprites as objects of the BitmapData class, to simplify things, but I'm looking for any way that avoids operating the objects pixel-by-pixel. I'm guessing that I can use some kind of bitmap filter, but I'm just not familiar enough with ActionScript to know specifics.
try threshold with bitmapData of that Sprite.
Before                           After √
var bmd:BitmapData = new BitmapData(sprite.width, sprite.height, true, 0x0);
bmd.draw(sprite);
bmd.threshold(bmd, bmd.rect, new Point(), "==", 0xff00ff00); // 0xff00ff00 = GREEN
you may change the green color, also you are free to setting more range of colours to be transparented, for example, some kind of detecting smooth corners like this:
just need to play with operation argument of threshold, i used "==", but many others available.
refrences :
Adobe
Doc

Storing the result of blended images in AS3.0

I am making an interactive animation which background images are masked by a shape captured in a camera. In the every frames, camera changes images into black and white images and then it's used to mask background images.
In the code, "now" is the images captured by camera, "P1" is the back ground image.
After I masked by using blend mode multiply, I wish to store the result image which is masked and use for other things.
I don't understand when I use blendmode function, how the result is defined well.
Can I capture, copy or store the result image?
var P1:MovieClip = new p1();
var mskimg:MovieClip = new maskimage();
var bitmap_obj:Bitmap = new Bitmap(now);
addChild(P1);
P1.blendMode = BlendMode.LAYER;
addChild(bitmap_obj);
bitmap_obj.blendMode = BlendMode.MULTIPLY;
You can draw a masked object if you put everything that you intend to be drawn into a container, then call BitmapData.draw() to draw the complete layered object to some BitmapData. Then, you can use that bitmap data to do whatever you want, Graphics.beginBitmapFill(), FileReference.save(), whatever. The operation is however expensive, so it'll be better if you trigger the drawing by responding to a user action, and not do that every frame by default, unless required by your application's logic.

Canvas, negative coordinates: Is it bad to draw paths that start off canvas, and continue on?

I only want to show a portion of a shape drawn on a canvas.
My line is essentially this, and it works fine:
ctx.fillRect( xPosition, rectHeight - offsetV , rectWidth, rectHeight);
The second variable there is going to be negative. So, my quesiton is: is it bad practice (or am I setting myself for errors down the road) to draw a path that starts off the canvas (with a negative coordinate) and then continue drawing on to the canvas.
No problem at all. If you have very large number of drawing object you can (like GameAlchemist said) prevent drawing that object .If you use canvas like map for explore (zoom out/in ctx, translate whole context) that preventing draw can cost more that clip cost. And its complicated ...
I have some expire with drawing object out of canvas. You can have a problem if you put calculation and other (no drawing) staff intro draw function.
Important :
-Make canvas draw function code clear(only draw canvas code).
-If your app no need for const update make update call only when it needs.
-Clear canvas only in (0,0,canvas.w,canvas.h)
-Use style only when it needs (stroke,fill,font etc.)

AS3 Is it possible to duplicate a Shape object?

I'm trying to make a shape available for duplicating. Here's an explanation of what I've done, what I'm trying to do, and where I'm stuck:
Drew a shape manually in the Flash IDE (paintbrush).
Created a new movieclip containing the shape; exports as a class.
Instantiate the class (var mc:MovieClip = new shapeMovieClip()).
Add a reference to the shape in mc that I want (var myShape:Shape = mc.getChildAt(0) as Shape;
This works perfect and I now have my shape, but how can I duplicate it without instantiating the parent MovieClip class - is it possible?
I've tried creating a new shape and using the copyFrom() graphics method with no avail, I believe this method just copies draw calls when they're made on the referenced graphics and instead I just have a reference of a shape that's already been drawn.
Right now I'm extending the movieclip as a sprite instead, instantiating, pulling the shape from the parent and saving its reference, and then nulling the sprite. Does anyone know of a better, more lightweight, strategy for duplicating Shapes in this manner?
Basically depends on whether you need to scale your shapes. If you don't, and you can work it out with a fixed sized bitmap representation of the shape, then you will get much better performance drawing your shape to a BitmapData (it's called rasterisation) and instanciating Bitmap objects (as other commenters have pointed out). The code would go something like this:
var base:Sprite = new shapeMovieClip();
var bmd:BitmapData = new BitmapData(base.width, base.height, true, 0);
bmd.draw(base);
var clip1:Bitmap = new Bitmap(bmd);
var clip2:Bitmap = new Bitmap(bmd);
If you do need to scale the clips, you will get pixelation using bitmaps. When scaling down Bitmap.smoothing can help to some extent (also when rotating), but if you need to scale up, you will probably have to use some kind of mip-mapping. This is basically creating a few bitmaps of the shape at different scale levels, and then swap them depending on the current scale. Coding this has some complexity (using a helper parent to adjust the scale can help) but it will definitely perform better than using many shape symbols (with or without a sprite parent).
This is very old, but it still comes up high in Google, so I just wanted to share a true shape duplicating method:
var shapeonstage:Shape = shapeMadeInIDE;
var g:Vector.<IGraphicsData> = shapeonstage.graphics.readGraphicsData();
var shapecopy:Shape = new Shape();
shapecopy.graphics.drawGraphicsData(g)
And boom. It works. Had to share this because it would have helped me a looooong time ago and in so many ways.
UPDATE:
There is some clarification I'd like to add why you would want to use this method, for duplicating AND for storing references to Shapes that are within an swf.
If you target a Shape object on the stage or in a movie clip, the Flash Rendering engine does something strange. It will RECYCLE Shape objects to render new graphics thus making your Shape reference point to a COMPLETELY different rendering.
For example:
Create an fla with a movieclip.
Inside the movie clip make 10 frames.
On each frame, draw a different shape
In your code, get a reference to the shape (The Shape on Frame 1)
Draw and verify your shape (draw to a bitmap then put the bmp on stage)
Now let the flash engine play for 5 frames
Draw and verify your shape again
The second time you draw your shape without EVER reassigning your shape reference, will SOMETIMES yield a completely different shape.
Just know, this little quirk can leave you pulling your hair out if you don't know what you're looking for :)