HLSL / XNA环境光纹理与多通道照明混合在一起

时间:2013-10-19 21:43:33

标签: c# models xna-4.0 hlsl light

我最近在照明方面遇到了一些麻烦。我在google上找到了一个在这个例子上工作得很好的源代码。但是,当我尝试将它实现到我当前的项目时,我得到了一些非常奇怪的错误。主要的一点是,当我只激活环境光时,我的纹理会“混淆”,这意味着模型会获得另一个you can see it there的纹理。

我对模型的每个网格都使用相同的效果。我想这可能是问题,但我真的不知道如何“重置”新模型的效果。有可能吗?

这是我的着色器:

float4x4 WVP;

float4x4 WVP;
float3x3 World;

float3 Ke;
float3 Ka;
float3 Kd;
float3 Ks;
float specularPower;

float3 globalAmbient;
float3 lightColor;

float3 eyePosition;
float3 lightDirection;
float3 lightPosition;
float spotPower;

texture2D Texture;
sampler2D texSampler = sampler_state
{
    Texture = <Texture>;
    MinFilter = anisotropic;
    MagFilter = anisotropic;
    MipFilter = linear;
    MaxAnisotropy = 16;
};

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

struct VertexShaderOutput
{
    float4 Position : POSITION0;
    float2 Texture  : TEXCOORD0;
    float3 PositionO: TEXCOORD1;
    float3 Normal   : NORMAL0;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

    output.Position = mul(input.Position, WVP);

    output.Normal = input.Normal;

    output.PositionO = input.Position.xyz;

    output.Texture = input.Texture;

    return output;
}

float4 PSAmbient(VertexShaderOutput input) : COLOR0
{
    return float4(Ka*globalAmbient + Ke,1) * tex2D(texSampler,input.Texture);
}

float4 PSDirectionalLight(VertexShaderOutput input) : COLOR0
{
    //Difuze
    float3 L = normalize(-lightDirection);
    float diffuseLight = max(dot(input.Normal,L), 0);
    float3 diffuse = Kd*lightColor*diffuseLight;

    //Specular
    float3 V = normalize(eyePosition - input.PositionO);
    float3 H = normalize(L + V);
    float specularLight = pow(max(dot(input.Normal,H),0),specularPower);
    if(diffuseLight<=0) specularLight=0;
    float3 specular = Ks * lightColor * specularLight;

    //sum all light components
    float3 light = diffuse + specular;

    return float4(light,1) * tex2D(texSampler,input.Texture);
}

technique MultiPassLight
{
    pass Ambient
    {
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PSAmbient();
    }
    pass Directional
    {
        PixelShader = compile ps_3_0 PSDirectionalLight();
    }
}

以下是我实际应用效果的方法:

public void ApplyLights(ModelMesh mesh, Matrix world, 
    Texture2D modelTexture, Camera camera, Effect effect, 
    GraphicsDevice graphicsDevice)
{
    graphicsDevice.BlendState = BlendState.Opaque;

    effect.CurrentTechnique.Passes["Ambient"].Apply();

    foreach (ModelMeshPart part in mesh.MeshParts)
    {
        graphicsDevice.SetVertexBuffer(part.VertexBuffer);
        graphicsDevice.Indices = part.IndexBuffer;

        // Texturing
        graphicsDevice.BlendState = BlendState.AlphaBlend;
        if (modelTexture != null)
        {
            effect.Parameters["Texture"].SetValue(
                modelTexture
            );
        }

        graphicsDevice.DrawIndexedPrimitives(
            PrimitiveType.TriangleList,
            part.VertexOffset,
            0,
            part.NumVertices,
            part.StartIndex,
            part.PrimitiveCount
        );

        // Applying our shader to all the mesh parts
        effect.Parameters["WVP"].SetValue(
            world *
            camera.View *
            camera.Projection
        );
        effect.Parameters["World"].SetValue(world);
        effect.Parameters["eyePosition"].SetValue(
            camera.Position
        );


        graphicsDevice.BlendState = BlendState.Additive;

        // Drawing lights
        foreach (DirectionalLight light in DirectionalLights)
        {
            effect.Parameters["lightColor"].SetValue(light.Color.ToVector3());
            effect.Parameters["lightDirection"].SetValue(light.Direction);

            // Applying changes and drawing them
            effect.CurrentTechnique.Passes["Directional"].Apply();
            graphicsDevice.DrawIndexedPrimitives(
                PrimitiveType.TriangleList, 
                part.VertexOffset, 
                0, 
                part.NumVertices, 
                part.StartIndex, 
                part.PrimitiveCount
            ); 
        }
    }

我在加载效果时也应用了这个:

effect.Parameters["lightColor"].SetValue(Color.White.ToVector3());
effect.Parameters["globalAmbient"].SetValue(Color.White.ToVector3());
effect.Parameters["Ke"].SetValue(0.0f);
effect.Parameters["Ka"].SetValue(0.01f);
effect.Parameters["Kd"].SetValue(1.0f);
effect.Parameters["Ks"].SetValue(0.3f);
effect.Parameters["specularPower"].SetValue(100);

非常感谢

更新: 我试图在绘制时为每个模型加载效果,但它似乎没有改变任何东西。我想这是因为XNA检测到之前已经加载了效果,并且不想加载新效果。知道为什么吗?

1 个答案:

答案 0 :(得分:2)

好吧,问题实际上是非常愚蠢的。我对每个网格应用了相同的效果。我只是在加载模型时添加了一个Effect.Clone()并且它有效!