TSE Simple Full Screen Shader Effect
by decryptoid · 11/15/2005 (2:49 pm) · 61 comments
Download Code File
Remember to back up your files before using this example code!
Files... You may also download the files here:
Code Files
Download the code file and unzip the files as follows:
-- Unzip fullscreenEffects.cs into example/[yourmod directory]/client/scripts/
where [yourmod directory] is the name of your mod directory
-- Unzip all four .hlsl files to the /example/shaders/
-- Unzip both gameTSCtrl files to /engine/game/
Now Compile!...
Script Changes...
-- Open example/[yourmod directory]/client/init.cs and add the following code right after the IdleTimeout line...
-- Open example/[yourmod directory]/client/scripts/default.bind.cs and add the following code to the end:
Now Delete all your dso files... if you don't know how to do this just do a search on your mod directory for "*.dso" and delete all the files.
Run the game... you should be done!
Pressing "v" on the keyboard should toggle between heatvision, nightvision and regular vision.
About the shaders...
The shaders (hlsl files) are very simple. You can open them up in a text editor and see how they work. All they really do is convert the image to black and white then invert for heatvision and add all color channels to green for nightvision.
About the engine changes
Look at the new gameRenderEffect function in gameTSCtrl.cpp file for most of the changes. It's basically a copy of the regular GameRender function, but it renders to a surface then back to the screen with the shader.
What else you can do...
-- Add Bitmap "static" to the shaders
-- Write a cool custom screen shader!!
-- Look at the other shaders in the shaders directory to see how they work
-- Look in other parts of the engine code to see how to pass additional values to a custom shader. All the glowbuffer stuff is good for this
-- Modify this example code to be more dynamic. The two shaders are kinda hardcoded in there and it would be better to pass shader names in a script... that way you wouldn't have to re-compile for every shader change
NOTE: Since every one was waiting I made this example fairly quick. There may be unused variables or errors I didn't catch. Hopefully I included all the right files!
Problems:
-- The game will crash if the two shaders do not get loaded. This will happen if you put the exec line for the fullscreenEffect.cs file in the wrong place.
Remember to back up your files before using this example code!
Files... You may also download the files here:
Code Files
Download the code file and unzip the files as follows:
-- Unzip fullscreenEffects.cs into example/[yourmod directory]/client/scripts/
where [yourmod directory] is the name of your mod directory
-- Unzip all four .hlsl files to the /example/shaders/
-- Unzip both gameTSCtrl files to /engine/game/
Now Compile!...
Script Changes...
-- Open example/[yourmod directory]/client/init.cs and add the following code right after the IdleTimeout line...
// full screen effects
exec("./scripts/fullscreenEffects.cs");It is important that you place it immediately after the IdleTimeout line because the fullscreenEffects file must be loaded before the PlayGui is loaded or it will not work.-- Open example/[yourmod directory]/client/scripts/default.bind.cs and add the following code to the end:
//---------------------------------------
// fullscreen shader effects
//---------------------------------------
moveMap.bind(keyboard, "v", toggleEffectMode);
function toggleEffectMode(%val)
{
if(%val)
{
PlayGui.toggleEffectMode(0);
}
}Now Delete all your dso files... if you don't know how to do this just do a search on your mod directory for "*.dso" and delete all the files.
Run the game... you should be done!
Pressing "v" on the keyboard should toggle between heatvision, nightvision and regular vision.
About the shaders...
The shaders (hlsl files) are very simple. You can open them up in a text editor and see how they work. All they really do is convert the image to black and white then invert for heatvision and add all color channels to green for nightvision.
About the engine changes
Look at the new gameRenderEffect function in gameTSCtrl.cpp file for most of the changes. It's basically a copy of the regular GameRender function, but it renders to a surface then back to the screen with the shader.
What else you can do...
-- Add Bitmap "static" to the shaders
-- Write a cool custom screen shader!!
-- Look at the other shaders in the shaders directory to see how they work
-- Look in other parts of the engine code to see how to pass additional values to a custom shader. All the glowbuffer stuff is good for this
-- Modify this example code to be more dynamic. The two shaders are kinda hardcoded in there and it would be better to pass shader names in a script... that way you wouldn't have to re-compile for every shader change
NOTE: Since every one was waiting I made this example fairly quick. There may be unused variables or errors I didn't catch. Hopefully I included all the right files!
Problems:
-- The game will crash if the two shaders do not get loaded. This will happen if you put the exec line for the fullscreenEffect.cs file in the wrong place.
#22
Here's how you get this working in TGEA1.0:
First replace your gameTSCtrl.cpp and gameTSCtrl.h with the files included in this resource.
Open your new gameTSCtrl.cpp and find the constructor:
Select everything inside of it and remove it so it looks like
Now take that code you just removed and create a new function with it..:
Note the change from "static_cast" to "dynamic_cast"
Find GameTSCtrl::renderWorldEffect. Within that function find this line:
Now above GameTSCtrl::consoleInit() place this code:
Inside ::consoleInit, add this line:
Now open example/client/init.cs and find
Clean. Compile. Run.
Now... if you added the commands to your default.bind.cs you should be able to cycle through your shaders by pressing the "v" key. Don't forget to add the shader definitions. I just C&P'd them into common/client/shaders.cs.
Hope this helped!
EDIT: Don't forget to visit my project and register as a thanks! www.versusthegame.com The best indie game you never heard of. :)
02/24/2007 (12:00 am)
Heheh.. gameTSCtrl is being initialized as soon as the engine starts in TGEA1.0. This resource attempts to load shaders prior to the creation of the GFX object. Soooo... a little rearranging gets this working with no issues.Here's how you get this working in TGEA1.0:
First replace your gameTSCtrl.cpp and gameTSCtrl.h with the files included in this resource.
Open your new gameTSCtrl.cpp and find the constructor:
GameTSCtrl::GameTSCtrl()
Select everything inside of it and remove it so it looks like
GameTSCtrl::GameTSCtrl()
{
}Now take that code you just removed and create a new function with it..:
Note the change from "static_cast" to "dynamic_cast"
void GameTSCtrl::initShaders()
{
// COGMOD022307: initShaders() will allow us to initialize the shader effects via scripting.
//--------------------------------------------------------------------------
// hanna
// fullscreen shader effects
//--------------------------------------------------------------------------
mNightEffectShader = NULL;
mHeatEffectShader = NULL;
effectMode = 0;
mFilterColor = ColorF(1,1,1,1);
//initialize the shaders and get the video mode...
//COG: should be a dynamic_cast now
mNightEffectShader = dynamic_cast<ShaderData*>(Sim::findObject( "nightVision" ) );
if( mNightEffectShader )
{
mNightEffectShader->initShader();
Con::errorf("Night Effect Vision LOADED!!");
}else{
Con::errorf("*** Night Effect Vision Shader ERROR - Shader Data not found!!");
}
//COG: should be a dynamic_cast now
mHeatEffectShader = dynamic_cast<ShaderData*>(Sim::findObject( "heatVision") );
if( mHeatEffectShader )
{
mHeatEffectShader->initShader();
Con::errorf("Heat Effect Vision LOADED!!");
}else{
Con::errorf("Heat Effect Vision Shader ERROR - no shader data!!");
}
GFXVideoMode mode = GFX->getVideoMode();
mSurface[0].set( mode.resolution.x, mode.resolution.y, GFXFormatR8G8B8A8, &GFXDefaultRenderTargetProfile, 1 );
}Find GameTSCtrl::renderWorldEffect. Within that function find this line:
GFX->startActiveRenderSurface( mSurface[0] );and replace it with these 2 lines of code
GFX->pushActiveRenderSurfaces(); GFX->setActiveRenderSurface(mSurface[0] );Find
GFX->endActiveRenderSurface();and replace it with
GFX->popActiveRenderSurfaces();
Now above GameTSCtrl::consoleInit() place this code:
static void InitShaders(SimObject *obj,S32,const char **argv)
{
//COGMOD022307: scripting hooks
GameTSCtrl *ctrl = static_cast<GameTSCtrl*>(obj);
ctrl->initShaders();
}Inside ::consoleInit, add this line:
Con::addCommand("GameTSCtrl", "initShaders", InitShaders, "GameTSCtrl.initShaders(value)", 3, 3);Now open example/client/init.cs and find
loadMaterials(); initRenderInstManager();and ABOVE that add
PlayGui.initShaders(0);
Clean. Compile. Run.
Now... if you added the commands to your default.bind.cs you should be able to cycle through your shaders by pressing the "v" key. Don't forget to add the shader definitions. I just C&P'd them into common/client/shaders.cs.
Hope this helped!
EDIT: Don't forget to visit my project and register as a thanks! www.versusthegame.com The best indie game you never heard of. :)
#23
02/25/2007 (5:37 pm)
Thanks Bryce for that however, I am still getting the 'GameRenderFilters': identifier not found error. Did you copy over the GameRenderFilter function and all the needed parts from an earlier build to get yours working in 1.0?
#24
02/25/2007 (8:18 pm)
just comment that out. It works fine even without that.
#25
.cpp
This section here is confusing:
Ive tried it with this left in: GameTSCtrl::GameTSCtrl()
{
}
And completely removed, and even tried putting that new initshaders code inside it...
Can you upload your .cpp file? Or am I missing something from the header file?
Cheers
addikt
02/26/2007 (2:07 am)
Error:gameTSCtrl.cpp
..\engine\game\gameTSCtrl.cpp(41) : error C2039: 'initShaders' : is not a member of 'GameTSCtrl'
j:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\#TGEARC1\engine\game/gameTSCtrl.h(28) : see declaration of 'GameTSCtrl'
..\engine\game\gameTSCtrl.cpp(48) : error C2065: 'mNightEffectShader' : undeclared identifier
..\engine\game\gameTSCtrl.cpp(49) : error C2065: 'mHeatEffectShader' : undeclared identifier
..\engine\game\gameTSCtrl.cpp(50) : error C2065: 'effectMode' : undeclared identifier
..\engine\game\gameTSCtrl.cpp(51) : error C2065: 'mFilterColor' : undeclared identifier
..\engine\game\gameTSCtrl.cpp(58) : error C2227: left of '->initShader' must point to class/struct/union/generic type
type is ''unknown-type''
..\engine\game\gameTSCtrl.cpp(71) : error C2227: left of '->initShader' must point to class/struct/union/generic type
type is ''unknown-type''
..\engine\game\gameTSCtrl.cpp(81) : error C2065: 'mSurface' : undeclared identifier
..\engine\game\gameTSCtrl.cpp(81) : error C2228: left of '.set' must have class/struct/union
..\engine\game\gameTSCtrl.cpp(364) : error C2039: 'initShaders' : is not a member of 'GameTSCtrl'
j:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\#TGEARC1\engine\game/gameTSCtrl.h(28) : see declaration of 'GameTSCtrl'.cpp
//----------------------------------------------------------------------------
// Class: GameTSCtrl
//----------------------------------------------------------------------------
IMPLEMENT_CONOBJECT(GameTSCtrl);
void GameTSCtrl::initShaders()
{
//--------------------------------------------------------------------------
// hanna
// fullscreen shader effects
//--------------------------------------------------------------------------
mNightEffectShader = NULL;
mHeatEffectShader = NULL;
effectMode = 0;
mFilterColor = ColorF(1,1,1,1);
//initialize the shaders and get the video mode...
mNightEffectShader = dynamic_cast<ShaderData*>(Sim::findObject( "nightVision" ) );
if( mNightEffectShader )
{
mNightEffectShader->initShader();
Con::errorf("Night Effect Vision LOADED!!");
}else{
Con::errorf("*** Night Effect Vision Shader ERROR - Shader Data not found!!");
}
mHeatEffectShader = dynamic_cast<ShaderData*>(Sim::findObject( "heatVision" ) );
if( mHeatEffectShader )
{
mHeatEffectShader->initShader();
Con::errorf("Heat Effect Vision LOADED!!");
}else{
Con::errorf("Heat Effect Vision Shader ERROR - no shader data!!");
}
GFXVideoMode mode = GFX->getVideoMode();
mSurface[0].set( mode.resolution.x, mode.resolution.y, GFXFormatR8G8B8A8, &GFXDefaultRenderTargetProfile, 1 );
}
//---------------------------------------------------------------------------This section here is confusing:
Quote:
Select everything inside of it and remove it so it looks like
GameTSCtrl::GameTSCtrl()
{
}
Now take that code you just removed and create a new function with it..:
Note the change from "static_cast" to "dynamic_cast"
Ive tried it with this left in: GameTSCtrl::GameTSCtrl()
{
}
And completely removed, and even tried putting that new initshaders code inside it...
Can you upload your .cpp file? Or am I missing something from the header file?
Cheers
addikt
#26
02/26/2007 (6:35 am)
I've emailed you my gameTSCtrl files.
#27
I added the correct line to the client>init.cs and to the default.bind...
Edit: Ive doubled/Triple checked everything. The shader profiles are loading before the playGUI and the controls are showing up in the config after setting them in the default.bind... weird
Any ideas?
addikt
02/26/2007 (12:51 pm)
Thanks heaps Matt, I got that compiled perfectly. Now the only issue I have is that when I press 'v' nothing happens, and when I press it again the game crashes... :(I added the correct line to the client>init.cs and to the default.bind...
Edit: Ive doubled/Triple checked everything. The shader profiles are loading before the playGUI and the controls are showing up in the config after setting them in the default.bind... weird
Any ideas?
addikt
#28
Try typing PlayGui.setEffectMode(1); in the console
02/26/2007 (5:24 pm)
And you added the shaders? Hmm.Try typing PlayGui.setEffectMode(1); in the console
#29
Is it working for you guys?
Maybe we can compare files....
addikt
02/26/2007 (6:05 pm)
It crashes when I type that in the console... Is it working for you guys?
Maybe we can compare files....
addikt
#30
02/26/2007 (6:50 pm)
Yes. If you used my files directly it should have worked. You've created the shaderdata I'm assuming?
#31
Edit: I tried loading the shader data in shaders.cs and moved the exec of shaders.cs up the top of the initClient function in the client > init.cs... but still crashes...
02/26/2007 (7:30 pm)
How do you mean? I have the shader files in the shader folder and have init the fullscreenshader.cs as per the recommended place in the client>init.csEdit: I tried loading the shader data in shaders.cs and moved the exec of shaders.cs up the top of the initClient function in the client > init.cs... but still crashes...
#32
Hdr etc?
addikt
02/27/2007 (7:02 pm)
Quick question what graphics options do you guys have selected?Hdr etc?
addikt
#33
02/27/2007 (7:04 pm)
I don't. That might affect it. HDR doesn't work for me, so I haven't tried with it. I think HDR is broken on 7800s. I did have this running on MS 4.0 with full HDR and Bloom without a problem. Not sure about now though.
#35
I had misread this instruction and place the line in the wrong section of the init.cs
All working now, I would like to thank Bryce and Matt for putting up with my 'dumbness' for lack of a better word. Im very happy :)
addikt
02/28/2007 (1:20 am)
Ok, im officially spending too many latenights working on this lol, I found the problem:Quote:Now open example/client/init.cs and find
loadMaterials();
initRenderInstManager();
and ABOVE that add
PlayGui.initShaders(0);
I had misread this instruction and place the line in the wrong section of the init.cs
All working now, I would like to thank Bryce and Matt for putting up with my 'dumbness' for lack of a better word. Im very happy :)
addikt
#36
Any ideas why I am getting this and could you please also send me you cpp file so I can compare?
Many thanks.
03/12/2007 (2:28 pm)
Hi, I'm getting the same compile errors as addiktive ('initShaders' : is not a member of 'GameTSCtrl' etc).Any ideas why I am getting this and could you please also send me you cpp file so I can compare?
Many thanks.
#37
my playgui.initShaders(0); seems to work fine (I get the 2 LOADED messages). However, when I seteffectmode(1) my screen goes completely white and when I seteffectmode(2) my screen goes black.
It is *trying* to apply it I think! The gui is still visible, as is the Kork label as he endlessly runs round in circles. If I seteffectmode(0); everything returns to normal.
Any thoughts, people?
Thanks...
03/13/2007 (11:09 am)
Hi again, Matt's been kind enough to send me his cpp and it is now compiling. However, I have quizzed him about a new issue I have and also thought I'd post it here.my playgui.initShaders(0); seems to work fine (I get the 2 LOADED messages). However, when I seteffectmode(1) my screen goes completely white and when I seteffectmode(2) my screen goes black.
It is *trying* to apply it I think! The gui is still visible, as is the Kork label as he endlessly runs round in circles. If I seteffectmode(0); everything returns to normal.
Any thoughts, people?
Thanks...
#38
03/13/2007 (2:23 pm)
I think I may have found the root of the problem. Sorry, but the code I sent you had fragments of some of the culling code I've been working with in it. I'm sending you an updated version.
#39
03/14/2007 (8:25 am)
Massive thanks to both Matt and Addiktive on this one - it's now working a treat! Thanks guys.
#40
05/06/2007 (3:50 pm)
could someone send me the updated files for 4.0 please? 
addiktive
addikt