Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: 1 2 [3] 4 5
  Print  
Author Topic: Shader FAQ  (Read 13580 times)
billythekid
Community Member
*
Posts: 814


« Reply #40 on: May 12, 2006, 07:01:52 AM »

Well DX itself is probably reading the annotation then, but you do not have to have the annotation. However I think I have realized the problem. Currently your shader is only setup to handle one and only one texture. So when you try to apply a texture to your mesh, it overrides Toon.bmp and no longer works at all.

Since you said you are going to read up on shaders, read up on the typical way of handling a texture in the vertex and pixel shaders. Then leaving the original shader you posted as is, try adding in the functionality for typical texture mapping. And this second texture is where you need to use the TEXTURE0 semantic.

If that does not work, then try using TEXTURE0 for diffuse and TEXTURE1 for toon. You have four texture layers for a reason.  :wink:
Logged
BlindSide
Customers
Community Member
*****
Posts: 759


WWW
« Reply #41 on: May 12, 2006, 04:09:13 PM »

Just wondering, but why not just use a texture as a parm in the shader instead of trying to texture it using the actor? Before rendering the actor, use shader.set... to set the diffuse texture.
Logged

Blind's Dev Blog - www.smithbower.com/devblog/

Irc.Desolation.Org :: #TV3DLicensed :: Moderated IRC channel for all your TV3D needs :: Non-Licensed users welcome.
PhunkeyMonkey
Customers
Community Member
*****
Posts: 117


WWW
« Reply #42 on: May 17, 2006, 07:16:34 PM »

Thanks billythekid and BlindSide, I've used both of your suggestions and advice to get the shader working properly (with some help from my classmates  :wink: )

This is my new edited Shader:
Code:

float4x4 world_view_projection : WorldViewProjection;
float4x4 view_matrix : View;
float3 lightDir : LIGHTDIR1_DIRECTION = { 0, 0, 1 };
//float4 diffuseMaterial : DIFFUSE = { .51f, .6f, 1, 0 };

struct VS_INPUT
{
   float4 Pos    : POSITION0;
   float3 Normal : NORMAL0;
   float2 Tex    : TEXCOORD0; //Added
};

struct VS_OUTPUT
{
   float4 Pos      : POSITION0;
   float2 TexCoord : TEXCOORD0;
   float2 _Tex     : TEXCOORD1; //Added
};

VS_OUTPUT NPR_ToonWithDynamicSpecular_Single_Pass_Vertex_Shader_vs_main( VS_INPUT In )
{
   VS_OUTPUT Out;

   Out.Pos = mul(In.Pos, world_view_projection);

   float3 posW    = mul( In.Pos, view_matrix );
   float3 normalW = mul( In.Normal, (float3x3)view_matrix );

   float diffuse = max(0,dot(normalW, -lightDir));

   Out.TexCoord.x = diffuse;
   Out.TexCoord.y = 0;
   Out._Tex = In.Tex; //Added

   return Out;
}

texture ToonShaderTexture_Tex : TEXTURE0
/*<
   string ResourceName = "Toon.bmp";
>*/;
sampler ToonShaderTexture = sampler_state
{
   Texture = (ToonShaderTexture_Tex);
};

//Added
texture MyTexture_Tex : TEXTURE1
/*<
   string ResourceName = "nmcdonald_test_spottexture.jpg";
>*/;
sampler MyTexture = sampler_state
{
   Texture = (MyTexture_Tex);
};


struct PS_INPUT
{
   float2 TexCoord : TEXCOORD0;
   float2 _Tex   : TEXCOORD1; //Added
   float4 Color    : COLOR0;
};

struct PS_OUTPUT
{
   float4 Color    : COLOR0;
};

PS_OUTPUT NPR_ToonWithDynamicSpecular_Single_Pass_Pixel_Shader_ps_main( PS_INPUT In )
{
    PS_OUTPUT Out;
 
  Out.Color = tex2D(MyTexture,In._Tex);
    Out.Color += tex2D(ToonShaderTexture,In.TexCoord); //Added
    Out.Color = Out.Color/2;
    return Out;
}

//--------------------------------------------------------------//
// Technique Section for Effect Workspace.NPR.ToonWithDynamicSpecular
//--------------------------------------------------------------//
technique ToonWithDynamicSpecular
{
   pass First_Pass
   {
      VertexShader = compile vs_1_1 NPR_ToonWithDynamicSpecular_Single_Pass_Vertex_Shader_vs_main();
      PixelShader = compile ps_1_4 NPR_ToonWithDynamicSpecular_Single_Pass_Pixel_Shader_ps_main();
   }
}


For some reason I could not get this code to do anything:
Code:
int tex1 = pTFactory->LoadTexture("Toon.bmp");
int tex2 = pTFactory->LoadTexture("nmcdonald_test_spottexture.jpg");
pActor->SetTextureEx(cTV_LAYER_0,tex1);
pActor->SetTextureEx(cTV_LAYER_1,tex2);


But thanks to BlindSide's comment I was able to load the textures from the Shader instead of from the Actor:
Code:
int tex1 = GameLogic::GetInstance()->GetTFactory()->LoadTexture("Toon.bmp");
int tex2 = GameLogic::GetInstance()->GetTFactory()->LoadTexture("nmcdonald_test_spottexture.jpg");
pShader->SetEffectParamTexture("ToonShaderTexture_Tex",tex1);
pShader->SetEffectParamTexture("MyTexture_Tex",tex2);


You guys have been a great help, thanks again.

Dayne
Logged

The above post may have been posted by one of many programmers at our college, if you find it offensive, please let us know.
bruffner
Customers
Community Member
*****
Posts: 200


« Reply #43 on: August 11, 2006, 08:05:13 PM »

I have recompiled with the latest beta dll enjoy Smiley  Ill see if i can be a bit more on time for the next build.

http://www.shelter7.com/testing/soulshadsetup2151.exe
Logged

Visit us at:
Dan
Customers
Community Member
*****
Posts: 707


WWW
« Reply #44 on: September 01, 2006, 06:39:07 AM »

I have a couple of questions that are not directly answered in the FAQ.

1) From the syntax of the method to set a shader on a mesh, it would appear that only one shader can be assigned to a mesh (or mesh group)

Code:
tvmesh.SetShaderEx(sShader,bMatchMeshFormat,iGroupIndex)


Am I correct in assuming that a shader needs to apply the complete effect required to a mesh. IE, Shaders are not additives?

2) When a shader is assigned to a mesh. Are all other material and textures  for the mesh ignored by TV. IE, The shader assigned is solely responsible for the rendering, TV Simply passes the details along? A Mesh with a material and texture set would render plain white if the shader did nothing?

3) How is a screen shader applied? I've checked the TVScene class and TVEngine but can't easily see it..

4) Can Multiple Screen shaders be applied at once? IE could you have an effect for a heat haze and a concusion being applied by 2 shaders

Thanks.
Logged
BlindSide
Customers
Community Member
*****
Posts: 759


WWW
« Reply #45 on: September 01, 2006, 08:40:23 AM »

TV uses shaders internally to light a mesh, or it'll use the FF Pipeline. So when you apply your custom shader, you are replacing the lighting pipeline used by TrueVision.

Think of it like this - for every different tv object, and for every kind of light (including projection), and for every bumpmapping mode, plus glow, there is a different TV shader lighting the mesh. Now you can see how easy it is to hit the 400 shader mark.

However, there are things called multipass shaders. These are commonly used to render things like multiple lights (N llights per pass, if there are more than N lights then the result of the first pass is blended with the result of the second), and fur (each "shell" requires another pass). To do multipass, you have two options. Do the blending manually, by rendering the mesh with one shader, setting the blend mode of the mesh/scene to additive or whatever, and rendering the mesh again with a different shader. This is how I use lightmaps with bumpmapping. The other method is to setup the blending in the shader itself using multiple passes in each technique. All you need to do this case is render the mesh, change the pass in the shader, and render the mesh again.

Multiple screen shaders cannot be applied at once, unless you create a shader that manages it in one pass. Basically a screen shader is just a shader that does operations on a texture that is applied to a quad - it isn't very special other than that. In order to use multiple screen shaders, you must pass the texture to your shader, take the result and pass it to the second shader, and so on and so forth.
Logged

Blind's Dev Blog - www.smithbower.com/devblog/

Irc.Desolation.Org :: #TV3DLicensed :: Moderated IRC channel for all your TV3D needs :: Non-Licensed users welcome.
Zaknafein
Customers
Community Member
*****
Posts: 2670


WWW
« Reply #46 on: September 01, 2006, 09:25:43 AM »

In addition to BlindSide's explaination...

3) Try Screen2DImmediate.DrawFullscreenQuadWithShader

Edit : Also, BlindSide talks a lot about multi-pass shaders and how they're a solution to many problems. Yes they are, but the "rule of thumb" in my opinion stays : "If you can AVOID multi-pass, avoid it."
It is much faster to make lengthier single-pass shaders then a series of small passes that chain one into another, and it's much less of a headache to make it work. In fact, the best approach to mixing shaders is, like Valve suggests and like Waterman does in his crazy APL programs, generate the shaders mixes with code. That's a more advanced topic though... and even I haven't tried it yet, so I can't speak much about it.
Logged

zaknafein.
>> the instruction limit : my blog & samples repository! <<
BlindSide
Customers
Community Member
*****
Posts: 759


WWW
« Reply #47 on: September 01, 2006, 05:40:56 PM »

I totally agree with Zak - a new pass requires state changes, and other messy dx stuff. Much slower - however it is a little easier for new guys (like myself) to do than to deal with instruction count limits and the like.

Like Zak said, multipass if you have too, but avoid it if possible.
Logged

Blind's Dev Blog - www.smithbower.com/devblog/

Irc.Desolation.Org :: #TV3DLicensed :: Moderated IRC channel for all your TV3D needs :: Non-Licensed users welcome.
Shaedo
Customers
Community Member
*****
Posts: 68


WWW
« Reply #48 on: September 02, 2006, 09:17:49 AM »

Quote from: "PhunkeyMonkey"
Thanks billythekid and BlindSide, I've used both of your suggestions and advice to get the shader working properly (with some help from my classmates  :wink: )

This is my new edited Shader:
Code:

float4x4 world_view_projection : WorldViewProjection;
float4x4 view_matrix : View;
float3 lightDir : LIGHTDIR1_DIRECTION = { 0, 0, 1 };
//float4 diffuseMaterial : DIFFUSE = { .51f, .6f, 1, 0 };

struct VS_INPUT
{
   float4 Pos    : POSITION0;
   float3 Normal : NORMAL0;
   float2 Tex    : TEXCOORD0; //Added
};

struct VS_OUTPUT
{
   float4 Pos      : POSITION0;
   float2 TexCoord : TEXCOORD0;
   float2 _Tex     : TEXCOORD1; //Added
};

VS_OUTPUT NPR_ToonWithDynamicSpecular_Single_Pass_Vertex_Shader_vs_main( VS_INPUT In )
{
   VS_OUTPUT Out;

   Out.Pos = mul(In.Pos, world_view_projection);

   float3 posW    = mul( In.Pos, view_matrix );
   float3 normalW = mul( In.Normal, (float3x3)view_matrix );

   float diffuse = max(0,dot(normalW, -lightDir));

   Out.TexCoord.x = diffuse;
   Out.TexCoord.y = 0;
   Out._Tex = In.Tex; //Added

   return Out;
}

texture ToonShaderTexture_Tex : TEXTURE0
/*<
   string ResourceName = "Toon.bmp";
>*/;
sampler ToonShaderTexture = sampler_state
{
   Texture = (ToonShaderTexture_Tex);
};

//Added
texture MyTexture_Tex : TEXTURE1
/*<
   string ResourceName = "nmcdonald_test_spottexture.jpg";
>*/;
sampler MyTexture = sampler_state
{
   Texture = (MyTexture_Tex);
};


struct PS_INPUT
{
   float2 TexCoord : TEXCOORD0;
   float2 _Tex   : TEXCOORD1; //Added
   float4 Color    : COLOR0;
};

struct PS_OUTPUT
{
   float4 Color    : COLOR0;
};

PS_OUTPUT NPR_ToonWithDynamicSpecular_Single_Pass_Pixel_Shader_ps_main( PS_INPUT In )
{
    PS_OUTPUT Out;
 
  Out.Color = tex2D(MyTexture,In._Tex);
    Out.Color += tex2D(ToonShaderTexture,In.TexCoord); //Added
    Out.Color = Out.Color/2;
    return Out;
}

//--------------------------------------------------------------//
// Technique Section for Effect Workspace.NPR.ToonWithDynamicSpecular
//--------------------------------------------------------------//
technique ToonWithDynamicSpecular
{
   pass First_Pass
   {
      VertexShader = compile vs_1_1 NPR_ToonWithDynamicSpecular_Single_Pass_Vertex_Shader_vs_main();
      PixelShader = compile ps_1_4 NPR_ToonWithDynamicSpecular_Single_Pass_Pixel_Shader_ps_main();
   }
}


For some reason I could not get this code to do anything:
Code:
int tex1 = pTFactory->LoadTexture("Toon.bmp");
int tex2 = pTFactory->LoadTexture("nmcdonald_test_spottexture.jpg");
pActor->SetTextureEx(cTV_LAYER_0,tex1);
pActor->SetTextureEx(cTV_LAYER_1,tex2);


But thanks to BlindSide's comment I was able to load the textures from the Shader instead of from the Actor:
Code:
int tex1 = GameLogic::GetInstance()->GetTFactory()->LoadTexture("Toon.bmp");
int tex2 = GameLogic::GetInstance()->GetTFactory()->LoadTexture("nmcdonald_test_spottexture.jpg");
pShader->SetEffectParamTexture("ToonShaderTexture_Tex",tex1);
pShader->SetEffectParamTexture("MyTexture_Tex",tex2);


You guys have been a great help, thanks again.

Dayne


I've just tried this shader out as I am looking to implement a textured toon shader into my project, but I can't seem to get it to work properly. It creates/applies the shader correctly, but the effect doesn't update as I rotate/move the model. It's as if it's just applying the effect once. I'm applying the shader in the same way as PhunkeyMonkey is. It's probably just me being stupid. Does the model need 2 UV sets when it's exported from the modelling application?

Thanks.

Toby.
Logged
Dan
Customers
Community Member
*****
Posts: 707


WWW
« Reply #49 on: September 05, 2006, 04:38:29 AM »

Thanks Zak + Blindside. Your posts helped.

When the HLSL text is passed to TV is it then compiled?

Code:
Shader.CreatePixelShaderFromString(MyHLSLTextStr)


I notice a Boolean is returned. Is that a flag to state it was able to create the shader or that errors occured? True = ?

I assume if errors occured the last could be investigated by the

Code:
Shader.GetLastError()


What have you guys found to be the best editor when working with HLSL code to work with TV. Notepad?  I havn't tried soulshader. The fact truevision is used as the renderer would prevent the compatibility issues I've heard of when using tools like rendermonkey.
Logged
Zaknafein
Customers
Community Member
*****
Posts: 2670


WWW
« Reply #50 on: September 05, 2006, 09:57:00 AM »

Quote from: "Dan"
What have you guys found to be the best editor when working with HLSL code to work with TV. Notepad?  I havn't tried soulshader. The fact truevision is used as the renderer would prevent the compatibility issues I've heard of when using tools like rendermonkey.

I use Visual C++.NET 2005 with the DirectX SDK plug-in. It sports proper syntax highlighting and compilation/de-assembly. May I quote myself... Tongue
Quote from: "Zaknafein"
First, you need to use VS.Net 2003 or 2005, and have VC++.Net installed.
Then you make a new VC++.Net project, add it to your solution, and add the .fx file as a "code file" item.
Then in the properties page of the .fx file, go in "Custom Build Step" and set the command line to :
Quote
fxc.exe -Zi -Cc -nologo -T fx_2_0 -Fc "$(InputDir)$(InputName).html" "$(InputDir)$(InputName).fx"

And the outputs to :
Quote
$(InputDir)$(InputName).fxo

For all this to work, you need to have a recent version of the DirectX SDK installed too.

This will allow you to build the file with the VC++.Net project, and check for compilation errors. Problem is that if you feed the shader with wrong parameters at runtime, then there's little you can do to debug... that I know of.


It doesn't offer a 3D view of the shader's effect, but most shaders I make are specific to a precise context, so if you feed them dummy data and parameters they would look just wrong... so I prefer testing them with a small TV3D testbed that I write for each shader; a real-world test.
Logged

zaknafein.
>> the instruction limit : my blog & samples repository! <<
BlindSide
Customers
Community Member
*****
Posts: 759


WWW
« Reply #51 on: September 05, 2006, 02:15:01 PM »

Most complex shaders won't operate by themselves - they usually require setup, extra data, multiple passes, etc.

The best way to test a shader like Zak said, is to write an app for it. Sadly, you'll rarely stumble upon a plug n' play shader of any complexity.
Logged

Blind's Dev Blog - www.smithbower.com/devblog/

Irc.Desolation.Org :: #TV3DLicensed :: Moderated IRC channel for all your TV3D needs :: Non-Licensed users welcome.
Mietze
Community Member
*
Posts: 277

Pleeease, don't let it crash!


WWW
« Reply #52 on: September 05, 2006, 03:57:32 PM »

I guess the new FOG-semantics should be added to the list Smiley

There now is:

float FOG_START
float FOG_END
float FOG_DENSITY
float3 FOG_COLOR

if i remember right Smiley
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Zaknafein
Customers
Community Member
*****
Posts: 2670


WWW
« Reply #53 on: September 05, 2006, 04:56:38 PM »

They're in? There's no info in the beta update thread about them.
Logged

zaknafein.
>> the instruction limit : my blog & samples repository! <<
Mietze
Community Member
*
Posts: 277

Pleeease, don't let it crash!


WWW
« Reply #54 on: September 05, 2006, 11:24:33 PM »

Quote from: "Zaknafein"
They're in? There's no info in the beta update thread about them.
Yes, i already use them. Smiley
Logged

Check out my blog at www.e-studioz.de - The finest in gross trash Wink
Zaknafein
Customers
Community Member
*****
Posts: 2670


WWW
« Reply #55 on: September 15, 2006, 10:05:05 PM »

They indeed work well, and here's a sample SM2.0 code for fogging :

Code:
// ** Declarations **
// Misc semantics
float4x4 matWorld : WORLD;
float3 viewPos : VIEWPOSITION;
// Fog semantics
float fogStart : FOGSTART;
float fogEnd : FOGEND;
float fogDensity : FOGDENSITY;
float3 fogColor : FOGCOLOR;  
int fogType; // This one needs to be set via a shader parameter!
// Constants
#define FOG_TYPE_NONE    0
#define FOG_TYPE_EXP     1
#define FOG_TYPE_EXP2    2
#define FOG_TYPE_LINEAR  3

...

// ** In vertex shader **
// IN.pos is mapped to the POSITION vertex shader input semantic
float4 worldPos = mul(IN.pos, matWorld);
// OUT.fog is mapped to the FOG vertex shader output semantic
float dist = distance(worldPos.xyz, viewPos);
OUT.fog = (fogType == FOG_TYPE_NONE) +
1 / exp(dist * fogDensity) * (fogType == FOG_TYPE_EXP) +
1 / exp(pow(dist * fogDensity, 2)) * (fogType == FOG_TYPE_EXP2) +
saturate((fogEnd - dist) / (fogEnd - fogStart)) * (fogType == FOG_TYPE_LINEAR);
Logged

zaknafein.
>> the instruction limit : my blog & samples repository! <<
Zaknafein
Customers
Community Member
*****
Posts: 2670


WWW
« Reply #56 on: September 26, 2006, 09:53:31 AM »

FYI, there is an up-to-date list of supported semantics on the wiki :
http://wiki.truevision3d.com/semantics#1_-_global_shader-level_semantics
Logged

zaknafein.
>> the instruction limit : my blog & samples repository! <<
BlindSide
Customers
Community Member
*****
Posts: 759


WWW
« Reply #57 on: September 26, 2006, 04:59:32 PM »

Thanks Zak, very useful.
Logged

Blind's Dev Blog - www.smithbower.com/devblog/

Irc.Desolation.Org :: #TV3DLicensed :: Moderated IRC channel for all your TV3D needs :: Non-Licensed users welcome.
Waterman
Customers
Community Member
*****
Posts: 1141


« Reply #58 on: September 26, 2006, 05:13:10 PM »

Indeed useful text! Good job!

A small typo at the next section:

Code:
float2 a;  // A 2-dimensional vector
float3 b;  // A 3-dimensional vector
float4 c;  // A 4-dimensional vector


You mean "element", not "dimensional".  An "n-dimensional vector" is a major contradiction :-).
Logged

Things should be described as simply as possible - but not simpler [A. Einstein]
Zaknafein
Customers
Community Member
*****
Posts: 2670


WWW
« Reply #59 on: September 26, 2006, 05:56:08 PM »

Sorry for arguing, but how is that a contradiction? Isn't a vector in 3D space a 3D vector?

Thanks though, you and Blind. And heck, that's an interesting blog you got there Blind.
Logged

zaknafein.
>> the instruction limit : my blog & samples repository! <<
Pages: 1 2 [3] 4 5
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks