纹理混合+光照?
这样的shader哪错了,渲染的效果是全黑:
struct Mtrl
{
float4 ambient;
float4 diffuse;
float4 spec;
float specPower;
};
struct SpotLight
{
float4 ambient;
float4 diffuse;
float4 spec;
float3 posW;
float3 dirW;
float spotPower;
};
uniform extern float4x4 gViewProj;
uniform extern texture gTex0;
uniform extern texture gTex1;
uniform extern texture gTex2; //三张文理图
uniform extern texture gBlendMap; //extern 表示输入Shader的这个变量的值是统一的(也就是说这个变量的初始化值来源于Shader外界的输入).
uniform extern float3 gEyePosW; //世界空间的摄像机的位置
//加入灯光
uniform extern Mtrl gMtrl;
uniform extern SpotLight gLight;
static float gTexScale = 16.0f;
sampler Tex0S = sampler_state
{
Texture = <gTex0>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
sampler Tex1S = sampler_state
{
Texture = <gTex1>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
sampler Tex2S = sampler_state
{
Texture = <gTex2>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
sampler BlendMapS = sampler_state
{
Texture = <gBlendMap>;
MinFilter = LINEAR;
MagFilter = LINEAR;
MipFilter = POINT;
AddressU = WRAP;
AddressV = WRAP;
};
struct OutputVS
{
float4 posH :POSITION0;
float2 tiledTexC :TEXCOORD0;
float2 nonTiledTexC :TEXCOORD1;
float shade :TEXCOORD2;
float3 normalW :TEXCOORD3;
float3 posW :TEXCOORD4;//经过世界变换的顶点坐标
float3 toEyeW :TEXCOORD5;//方向向量
};
OutputVS TerrainVS(float3 posL:POSITION0,
float3 normalL:NORMAL0,
float2 tex0:TEXCOORD0)
{
OutputVS outVS = (OutputVS)0;
float3 normalW = normalL;//这里的地形不需要经过世界变换
outVS.shade = 1.0f;
outVS.posH = mul(float4(posL,1.0f),gViewProj);
outVS.tiledTexC = tex0*gTexScale;
outVS.nonTiledTexC = tex0;
outVS.normalW = normalW;
outVS.posW = posL;
outVS.toEyeW = gEyePosW - posL; //摄像机和顶点的位置
return outVS;
}
float4 TerrainPS(float2 tiledTexC:TEXCOORD0,
float2 nonTiledTexC:TEXCOORD1,
float shade:TEXCOORD2,
float3 normalW : TEXCOORD3,
float3 posW : TEXCOORD4,
float3 toEyeW : TEXCOORD5
):COLOR
{
//计算光照
normalW = normalize(normalW);
toEyeW = normalize(toEyeW);
float3 lightVecW = normalize(gLight.posW - posW);
//计算反射向量
float3 r = reflect(-lightVecW, normalW);
//计算光照
float t = pow(max(dot(r, toEyeW), 0.00001f), gMtrl.specPower);
float s = max(dot(lightVecW, normalW), 0.00001f);
float3 spec = t * (gMtrl.spec * gLight.spec).rgb;
float3 diffuse = s * (gMtrl.diffuse * gLight.diffuse).rgb;//待看
float3 ambient = gMtrl.ambient * gLight.ambient;
float spot = pow(max( dot(-lightVecW, gLight.dirW), 0.00001f), gLight.spotPower);
//计算纹理的颜色
float3 c0 = tex2D(Tex0S,tiledTexC).rgb;
float3 c1 = tex2D(Tex1S,tiledTexC).rgb;
float3 c2 = tex2D(Tex2S,tiledTexC).rgb;
float3 B = tex2D(BlendMapS,nonTiledTexC).rgb;
float totalInverse = 1.0f/(B.r+B.g+B.b);
c0 = c0 * B.r * totalInverse;
c1 = c1 * B.g * totalInverse;
c2 = c2 * B.b * totalInverse;
float3 final = (c0 + c1 + c2) * shade;
float4 texColor = float4(final,1.0f);
//计算最后的颜色
float3 litColor = spot*ambient*texColor.rgb + spot*1.0f*(diffuse*texColor.rgb + spec);
return float4(litColor,gMtrl.diffuse.a*texColor.a);
}
technique TerrainTech
{
pass P0
{
vertexShader = compile vs_3_0 TerrainVS();
pixelShader = compile ps_3_0 TerrainPS();
}
}
[解决办法]
是不是传入的数据有问题?
[解决办法]
修改你的shader,分别只输出3张贴图的某一张,然后看输出的纹理是否正确。