Introduction to HLSL shaders

Posted By: Marco_Grubert

Introduction to HLSL shaders - 03/17/05 06:53

Courtesy of Matt_AufderHeide :

Here is a basic HLSL shader:
Code:

//this is the only matrix you need for this shader.. world*view*projection
float4x4 matWorldViewProj;

here is the vertex shader output
struct VS_OUTPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
};

//simple vertex shader calculates the vertex position and tex coords
VS_OUTPUT mainVS( float4 Pos : POSITION, float2 Tex : TEXCOORD0 )
{
VS_OUTPUT Out = (VS_OUTPUT) 0;
Out.Pos = mul(Pos,matWorldViewProj );
Out.Tex = Tex;
return Out;
}

texture entSkin1;

//define the sampler for the entity skin
sampler basemap = sampler_state
{
Texture = <entSkin1>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = clamp;
AddressV = clamp;
};


// Simple pixel shader samples the entity skin according to Tex.xy coords
float4 mainPS(float2 Tex: TEXCOORD0) : COLOR
{
Color = tex2D( basemap, Tex.xy);
return Color;
}

technique blur
{

pass Object
{
alphablendenable=false;
ZENABLE = TRUE;
VertexShader = compile vs_1_1 mainVS();
PixelShader = compile ps_2_0 mainPS();
}

}



The vertex shader just calculates the vertex position according to the projection matrix and passes the texture coords to the pixel shader. The pixel shader just colors the pixel depending on the texture coords and the ent skin.

This is the about the most basic a shader you can get, and will render an unshaded, textured model. But the structure is what's important here.

If you understand this shader.. then you can simply start adding other effects into it, such as a vertex lighting, and detail mapping. To do detail mapping, simply make another skin (and put into entskin2 in MED)like a greyscale texture of metal rust or something and then add anoter sampler like this:
Code:

sampler detailmap = sampler_state
{
Texture = <entSkin2>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = wrap; //you want it wrap because it will be scaled smaller
AddressV = wrap;
};


to add detail mapping coords you can either caluculate them in the vertex shader and pass a second set of tex coords, or you can just multiply the tex.xy in the pixel shader. For simplicity's sake let's do the latter:

Code:

// pixel shader that does detail mapping
float4 mainPS(float2 Tex: TEXCOORD0) : COLOR
{
Color =

//sample base color
tex2D( basemap, Tex.xy) *

//multiply with the detail texture and scale the texture coords
tex2D(detailmap,(Tex.xy*5));

return Color;
}


Posted By: oronll

Re: Introduction to HLSL shaders - 08/07/05 00:33

this doesnt work .. it says color is not defined .. ??

just make it float4 Color ...
Posted By: NMS

Re: Introduction to HLSL shaders - 08/12/05 08:06

Where must i put these script to work in 3dgs?
Posted By: NMS

Re: Introduction to HLSL shaders - 08/12/05 08:13

Example, I found this FX shader effect made on 3dsmax, does it work on 3dgs?
If it does, where can i learn how to put it in my levels?
If it doesn't work and need to be converted, how can i do?

Plz help, cuz in the shaders part i'm really out of it :S

This code puts a specular effect in the game, like halo.
CODE:
--------------------------------------------------------------------------

/******************************************************************************
HLSL Normal Map Specular Shader

This shader is based on several shaders written by Kevin Bjorke of Nvidia and
included with the Cg plugin for 3DS Max 5.1.

This shader uses a normal map for per-pixel lighting - to give the illusion that
the model surface contains more detail than is really there. It also adds a specular
component for making models look shiny.

It accepts the following inputs:

Ambient Color
Diffuse Color
Diffuse Texture
Specular Mask (alpha channel of diffuse texture)
Normal Map Texture
DXT5 Compressed Normal Map
Specular Color
Shininess
Point Light Position
Point Light Color

It requires hardware support for DirectX 9.

Normal Map compression is an option that you can use with this shader.
Copy the red channel of your normal map to the alpha channel. Then delete the
red and blue channels and save your normal map in DXT5 DDS format. Put your
compressed normal map in the DXT5 Normal Map slot for the shader. Then choose
"Compressed" as the technique instead of "complete."

This shader is intended to be used by the DirectX 9 shader of 3DS Max
but it could also be used in other applications that support the FX format.

******************************************************************************/

/************* TWEAKABLES **************/

half4 ambient : Ambient
<
string UIName = "Ambient Color";
> = {0.25f, 0.25f, 0.25f, 1.0f};

half4 surfColor : Diffuse
<
string UIName = "Diffuse Color";
> = {1.0f, 1.0f, 1.0f, 1.0f};

texture colorTexture : DiffuseMap
<
string name = "default_color.dds";
string UIName = "Diffuse Texture";
string TextureType = "2D";
>;

half4 specularColor : Specular
<
string UIName = "Specular Color";
> = { 0.2f, 0.2f, 0.2f, 1.0f };

half shininess <
string UIWidget = "slider";
float UIMin = 1;
float UIMax = 128;
string UIName = "Shininess";
> = 40;

texture normalMap : NormalMap
<
string name = "default_bump_normal.dds";
string UIName = "Normal Map";
string TextureType = "2D";
>;

texture CnormalMap : CNormalMap
<
string name = "default_bump_normal.dds";
string UIName = "DXT5 Normal Map";
string TextureType = "2D";
>;

/************** light info **************/

half4 light1Pos : POSITION
<
string UIName = "Light Position";
string Object = "PointLight";
string Space = "World";
int refID = 0;
> = {100.0f, 100.0f, 100.0f, 0.0f};

half4 light1Color : LIGHTCOLOR
<
int LightRef = 0;
> = { 1.0f, 1.0f, 1.0f, 0.0f };

/***********************************************/
/*** automatically-tracked "tweakables" ********/
/***********************************************/

half4x4 wvp : WorldViewProjection < string UIWidget = "None"; >;
half4x4 worldI : WorldInverse < string UIWidget = "None"; >;
half4x4 viewInvTrans : ViewInverse < string UIWidget = "None"; >;
half4x4 world : World < string UIWidget = "None"; >;

/****************************************************/
/********** CG SHADER FUNCTIONS *********************/
/****************************************************/

// input from application
struct a2v {
half4 position : POSITION;
half2 texCoord : TEXCOORD0;
half3 tangent : TANGENT;
half3 binormal : BINORMAL;
half3 normal : NORMAL;
};

// output to fragment program
struct v2f {
half4 position : POSITION;
half2 texCoord : TEXCOORD0;
float3 eyeVec : TEXCOORD1;
half3 lightVec : TEXCOORD2;
};

// blinn lighting with lit function
half4 blinn2(half3 N,
half3 L,
half3 V,
uniform half4 diffuseColor,
uniform half4 specularColor,
uniform half shininess
)
{
half3 H = normalize(V+L);
half4 lighting = lit(dot(L,N), dot(H,N), shininess);
return diffuseColor*lighting.y + specularColor*lighting.z;
}

/**************************************/
/***** VERTEX SHADER ******************/
/**************************************/

v2f v(a2v In,
uniform half4x4 worldViewProj, // object to clip space
uniform half4x4 WorldIMatrix, //world to object space
uniform half4 lightPosition,
uniform half4x4 ViewInvTrans,
uniform half4x4 world // object to world space
)
{
v2f Out;

// transform vertex position to homogeneous clip space
Out.position = mul(In.position, worldViewProj);

//pass texture coordinates
Out.texCoord = In.texCoord;

// compute the 3x3 tranform from tangent space to object space
half3x3 objTangentXf;

objTangentXf[0] = In.binormal.xyz;
objTangentXf[1] = -In.tangent.xyz;
objTangentXf[2] = In.normal.xyz;

//put the world space light position in object space
half4 objSpaceLightPos = mul(lightPosition, WorldIMatrix);
half3 objLightVec = objSpaceLightPos.xyz - In.position.xyz;
// xform light vector from obj space to tangent space
Out.lightVec = mul(objTangentXf, objLightVec );

//compute the eye vector in world space and put it in object space
half4 objSpaceEyePos = mul(ViewInvTrans[3], WorldIMatrix);
// xform eye vector from obj space to tangent space
half3 objEyeVec = objSpaceEyePos.xyz - In.position.xyz;
Out.eyeVec = mul(objTangentXf, objEyeVec);

return Out;
}

/**************************************/
/***** FRAGMENT PROGRAM ***************/
/**************************************/

float4 f(v2f In,
uniform sampler2D colorTex,
uniform sampler2D bumpTex,
uniform half4 ambient,
uniform half4 diffuseColor,
uniform half4 specularColor,
uniform half shininess,
uniform half4 light1Color
) : COLOR
{
//fetch the diffuse map
half4 colorMap = tex2D(colorTex, In.texCoord.xy);

// fetch the bump normal from the normal map
half3 normal = tex2D(bumpTex, In.texCoord).xyz * 2.0 - 1.0;

// calculate lighting vectors
half3 N = normalize(normal);
half3 V = normalize(In.eyeVec);
half3 L = normalize(In.lightVec);

//lighting

//ambient light
half4 C = ambient*colorMap;

//diffuse and specular
C += light1Color * blinn2(N, L, V, colorMap*diffuseColor, specularColor*colorMap.a, shininess);

return C;
}

float4 f2(v2f In,
uniform sampler2D colorTex,
uniform sampler2D bumpTex,
uniform half4 ambient,
uniform half4 diffuseColor,
uniform half4 specularColor,
uniform half shininess,
uniform half4 light1Color
) : COLOR
{
//fetch the diffuse map
half4 colorMap = tex2D(colorTex, In.texCoord.xy);

// fetch the bump normal from the normal map
//swizzle the compressed dxt5 format
half3 normal = tex2D(bumpTex, In.texCoord).wyz * 2.0 - 1.0;
//generate the z component of the vector
normal.z = sqrt(1 - normal.x * normal.x - normal.y * normal.y);

// calculate lighting vectors
half3 N = normal;
half3 V = normalize(In.eyeVec);
half3 L = normalize(In.lightVec);

//lighting

//ambient light
half4 C = ambient*colorMap;

//diffuse and specular
C += light1Color * blinn2(N, L, V, colorMap*diffuseColor, specularColor*colorMap.a, shininess);

return C;
}

/****************************************************/
/********** SAMPLERS ********************************/
/****************************************************/

sampler2D colorTextureSampler = sampler_state
{
Texture = <colorTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Anisotropic;
};

sampler2D normalMapSampler = sampler_state
{
Texture = <normalMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Anisotropic;
};

sampler2D CnormalMapSampler = sampler_state
{
Texture = <CnormalMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Anisotropic;
};

/****************************************************/
/********** TECHNIQUES ******************************/
/****************************************************/

technique Complete
{
pass envPass
{
VertexShader = compile vs_1_1 v(wvp,worldI,light1Pos,viewInvTrans,world);
ZEnable = true;
ZWriteEnable = true;
CullMode = CW;
PixelShader = compile ps_2_0 f(colorTextureSampler,normalMapSampler,ambient,surfColor,specularColor,shininess,light1Color);
}
}

technique Compressed
{
pass envPass
{
VertexShader = compile vs_1_1 v(wvp,worldI,light1Pos,viewInvTrans,world);
ZEnable = true;
ZWriteEnable = true;
CullMode = CW;
PixelShader = compile ps_2_0 f2(colorTextureSampler,CnormalMapSampler,ambient,surfColor,specularColor,shininess,light1Color);
}
}
Posted By: ello

Re: Introduction to HLSL shaders - 08/12/05 08:14

into a fx-file which is included into the material via effect="you fx-file"; , or directly into the effect="..."; of the material
Posted By: NMS

Re: Introduction to HLSL shaders - 08/12/05 09:03

what??? :s

Sry, i didnt caught that.
Posted By: ello

Re: Introduction to HLSL shaders - 08/12/05 09:27

well that was my answer to ths: "Where must i put these script to work in 3dgs?"
Posted By: NMS

Re: Introduction to HLSL shaders - 08/12/05 10:26

This file is already a .FX file, but problem is to put this effect working in my level.
How can i do it?
Posted By: ello

Re: Introduction to HLSL shaders - 08/12/05 10:30

as i told you. you have to declare a material

material mat_name
{
effect="the fx-file";
}

and thats it.

next you will get thousands of errors, which you will have to remove, and than you will see something you didnt expect and will have to find a solution for that;) thats all about implementing shaders found anywhere in the net
Posted By: NMS

Re: Introduction to HLSL shaders - 08/12/05 12:13

Ok, thnks
Posted By: Nowherebrain

Re: Introduction to HLSL shaders - 10/31/05 20:51

and you attach this to an entity?
Posted By: figurefour

Re: Introduction to HLSL shaders - 10/05/06 06:51

Just a question about the Shaders, used the example from the AUM Magizine 56 in realtion to the colours used Is it possible to have more then 3 colours representing the bitmaps.

Just wondering incase the max number is 3.

Have tried to use more though get a strange error message.

Just really like to know if it is possible I'll get back to it otherwise well think of another way.

Thanks in advance.

The main question here is, Is it possible to use more then 3 colours to swap the bitmaps too?.
Posted By: Anonymous

Re: Introduction to HLSL shaders - 10/23/06 16:10

I'm complete new ... I've understand how a shader work, and I've downloaded the DirectX SDK, but how can I create a shader - document (*.fx) ...
Need i compile it or something like that or just like .php create a .txt file and rename it to .fx?
and: can i use ALL functions of a shader in GS6 ?(I've got the extra ed.)
thanks for all answers!
Posted By: Uhrwerk

Re: Introduction to HLSL shaders - 10/23/06 17:04

Quote:

Need i compile it or something like that or just like .php create a .txt file and rename it to .fx?




You have to create just a text file and name it to .fx.

Quote:

can i use ALL functions of a shader in GS6 ?(I've got the extra ed.) thanks for all answers!




The extra edition doesn't support any shaders.
Posted By: tompo

Re: Introduction to HLSL shaders - 04/13/07 12:30

sorry but when I attach this .fx to material... my model is invisible
Why?! No errors appears but no visible model to
Posted By: Nimso

Re: Introduction to HLSL shaders - 07/31/07 22:10

I get the same thing as tompo, but mine has an error.
just after SUCCESSFULLY compiling the level, I get the message:

"Error in effect: grasser(1) : syntax error"

(grasser is the name of my material, it's a grass shader)

Why is this???

remember this is AFTER compiling successfully!

EDIT: One last thing, would the shaders from Hitman 4 Blood Money work on 3DGS A6, they are .fx's and I'm pretty sure that A6 can handle shader 2.0!
Posted By: Nimso

Re: Introduction to HLSL shaders - 10/27/07 12:42

is anyone EVER gonna post here or what, coz I STILL can't figure out why the hell my copy of Gamestudio so STONED!

it just doesn't like BASIC HLSL scripts!

it can't handle "float" with any number after it, neither does it ever understand the " : " in a script, I've tested these shaders on other things, it's just too Stoned!!!!!!!!!
Posted By: JibbSmart

Re: Introduction to HLSL shaders - 10/28/07 01:04

start a thread asking the question in detail with examples of your code.

no example = no visible problem = no solution.

julz
Posted By: adoado

Re: Introduction to HLSL shaders - 10/28/07 06:09

Quote:

is anyone EVER gonna post here or what, coz I STILL can't figure out why the hell my copy of Gamestudio so STONED!





What version of 3dgs do you have?
Posted By: Nimso

Re: Introduction to HLSL shaders - 11/04/07 12:38

That last post answered the problem, I had 6.22 which I found couldn't handle the shaders anyway, but I now have them working, I got A7.04,

Thanks to anyone that helped anyway,

to the guy who got invisible models, it's most probably the shader coding, I get it sometimes!
© 2024 lite-C Forums