Ok, so, 5 days ago I posted this into the "Learning to write shaders" forum, nobody really helped me. I figured if I posted it here someone might actualy see it and help me finally figure this out!
This shader, from dark shader, is a normal mapping+specular mapping shader. However, the problem is, it uses bones to calculate the specular/normal mapping stuff correctly in animated models. I'm not using bones or animation anywhere in my game because it is a space shooter and I don't need to. I did try adding bones in blender... just no... blender doesen't export directx right, and I don't want to put in bones I don't need. For the past 5 days I hae been trying to modify the shader myself, and looking on the internet for a shader that does normal+specular mapping without bones. No luck.
So I was hoping someone who knows HLSL could modify this shader for me so it doesen't use or requrie bones. Code is posted below.
string Description = "A model shader from FPSCreator, including normal mapping and specular highlights. Requires a boned weighted model";
string Thumbnail = "BumpBone.png";
float4x4 WorldIT : WorldInverseTranspose;
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4 eyepos : CameraPosition;
float4x4 boneMatrix[32] : BoneMatrixPalette;
/************* Variables **************/
float4 FixedLightSource
<
string UIWidget = "Fixed Light Source";
> = {100.0f, 0.0f, -100.0f, 0.0f};
float4 SurfColor : Diffuse
<
string UIName = "Surface Color";
string UIWidget = "Color Picker";
> = {1.0f, 1.0f, 1.0f, 1.0f};
float SpecExpon : Power
<
string UIWidget = "slider";
float UIMin = 1.0;
float UIMax = 128.0;
float UIStep = 1.0;
string UIName = "specular power";
> = 50.0;
float Alphavalue : AlphaOverride
<
string UIName = "Alpha Override";
> = 0.5f;
/************* TEXTURES **************/
texture colorTexture : DiffuseMap
<
string Name = "default_color.dds";
string type = "2D";
>;
texture normalTexture : DiffuseMap
<
string Name = "default_bump_normal.dds";
string type = "2D";
>;
texture specularMap : ReflectMap
<
string Name = "default_mask.dds";
string type = "2D";
>;
texture noiseMap : ReflectMap
<
string Name = "default_mask.dds";
string type = "2D";
>;
sampler2D colorSampler = sampler_state
{
Texture = <colorTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D normalSampler = sampler_state
{
Texture = <normalTexture>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D reflectSampler = sampler_state
{
Texture = <specularMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
sampler2D noiseSampler = sampler_state
{
Texture = <noiseMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
};
/************* DATA STRUCTS **************/
struct appdata {
float3 Position : POSITION;
float4 UV : TEXCOORD0;
float4 Normal : NORMAL;
float4 Tangent : TANGENT0;
float4 Binormal : BINORMAL0;
float4 Blendweight : TEXCOORD1;
float4 Blendindices : TEXCOORD2;
};
/* data passed from vertex shader to pixel shader */
struct vertexOutput {
float4 HPosition : POSITION;
float4 TexCoord : TEXCOORD0;
float3 LightVec : TEXCOORD1;
float3 WorldNormal : TEXCOORD2;
float3 WorldEyeVec : TEXCOORD3;
float3 WorldTangent : TEXCOORD4;
float3 WorldBinorm : TEXCOORD5;
float Depth : TEXCOORD6;
};
/*********** vertex shader ******/
vertexOutput mainVS(appdata IN)
{
//smooth tangents/binormals
float3 normal = normalize(IN.Normal);
float3 binormal = normalize(cross(normal,normalize(IN.Tangent)));
float3 tangent = normalize(cross(binormal,normal));
vertexOutput OUT;
float3 netPosition = 0, netNormal = 0, netTangent = 0, netBinormal = 0;
//calculate vertex position from bone matrices
for (int i = 0; i < 4; i++)
{
float index = IN.Blendindices[i];
float3x4 model = float3x4(boneMatrix[index][0], boneMatrix[index][1], boneMatrix[index][2]);
float3 vec3 = mul(model,float4(IN.Position, 1));
vec3 = vec3 + boneMatrix[index][3].xyz;
float3x3 rotate = float3x3(model[0].xyz, model[1].xyz, model[2].xyz);
float3 norm3 = mul(rotate, normal);
float3 tangent3 = mul(rotate, tangent);
float3 binormal3 = mul(rotate, binormal);
netPosition += vec3 * IN.Blendweight[i];
netNormal += norm3 * IN.Blendweight[i];
netTangent += tangent3 * IN.Blendweight[i];
netBinormal += binormal3 * IN.Blendweight[i];
}
float4 tempPos = float4(netPosition,1.0);
OUT.HPosition = mul(tempPos, WorldViewProj);
OUT.TexCoord = IN.UV;
OUT.WorldNormal = normalize(mul(netNormal, World).xyz);
OUT.WorldTangent = normalize(mul(netTangent, World).xyz);
OUT.WorldBinorm = normalize(mul(netBinormal, World).xyz);
//create tangent space matrix
float3x3 TSM = { OUT.WorldTangent,OUT.WorldBinorm,OUT.WorldNormal };
TSM = transpose( TSM );
//transform everything to tangent space in VS
float3 worldSpacePos = mul(tempPos, World).xyz;
OUT.LightVec = mul(normalize(FixedLightSource - worldSpacePos),TSM);
OUT.WorldNormal = mul(OUT.WorldNormal, TSM );
OUT.WorldEyeVec = mul(normalize(eyepos - worldSpacePos),TSM);
OUT.Depth = (abs(worldSpacePos.z-(eyepos.z+60)) / 20.0);
return OUT;
}
/********* pixel shader ********/
float4 mainPS(vertexOutput IN) : COLOR
{
float4 height = (tex2D(normalSampler,IN.TexCoord.xy).w) * 0.01 - 0.005;
float3 Vn = normalize(IN.WorldEyeVec);
//view vec already in tangent space from VS
float2 baseuv = IN.TexCoord.xy;
baseuv = baseuv + (Vn.xy*height);
//sharpen base texture with basic edge detect
float4 base = tex2D(colorSampler,baseuv);
base = base + tex2D(colorSampler,baseuv+float2(0.0002,0.0002));
base = base - tex2D(colorSampler,baseuv-float2(0.0002,0.0002));
//multiply by some noise
base = base * (1 - tex2D(noiseSampler,baseuv*2).b*0.1);// + tex2D(reflectSampler,baseuv)*0.5;
//get normal
float3 normal = tex2D(normalSampler,baseuv).rgb*2 - 1;
normal = normal + (tex2D(noiseSampler,baseuv*3)*2-1)*0.025; //test normal with noise
normal = normalize(normal);
float3 Ln = normalize(IN.LightVec);
float3 Hn = normalize(Vn + Ln);
float4 lighting = lit(dot(Ln,normal),dot(Hn,normal),SpecExpon);
//multiply spec by some noise to reduce plastic look
float spec = lighting.z*tex2D(reflectSampler,baseuv).r * (tex2D(noiseSampler,baseuv*3).b+0.3)*1.3;
float diff = lighting.y;
float4 result = diff*base*SurfColor + spec;
result.w=base.w * Alphavalue;
return result;
}
/****** technique *******/
technique dx9textured
{
pass p0
{
// lighting
Lighting = FALSE;
SpecularEnable = FALSE;
FogEnable = FALSE;
VertexShader = compile vs_2_0 mainVS();
ZEnable = true;
ZWriteEnable = true;
CullMode = None;
PixelShader = compile ps_2_0 mainPS();
}
}
If this helps, I think the root of the problem is near the top where it says:
"float4x4 boneMatrix[32] : BoneMatrixPalette;"
The dark shader help file says:
"BoneMatrixPalette - an array of matrices for calculating the position of vertices from bones in the model"
So, I don't know but I think if you change that everything else will fall into place. But maybe not.
Thank you!



Your signature has been erased by a mod - Please reduce it to (600x120) maximum size