Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/20/24 20:05
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 649 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Baklazhan, Hanky27, firatv, wandaluciaia, Mega_Rod
19052 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Introduction to HLSL shaders #42760
03/17/05 06:53
03/17/05 06:53
Joined: Sep 2003
Posts: 3,236
San Diego, CA
M
Marco_Grubert Offline OP
Expert
Marco_Grubert  Offline OP
Expert
M

Joined: Sep 2003
Posts: 3,236
San Diego, CA
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;
}



Re: Introduction to HLSL shaders [Re: Marco_Grubert] #42761
08/07/05 00:33
08/07/05 00:33
Joined: Mar 2005
Posts: 123
O
oronll Offline
Member
oronll  Offline
Member
O

Joined: Mar 2005
Posts: 123
this doesnt work .. it says color is not defined .. ??

just make it float4 Color ...

Last edited by oronll; 08/07/05 00:50.
Re: Introduction to HLSL shaders [Re: oronll] #42762
08/12/05 08:06
08/12/05 08:06
Joined: Aug 2005
Posts: 107
NMS Offline
Member
NMS  Offline
Member

Joined: Aug 2005
Posts: 107
Where must i put these script to work in 3dgs?


http://www.coniserver.net/ubbthreads/showflat.php?Cat=0&Number=560847&an=0&page=0#560847 Cmon every one! Help me building this camera script! The first of its kind in 3dgs! Thnkx to everyone who helped me in my questions
Re: Introduction to HLSL shaders [Re: NMS] #42763
08/12/05 08:13
08/12/05 08:13
Joined: Aug 2005
Posts: 107
NMS Offline
Member
NMS  Offline
Member

Joined: Aug 2005
Posts: 107
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);
}
}

Last edited by NMS; 08/12/05 08:16.

http://www.coniserver.net/ubbthreads/showflat.php?Cat=0&Number=560847&an=0&page=0#560847 Cmon every one! Help me building this camera script! The first of its kind in 3dgs! Thnkx to everyone who helped me in my questions
Re: Introduction to HLSL shaders [Re: NMS] #42764
08/12/05 08:14
08/12/05 08:14
Joined: Oct 2002
Posts: 8,939
planet.earth
ello Offline
Senior Expert
ello  Offline
Senior Expert

Joined: Oct 2002
Posts: 8,939
planet.earth
into a fx-file which is included into the material via effect="you fx-file"; , or directly into the effect="..."; of the material


www.earthcontrol.de
quoted: We want to maintain a clean, decent, American family suited forum look... which means you may post zombies or chainsaw massacres, but no erotic.
Re: Introduction to HLSL shaders [Re: ello] #42765
08/12/05 09:03
08/12/05 09:03
Joined: Aug 2005
Posts: 107
NMS Offline
Member
NMS  Offline
Member

Joined: Aug 2005
Posts: 107
what??? :s

Sry, i didnt caught that.

Last edited by NMS; 08/12/05 09:04.

http://www.coniserver.net/ubbthreads/showflat.php?Cat=0&Number=560847&an=0&page=0#560847 Cmon every one! Help me building this camera script! The first of its kind in 3dgs! Thnkx to everyone who helped me in my questions
Re: Introduction to HLSL shaders [Re: NMS] #42766
08/12/05 09:27
08/12/05 09:27
Joined: Oct 2002
Posts: 8,939
planet.earth
ello Offline
Senior Expert
ello  Offline
Senior Expert

Joined: Oct 2002
Posts: 8,939
planet.earth
well that was my answer to ths: "Where must i put these script to work in 3dgs?"

Re: Introduction to HLSL shaders [Re: ello] #42767
08/12/05 10:26
08/12/05 10:26
Joined: Aug 2005
Posts: 107
NMS Offline
Member
NMS  Offline
Member

Joined: Aug 2005
Posts: 107
This file is already a .FX file, but problem is to put this effect working in my level.
How can i do it?


http://www.coniserver.net/ubbthreads/showflat.php?Cat=0&Number=560847&an=0&page=0#560847 Cmon every one! Help me building this camera script! The first of its kind in 3dgs! Thnkx to everyone who helped me in my questions
Re: Introduction to HLSL shaders [Re: NMS] #42768
08/12/05 10:30
08/12/05 10:30
Joined: Oct 2002
Posts: 8,939
planet.earth
ello Offline
Senior Expert
ello  Offline
Senior Expert

Joined: Oct 2002
Posts: 8,939
planet.earth
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


www.earthcontrol.de
quoted: We want to maintain a clean, decent, American family suited forum look... which means you may post zombies or chainsaw massacres, but no erotic.
Re: Introduction to HLSL shaders [Re: ello] #42769
08/12/05 12:13
08/12/05 12:13
Joined: Aug 2005
Posts: 107
NMS Offline
Member
NMS  Offline
Member

Joined: Aug 2005
Posts: 107
Ok, thnks


http://www.coniserver.net/ubbthreads/showflat.php?Cat=0&Number=560847&an=0&page=0#560847 Cmon every one! Help me building this camera script! The first of its kind in 3dgs! Thnkx to everyone who helped me in my questions
Page 1 of 2 1 2

Moderated by  Blink, Hummel, Superku 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1