0

所以我想自己学习一些 HLSL,但我很难过。我正在编写一个自定义着色器,它具有环境照明和简单的点光源颜色。

这是着色器代码:

`float4x4 World;
float4x4 View;
float4x4 Projection;

// TODO: add effect parameters here.

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.5;

float4 DiffuseColor = float4(1, 1, 1, 1);

float3 LightPosition = float3(32, 32, 64);
float4 LightDiffuseColor = float4(0.3, 0.05, 0, 1); // intensity multiplier
float4 LightSpecularColor = float4(1, 1, 1, 1); // intensity multiplier
float LightDistance = 50;

texture Texture;
sampler2D textureSampler = sampler_state {
    Texture = (Texture);
    MinFilter = Point;
    MagFilter = Point;
    AddressU = Wrap;
    AddressV = Wrap;
};

struct VertexShaderInput
{
    float4 Pos: POSITION;
    float2 TexCoords : TEXCOORD0;
    float4 Normal : NORMAL0;
};

struct VertexShaderOutput
{
    float4 PosOut : POSITION;
    float2 TextureCoordinate : TEXCOORD0;
    float3 Normal : TEXCOORD1;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    float4 worldPosition = mul(input.Pos, World);
    float4 viewPosition = mul(worldPosition, View);
    output.PosOut= mul(viewPosition, Projection);

    output.TextureCoordinate = input.TexCoords;
    output.Normal = mul(input.Normal, World);

    return output;
}

float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{ 
    float attenuation = saturate(1.0f - (length(input.PosOut - LightPosition) / LightDistance));

    float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
    textureColor.a = 1;

    float4 lightCol = 
    //LightDiffuseColor;
    mul(LightDiffuseColor, attenuation); 

    float4 ambient = (AmbientColor * AmbientIntensity);
    ambient.a = 1;

    return saturate(textureColor * ambient + lightCol);
}

technique Textured
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_2_0 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}

`

问题已缩小到像素着色器中的这一部分:

float attenuation = saturate(1.0f - (length(input.PosOut - LightPosition) / LightDistance));    
...
float4 lightCol = 
//LightDiffuseColor;
mul(LightDiffuseColor, attenuation); 

return saturate(textureColor * ambient + lightCol);

如果我只使用 LightDiffuseColor,它会正常工作,但是一旦我尝试将它相乘,它就会抛出这个错误:

GameContentShadersSimpleTexture.fx(35,21) error X4502 invalid ps_2_0 input semantic 'POSITION'

我正在使用 XNA 作为引擎。我在这里有点难过。谁能帮我?

谢谢,涂鸦。

编辑:Mkay,所以我把它缩小到一个非常精确的位置。当调用长度函数时(input.PosOut - LightPosition)

4

1 回答 1

0

好的,所以我有一个解决方案。我应该使用顶点光照,而不是对点光源使用逐像素光照。像素着色器输入不支持位置语义,如此处所示。但是位置仍然需要作为顶点着色器的输出,因此它包含在像素着色器输入中,但不能使用。我的更新代码如下所示:

float4x4 World;
float4x4 View;
float4x4 Projection;

// TODO: add effect parameters here.

float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.5;

float4 DiffuseColor = float4(1, 1, 1, 1);

float3 LightPosition = float3(32, 32, 48);
float4 LightDiffuseColor = float4(0.3, 0.05, 0, 1);
float4 LightSpecularColor = float4(1, 1, 1, 1); // intensity multiplier
float LightDistance = 100;

texture Texture;
sampler2D textureSampler = sampler_state {
    Texture = (Texture);
    MinFilter = Point;
    MagFilter = Point;
    AddressU = Wrap;
    AddressV = Wrap;
};

struct VertexShaderInput
{
    float4 Position : POSITION0;
    float4 Normal : NORMAL0;
    float2 TextureCoordinate : TEXCOORD0;
};

struct PixelShaderInput
{
    float4 Position : POSITION0;
    float4 Color : COLOR1;
    float3 Normal : TEXCOORD0;
    float2 TextureCoordinate : TEXCOORD1;
};

PixelShaderInput VertexShaderFunction(VertexShaderInput input)
{
    PixelShaderInput output;

    float4 worldPosition = mul(input.Position, World);
    float4 viewPosition = mul(worldPosition, View);
    output.Position= mul(viewPosition, Projection);

    output.Color = mul(LightDiffuseColor,saturate(1.0f - (length(mul(input.Position, World) - LightPosition) / LightDistance)));
    output.TextureCoordinate = input.TextureCoordinate;
    output.Normal = mul(input.Normal, World);

    return output;
}

float4 PixelShaderFunction(PixelShaderInput input) : COLOR0
{ 
    float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
    textureColor.a = 1;

    float4 ambient = (AmbientColor * AmbientIntensity);
    ambient.a = 1;

    return saturate(textureColor * ambient + input.Color);
}

technique Textured
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_1_1 VertexShaderFunction();
        PixelShader = compile ps_2_0 PixelShaderFunction();
    }
}
于 2013-09-21T20:04:40.637 回答