【问题标题】:HLSL Point Light shader causing tiles to be shaded separatelyHLSL 点光源着色器导致瓷砖被单独着色
【发布时间】:2011-07-29 00:05:39
【问题描述】:

在决定我想要比 BasicEffect 提供的更好的照明后,我最近开始学习 HLSL。在浏览了许多教程后,我发现了这一点并决定从中学习: http://brooknovak.wordpress.com/2008/11/13/hlsl-per-pixel-point-light-using-phong-blinn-lighting-model/

不过,上面的着色器似乎在我的游戏中效果不佳,因为我的游戏使用基于图块的方法,这意味着网格状结构中的多个模型。

发生的情况是,我的每个图块都与其他图块分开着色。请参阅此图像以获得视觉参考: http://i.imgur.com/1Sfi2.png 我知道这是因为每个图块都有自己的模型,并且着色器在模型的网格上执行时不会考虑其他模型。

现在,问题来了。如何将所有瓷砖遮蔽在一起?我知道我可能必须从头开始编写着色器才能完成此操作,但如果有人能给我一些关于如何实现我想要的效果的提示,我将不胜感激。

时间不早了,我可能忘记了什么。如果您需要更多信息,请告诉我,我会添加它。

谢谢你,梅里格林

编辑:

这是我绘制模型的代码:

public void DrawModel(Model model, Matrix modelTransform, Matrix[] absoluteBoneTransforms, Vector3 color, float alpha = 1.0f, Texture2D texture = null)
{
    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
    {
        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (ModelMeshPart part in mesh.MeshParts)
            {
                part.Effect = effect;
                Matrix world = absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform;
                effect.Parameters["World"].SetValue(absoluteBoneTransforms[mesh.ParentBone.Index] * modelTransform);
                effect.Parameters["View"].SetValue(camera.view);
                effect.Parameters["Projection"].SetValue(camera.projection);
                effect.Parameters["CameraPos"].SetValue(camera.cameraPosition);
                Vector3 lookAt = camera.cameraPosition + camera.cameraDirection;
                effect.Parameters["LightPosition"].SetValue(new Vector3(lookAt.X, 1.0f, lookAt.Z - 5.0f));
                effect.Parameters["LightDiffuseColor"].SetValue(new Vector3(0.45f, 0.45f, 0.45f));
                effect.Parameters["LightSpecularColor"].SetValue(new Vector3(0.45f, 0.45f, 0.45f));
                effect.Parameters["LightDistanceSquared"].SetValue(40.0f);
                effect.Parameters["DiffuseColor"].SetValue(color);
                effect.Parameters["AmbientLightColor"].SetValue(Color.Black.ToVector3());
                effect.Parameters["EmissiveColor"].SetValue(Color.White.ToVector3());
                effect.Parameters["SpecularColor"].SetValue(Color.White.ToVector3());
                effect.Parameters["SpecularPower"].SetValue(10.0f);
                if (texture != null)
                {
                    effect.Parameters["DiffuseTexture"].SetValue(texture);
                }
                mesh.Draw();
            }
        }
        pass.Apply();
    }
}

【问题讨论】:

  • 看起来您的问题不是由单独的图块引起的,因为照明计算应考虑到这一点,并且生成的照明将在所有图块中平滑混合。你能发布你的抽奖代码吗?
  • 好的,我在帖子中添加了代码。
  • 只是一个快速建议,尝试在 mesh.Draw() 之上添加“pass.Apply()”。
  • 看起来这可能与您的法线有关。共享顶点的图块是否也共享法线?
  • 如果在像素着色器中您要做的第一件事是返回 float4(input.Normal, 1),您可以测试检查您的法线是否全部正确。运行时所有图块的颜色应该相同,并且在顶点着色器中而不是这样做: output.Normal = mul(input.Normal, (float3x3)World);你做 output.Normal = input.Normal.

标签: graphics 3d xna shader hlsl


【解决方案1】:

看来这一次普通人是反派。在 Blender 中更正法线后,现在一切似乎都正常了。

我要感谢 meds 和 Andrew Russell。如果没有你的帮助,我不会弄明白的!

所以现在我知道了,当您的照明出现问题时,请务必先检查法线。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多