Game Development Community

Dynamic masking

by Argiris Bendilas · in Torque Game Builder · 06/26/2007 (4:00 am) · 15 replies

Hi everyone.

I was wondering if there is a way to apply dynamic masking to an object, similarly to how Flash does.

Assume you have an object on screen and you want to use a circle as a mask, so that when you move the circle around, some parts of the object are shown and some are hidden, all in real-time.

Can this be implemented and if so, how?

Thank you in advance.

#1
06/26/2007 (5:59 am)
Can be done, just needs the correct src and dst blend factors and you need to make sure that the circle is at the "top layer" and everything else behind it ...
Stuff thats outside the mask (ie does not collide with it) is hidden
What is inside is shown.

The mask is pure black with white in the middle or if you want alpha, with black and full alpha on the outside an alpha 0 in the circle.

You just need to make sure that your mask object is large enough to hide potential objects that are shown at the moment.
#2
06/26/2007 (9:16 am)
Hi Marc.

Thank you for your reply.

Do you mind giving me an exact example of how that would work? I played around with the blend factors but the combinations are too many.

Besides, do I change the blend factors of just the mask or the object I want masked as well?
#3
06/27/2007 (1:00 am)
You would change the blend factors only for the mask.
As the mask image is on the topmost layer it will be above all others.

The needed combination for this case is "multiply blend"
src: GL_DST_COLOR
dst: GL_ZERO

this will darken.

If you don't change the factors although you can use image alpha as the default setup blend factors are for alpha blend which allows you full control over "see through the mask". So I would suggest using this one.


All in all this won't be a simple "clicks solution", you will need to add cast collision tests, handle sets of hidden and visible objects and the like ...

the only 2 ways I could think of making this simpler in the long run or if needed for other things as well:

1. write the mask to the accumulation buffer if you own Pro and use that one (note that especially the lower end cards do not feature accum buffers)
2. add shader support and use a pixel shader to achieve this effect (again a problem for lower end cards)
#4
06/27/2007 (2:06 pm)
I'm not sure that's the same kind of masking being refered to. Alpha control works fine in Torque, but true image masking would be the ability to use a mask image to hide part of another image, without the mask being visible or obscuring the background. For instance:

Background, Object, and Mask:
img165.imageshack.us/img165/5181/backgroundpx5.png img523.imageshack.us/img523/471/objectpv5.png img165.imageshack.us/img165/4719/maskiz3.png
Layout of Pieces, and Result:
img267.imageshack.us/img267/8616/layoutvr8.png img267.imageshack.us/img267/5731/resultzw2.png
As far as I can tell, this would not be possible just using blend modes, although it might work to an extent by using stacked scenewindows.
#5
06/28/2007 (10:43 am)
Kalle, that's what I'm trying to achieve.

Are these images from Torque or an image editing program?
#6
06/29/2007 (4:04 pm)
These were just image editted, I'm looking into adding it to Torque, but need to do some more research.

If anyone's done something like this, I'd be interested to hear how; right now I'm thinking that DynamicTexture might be a good starting place, but there may be an easier way.
#7
07/02/2007 (2:39 am)
If you open up the "TGB" example project there is a level called "blending.t2d". Open her up and you will see exactly what you are looking for ;)
#8
07/02/2007 (12:47 pm)
Hi Phillip.

Are you referring to TGB 1.5 by any chance?
#9
07/02/2007 (2:41 pm)
Interesting thing... i was thinkin on askin the same thing, in flash its easily done, but here, in TGB i have no idea... so, i repeat the question, are you talkin about the TGB 1.5 beta 3 by any chance?... what demo?
#10
07/02/2007 (3:09 pm)
No it is refering to 1.1.3, game "TGB" -data-levels, blending.t2d

the other t2ds there are of use as well if you have never played with them.
#11
07/02/2007 (4:08 pm)
Yeah, TGB 1.1.3, sorry I didn't mention.
#12
07/03/2007 (9:05 am)
The problem with stock blending solutions is that there is currently no way to limit the blend to a subset of objects short of creating independent scenegraphs, unless I've just not come across the right combination of blend settings.

For example, you can't (in blending.t2d) have one cloud be invisible until the 'spotlight' sprite passes over it but have the rest of the sprites and the background visible all the time.

A month or two ago I ended up adding a stencil buffer implementation that works fairly well and wasn't too complicated. Any object can be designated as writing to the stencil buffer or rendered through the current stencil buffer as a mask. The most time consuming part of this was enabling all the different objects (3dmesh, 2Dvector, textobject, etc.) since most of them have their own render method, and to get it to work on D3D I had to fix a couple of old holes in the D3D wrapper.

I did some research to make sure my existing min. hardware requirements wouldn't get thrown off, but stencil buffer support in hardware is pretty ubiquitous, and has been for a number of years so it worked out well.
#13
07/03/2007 (2:32 pm)
It would be great if this were added to Torque directly, as a supported feature of the engine. I've wanted to do this, and from this thread, I think it's pretty obvious that others have wanted to as well... But apparently you have to modify the engine as Luke has to get it. (Luke, is your solution cross-platform?)
#14
07/03/2007 (2:38 pm)
It should be cross-platform safe (both OGL and D3D support) but I haven't done a build on Mac or Linux yet to verify it. I've been waiting for the 1.5 release to roll the changes in before dealing with other platform builds. But there is no reason why it wouldn't work assuming the hardware supports stencil buffers.
#15
01/22/2009 (9:19 am)
Luke D I was wondering if you made your stencil buffer implementation available. If not would you mind sending it to me? I would really like to see what you did. I am currently working on a project that requires masking and would greatly appreciate if you could make that resource available.