Game Development Community

dev|Pro Game Development Curriculum

TGEA Fullscreen Fx Shader

by Frank Bignone · 08/29/2007 (12:19 pm) · 15 comments

Download Code File

This resource will let you add fullscreen fx shaders inside your TGEA codebase (TGEA 1.0.2).

First you define your own shader, for example:
new ShaderData( HotSpotFxShader )
{
	DXVertexShaderFile	= "shaders/canvasfx/hotSpotV.hlsl";
	DXPixelShaderFile	= "shaders/canvasfx/hotSpotP.hlsl";
	pixVersion 			= 2.0;
};
and then you activate it through some console commands:
function doFxSpot()
{
	canvas.setCanvasFxVars($spotFxVars[0] SPC $spotFxVars[1] SPC $spotFxVars[2] SPC $spotFxVars[3]);
	canvas.setCanvasFx(HotSpotFxShader);
	canvas.startCanvasFx();
}
The first command setCanvasFxVars is to pass some parameters to your shaders. You can setup up to 16 float values that can be used lated inside your shaders. You can also setup up to two textures that can be used for effect, plus set-up a number generator if you have some special effect requiring ramp number generator.
canvas.setCanvasFxGen(-1,1,1000);
	canvas.setCanvasFxMaps("shaders/canvasfx/noise.dds", "shaders/canvasfx/Random3D.dds");

Concerning the number generation, the first two parameters are min / max value and the third one is the time of one period.

The shaders can use also some parameters passed directly to him, like:
// CanvasFX vertex shader constants
#define VC_CANVAS_P1		4	// Parameter 1
#define VC_CANVAS_P2		5	// Parameter 2
#define VC_CANVAS_P3		6	// Parameter 3
#define VC_CANVAS_P4		7	// Parameter 4
#define VC_SIMTIME		8	// Simulation time (1st linear, 2nd cos, 3rd sin)
#define VC_GENTIME		9	// Generated time (1st linear, 2nd cos, 3rd sin)
#define VC_MOUSE_POS		10            // Mouse position (absolute pixel)
// Enf of CanvasFx constants

// CanvasFX pixel shader constants
#define PC_CANVAS_P1		0	// Parameter 1
#define PC_CANVAS_P2		1	// Parameter 2
#define PC_CANVAS_P3		2	// Parameter 3
#define PC_CANVAS_P4		3	// Parameter 4
#define PC_SIMTIME		4	// Simulation time (1st linear, 2nd cos, 3rd sin)
#define PC_GENTIME		5	// Generated time (1st linear, 2nd cos, 3rd sin)
#define PC_MOUSE_POS		6	// Mouse position (relative 0..1)
// Enf of CanvasFx constants

Included in the code example, I have put the sepia tones effect:

gallery.indie-zone.com/main.php?g2_view=core.DownloadItem&g2_itemId=74&g2_serialNumber=1

and also the spot rendering effect which highlights the screen around the mouse pointer (like a flash light effect).

gallery.indie-zone.com/main.php?g2_view=core.DownloadItem&g2_itemId=71&g2_serialNumber=1

plus a wave effect. To activate them, just use the following script command:
doFxSepia();
  doFxSpot();
  doFxWave();

As an example of what you can do: old tv effect (add some noise on your screen), local deformation, thermal rendering...

One very important note concerning the vertices used to draw the screen texture. By default, the quad is not subdivided, but you can change the #define INCR_FX to subdivide it (defined in guiCanvas.cpp). It is important if you are doing fx that are working with the vertex buffer and need to change the vertices slightly. I usually use 20 as a default value (in current basecode it is set to 1).

There are certainly some bugs and issues, feel free to upgrade it and post your comments here.

[edit]
Adding missing shdrConsts.h file inside the archive.
Making the archive more friendly with winzip-like tool.

About the author

Real programmers don't waste time recompiling; they patch the binary files... ... Real programmers don't waste time patching binary files; they patch memory.


#1
08/21/2007 (10:24 am)
this archive will not open with winzip....please use winrar or 7-zip
#2
08/21/2007 (11:50 am)
Awesome. This will do a lot of good for a lot of people.
#3
08/30/2007 (9:01 am)
Cool, although this has already been done before, unless this is an enhancement to that.
#4
08/30/2007 (3:33 pm)
This is a pretty big enhancement. Being able to modify specific parameters in TorqueScript, the use of a texture lookup, there are some very spiffy things that enhance it.
#5
08/30/2007 (7:53 pm)
There are some differences between the two resources. First, we do not upgrade the same class, one is based on gameTSCtrl, this one is based on Canvas. Then this resource allows you also to do some funny things inside the vertex shader and not only pixel shader as the screen is tesselated too. Last things as mentionned Matt, is that you have more control on the shaders: position of mouse, generated time, user parameters, additionnal textures, and of course not hardcoded effect.

One drawback is as Canvas is upgraded, the effect will apply on all elements displayed on your screen (3D world + GUI) which may be the next upgrade of this resource to have some GUI not affected by this effect...
#6
08/31/2007 (8:01 am)
For test, here are the two shaders from the previous resources.

Create a file called nightVisionP.hlsl inside shaders/canvasfx:
#define IN_HLSL
#include "../shdrConsts.h"
//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
struct ConnectData
{
   float2 tex0            : TEXCOORD0;

};

struct Fragout
{
   half4 col : COLOR0;
};

float3 grayScaleWeights				: register(PC_CANVAS_P1);
float3 saturation				    : register(PC_CANVAS_P2);

//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform sampler2D diffuseMap0  : register(S0)
              )
{
   Fragout OUT;

   float4 texelColor =  tex2D( diffuseMap0, IN.tex0 );

   // makes it b&w
   float4 scaledColor = texelColor * float4( grayScaleWeights, 1.0 );
   float luminance = scaledColor.r + scaledColor.g + scaledColor.b ;

   float4 bnw = float4( luminance * saturation, texelColor.a );
   
   OUT.col = bnw;
   
   return OUT;
}

Create a file called heatVisionP.hlsl inside shaders/canvasfx:
#define IN_HLSL
#include "../shdrConsts.h"
//-----------------------------------------------------------------------------
// Structures                                                                  
//-----------------------------------------------------------------------------
struct ConnectData
{
   float2 tex0            : TEXCOORD0;

};


struct Fragout
{
   half4 col : COLOR0;
};

float3 grayScaleWeights				: register(PC_CANVAS_P1);

//-----------------------------------------------------------------------------
// Main                                                                        
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
              uniform sampler2D diffuseMap0  : register(S0)
)
{
   Fragout OUT;

   float4 texelColor =  tex2D( diffuseMap0, IN.tex0 );

   // makes it b&w
   float4 scaledColor = texelColor * float4( grayScaleWeights, 1.0 );
   float luminance = scaledColor.r + scaledColor.g + scaledColor.b ;
   luminance = 1 - luminance;
   
   OUT.col = float4( luminance , luminance, luminance, texelColor.a ); // + (staticColor*0.5); 

   return OUT;
}

and add the following to shaders.cs inside client/scripts.cs
new ShaderData( HeatVisionFxShader )
{
	DXVertexShaderFile	= "shaders/canvasfx/defaultV.hlsl";
	DXPixelShaderFile	= "shaders/canvasfx/heatVisionP.hlsl";
	pixVersion 			= 2.0;
};

$heatVisionFxVars[0] = "0.30 0.59 0.11 0";
$heatVisionFxVars[1] = "0 0 0 0";
$heatVisionFxVars[2] = "0 0 0 0";
$heatVisionFxVars[3] = "0 0 0 0";

function doFxHeatVision()
{
	canvas.setCanvasFxVars($heatVisionFxVars[0] SPC $heatVisionFxVars[1] SPC $heatVisionFxVars[2] SPC $heatVisionFxVars[3]);
	canvas.setCanvasFx(HeatVisionFxShader);
	canvas.startCanvasFx();
}

new ShaderData( NightVisionFxShader )
{
	DXVertexShaderFile	= "shaders/canvasfx/defaultV.hlsl";
	DXPixelShaderFile	= "shaders/canvasfx/nightVisionP.hlsl";
	pixVersion 			= 2.0;
};

$nightVisionFxVars[0] = "0.30 0.59 0.11 0";
$nightVisionFxVars[1] = "1 3 1 0";
$nightVisionFxVars[2] = "0 0 0 0";
$nightVisionFxVars[3] = "0 0 0 0";

function doFxNightVision()
{
	canvas.setCanvasFxVars($nightVisionFxVars[0] SPC $nightVisionFxVars[1] SPC $nightVisionFxVars[2] SPC $nightVisionFxVars[3]);
	canvas.setCanvasFx(NightVisionFxShader);
	canvas.startCanvasFx();
}
#7
09/04/2007 (2:36 am)
Ah, I see now. That does sound superior to the original full-screen shader resource. I especially like what you said in regard to the vertex shader being available for per-vertex effects.

Just as a note, I haven't looked at any code, but you may be able to separate the fullscreen effect from the world and the GUI. Since, IIRC, the Canvas renderer is setup with some sort of stand alone 3D rendering(might have to do weird type casts tho).
#8
09/05/2007 (8:18 pm)
Cool thanx
#9
09/10/2007 (7:38 am)
Nice resource, thx thx.
#10
12/29/2007 (4:01 am)
This is a great resource, only gripe is that it also applies the shader effect on any of your GUIs that are visible.
#11
12/30/2007 (12:29 am)
I have one release of the resource which allow to apply different shaders to visible GUI and TSControl. When I will have time, I will update it.
#12
02/11/2008 (9:25 am)
Any updates on this? Having shader seperate from GUI....

Or even better applying a shader to specific GUI's? Am I pushing my luck here, or what? :-P
#13
03/19/2008 (7:04 am)
So. Bad news =(

RenderSurface calls no longer exist in the 1.7 Beta. There is a new ActiveRenderTarget system that has replaced it. My rendering code knowledge is below average, but I've already taken a crack at it. I got the full screen shader to work, but the Z Buffer is completely FUBAR.

If someone wants to take a shot at it, I can pass on what I know. I gotta get back to writing docs, but would love to see a new FS Shader resource, or have this one updated to the new 1.7 Beta.
#14
03/22/2008 (5:11 am)
I will look at it next month when i will merge code of Pelorea and new release of TGEA.
#15
07/11/2008 (2:10 pm)
Hi!! and how about a motion blur screen?