我的问题是,当我从点光源计算和添加镜面光时,如果我在像素着色器中硬编码一个数字就可以了,但是如果该数字来自一个变量(它是指数),那么结果是块状的,阴影和光线之间非常鲜明的对比。
这是我的像素着色器代码:
cbuffer lights :register(b0)
{
row_major float4x4 worldM;
row_major float4x4 TransformM;
float4 PointLightpos;
float4 PointLightcolor;
float4 AmbientLightColor;
float4 intensities;
float4 attenuations;
float4 cameraPos;
float4 ambient;
float4 diffuse;
float4 specular;
row_major float4x4 skinMatrix[50];
};
Texture2D tex : register(t0);
Texture2D nTex: register(t1);
SamplerState mySampler : SAMPLER;
struct Input {
float4 outPos : SV_POSITION;
float3 Color : COLOR;
float3 normal : NORMAL;
float2 UV : TEXCOORD;
float4 worldPos : POSITION;
float3 tangents : TANGENT;
};
struct Output
{
float4 Color : SV_TARGET;
};
Output main(Input input)
{
Output output;
//* ambientspecularExp
//pointLightDiffuse
float4 plight = PointLightcolor * intensities.y;
float4 pointLightDir = normalize(PointLightpos - input.worldPos);
float4 normalW = float4(input.normal.x, input.normal.y, input.normal.z, 1);
float4 diffuseFactor = max(dot(pointLightDir, normalW), 0);
//Atten = 1/( att0i + att1i * d + att2i * d²)
float d = distance(PointLightpos, input.worldPos);
float attenuation = 1 / (attenuations.x + (attenuations.y * d) + (attenuations.z * pow(d, 2)));
float4 aL = (AmbientLightColor * intensities.x)*attenuation;
//diffuseFactor *= attenuation;
float4 diffuseLight = (plight *diffuseFactor * diffuse)*attenuation;
//Specular * PointLightcolor
float r = reflect(-pointLightDir, normalW);
float v = normalize(cameraPos - input.worldPos);
//float4 spec = float4(max((pow(dot(r, v), specularExp) * specular), 0));
float4 spec = float4(((pow(dot(r, v), intensities.z) * specular))*attenuation);