4
votes

I've recently started learning HLSL after deciding that I wanted better lighting than what BasicEffect offered. After going through many tutorials, I found this and decided to learn from it: http://brooknovak.wordpress.com/2008/11/13/hlsl-per-pixel-point-light-using-phong-blinn-lighting-model/

It seems that the shader above doesn't work very well in my game though, because my game uses a tile based approach, which means multiple models in a grid-like formation.

What happens is that each of my tiles gets shaded separately from the others. Please see this image for a visual reference: http://i.imgur.com/1Sfi2.png I understand that this is because each tile has it's own model and the shader doesn't take into account other models as it's executing on the meshes of a model.

Now, for the question. How does one go about to shade all the tiles together? I understand that I may have to write a shader from scratch to accomplish this, but if anyone could give me some tips on how to achieve the effect I want, I'd really appreciate it.

It's late so there's a possibility that I've forgotten something. If you need more information, please tell me and I'll add it.

Thanks, Merigrim

EDIT:

Here is my code for drawing a model:

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();
    }
}
1
It doesn't look like your problem is caused by the tiles being seperate since the lighting calculations should take that into account and the resultant lighting would blend smoothly across all the tiles. Can you post your draw code?meds
OK, I added the code to my post.Merigrim
Just a quick suggest, try adding 'pass.Apply()' on top of mesh.Draw().meds
Looks like it could be something to do with your normals. Do tiles that share vertices also share normals?Andrew Russell
You can test to check if your normals are all correct if in your pixel shader the first thing you do is return float4(input.Normal, 1). The colour of all the tiles should be the same when you run it, and in your vertex shader instead of doing this: output.Normal = mul(input.Normal, (float3x3)World); you do output.Normal = input.Normal.meds

1 Answers

1
votes

It seems that the normals were the villain this time around. After correcting the normals in Blender, everything seems to work now.

I want to thank meds and Andrew Russell. Without your help I wouldn't have figured it out!

So now I know, when you have problems with your lighting, always check the normals first.