3D Rad - Free 3D game maker - Forum

This forum is now archived!

This forum is locked, and is a read-only version. A new community-ran forum can be found at classdev.net

News:

The 3DRad community can be found at classdev.net.

Pages: [1]

Author Topic: Shader Pak - Tutorial 1  (Read 4035 times)

« on: December 07, 2009, 01:05:43 AM »
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:
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...
« Last Edit: December 07, 2009, 04:27:28 AM by genetransfer »
using 3Drad 7.22

system specs:
Windows 7 Home Premium 64-bit
Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 CPUs), ~3.2Ghz
8 gig ram
Geforce GTX 650 1024 MB GDDR5
DirectX 11
« Reply #1 on: December 07, 2009, 02:49:32 AM »
Hi genetransfer, the tutorial's looking good!

Just to let you know (before you get complaints): In your Custom Shader Demo download you forgot to include the index jpegs for the Skinmesh and Rigidbody "Default_Base_Project_Terrains"
AKA: The 3D Raddict http://www.3draddict.com/
« Reply #3 on: December 07, 2009, 03:21:34 AM »
Big Thanks AllanF! missed that, updated above files and should be good to go!
@Fernando - lol
using 3Drad 7.22

system specs:
Windows 7 Home Premium 64-bit
Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 CPUs), ~3.2Ghz
8 gig ram
Geforce GTX 650 1024 MB GDDR5
DirectX 11
« Reply #4 on: December 07, 2009, 04:26:53 AM »
Continued...

STEP 6 : INITIALIZING SECTION - TEXTURES

ok, with our Initial shader checking in place we can move on to adding in some other things we will need like textures.
now textures are defined in the shader in 2 parts

Code: [Select]
//shader code

texture diffuseMap : DIFFUSE <
    string ResourceName = "default_color.dds";
    string UIName =  "Color Texture";
    string ResourceType = "2D";
>;

sampler DiffuseMapSampler = sampler_state
{
   Texture   = <diffuseMap>;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = LINEAR;
   AddressU  = WRAP;
   AddressV  = WRAP;
};

above we have our texture as defined in the shader and our texture sampler. the texture sampler is used n the pixel shader so you don't have to worry about that in 3drad except when overiding the defaults.

a word on defaults:

texture diffuseMap,normalMap and environmentMap are 3drads hard coded(for lack of a better word) default values for built in shaders. now we can use these names though any FxTextures we load in won't be picked up by the shader. so what we have to do is overide the default names. We do this by altering the name in the shader source code like below when compared to above.

Code: [Select]
//shader code

texture DiffuseMap : DIFFUSE <
    string ResourceName = "default_color.dds";
    string UIName =  "Color Texture";
    string ResourceType = "2D";
>;

sampler DiffuseMapSampler = sampler_state
{
   Texture   = <DiffuseMap>;
   MinFilter = LINEAR;
   MagFilter = LINEAR;
   MipFilter = LINEAR;
   AddressU  = WRAP;
   AddressV  = WRAP;
};

you see above I changed the name 'diffuseMap' to 'DiffuseMap' in both texture declaration and the sampler.
with that done you can now use your FxTextures to overide the skinmeshes default textures.

now I haven't done that in the above project yet as I wanted to demostrate a standard use but after we get everything done by all means change the names. I did however use 'NormalMap' instead of default 'normalMap' so we could use my custom texture for the normal map.

ok back to the script...

so we want to load in 2 textures for this example(the textures are supplied in the package above)

so we create a couple of string with paths below our shader model check (alternatley you may want to put the strings
inside the if statement but were useing the same textures on all our shader models for this so to keep things simple I just put the paths there):

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);
      //------------------------------------------------------------
      //Texture Strings
      string Diffuse_Map_Path_String = "c:\\3D Rad\\3DRad_res\\fxTextures\\default_fxTextures\\default_diffuse_map.dds";
      string Normal_Map_Path_String = "c:\\3D Rad\\3DRad_res\\fxTextures\\default_fxTextures\\default_normal_map.dds";
      //------------------------------------------------------------
      //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
      {
//---

now that we have our path strings we can create our textures inside the if statements:

remeber we declared our values above the main() section and we will use the texture handle here

cFxTexture_Diffuse_Map
cFxTexture_Normal_Map

//below is just SM1_1 but it will be the same for all 3 versions
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);
      //------------------------------------------------------------
      //Texture Strings
      string Diffuse_Map_Path_String = "c:\\3D Rad\\3DRad_res\\fxTextures\\default_fxTextures\\default_diffuse_map.dds";
      string Normal_Map_Path_String = "c:\\3D Rad\\3DRad_res\\fxTextures\\default_fxTextures\\default_normal_map.dds";
      //------------------------------------------------------------
      //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
      {
         //---------------------------------------------------------
         //create textures
         cFxTexture_Diffuse_Map = iShaderTextureCreate(Diffuse_Map_Path_String);
         cFxTexture_Normal_Map = iShaderTextureCreate(Normal_Map_Path_String);
         //---------------------------------------------------------
         //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;
         //---------------------------------------------------------
      }
//---

and that's pretty much it for creating the textures we will go into applying them later. ok onto step 7.

STEP 7 : INITIALIZING SECTION - APPLYING SHADERS (iShaderSet)

Rememer when we put in our shader source code there was a word we put nect to HLSLSTART
ours were Bump_Gloss_Fx_1_1,Bump_Gloss_Fx_2_0 and Bump_Gloss_Fx_3_0 respectively for the 3 shader sources.

here is where we will need that name:

but before we do Now it is time to associate the skinmeshes we want to be effected by this shader script. so close the script and link up the skinmeshes with the script, either highlight script and tick checkbox next to skinmesh names in the object list or vice versa.

in my script the handles for the skinmesh objects (terrain and andro) are OBJ_0 and OBJ_66

now let's look at the iShaderSet() function closer
in the code below you see that I have placed it in the shader quality if statements. now the only reason I've put the same shader in all 3 qualities is that were just useing the same shader quality no matter what quality is set to. but you would ideally load the LOD version of your shaders this way. sometimes you will use the same shader in all 3 slots and even accross SM 1,2 & 3 like an emmisive shader with no lighting. you would just compile them under different shader models(see source shader techniques)

anyway first param is our skinmesh and the second is our name of the shader. I just used the name dirrectly "Bump_etc.." though you could create a string if you find that easier.

Code: [Select]
//------------------------------------------------------------
      //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
      {
         //---------------------------------------------------------
         //create textures
         cFxTexture_Diffuse_Map = iShaderTextureCreate(Diffuse_Map_Path_String);
         cFxTexture_Normal_Map = iShaderTextureCreate(Normal_Map_Path_String);
         //---------------------------------------------------------
         //quality of shader selection based on shader model 1
         if(cShader_Quality == 0)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_1_1");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_1_1");
         }
         if(cShader_Quality == 1)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_1_1");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_1_1");
         }
         if(cShader_Quality == 2)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_1_1");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_1_1");
         }
         //set active shader model
         cShaderModel_Active = 1;
         //---------------------------------------------------------
      }

alright that's the shaders applied to the skinmeshes.


To be continued about to sit down for dinner back after I feed :)
using 3Drad 7.22

system specs:
Windows 7 Home Premium 64-bit
Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 CPUs), ~3.2Ghz
8 gig ram
Geforce GTX 650 1024 MB GDDR5
DirectX 11
« Reply #5 on: December 07, 2009, 05:12:56 AM »
Gene this is really really great. :) :) :)
I have to check this out later after work .

Just a small tip to avoid using OBJ_X when applying the shader to the SM's you could use a small loop to apply to all linked sm's and make it generic.

int i=0;
while (iObjectHandle(i) != -1)
{
   iShaderSet(iObjectHandle(i),"ShaderName");
   i++;
}
« Reply #6 on: December 07, 2009, 05:22:48 AM »
STEP 8 - UPDATE THE SHADER

Now that we have our shaders applied to our skinmeshes and our alternate textures loaded we can go about updateing the shader values so that we can overide it's original properties.

there are a few more variable we can overide than I will go into here (as this shader also has scrolling capabilities but for now we will just have a look at the main values as define above our main section.

we have:

Code: [Select]
//------------------------------------------------------------------
//FxTextures
int cFxTexture_Diffuse_Map = 0;
int cFxTexture_Normal_Map = 0;
//------------------------------------------------------------------
//values
float cTileCount = 64.0f;
float cBumpScale = 1.0f;
float cSpecIntesity = 0.55f;
float cPhongExp =  32.0f;

shader values have to be called every loop as when more than one skinmesh is useing the shader if you only alter the values of one the rest will look the same.

so we have to apply our textures and values. lets have a look at setting a texture and a float.
in our update section we would set our values like so:

Note: below in the first line I use 'DiffuseMap' but at present this will not overide 3drad's name or the skinmeshes default texture.
         this is because the name is 'diffuseMap' in the shader source itself. afterwerds we can change it in the source to see the
         alternate diffuse map applied to the scene(pink and grey checker).
Code: [Select]
iShaderTextureSet(OBJ_0,"DiffuseMap",cFxTexture_Diffuse_Map);
iShaderTextureSet(OBJ_0,"NormalMap",cFxTexture_Normal_Map);

iShaderFloatSet(OBJ_0,"TileCount",cTileCount );
iShaderFloatSet(OBJ_0,"bumpScale",cBumpScale);
iShaderFloatSet(OBJ_0,"SpecIntesity",cSpecIntesity);
iShaderFloatSet(OBJ_0,"PhongExp",cPhongExp);

iShaderTextureSet(OBJ_66,"DiffuseMap",cFxTexture_Diffuse_Map);
iShaderTextureSet(OBJ_66,"NormalMap",cFxTexture_Normal_Map);

iShaderFloatSet(OBJ_66,"TileCount",1.0f);
iShaderFloatSet(OBJ_66,"bumpScale",1.0f);
iShaderFloatSet(OBJ_66,"SpecIntesity",0.75f);
iShaderFloatSet(OBJ_66,"PhongExp",32.0f);

iShaderTextureSet( our skinmesh object, "the name of our texture name in shader to overide", our texture index);

iShaderFloatSet(our skinmesh object, "the name of our value to overide", our value);

easy enough?

now as you see this way of setting our values will take up miles of code if we want to apply this same shader to a whole bunch of different skinmeshes so we have to find a way to streamline this as well as onit values that are not supported in shader model 1_1 as the shader model 1_1 version you can't alter the Specularity or phong exponent.

so the best way we could do it is to create a function to do this:

you will generally place your function below your main for keeping easy reading as order just helps out a whole bunch when doing any code but you may do it your own way of coarde I just find this a clean orderly method for my way of doing things.

Code: [Select]
//---end of main
}
//---
//******************************************************************
//Function to update shader values easily
//******************************************************************
//this function is to provide easy value setting for each object with applied shader - it keeps your script clean :)

//int OBJ_X
//int ActiveShaderModel
//int DiffuseMapIndex
//int  NormalMapIndex
//float Tile
//float bump
//float spec
//float phong

void ApplyBumpGlossShader(int OBJECT,int ActiveShaderModel,int DiffuseMapIndex,int NormalMapIndex,
                          float Tile,float bump,float spec,float phong)
{
      iShaderTextureSet(OBJECT,"DiffuseMap",DiffuseMapIndex);
      iShaderTextureSet(OBJECT,"NormalMap",NormalMapIndex);
      iShaderFloatSet(OBJECT,"TileCount",Tile);
      iShaderFloatSet(OBJECT,"bumpScale",bump);
      if(ActiveShaderModel > 1)//extra values not in the shader model 1 version of the shaders
      {
         iShaderFloatSet(OBJECT,"SpecIntesity",spec);
         iShaderFloatSet(OBJECT,"PhongExp",phong);
      }
}

this function is a way for us to apply the values to as many skinmeshes as we want while keeping our clutter code to a minimum.

so to use this finction we would place this in our update section (under our Init() section in main:

Code: [Select]
//---start our main
//---initialize section in main
//---
   //***************************************************************
   //UPDATE CODE
   //***************************************************************
   //---------------------------------------------------------------
   //shader code has to be updated every loop
   //---------------------------------------------------------------
   if(cShaderModel_Active > 0)
   {
      //default terrain skinmesh
      ApplyBumpGlossShader(OBJ_0,cShaderModel_Active,cFxTexture_Diffuse_Map,cFxTexture_Normal_Map,cTileCount,cBumpScale,cSpecIntesity,cPhongExp);
      //andro skinmesh
      ApplyBumpGlossShader(OBJ_66,cShaderModel_Active,cFxTexture_Diffuse_Map,cFxTexture_Normal_Map,1.0f,1.0f,0.75f,8.0f);
     
   }
   //***************************************************************
}
//---end of main

//our functions

now in the update code I put if(cShaderModel_Active >0){} all this is saying is that a shader model is supported but it doesn't matter which on here as all the shader have similar values exept the 1_1 version which is missing some editable values due to instruction set limitations.

ApplyBumpGlossShader() <--------------------------------------we call our function
param 1 of function -> SkinMesh object
param 2 of function -> cShaderModel_Active <-------------------we added this to the function so we can tell when to exclude some values.
param 3 of function -> our alternate diffuse map texture index
param 4 of function -> our alternate normal map texture index
param 5 of function -> our UV tilecount value<----1.0f is default
param 6 of function -> our bump scale value <----bump intesity 1.0f is default
param 7 of function -> our specularity value between 0.0f and 1.0f;
param 8 of function -> our phong exponent value (shiny spot size) 0.01f - 256.0f

so when ever we want to apply shader values to a skinmesh we call one line.

so that about does it the game code will look like this:

Code: [Select]
//---Shader source code above here

//////////////////////////////////////////////////////////////////////////////////
//The following is our regular script code which sets and manipulates the shader above
//////////////////////////////////////////////////////////////////////////////////
//******************************************************************
//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;
//------------------------------------------------------------------
//values
float cTileCount = 64.0f;
float cBumpScale = 1.0f;
float cSpecIntesity = 0.55f;
float cPhongExp =  32.0f;
//******************************************************************
//MAIN
//******************************************************************
void Main()
{
   //***************************************************************
   //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);
      //------------------------------------------------------------
      //Texture Strings
      string Diffuse_Map_Path_String = "c:\\3D Rad\\3DRad_res\\fxTextures\\default_fxTextures\\default_diffuse_map.dds";
      string Normal_Map_Path_String = "c:\\3D Rad\\3DRad_res\\fxTextures\\default_fxTextures\\default_normal_map.dds";
      //------------------------------------------------------------
      //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
      {
         //---------------------------------------------------------
         //create textures
         cFxTexture_Diffuse_Map = iShaderTextureCreate(Diffuse_Map_Path_String);
         cFxTexture_Normal_Map = iShaderTextureCreate(Normal_Map_Path_String);
         //---------------------------------------------------------
         //quality of shader selection based on shader model 1
         if(cShader_Quality == 0)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_1_1");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_1_1");
         }
         if(cShader_Quality == 1)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_1_1");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_1_1");
         }
         if(cShader_Quality == 2)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_1_1");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_1_1");
         }
         //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
      {
         //---------------------------------------------------------
         //create textures
         cFxTexture_Diffuse_Map = iShaderTextureCreate(Diffuse_Map_Path_String);
         cFxTexture_Normal_Map = iShaderTextureCreate(Normal_Map_Path_String);
         //---------------------------------------------------------
         //quality of shader selection based on shader model 3
         if(cShader_Quality == 0)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_2_0");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_2_0");
         }
         if(cShader_Quality == 1)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_2_0");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_2_0");
         }
         if(cShader_Quality == 2)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_2_0");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_2_0");
         }
         //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
      {
         //---------------------------------------------------------
         //create textures
         cFxTexture_Diffuse_Map = iShaderTextureCreate(Diffuse_Map_Path_String);
         cFxTexture_Normal_Map = iShaderTextureCreate(Normal_Map_Path_String);
         //---------------------------------------------------------
         //quality of shader selection based on shader model 3

         if(cShader_Quality == 0)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_3_0");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_3_0");
         }
         if(cShader_Quality == 1)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_3_0");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_3_0");
         }
         if(cShader_Quality == 2)
         {
            iShaderSet(OBJ_0,"Bump_Gloss_Fx_3_0");
            iShaderSet(OBJ_66,"Bump_Gloss_Fx_3_0");
         }
         //set active shader model
         cShaderModel_Active = 3;     
         //---------------------------------------------------------
      }
      else//if none of the above combinations are supported do something here
      {
         //---------------------------------------------------------
         //
         //---------------------------------------------------------
      }
      //------------------------------------------------------------
   }
   //***************************************************************
   //UPDATE CODE
   //***************************************************************
   //---------------------------------------------------------------
   //shader code has to be updated every loop
   //---------------------------------------------------------------
   if(cShaderModel_Active > 0)
   {
      //default terrain skinmesh
      ApplyBumpGlossShader(OBJ_0,cShaderModel_Active,cFxTexture_Diffuse_Map,cFxTexture_Normal_Map,cTileCount,cBumpScale,cSpecIntesity,cPhongExp);
      //andro skinmesh
      ApplyBumpGlossShader(OBJ_66,cShaderModel_Active,cFxTexture_Diffuse_Map,cFxTexture_Normal_Map,1.0f,1.0f,0.75f,8.0f);
     
   }
   //***************************************************************
}
//******************************************************************
//Function to update shader values easily
//******************************************************************
//this function is to provide easy value setting for each object with applied shader - it keeps your script clean :)

//int OBJ_X
//int ActiveShaderModel
//int DiffuseMapIndex
//int  NormalMapIndex
//float Tile
//float bump
//float spec
//float phong

void ApplyBumpGlossShader(int OBJECT,int ActiveShaderModel,int DiffuseMapIndex,int NormalMapIndex,
                          float Tile,float bump,float spec,float phong)
{
      iShaderTextureSet(OBJECT,"DiffuseMap",DiffuseMapIndex);
      iShaderTextureSet(OBJECT,"NormalMap",NormalMapIndex);
      iShaderFloatSet(OBJECT,"TileCount",Tile);
      iShaderFloatSet(OBJECT,"bumpScale",bump);
      if(ActiveShaderModel > 1)//extra values not in the shader model 1 version of the shaders
      {
         iShaderFloatSet(OBJECT,"SpecIntesity",spec);
         iShaderFloatSet(OBJECT,"PhongExp",phong);
      }
}


ok so that's a brief over view of what's happenning in this project and should give you some basic ideas of the 3drad side of things workfolow of a script. either tonight or tommorow I'll do one covering cube light's or at least get a project up here tonight for you to try out and I'll explain it tommorrow.

after that I'll go through and show you an overview of making a simple shader and after that I'll do a few other shader like playing animation reels on skinmeshes, noise, and plasma. these ones will be like the alpha shader demo that comes with 3drad as I will let you set up the shader checking.

in some cases you won't be able to reproduce a shader model 3_0 or 2_0 in a 1_1 which means you'll either have to just use a standard shader instead for the 1_1 version or lift the minimum required specs for you game to run. the game I will be working on next year has minimum of shader model 3_0 due to the limitations of the lower shader model, and new video cards are relativley cheap compared to what they used to be. but food for thought anyway. hope this is helpful.
using 3Drad 7.22

system specs:
Windows 7 Home Premium 64-bit
Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 CPUs), ~3.2Ghz
8 gig ram
Geforce GTX 650 1024 MB GDDR5
DirectX 11
« Reply #7 on: December 07, 2009, 05:25:01 AM »
Gene this is really really great. :) :) :)
I have to check this out later after work .

Just a small tip to avoid using OBJ_X when applying the shader to the SM's you could use a small loop to apply to all linked sm's and make it generic.

int i=0;
while (iObjectHandle(i) != -1)
{
   iShaderSet(iObjectHandle(i),"ShaderName");
   i++;
}

for this I'm just keeping it simple but that is a totally awsome method of applying the shader Thanks for that it'll really come in handy!
using 3Drad 7.22

system specs:
Windows 7 Home Premium 64-bit
Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 CPUs), ~3.2Ghz
8 gig ram
Geforce GTX 650 1024 MB GDDR5
DirectX 11
« Reply #8 on: December 10, 2009, 01:51:52 AM »
I added this to the tip & faq thread.
Also I will donate you some $ for your efforts if I ever get home from work :)

But.. would you do custom shader stuff for 3d-Rad for donations ?
« Reply #9 on: December 10, 2009, 02:36:43 AM »
cool Thanks very much shadmar! and thank you for the potential donation really appreciate that :).

Quote
But.. would you do custom shader stuff for 3d-Rad for donations ?

absolutely that won't be a problem. that how I've been doing things for people who use 3Impact of late, but it's probably better for me (and everyone here) to put the shaders up for free and anyone who find's them helpful and can afford it can donate if they want but you can make request if you want and if I can make it happen then I'll put it up as time permits.

thanks again.
« Last Edit: December 10, 2009, 02:42:11 AM by genetransfer »
using 3Drad 7.22

system specs:
Windows 7 Home Premium 64-bit
Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz (4 CPUs), ~3.2Ghz
8 gig ram
Geforce GTX 650 1024 MB GDDR5
DirectX 11
Pages: [1]