【发布时间】:2014-10-09 07:31:09
【问题描述】:
真的希望有人可以在这里帮助我 - 我很少无法解决 C# 中的错误,因为我在这方面有相当多的经验,但我没有太多关于 HLSL 的工作。
下面链接的图片是同一模型(运行时以编程方式生成)两次,第一次(白色)使用 BasicEffect,第二次使用我的自定义着色器,如下所示。它与 BasicEffect 一起工作的事实让我认为生成模型的法线或类似的东西不是问题。
我已经包含了不同级别的细分以更好地说明问题。值得一提的是,这两种效果都使用了相同的光照方向。
https://imagizer.imageshack.us/v2/801x721q90/673/qvXyBk.png
这是我的着色器代码(随意拆开,非常欢迎任何提示):
float4x4 WorldViewProj;
float4x4 NormalRotation = float4x4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1);
float4 ModelColor = float4(1, 1, 1, 1);
bool TextureEnabled = false;
Texture ModelTexture;
sampler ColoredTextureSampler = sampler_state
{
texture = <ModelTexture>;
magfilter = LINEAR; minfilter = LINEAR; mipfilter = LINEAR;
AddressU = mirror; AddressV = mirror;
};
float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.1;
float3 DiffuseLightDirection = float3(1, 0, 0);
float4 DiffuseColor = float4(1, 1, 1, 1);
float DiffuseIntensity = 1.0;
struct VertexShaderInput
{
float4 Position : POSITION0;
float4 Normal : NORMAL0;
float2 TextureCoordinates : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
float2 TextureCoordinates : TEXCOORD0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;
output.Position = mul(input.Position, WorldViewProj);
float4 normal = mul(input.Normal, NormalRotation);
float lightIntensity = dot(normal, DiffuseLightDirection);
output.Color = saturate(DiffuseColor * DiffuseIntensity * lightIntensity);
output.TextureCoordinates = input.TextureCoordinates;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float4 pixBaseColor = ModelColor;
if (TextureEnabled == true)
{
pixBaseColor = tex2D(ColoredTextureSampler, input.TextureCoordinates);
}
float4 lighting = saturate((input.Color + AmbientColor * AmbientIntensity) * pixBaseColor);
return lighting;
}
technique BestCurrent
{
pass Pass1
{
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
【问题讨论】:
-
要尝试的几件事: 1) 确保在点积中使用法线之前对其进行规范化。 2)法线和光的方向向量需要指向相同的点积方向(从表面出来)。如果您对光的意图是 +x 方向,那么您需要在进行点积之前将其否定为 -x 方向。 3) 可能不是问题,但要确保
input.Normal的第四个分量为0 或NormalRotation矩阵不包含任何翻译。 -
在点积中使用之前还要确保光的方向是标准化的
-
第一句话立即解决了我的问题,将法线归一化。可以花几天时间俯瞰它!非常感谢您的快速回复。如果您将其作为答案发送,我会标记它。