Adjusting the objects transparency level
by Guimo · 12/08/2006 (10:30 am) · 10 comments
I was programming my game and found that I needed to control the alpha level used to render my objects. I mean, setting an object so it is 50% transparent or 20% transparent so I can see behind it. But unfortunately Torque only provides with some cloaking functions for this and thats all.
I looked for similar resources in the GG site but found nothing useful on this subject. So I decided to hack into the problem and found its something easy to solve so here I present a solution for this.
Note this change is for client side presentation only and has absolutely no effect in the simulation. The state change is not propagated (ghosted) to the other players or the server. This is the intended behaviour but if you want to propagate this value you may add some pack/unpack methods to the shapebase class or send some clientCommand/serverCommand messages, but that is not the intended usage. I find it a lot more useful to be able to control the visibility in an independent client than in all the simulation.
This solution covers non static objects only, but you may expand the idea easilly for static objects.
THEORY
The presence of cloaking functions in the TGE suggest it has support for setting alpha levels so I started my quest searching the engine code for 'cloak' and found some lines of code. One of those lines was like this one:
So, its not hard to think that the setAlphaAlways method has something to do with setting transparency levels so after searching this word my suspicion was confirmed, in order to make it clearer here is a small snippet found in the player.cc code:
Which can be read like: If you are cloacking set the rendering alpha level to a cloaking level, else set the alpha level to 1.0f. Then if we can set an AlphaAlways level to other than 1 we are done.
SOLUTION
The solution is to add an attribute to the ShapeBase class, expose that attribute to the console and use this attribute when rendering. Also, we must tell the engine that in prerender preparation, this object should be considered a transparent object if the value is lower than 1.
In the following changes a (+) means you should add the line to the code. A (-) means you should remove or comment this line.
First, lets create out attribute to hold the transparency value. As every rendering object derives from the ShapeBase class lets start by creating an attribute there. Open shapebase.h and find and add:
As this is a protected attribute, in the same file find and add the following accesor methods:
Now open the shapebase.cc and in the ShapeBase constructor find and add:
Then in the same shapebase.cc file find the method ShapeBase::setHidden(bool hidden) and right after that function add:
Ok, now we have an attribute to hold the transparency level. In order to to expose the method to the console, go to the end of the ShapeBase.cc file and add:
We are ready for final modifications. In the Player.cc and ShapeBase.cc files, find calls to setAlphaAlways(1.0) and set them to setAlphaAlways(mTransparencyLevel). THIS IS AN IMPORTANT STEP SO DONT MISS IT!
Note that if you do a full seach of setAlphaAlways(1.0) you will also find a reference for setAlphaAlways(1.0) in the tsStatic.cc. This is the one you may start exploring if you want to affect static shapes as well but this is out of the scope of this resource.
And the final touch... in ShapeBase.cc locate the method bool ShapeBase::prepRenderImage. In this method the engine decides which objects are going to be rendered and if the engine should consider them transparent. In this method, find the two lines with the following condition and update them as:
Finally locate the two lines with the following condition and update them as:
Ok, thats enough. Recompie the engine and run your game.
USAGE
In the command window you may write something like:
%myobject.setTransparencyLeve(0.5);
2132.setTransparencyLeve(0.2);
Of course you should use a reference for the object in your simulation. You will see your object as a ghost shape in your game.
BENEFITS
Now you can control the transparency of the object by using code. You dont have to create an animation in order to set the transparency as sometimes is suggested.
You can use this for many scenarios. Maybe you want to create a pair of xRay googles which allows your player and only your player (not everyone in the network) to see through a ghostly wall where your enemies hide.
This may also be used for a different cloaking effect in an RTS where your opponents just wont see your units at all but you still can see your units at a 50% transparency. Just set the alpha level for your units and command the server to tell the other players to ghost your units at 5% level. When you decloak you may command the server to tell everybody to set alpha to 100%
For me... I wanted to render an object using the GUIObjectView control and set the object transparency level so I still can see the scene through it and this resource allows this completely.
Create Illusion spells where only one player sees a dragon comming and starts attacking him while the others can only guess why the player is running or shooting wildly. You may even set the illusion over other player and hide that player so the affected one attacks and tries to kill the innocent bystander.
For the bolder, this feature allows you to show something in the simulation while you may hide it for the others. So you may render a special 3D GUI for one player while the others wont be able to see it at all.
IMPROVEMENTS
This resource covers only changing a non static object but you can cover the TSStatic object as well.
Also, IMHO this feature is enough to override the Cloak and Fade effects restrictions and allows a better control of the transparency.
Also, I wanted to expose the attribute creating a initPersistFields method but it just didn't work. Maybe someone with more experience may do this.
SCREENSHOT
This is a shot of my current prototype called Starquake. Just for demonstration I have composed this image from 80%, 50% and 20% transparency levels of one of the game ships.
I looked for similar resources in the GG site but found nothing useful on this subject. So I decided to hack into the problem and found its something easy to solve so here I present a solution for this.
Note this change is for client side presentation only and has absolutely no effect in the simulation. The state change is not propagated (ghosted) to the other players or the server. This is the intended behaviour but if you want to propagate this value you may add some pack/unpack methods to the shapebase class or send some clientCommand/serverCommand messages, but that is not the intended usage. I find it a lot more useful to be able to control the visibility in an independent client than in all the simulation.
This solution covers non static objects only, but you may expand the idea easilly for static objects.
THEORY
The presence of cloaking functions in the TGE suggest it has support for setting alpha levels so I started my quest searching the engine code for 'cloak' and found some lines of code. One of those lines was like this one:
image.shapeInstance->setAlphaAlways(0.04 + (1 - mCloakLevel) * 0.96);
So, its not hard to think that the setAlphaAlways method has something to do with setting transparency levels so after searching this word my suspicion was confirmed, in order to make it clearer here is a small snippet found in the player.cc code:
if (image.dataBlock->cloakable && mCloakLevel != 0.0) image.shapeInstance->setAlphaAlways(0.04 + (1 - mCloakLevel) * 0.96); else image.shapeInstance->setAlphaAlways(1.0);
Which can be read like: If you are cloacking set the rendering alpha level to a cloaking level, else set the alpha level to 1.0f. Then if we can set an AlphaAlways level to other than 1 we are done.
SOLUTION
The solution is to add an attribute to the ShapeBase class, expose that attribute to the console and use this attribute when rendering. Also, we must tell the engine that in prerender preparation, this object should be considered a transparent object if the value is lower than 1.
In the following changes a (+) means you should add the line to the code. A (-) means you should remove or comment this line.
First, lets create out attribute to hold the transparency value. As every rendering object derives from the ShapeBase class lets start by creating an attribute there. Open shapebase.h and find and add:
bool mHidden; ///< in/out of world + F32 mTransparencyLevel; //the transparency level of the object
As this is a protected attribute, in the same file find and add the following accesor methods:
bool isHidden() { return mHidden; }
+ //Sets the object transparency level
+ void setTransparencyLevel(F32 lvl);
+ F32 getTransparencyLevel() { return mTransparencyLevel; };Now open the shapebase.cc and in the ShapeBase constructor find and add:
mHidden = false; + mTransparencyLevel = 1.0f;
Then in the same shapebase.cc file find the method ShapeBase::setHidden(bool hidden) and right after that function add:
void ShapeBase::setTransparencyLevel(F32 lvl)
{
if(lvl>1.0f) lvl = 1.0f;
if(lvl<0.0f) lvl = 0.0f;
mTransparencyLevel = lvl;
}Ok, now we have an attribute to hold the transparency level. In order to to expose the method to the console, go to the end of the ShapeBase.cc file and add:
ConsoleMethod( ShapeBase, setTransparencyLevel, void, 3, 3, "(float lvl)")
{
F32 lvl = dAtof(argv[2]);
object->setTransparencyLevel(lvl);
}
ConsoleMethod( ShapeBase, getTransparencyLevel, F32, 2, 2, "")
{
return object->getTransparencyLevel();
}We are ready for final modifications. In the Player.cc and ShapeBase.cc files, find calls to setAlphaAlways(1.0) and set them to setAlphaAlways(mTransparencyLevel). THIS IS AN IMPORTANT STEP SO DONT MISS IT!
Note that if you do a full seach of setAlphaAlways(1.0) you will also find a reference for setAlphaAlways(1.0) in the tsStatic.cc. This is the one you may start exploring if you want to affect static shapes as well but this is out of the scope of this resource.
And the final touch... in ShapeBase.cc locate the method bool ShapeBase::prepRenderImage. In this method the engine decides which objects are going to be rendered and if the engine should consider them transparent. In this method, find the two lines with the following condition and update them as:
-if (mCloakLevel == 0.0f && image.shapeInstance->hasSolid() && mFadeVal == 1.0f) +if (mCloakLevel == 0.0f && image.shapeInstance->hasSolid() && mFadeVal == 1.0f && mTransparencyLevel == 1.0f)
Finally locate the two lines with the following condition and update them as:
-if ((mCloakLevel != 0.0f || mFadeVal != 1.0f || mShapeInstance->hasTranslucency()) || +if ((mCloakLevel != 0.0f || mFadeVal != 1.0f || mShapeInstance->hasTranslucency()) || mTransparencyLevel != 1.0f ||
Ok, thats enough. Recompie the engine and run your game.
USAGE
In the command window you may write something like:
%myobject.setTransparencyLeve(0.5);
2132.setTransparencyLeve(0.2);
Of course you should use a reference for the object in your simulation. You will see your object as a ghost shape in your game.
BENEFITS
Now you can control the transparency of the object by using code. You dont have to create an animation in order to set the transparency as sometimes is suggested.
You can use this for many scenarios. Maybe you want to create a pair of xRay googles which allows your player and only your player (not everyone in the network) to see through a ghostly wall where your enemies hide.
This may also be used for a different cloaking effect in an RTS where your opponents just wont see your units at all but you still can see your units at a 50% transparency. Just set the alpha level for your units and command the server to tell the other players to ghost your units at 5% level. When you decloak you may command the server to tell everybody to set alpha to 100%
For me... I wanted to render an object using the GUIObjectView control and set the object transparency level so I still can see the scene through it and this resource allows this completely.
Create Illusion spells where only one player sees a dragon comming and starts attacking him while the others can only guess why the player is running or shooting wildly. You may even set the illusion over other player and hide that player so the affected one attacks and tries to kill the innocent bystander.
For the bolder, this feature allows you to show something in the simulation while you may hide it for the others. So you may render a special 3D GUI for one player while the others wont be able to see it at all.
IMPROVEMENTS
This resource covers only changing a non static object but you can cover the TSStatic object as well.
Also, IMHO this feature is enough to override the Cloak and Fade effects restrictions and allows a better control of the transparency.
Also, I wanted to expose the attribute creating a initPersistFields method but it just didn't work. Maybe someone with more experience may do this.
SCREENSHOT
This is a shot of my current prototype called Starquake. Just for demonstration I have composed this image from 80%, 50% and 20% transparency levels of one of the game ships.
#2
12/09/2006 (4:39 pm)
Thank you very much... I didn't know how to do that. Updated.
#3
01/20/2007 (9:57 pm)
Nice. I'd like to give it a shot someday.
#4
Have compiled with no errors, and can set TransparencyLevel from the console with no errors, but it takes no effect to the shape.
04/26/2007 (6:58 am)
Have tryed to implement it with TGE 1.4+ TLK 1.4 but with no effect.Have compiled with no errors, and can set TransparencyLevel from the console with no errors, but it takes no effect to the shape.
#5
Are you sure you have already done the modification in Player.cc and ShapeBase.cc files, find calls to
Also, make sure you have modified the ShapeBase.cc as the resource shows.
If you dont do everything then it wont work.
Luck!
Guimo
06/28/2007 (8:02 am)
Sorry for the late answer. I implemented it using TGE 1.4 + TLK 1.4 and it works perfectly.Are you sure you have already done the modification in Player.cc and ShapeBase.cc files, find calls to
... setAlphaAlways(1.0)and set them to
... setAlphaAlways(mTransparencyLevel).
Also, make sure you have modified the ShapeBase.cc as the resource shows.
If you dont do everything then it wont work.
Luck!
Guimo
#6
09/22/2007 (8:20 am)
Has anyone got this to run with TGE 1.5.x ?
#7
I'm getting the same resault as Alex. I compiled with no errors and can also set TransparencyLevel with no errors, but i get no transparency effect. I also rechecked every step you did above, with no luck.
Thanks.
Michael
10/01/2007 (2:44 am)
Hi Guimo,I'm getting the same resault as Alex. I compiled with no errors and can also set TransparencyLevel with no errors, but i get no transparency effect. I also rechecked every step you did above, with no luck.
Thanks.
Michael
#8
12/09/2007 (5:26 pm)
Its possible take the same effect with diff buildings or objects?
#9
This resource is client side only. You cannot set the transparency level in the server and expect it to be passed to the clients automatically.
This means the server should send a command to the client object using commandToClient and passing the ghostid. In the client the ghostid should be translated and that unit is used with this command in the client.
This is the intended usage as you may want a unit to be more transparent in a client than the other. For instance I wanted a ship to be invisible in a client and semitransparent in the other so I sent a command which set the transparency to 0 in one client and 0.5 in the other.
I'm really sorry for that. Now I want to test it so it works in 1.5
Luck!
Guimo
04/24/2008 (4:15 pm)
I was going through this resource and I noticed that I forgot to mention something extremely important.This resource is client side only. You cannot set the transparency level in the server and expect it to be passed to the clients automatically.
This means the server should send a command to the client object using commandToClient and passing the ghostid. In the client the ghostid should be translated and that unit is used with this command in the client.
This is the intended usage as you may want a unit to be more transparent in a client than the other. For instance I wanted a ship to be invisible in a client and semitransparent in the other so I sent a command which set the transparency to 0 in one client and 0.5 in the other.
I'm really sorry for that. Now I want to test it so it works in 1.5
Luck!
Guimo
#10
i think that this resource works:
www.garagegames.com/community/resources/view/17443
but i need only client side, mmm, ill think how to do that, maybe only call a setskinName in the x client, something like (p-seudo code):
%client.%object.setSkinName("objectTrans");
or call a clientCmdCommand...
08/23/2009 (3:26 pm)
do you know how make this work in TGEA?i think that this resource works:
www.garagegames.com/community/resources/view/17443
but i need only client side, mmm, ill think how to do that, maybe only call a setskinName in the x client, something like (p-seudo code):
%client.%object.setSkinName("objectTrans");
or call a clientCmdCommand...

Associate Orion Elenzil
Real Life Plus
it'd be even nicer if you edited it so that the code parts are inside "code" tags.
- it's easier to read that way. eg [ c o d e ] (code here) [ / c o d e ]. - but without the extra spaces.