|
HOME PAGE
|
|
|
WRITING CUSTOM SHADERS
FOR THE SKINMESH OBJECT
The SkinMesh object comes with a full set of pre-defined shaders
to render the most common types of surfaces.
By using scripting you can write additional
custom shaders in HLSL language.
The HLSL code for each shader in your script must be within a section delimited by '/*HLSLSTART name' and 'HLSLEND*/' tags.
You can define up to 256 shaders in your script code.
For a working example, please run 3D Rad, press Ctrl+O and open the ShaderDemo.3dr project. Double-click
the Script: shader code item in the Object List to see the shader code.
The demo shader renders the SkinMesh surface with a technique similar to the default shader, but with an additional
parameter that defines the global transparency for the rendered SkinMesh.
The demo script code (see Main() section) applies the shader to the SkinMesh at startup (iShaderSet() function call) and then,
run-time, simply modifies the transparency (alpha) parameter to make the SkinMesh gradually disappear.
NOTE: the Use custom shaders option, on the SkinMesh property dialog, must be
checked, before a custom shader can be applied to the SkinMesh.
GUIDELINES
When writing your own shaders for 3D Rad, the first rule to keep in mind is that your HLSL code must include rendering
techniques for both skinned (bone animated) and non-skinned (static) meshes.
In the example script, you can see that there are two distinct technique declarations named technique Skinning
and technique NoSkinning. These declarations must be present in your HLSL code section.
You can specify the minimum shader model (hardware) required to process your shader by using the
compile vs_X_X syntax for the vertex shader and the compile ps_X_X syntax for the pixel shader.
The 'X_X' is the shader model version. In the example, I have used '1_1' which means that the shader will
require at least a video card supporting shader model 1.1. This makes my shader work on practically any PC
because shader model 1.1 is supported by almost all video cards.
Keep in mind that the set of HLSL functions you can use in your shader code will change when you change shader model
version. So, for example, when programming for shader model '1_1', you have a limited set of HLSL functions compared
to programming for shader model '2_0' or '3_0'.
Obviously, writing a '3_0' shader will cause your SkinMesh rendering to
fail on a PC which only supports up to shader model 2.0 for example.
However, because you can add multiple HLSL sections to your script (define multiple shaders in a single script), you can
write a different shader for each different shader model (hardware) you want to target.
Then, in your script,
you can use the iVertexShaderVersion() and iPixelShaderVersion() functions to determine the shader model version supported
by the PC your project is running on and apply the right shader to your SkinMesh by using the iShaderSet() function.
Please see the script API reference for details.
SHADER PARAMETERS
Typically, the shader expects that some parameters defined in the HLSL code (like the fAlpha parameter in the example)
will be set, run-time, by the script, in the void Main() function.
To do this you can use functions like iShaderFloatSet() and iShaderTextureSet().
In the example I used the iShaderFloatSet() function to dynamically set the transparency (alpha) parameter.
Please see the script API reference for details about iShader...() functions.
Note that 3D Rad always dynamically sets some parameters (like vertex transformation matrices, textures, effect-specific parameters etc).
They are those that are required by the pre-defined shader you select on the property dialog for the SkinMesh object.
The parameters always controlled by 3D Rad when the default shader is selected on the SkinMesh dialog,
are as follows:
------------------------------------------------------------
float4x3 amPalette[MATRIX_PALETTE_SIZE_DEFAULT];
float4x4 mxWorld : WORLD;
float4x4 mxWorldIT : amPalette;
float4x4 mxViewProj : VIEWPROJECTION;
float4 lightDir : DIRECTION = {0.0f,0.0f,-1.0f,1.0f};
float4 lightColor : DIFFUSE = {1.0f,1.0f,1.0f,1.0f};
float4 lightAmbient : AMBIENT = {0.1f,0.1f,0.1f,1.0f};
float4 materialDiffuse : DIFFUSE = {0.8f,0.8f,0.8f,1.0f};
float4 materialSpecular : SPECULAR = {1.0f,1.0f,1.0f,1.0f};
float materialSpecularPower : SPECULARPOWER = 30.0f;
float3 worldEyePos = {0.0f,0.0f,0.0f);
texture diffuseMap;
int boneCount = 2;
------------------------------------------------------------
You can see all of them declared and used in the example script.
Note that the only user-defined parameter in the example is fAlpha.
This parameter is controlled by the example script, not 3D Rad, as said.
NOTE: the values assigned to the variables are simply the defaults the HLSL code
will use, if 3D Rad or the script fail to set the parameter.
Additional parameters automatically controlled by 3D Rad when you select another pre-defined shader on the SkinMesh dialog are
listed below. Please keep in mind that they may or may not be controlled, depending on the SkinMesh dialog settings.
------------------------------------------------------------
float ambient = 0.5f;
texture environmentMap;
float environmentMapBlend = 0.25;
texture normalMap;
float bumpScale = 1.0f;
float4 pointLightPos01 = {0.0f,0.0f,-1.0f,1.0f};
float4 pointLightPos02 = {1.0f,0.0f,-1.0f,1.0f};
float4 pointLightPos03 = {-1.0f,0.0f,-1.0f,1.0f};
float4 pointLightPos04 = {-1.0f,0.0f,1.0f,1.0f};
float4 pointLightColor01 = {1.0f,1.0f,1.0f,1.0f};
float4 pointLightColor02 = {1.0f,0.0f,0.0f,1.0f};
float4 pointLightColor03 = {0.0f,1.0f,0.0f,1.0f};
float4 pointLightColor04 = {0.0f,0.0f,1.0f,1.0f};
float3 fogColor = {1.0f,1.0f,1.0f};
float fogStart = 1;
float fogEnd = 25;
------------------------------------------------------------
These extra parameters aren't used in the example. I have declared them anyway
for your convenience, just in case you want to use them.
USING TOOLS TO WRITE SHADERS
You can use tools like RenderMonkey and FX
Composer, but, in order to successfully port the shader to 3D Rad, you will have to understand how
the shader in these tools uses the parameters the tool is automatically controlling.
Please keep in mind that, to program custom shaders for 3D Rad, some knowledge of HLSL
programming is required.
|
|
|
|
|