Greyscale shader with object highlighting (PostFX)
by Jack Stone · 07/01/2014 (7:38 pm) · 6 comments
This resource is a very useful addition for highlighting objects using a PostFX shader.
When turned on, the screen will change to black and white, except for designated objects which will appear as bright red.
This could be used for night vision, thermal vision, highlighting objectives or special items in a game, highlighting ammunition or health pickups, etc.
The effect can be switched on and off at will.
More Images can be seen here:
http://phoenixgamedevelopment.com/blog/?p=727
The way this system works is by setting the diffuse color of the objects to be highlighted to pure red, by using this line:
%mat.diffuseColor[0] = "255 0 0 255";
and then searching for that specific color in the shader by using this line:
if(ret.r > 0.98 && ret.g < 0.09){
When it is found, that pixel is set to red, while the rest are set to black and white. This is essentially a "green screen" type concept. As long as no other object in the game has exactly the same colour that the shader is replacing, this should work well.
A limitation of the system is that the selection of objects is done per material, not per object. Which means that multiple instances of the same object, which share the same material, will either be all highlighted or not highlighted at all. To circumvent this, it is possible to just create two version of the same object, with the same texture, but two different materials. Then, highlight one, but not the other.
Another possible limitation is that when using the "selected = 1" dynamic variable to select multiple objects, objects inside a simgroup will not be highlighted unless there is another object sharing the same material outside the simgroup.
To implement this resource, first download the code file from here: (2KB, .rar file).
http://www.phoenixgamedevelopment.com/downloads/ObjectHighlightingResource.rar
Place the shaders "objectshighlightP.hlsl" and "objecthighlightV.hlsl" into:
shaders/common/postFx
Place "ObjectHighlightingPostEffect.cs" into:
"scripts/client/postFX/"
Place "ObjectHighlighting.cs" into:
"scripts/server"
and execute is from "scripts/server/scriptExec.cs" by calling:
exec("./objecthighlighting.cs");
Using the effect is simple.
To highlight a single object, call:
enableselection(%obj);
and to switch back to regular vision call:
disableselection(%obj);
A more useful way to use this would be to add a dynamic variable called "selected" with a value of "1" to a group of objects in your scene. This can be done using the mission editor, or by calling: "%obj.selected = 1;" Bearing in mind, as mentioned before, that highlighting is done per-material, not per object.
Then, simply call:
enableselectionall(); and disableselectionall();
To enable and disable the effect.
When turned on, the screen will change to black and white, except for designated objects which will appear as bright red.
This could be used for night vision, thermal vision, highlighting objectives or special items in a game, highlighting ammunition or health pickups, etc.
The effect can be switched on and off at will.
More Images can be seen here:
http://phoenixgamedevelopment.com/blog/?p=727
The way this system works is by setting the diffuse color of the objects to be highlighted to pure red, by using this line:
%mat.diffuseColor[0] = "255 0 0 255";
and then searching for that specific color in the shader by using this line:
if(ret.r > 0.98 && ret.g < 0.09){
When it is found, that pixel is set to red, while the rest are set to black and white. This is essentially a "green screen" type concept. As long as no other object in the game has exactly the same colour that the shader is replacing, this should work well.
A limitation of the system is that the selection of objects is done per material, not per object. Which means that multiple instances of the same object, which share the same material, will either be all highlighted or not highlighted at all. To circumvent this, it is possible to just create two version of the same object, with the same texture, but two different materials. Then, highlight one, but not the other.
Another possible limitation is that when using the "selected = 1" dynamic variable to select multiple objects, objects inside a simgroup will not be highlighted unless there is another object sharing the same material outside the simgroup.
To implement this resource, first download the code file from here: (2KB, .rar file).
http://www.phoenixgamedevelopment.com/downloads/ObjectHighlightingResource.rar
Place the shaders "objectshighlightP.hlsl" and "objecthighlightV.hlsl" into:
shaders/common/postFx
Place "ObjectHighlightingPostEffect.cs" into:
"scripts/client/postFX/"
Place "ObjectHighlighting.cs" into:
"scripts/server"
and execute is from "scripts/server/scriptExec.cs" by calling:
exec("./objecthighlighting.cs");
Using the effect is simple.
To highlight a single object, call:
enableselection(%obj);
and to switch back to regular vision call:
disableselection(%obj);
A more useful way to use this would be to add a dynamic variable called "selected" with a value of "1" to a group of objects in your scene. This can be done using the mission editor, or by calling: "%obj.selected = 1;" Bearing in mind, as mentioned before, that highlighting is done per-material, not per object.
Then, simply call:
enableselectionall(); and disableselectionall();
To enable and disable the effect.
About the author
#3
07/02/2014 (10:19 pm)
Wow, that looks kind of great. Nice work!
#4
*tips hat* 8-}
07/02/2014 (10:50 pm)
Oh wow ... how cool is this. Thanks for this ... kudos to you.*tips hat* 8-}
#5
Script changes:
%mat=%obj.gettargetname(%i); return only the target. I added the getMaterialMapping to get the material:
Missing samplerNames in GrayScaleShaderSD:
in objecthighlightP.hlsl
in objecthighlightV.hlsl
With those changes it work like a charm. Thanks
03/01/2015 (5:13 pm)
I had some problem implementing this ressource to T3D v3.6.3. I had to make some changes to the scripts and the shader.Script changes:
%mat=%obj.gettargetname(%i); return only the target. I added the getMaterialMapping to get the material:
%target=%obj.gettargetname(%i); %mat = getMaterialMapping(%target);
Missing samplerNames in GrayScaleShaderSD:
singleton ShaderData( GrayScaleShaderSD )
{
DXVertexShaderFile = "shaders/common/postFx/objecthighlightV.hlsl";
DXPixelShaderFile = "shaders/common/postFx/objecthighlightP.hlsl";
***ADD START***
samplerNames[0] = "$inputTex";
***ADD END***
pixVersion = 3.0;
};Shader changes: I needed to add the declarations for variables.in objecthighlightP.hlsl
#include "./postFx.hlsl" ***ADD START*** uniform float4 targetViewport; ***ADD END*** float4 main( PFXVertToPix IN, uniform sampler2D inputTex : register(S0) ) : COLOR [...]
in objecthighlightV.hlsl
#include "./postFx.hlsl"
#include "./../torque.hlsl"
***ADD START***
uniform float4 rtParams0;
uniform float oneOverTargetSize;
***ADD END***
struct Vert
{
float4 pos : POSITION;
float2 tc : TEXCOORD0;
};With those changes it work like a charm. Thanks
#6
http://www.garagegames.com/community/blogs/view/22772
03/01/2015 (5:19 pm)
Well I just notice there's an updated ressource for this... But the post date is beforehttp://www.garagegames.com/community/blogs/view/22772

Torque Owner Richard Ranft
Roostertail Games