Stealth aka distorsion Post FX - Need Help
by elvince · in Torque 3D Professional · 01/21/2010 (4:28 am) · 13 replies
Hi,
My goal is to create a stealth effect. In fact it should create a texture on the model that will be transparent & slight distorsion.
It like a "invisible" cloak. I don't want the model to be fully invisible. I found a set of fx used in xna that do exactly what I want with a postfx shader.
creators.xna.com/fr-FR/sample/distortion
I have difficulty to implement it correctly and the documentation around PostFx is light at the moment.
Once it's done, I will share the changes in a resource, as I think it's a nice effect that may be useful for a lot of games.
I took some time to tweak up my code and get some results.
First I apply a custom material to my model to get the distorsion map based on normals:
Vertex
Next in PostFX Pixel Shader I do:
I started from the selection postFx ressource: http://www.torquepowered.com/community/resources/view/17821
In selection buffer I have the model mapped with the distortion map and all the rest in black.
result:

If you look, I just have a displacement of the background to the left and inverted on Y axis :(
If someone can help me, I will appreciate?
More in Post FX, what is the rendertime/renderbin I should set to get the screen texture with not my models drawn in it?
For the moment I'm PFXAfterDiffuse, so the model is with the mapped displacement texture. I try to put if fully transparent with Alpha = 0 but it is rendered. It might an option, but it's hard to get information in the current documentation.
My goal is to create a stealth effect. In fact it should create a texture on the model that will be transparent & slight distorsion.
It like a "invisible" cloak. I don't want the model to be fully invisible. I found a set of fx used in xna that do exactly what I want with a postfx shader.
creators.xna.com/fr-FR/sample/distortion
I have difficulty to implement it correctly and the documentation around PostFx is light at the moment.
Once it's done, I will share the changes in a resource, as I think it's a nice effect that may be useful for a lot of games.
I took some time to tweak up my code and get some results.
First I apply a custom material to my model to get the distorsion map based on normals:
#define IN_HLSL
#include "../shdrConsts.h"
#include "../hlslStructs.h"
float4 DistortionScale = 0.0003f;
VertexIn_PT main( VertexIn_PN IN, uniform float4x4 modelview : register(VC_WORLD_PROJ) )
{
VertexIn_PT OUT;
OUT.pos = mul(modelview,IN.pos);
float3 normalWV = IN.normal;
float amount = dot(normalWV, float3(0,0,1)) * DistortionScale;
OUT.uv0 = float2(.5,.5) + float2(amount * normalWV.xy);
return OUT;
}Vertex
float4 main(float2 displacement : TEXCOORD0) : COLOR
{
return float4(displacement, 0, 1);
}Next in PostFX Pixel Shader I do:
I started from the selection postFx ressource: http://www.torquepowered.com/community/resources/view/17821
In selection buffer I have the model mapped with the distortion map and all the rest in black.
#include "postFx.hlsl"
#include "shadergen:/autogenConditioners.h"
const float ZeroOffset = 0.5f / 255.0f;
float4 main( PFXVertToPix IN,
uniform sampler2D selectionBuffer :register(S0),
uniform sampler2D backBuffer : register(S1),
uniform float2 targetSize : register(C0) ) : COLOR0
{
float4 DistorsionMap=tex2D( selectionBuffer,IN.uv0);
// Look up the displacement
float2 displacement = DistorsionMap.rg;//tex2D(DistortionMap, IN.uv0).rg;
float4 finalColor = 0;
// We need to constrain the area potentially subjected to the gaussian blur to the
// distorted parts of the scene texture. Therefore, we can sample for the color
// we used to clear the distortion map (black). We used 0 to avoid any potential
// rounding errors.
if ((displacement.x == 0) && (displacement.y == 0))
{
finalColor = tex2D(backBuffer, IN.uv0);
}
else
{
// Convert from [0,1] to [-.5, .5)
// .5 is excluded by adjustment for zero
displacement -= .5 + ZeroOffset;
// Look up the displaced color, without multisampling
finalColor = tex2D(backBuffer, IN.uv0.xy + displacement);
}
return finalColor;
}result:

If you look, I just have a displacement of the background to the left and inverted on Y axis :(
If someone can help me, I will appreciate?
More in Post FX, what is the rendertime/renderbin I should set to get the screen texture with not my models drawn in it?
For the moment I'm PFXAfterDiffuse, so the model is with the mapped displacement texture. I try to put if fully transparent with Alpha = 0 but it is rendered. It might an option, but it's hard to get information in the current documentation.
About the author
Recent Threads
#2
I believe you're using a selection to render the player and the rest is black. This is good for edge detection.
I think you should render to texture the scene without the player.
Now having both maps you can cut the environment of the second map and apply blur/heat-haze in the specified area...
01/24/2010 (6:54 am)
Only a selection will not do the job.I believe you're using a selection to render the player and the rest is black. This is good for edge detection.
I think you should render to texture the scene without the player.
Now having both maps you can cut the environment of the second map and apply blur/heat-haze in the specified area...
#3
Any tips, or thing I should look to understand?
I also note that my displacement map from my normal is not ok, but I will work on it after.
01/24/2010 (8:07 am)
Thanks, I understand the principle. How do you render the scene without the player? I know that I can #name a texture during shader/postfx, but I don't know which one I should call to get the scene texture. I'm lost with the renderbin & so on.Any tips, or thing I should look to understand?
I also note that my displacement map from my normal is not ok, but I will work on it after.
#4
There you will see how filtering is applied.
There you see the AddInstResult - arAdded, arSkipped, arStop
You must skip only the player.
01/24/2010 (9:10 am)
Go to addElement() in your bin.There you will see how filtering is applied.
There you see the AddInstResult - arAdded, arSkipped, arStop
You must skip only the player.
#5
In fact, I can get a good result if I test in RenderPrePassMgr,RenderMeshMgr if my current matInst is "stealth" if so I skipped.
With this trick, the mesh is not rendered in the backbuffer which is good.
I'm wondering if I can play with renderOrder and send a arStop in my StealthManager and then avoid any other bin to get this mesh?
thanks
01/24/2010 (1:39 pm)
This tip was very useful.In fact, I can get a good result if I test in RenderPrePassMgr,RenderMeshMgr if my current matInst is "stealth" if so I skipped.
With this trick, the mesh is not rendered in the backbuffer which is good.
I'm wondering if I can play with renderOrder and send a arStop in my StealthManager and then avoid any other bin to get this mesh?
thanks
#6
It means the order should be at least 1.4+
It depends on what you want to achieve.
01/24/2010 (2:12 pm)
You should render on top of everything,because you will have trouble with translucent objects.... shadows..It means the order should be at least 1.4+
It depends on what you want to achieve.
#7
This seems to be ugly to implement thing like that.
Ideally I need to get the mesh texture before it get rendered to backbuffer and then apply a distorsion on backbuffer once everything had been rendered!
Seems so simple but so hard to understand how to get things done. T3D handles thing differently than tgea so most of post on postfx or so are outdated and no official doc available:( i hope it Will be done with 1.1beta!
01/24/2010 (3:13 pm)
Ok, so i must tweak all render before mine to avoid them rendering my mesh is stealth is on.This seems to be ugly to implement thing like that.
Ideally I need to get the mesh texture before it get rendered to backbuffer and then apply a distorsion on backbuffer once everything had been rendered!
Seems so simple but so hard to understand how to get things done. T3D handles thing differently than tgea so most of post on postfx or so are outdated and no official doc available:( i hope it Will be done with 1.1beta!
#8
Or it could be a CustomMaterial... Maybe adding a flag to the render instance that signifies it is cloaked and then the RenderMeshBin uses the cloak material rather than the regular one ( but only during the diffuse pass ).
That sounds pretty hacky though, this should really be done through the Material / ShaderGen / Feature system.
03/13/2010 (7:56 pm)
This sounds like something that should be done as a shader-gen feature, not a PostEffect. Or it could be a CustomMaterial... Maybe adding a flag to the render instance that signifies it is cloaked and then the RenderMeshBin uses the cloak material rather than the regular one ( but only during the diffuse pass ).
That sounds pretty hacky though, this should really be done through the Material / ShaderGen / Feature system.
#9
Could you explain le briefly how shader gen is working (what s rhe use of it) and i Will look in to this?
Thanks,
03/14/2010 (4:31 am)
In fact this is not possible in post fx. I did a lot of thing without any success. In fact i was always blocked by thé fact that thé normal pre processing needs to be done to get the mesh corretly drawn but as this using the back Buffet i m stuck as i Will not be able to get the diffuse Color of the background. Could you explain le briefly how shader gen is working (what s rhe use of it) and i Will look in to this?
Thanks,
#10
But James is right, it would probably take less effort to do it as a shadergen feature.
03/14/2010 (8:50 am)
Through creating a new render bin, and not rendering the selected meshes in other bins, you could go the postfx way. Using some wavy texture and the backbuffer, I'm sure some pretty neat results could be achieved. Perhaps you wouldn't really need normal preprocessing, and the depth buffer would suffice to finetune the effect. In that case, it could be made as a postfx.But James is right, it would probably take less effort to do it as a shadergen feature.
#11
But as the depth processing wrote in the back buffer the transparency Will show up this depth map if i use a postfx.
I Will try with a material with waving but i need that the texture is auto generate and don t rely on a image file to avoid hacking.
I m Still not sure to undrstand what s a shader gen but i Will look in the code to see if i can Find some example.
03/14/2010 (12:10 pm)
In fact i did create a new rendr bin as in your selection bin. But as the depth processing wrote in the back buffer the transparency Will show up this depth map if i use a postfx.
I Will try with a material with waving but i need that the texture is auto generate and don t rely on a image file to avoid hacking.
I m Still not sure to undrstand what s a shader gen but i Will look in the code to see if i can Find some example.
#12
03/15/2010 (8:53 am)
Probably the best example is by Pat Wilson and you can find it on the GameClay website.
Torque Owner elvince
Neo Studio