Game Development Community

Basic Shader

by Eric den Boer · in Torque 3D Professional · 07/22/2009 (8:26 am) · 13 replies

This might be stupid, but...

How would one create a very basic pixel and vertex shader? What are the parameters called that you can pass along to the shader, and where/how do the parameters get set?

I'm looking through all the PostFX code and all the shaders, but all the shaders have different structures passed along, different parameters, etc. Is there going to be a basic tutorial on using shaders in T3D some day?

All I'm looking for is a basic pixel and vertex shader to simply turn a pixel green or light a vertex.

Thanks in advance!

#1
07/22/2009 (9:31 am)
If you mean a shader for postfx, there is passthruP.hlsl, which does nothing but get the input color and output it.
#2
07/22/2009 (10:25 am)
Anything really, I just need a clear example on how to pass arguments etc. and how shaders work in T3D :)

I'll check out the passthruP.hlsl file! Thank you!

Is there a basic passthruV.hlsl as well?
#3
07/22/2009 (11:43 am)
All postFX use the same vertex shader, postFxV.hlsl. For post effects you don't really need to change it, since the geometry is always a screen aligned quad.
#4
07/22/2009 (2:49 pm)
Okay, another question...

How do I pass along the normal of a pixel or vertex? I tried adding:
float3 normal : NORMAL0
to the base vertex and pixel structs, but it either says that OUT is being used without being fully initialized, or it says that both NORMAL and NORMAL0 are invalid ps 2.0 or vs 2.0 variables.

Why is the normal of a pixel or vertex not there? Or am I missing something? I'm sure such a basic variable must've been implemented.
#5
07/22/2009 (3:06 pm)
The NORMAL0 is only used as vertex shader input. Only TEXCOORD0 to TEXCOORD16 (8 for PS 2.0), COLOR0 and COLOR1 can be used as pixel shader inputs. Those are the values that are interpolated between vertices and passed to pixels.

So, if you want the pixels to have access to the geometry normals, you need to have the vertex shader put the normal in a TEXCOORD and then you'll have access to it in the pixel shader. While they are named TEXCOORD, these inputs can be used to store generic interpolated 4-component (xyzw) data.

Since you're new to HLSL, I recommend downloading RenderMonkey from AMD's site and doing some shader experiments there first, to get the hang of how they work first. Also look for some HLSL introductory tutorials, like this: www.neatware.com/lbstudio/web/hlsl.html
#6
07/22/2009 (3:22 pm)
The normal of the vertex shader is fine with me :)

I'm not entirely new to shaders, but every engine works differently with them.

So what's the normal called in the struct? Is that the "eye" variable? :)
#7
07/22/2009 (7:49 pm)
This must not be a stupid question because i've got no clue whats going on :D
#8
07/23/2009 (6:08 am)
This book is the best book for beginner shader programmers that I have found. It is amazing in it's simplicity and how thorough it is. The author makes almost no assumptions of what the reader knows about shading.
#9
07/23/2009 (6:48 am)
Thanks for the link, I'll be sure to buy it. But.. that's not exactly what I'm after.

Simply put: how does torque "communicate" with its shaders? Every engine works differently (although working towards the same goal: passing variables along to handles in video memory). Where do I find the standard variables like a vertex's normal etc. What are they called and how can I use them inside the shader?

Edit: bought the book! :D
#10
07/23/2009 (6:50 am)
The normal in the vertex shader comes from the input vertex struct. In game/shaders/common/hlslStructs.h there are a bunch of pre-defined vertex input structures. Here's an example:

struct VertexIn_PNT
{
   float4 pos        : POSITION;
   float3 normal     : NORMAL;
   float2 uv0        : TEXCOORD0;
};

A "vertex normal" is standard HLSL and T3D does nothing strange with it. It comes from the vertex buffers, it isn't exactly a variable.
#11
07/23/2009 (6:52 am)
Manoel: Thanks! That's exactly what I'm looking for, now I can start working with them! :D
#12
07/23/2009 (6:55 am)
Check blobRefractVertV.hlsl and blobRefractVertP.hlsl (and the two included files "shdrConsts.h" and "hlslStructs.h") in the shaders/common folder. They do a refraction effect based on vertex normals, and work with standard geometry.
#13
07/23/2009 (7:04 am)
Thank you so much! It's so obvious when looking at it, makes me feel even "stupider" ;)