ok here is the first tutorial - custom shaders are available here if you want to try out the other variations but will work on a tutorial for the most complex ones and if anyone needs help understanding a particular shader.
You can download the project example here : Custom_Shader_Demo_v_1_1.rar
we will mainly be talking about the script where everything is happening. the rest is a simple scene with a custom rigid body and skinmesh and good old andro.
the above project demostrates the following:
adding multiple versions of a bump_gloss shader to a script
the application of a shader to skinmesh
loading in custom textures for the fx shader to use
manipulating values
automatically selecting shader version to compile
a force parameter to overide the auto load so you can test each shader model
a function to apply the values runtime to keep things neat and tidy avoiding repetition
also set shader quality
STEP 1 : PLANNING
ok before we do anything we want to figure out what we want to achieve.
well for this project we want to keep it simple so the things we know are:
we want to apply a custom bump_gloss shader to a skinmesh.
we want to use some custom textures not specific to our skinmesh.
we want to prevent the wrong shader model being loaded
we want to be able to overide prevention so we can test specific shader models
we want to provide the user with a choice of shader quality (think LOD for meshes and this is the shader eqivelant)
we want to be able to overide shader values in order to give each skinmesh a different look though shareing the same shader.
so with those objectives in my we can move forward to step 2.
STEP 2 : CREATING THE SCRIPT
ok after you have set up your scene (or just used the above files) we add a script object.
object -> add -> script
alright with our script created we double click on it and once it opens you can rename it to 'script_Bump_Gloss_Shader'
now at this stage all we have is this:
the script hasn't been associated with anything yet so we don't have to worry about anything at this stage. (those familiar with scripting will probably just go ahead and connect your skinmeshes to the script but right now we won't do that to keep things simple)
alright our script is created and we can move on to step 3.
STEP 3 : ADDING IN THE SHADER SOURCE
so, now that we have our script (you might want to save your progress at each step so as not to loose your progress), above
void main() we type the following:
So What the heck does that mean!?!
in between HLSLSTART and HLSLEND we place our shader source code. the word next the HLSLSTART is what we will call the shader. you can name it anything you want but Ideally yo would name it Type of shader,shader version and quality(ididn't worry about quality as for this example we will just use the same shader for all qualities of each shader but if you wanted to you would make a shader for each quality based on shader verion like:
now to add you shader source you have to understand that a .fx file is just a glorified text file so you would just open a .fx file in a text editor like notepad select everything copy and past into your script sections above main. see example project for this in action as tis tutorial will end up a mile long if I include shader sources for all shaders here.
so with our shader source pasted into the script we are ready to move to step 4.
STEP 4 : SETTING UP VALUES THAT WILL BE NEEDED
Above our void main() and below our HLSL Source code we put the values below:
These values will be used to manipulate the shader once it is applied. we can set some of these values directly though for simplicity this helps understand.
FxValues and FxValues are the variables that will overide the shaders source values. this will become clearer shortly.
STEP 5 : INITIALIZING SECTION
ok at this point we add an initializing section to our main()
here is where we will check our shader model, load in any extra textures we need and apply our shaders to the skinmesh.
so we want to first check to see if our shader models are supported by the hardware:
ok now when we first start up our game it will check our init() section and check for the highest shader model support by your video card.
with that information we can then set up so if statement to control which shader to load so
above you will see that we have some if statement that will decide the following (SM 3 example):
if ((VertexShaderModel of the card is equal to 3 & PixelShaderModel of the card is equal to 3 & we are not forceing a shadermodel to be used) or We are forceing a shader madel and it is 3)
if those conditions are met we check for quality
we set our quality value to 0, our hypothetical value for low, So the next statementwould be if(quality is equal to low){this section is active}
ok hope that section makes sense next to set shaders to the skinmesh and load in some custom textures.
To be continued...
You can download the project example here : Custom_Shader_Demo_v_1_1.rar
we will mainly be talking about the script where everything is happening. the rest is a simple scene with a custom rigid body and skinmesh and good old andro.
the above project demostrates the following:
adding multiple versions of a bump_gloss shader to a script
the application of a shader to skinmesh
loading in custom textures for the fx shader to use
manipulating values
automatically selecting shader version to compile
a force parameter to overide the auto load so you can test each shader model
a function to apply the values runtime to keep things neat and tidy avoiding repetition
also set shader quality
STEP 1 : PLANNING
ok before we do anything we want to figure out what we want to achieve.
well for this project we want to keep it simple so the things we know are:
we want to apply a custom bump_gloss shader to a skinmesh.
we want to use some custom textures not specific to our skinmesh.
we want to prevent the wrong shader model being loaded
we want to be able to overide prevention so we can test specific shader models
we want to provide the user with a choice of shader quality (think LOD for meshes and this is the shader eqivelant)
we want to be able to overide shader values in order to give each skinmesh a different look though shareing the same shader.
so with those objectives in my we can move forward to step 2.
STEP 2 : CREATING THE SCRIPT
ok after you have set up your scene (or just used the above files) we add a script object.
object -> add -> script
alright with our script created we double click on it and once it opens you can rename it to 'script_Bump_Gloss_Shader'
now at this stage all we have is this:
Code: [Select]
void Main()
{
//Your script goes here...
}
the script hasn't been associated with anything yet so we don't have to worry about anything at this stage. (those familiar with scripting will probably just go ahead and connect your skinmeshes to the script but right now we won't do that to keep things simple)
alright our script is created and we can move on to step 3.
STEP 3 : ADDING IN THE SHADER SOURCE
so, now that we have our script (you might want to save your progress at each step so as not to loose your progress), above
void main() we type the following:
Code: [Select]
//******************************************************************
//SHADER SOURCE CODE
//******************************************************************
/*HLSLSTART Bump_Gloss_Fx_1_1
HLSLEND*/
/*HLSLSTART Bump_Gloss_Fx_2_0
HLSLEND*/
/*HLSLSTART Bump_Gloss_Fx_3_0
HLSLEND*/
//******************************************************************
//MAIN
//******************************************************************
void Main()
{
//Your script goes here...
}
So What the heck does that mean!?!
in between HLSLSTART and HLSLEND we place our shader source code. the word next the HLSLSTART is what we will call the shader. you can name it anything you want but Ideally yo would name it Type of shader,shader version and quality(ididn't worry about quality as for this example we will just use the same shader for all qualities of each shader but if you wanted to you would make a shader for each quality based on shader verion like:
Code: [Select]
/*HLSLSTART Bump_Gloss_Fx_1_1_Low
HLSLEND*/
/*HLSLSTART Bump_Gloss_Fx_1_1_Med
HLSLEND*/
/*HLSLSTART Bump_Gloss_Fx_1_1_High
HLSLEND*/
/*HLSLSTART Bump_Gloss_Fx_2_0_Low
HLSLEND*/ etc......
now to add you shader source you have to understand that a .fx file is just a glorified text file so you would just open a .fx file in a text editor like notepad select everything copy and past into your script sections above main. see example project for this in action as tis tutorial will end up a mile long if I include shader sources for all shaders here.
so with our shader source pasted into the script we are ready to move to step 4.
STEP 4 : SETTING UP VALUES THAT WILL BE NEEDED
Above our void main() and below our HLSL Source code we put the values below:
Code: [Select]
//******************************************************************
//VALUES
//******************************************************************
//------------------------------------------------------------------
//Shader Quality value
float cShader_Quality = 0; //0 = low,1 = medium,2 = high - we are only useing 0
// This is so the user can select the quality of the shader based on their shader model
// This is good because even though a card that can compile shader model 3
// may not have the power to run it at decent frame rate so you could just copy the
// shader model 1.1 shader and rename it and change it so it will compile for shader model 3
// and be th elow quality version of the shader.
int cForceShaderModel = 0; //use this to force shader model when testing to overid the auto select,
// 1=shader model 1_1, 2=shader model 2_0, 3=shader model 3_0
// just note if you hard ware doesn't run shader model 3 it won't be applied
// set this to 'zero' to auto select
int cShaderModel_Active = 0;//value for storeing the active shader model
//------------------------------------------------------------------
//FxTextures
int cFxTexture_Diffuse_Map = 0;
int cFxTexture_Normal_Map = 0;
//------------------------------------------------------------------
//FxValues
float cTileCount = 64.0f;
float cBumpScale = 1.0f;
float cSpecIntesity = 0.55f;
float cPhongExp = 32.0f;
//******************************************************************
//MAIN
//******************************************************************
//----
These values will be used to manipulate the shader once it is applied. we can set some of these values directly though for simplicity this helps understand.
FxValues and FxValues are the variables that will overide the shaders source values. this will become clearer shortly.
STEP 5 : INITIALIZING SECTION
ok at this point we add an initializing section to our main()
Code: [Select]
//******************************************************************
//MAIN
//******************************************************************
void Main()
{
//***************************************************************
//INITIALIZING SECTION
//***************************************************************
if (iInitializing())
{
}
}
here is where we will check our shader model, load in any extra textures we need and apply our shaders to the skinmesh.
so we want to first check to see if our shader models are supported by the hardware:
Code: [Select]
//---
//***************************************************************
//INITIALIZING SECTION
//***************************************************************
if (iInitializing())
{
//------------------------------------------------------------
//some int used to find out the highest supported shader model of hardware
int VS_Model_Version_Major = iVertexShaderVersion(false);
int PS_Model_Version_Major = iPixelShaderVersion(false);
//---
ok now when we first start up our game it will check our init() section and check for the highest shader model support by your video card.
with that information we can then set up so if statement to control which shader to load so
Code: [Select]
//---
//***************************************************************
//INITIALIZING SECTION
//***************************************************************
if (iInitializing())
{
//------------------------------------------------------------
//some int used to find out the highest supported shader model of hardware
int VS_Model_Version_Major = iVertexShaderVersion(false);
int PS_Model_Version_Major = iPixelShaderVersion(false);
//------------------------------------------------------------
//load and apply shader based on video cards highest supported shader model
//------------------------------------------------------------
if((VS_Model_Version_Major == 1 && PS_Model_Version_Major == 1 && cForceShaderModel == 0) || cForceShaderModel == 1)//shader model 1
{
//---------------------------------------------------------
//quality of shader selection based on shader model 1
if(cShader_Quality == 0)
{
}
if(cShader_Quality == 1)
{
}
if(cShader_Quality == 2)
{
}
//set active shader model
cShaderModel_Active = 1;
//---------------------------------------------------------
}
else if((VS_Model_Version_Major == 2 && PS_Model_Version_Major == 2 && cForceShaderModel == 0) || cForceShaderModel == 2)//shader model 2
{
//---------------------------------------------------------
//quality of shader selection based on shader model 3
if(cShader_Quality == 0)
{
}
if(cShader_Quality == 1)
{
}
if(cShader_Quality == 2)
{
}
//set active shader model
cShaderModel_Active = 2;
//---------------------------------------------------------
}
else if((VS_Model_Version_Major == 3 && PS_Model_Version_Major == 3 && cForceShaderModel == 0) || cForceShaderModel == 3)//shader model 3
{
//---------------------------------------------------------
//quality of shader selection based on shader model 3
if(cShader_Quality == 0)
{
}
if(cShader_Quality == 1)
{
}
if(cShader_Quality == 2)
{
}
//set active shader model
cShaderModel_Active = 3;
//---------------------------------------------------------
}
else//if none of the above combinations are supported do something here
{
//---------------------------------------------------------
//
//---------------------------------------------------------
}
//------------------------------------------------------------
}
//---
above you will see that we have some if statement that will decide the following (SM 3 example):
if ((VertexShaderModel of the card is equal to 3 & PixelShaderModel of the card is equal to 3 & we are not forceing a shadermodel to be used) or We are forceing a shader madel and it is 3)
if those conditions are met we check for quality
we set our quality value to 0, our hypothetical value for low, So the next statementwould be if(quality is equal to low){this section is active}
ok hope that section makes sense next to set shaders to the skinmesh and load in some custom textures.
To be continued...