Game Development Community

Custom Material multi pass

by elvince · in Torque 3D Professional · 01/16/2010 (8:24 am) · 8 replies

Hi,

I'm trying to use the pass shader functionality & use the texture of 1 shader as input in my 2nd shader.
Obviously from what I read this should be written like my code above.

But in reality, I get a blank texture in #redArmor as input of Blue shader.
If someone can help me in this, I will be glad.

More, if you have good links on Shader/postFx within torque this could be great as it pretty difficult to jump into this without basic documentation.

My shaders blue & red are pretty basic and just force output color of Pixel shader to Blue or Red.

singleton CustomMaterial(BlueMaterial) 
{ 
   texture[0] = "#redArmor"; // this is our input, the redArmor from RedMaterial
   shader = BlueShader;   
   version = 2.0;
   mapTo = "Armor"; // this is the name of the texture that will be applied to the model
};

singleton CustomMaterial(test)
{  
   version = 2.0;
   texture[0] = "Texure";  // this is your input from wherever the input armor texture was located
   shader = RedShader; 
   mapTo = "#redArmor"; // we're going to output this to a temporary texture to make the pass to BlueMaterial    
   pass[0] = BlueMaterial;  //2nd pass    
};

#1
01/16/2010 (9:04 am)
pass[N] functionality is removed in T3D.
texture[N] is valid for postFx only.
custom materials now use sampler["diffsampler"] = "your_map"
#2
01/16/2010 (11:23 am)
Thanks for the answer.

I used texture[N] for basic shader and it's still working. Anyway I will use sampler.

Is it not samplerNames[N] the right variable?

Can you use another custom material as sampler? or now in T3D you are not able to link 2 custom material?
I know that post FX allow you to use $inTex $outTex

Where did you find those information? I read all docs on T3D and nothing about custom material/shader/postFx, I went to TGEA Doc, and this is where I find some stuffs. Did I miss anything or it's just your experience that drive you to the right usage of custom material?

#3
01/16/2010 (2:39 pm)
Custom material will not combine your passes.
Both samplerNames[N] and sampler[""] are valid (in ShaderData and CustomMaterial respectively).
texture[N] works in T3D 1.0
T3D 1.1A is using sampler[""],texture[N] is no longer valid for custom materials.
postFx is a separate tool,it has nothing to do with custom materials.
#4
01/16/2010 (5:44 pm)
Ok i got it.

Does the name used in sampler[] must key word or must match a variable name within shader?

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 but it s multipass shader.

From what I find today I may be able to do it with a new renderbin with a postfx.
Do you think it's a good approach?

Just in case my source is http://creators.xna.com/fr-FR/sample/distortion

if you can show me the path to follow to get this working i will appreciate!

Thanks
#5
01/17/2010 (6:46 am)
Yes,the name must correspond:
sampler["diffusemap"] = "your_map";

uniform sampler2D diffusemap: register(S0);

I don't have the time to read the shader documentation,but it seems you will have to render to SceneTexture, then to apply a distortion.
May be it is the same idea .. like the glow effect.
#6
01/17/2010 (3:15 pm)
I managed to get the buffer with all my scene render as a texture.
I have one another texture with the model only.

But I don't understand how create the distorsion map and apply it.

They are using the vertex/pixel shader to create a the distorsion map from the normal.

I thing I manage to implement the vertex but not the pixel. In fact, if I render this shader I have a Black Screen instead of my distorsion map. (should be my model in "yellow" color and all rest in black)

I'm sure I'm missing something easy, but I don't get how to extract only the distorsion map for my model and not a full screen. Do I need to use my selectionbuffer (my model and everything else in black) to get this?

Vertex:
#include "./postFx.hlsl"
#include "./../torque.hlsl"


uniform float4 rtParams0;
uniform float4 rtParams1;
uniform float4 rtParams2;
uniform float4 rtParams3;

float4  DistortionScale = 0.0003f;
                    
PFXVertToPix main( PFXVert IN,float3 N :Normal )
{
   PFXVertToPix OUT;
   
   OUT.hpos = IN.pos;
   OUT.uv0 = viewportCoordToRenderTarget( IN.uv, rtParams0 ); 
   OUT.uv1 = viewportCoordToRenderTarget( IN.uv, rtParams1 ); 
   OUT.uv2 = viewportCoordToRenderTarget( IN.uv, rtParams2 ); 
   OUT.uv3 = viewportCoordToRenderTarget( IN.uv, rtParams3 ); 

   OUT.wsEyeRay = IN.wsEyeRay;
   float3 normalWV = N;//mul(N, WorldViewXf);
   normalWV.y = -normalWV.y;
   
   float amount = dot(normalWV, float3(0,0,1)) * DistortionScale;
OUT.uv0 = float2(.5,.5) + float2(amount * normalWV.xy);

   return OUT;
}

Pixel Shader: [Not working properly]
#include "postFx.hlsl"  
#include "shadergen:/autogenConditioners.h"  
  
float4 main( PFXVertToPix IN)
{  	
      return ?? float4(IN.uv0, 0, 1);??
}

Original Shader:
struct PositionNormal
{
   float4 Position : POSITION;
   float3 Normal : NORMAL;
};

struct PositionDisplacement
{
   float4 Position : POSITION;
   float2 Displacement : TEXCOORD;
};

PositionDisplacement PullIn_VertexShader(PositionNormal input)
{
   PositionDisplacement output;

   output.Position = mul(input.Position, WorldViewProjection);
   float3 normalWV = mul(input.Normal, WorldView);
   normalWV.y = -normalWV.y;
   
   float amount = dot(normalWV, float3(0,0,1)) * DistortionScale;
   output.Displacement = float2(.5,.5) + float2(amount * normalWV.xy);

   return output;   
}

float4 DisplacementPassthrough_PixelShader(float2 displacement : TEXCOORD) : COLOR
{  
   return float4(displacement, 0, 1);
}
#7
01/17/2010 (3:45 pm)
The pixel shader is wrong - you must output a color.

If:
texture[0] = "$backBuffer";
then:

uniform sampler2D backBuffer : register(S0);

float4 main(PFXVertToPix IN) : COLOR0
{ ...
float4 color = tex2D(backBuffer, IN.uv0);  
...
// you fetch and apply a distortion
..
return color;
}
#8
01/21/2010 (4:30 am)
I opened a new thread in [url] http://www.torquepowered.com/community/forums/viewthread/109617[/url]

so now we are focusing on this effect and not multi pass material.

Ivan, your input will be more than valuable as I think you are clearly aware of the Shader/PostFx process.

Thanks,