如何在XNA中消除光/影对我模型的影响?

时间:2013-02-26 21:08:55

标签: graphics model 3d xna game-engine

我正在开发一个小型游戏,我会绘制一个具有重复纹理的场地(陆地)。我的问题是渲染结果。这给人的印象是看到我的立方体周围的一切看起来就像一个光影。 是否可以在我的绘图功能中标准化灯光或去除阴影效果?

抱歉我的英文不好..

以下是更好地了解我的问题的屏幕截图。 enter image description here

这里是我的代码绘制函数(带有vertexbuffer的实例化模型)

    // Draw Function (instancing model - vertexbuffer)
    public void DrawModelHardwareInstancing(Model model,Texture2D texture, Matrix[] modelBones,
                                     Matrix[] instances, Matrix view, Matrix projection)
    {
        if (instances.Length == 0)
            return;

        // If we have more instances than room in our vertex buffer, grow it to the neccessary size.
        if ((instanceVertexBuffer == null) ||
            (instances.Length > instanceVertexBuffer.VertexCount))
        {
            if (instanceVertexBuffer != null)
                instanceVertexBuffer.Dispose();

            instanceVertexBuffer = new DynamicVertexBuffer(Game.GraphicsDevice, instanceVertexDeclaration,
                                                           instances.Length, BufferUsage.WriteOnly);
        }

        // Transfer the latest instance transform matrices into the instanceVertexBuffer.
        instanceVertexBuffer.SetData(instances, 0, instances.Length, SetDataOptions.Discard);

        foreach (ModelMesh mesh in model.Meshes)
        {
            foreach (ModelMeshPart meshPart in mesh.MeshParts)
            {
                // Tell the GPU to read from both the model vertex buffer plus our instanceVertexBuffer.
                Game.GraphicsDevice.SetVertexBuffers(
                    new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0),
                    new VertexBufferBinding(instanceVertexBuffer, 0, 1)
                );

                Game.GraphicsDevice.Indices = meshPart.IndexBuffer;


                // Set up the instance rendering effect.
                Effect effect = meshPart.Effect;
                //effect.CurrentTechnique = effect.Techniques["HardwareInstancing"];
                effect.Parameters["World"].SetValue(modelBones[mesh.ParentBone.Index]);
                effect.Parameters["View"].SetValue(view);
                effect.Parameters["Projection"].SetValue(projection);
                effect.Parameters["Texture"].SetValue(texture);


                // Draw all the instance copies in a single call.
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                {
                    pass.Apply();

                    Game.GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0,
                                                           meshPart.NumVertices, meshPart.StartIndex,
                                                           meshPart.PrimitiveCount, instances.Length);
                }
            }



        }
    }
    // ### END FUNCTION DrawModelHardwareInstancing

2 个答案:

答案 0 :(得分:4)

问题是您正在使用的立方体网格。法线是平均的,但我想你希望它们与立方体的面正交。

您将不得不使用总共24个顶点(每侧4个)而不是8个顶点。每个角将有3个顶点,位置相同但法线不同,每个相邻面一个:

two cubes, one with shared normals, one with correct normals

如果无法将FBX导出器配置为正确导出法线,只需创建自己的立方体网格:

var vertices = new VertexPositionNormalTexture[24];

// Initialize the vertices, set position and texture coordinates
// ...

// Set normals

// front face
vertices[0].Normal = new Vector3(1, 0, 0);
vertices[1].Normal = new Vector3(1, 0, 0);
vertices[2].Normal = new Vector3(1, 0, 0);
vertices[3].Normal = new Vector3(1, 0, 0);

// back face
vertices[4].Normal = new Vector3(-1, 0, 0);
vertices[5].Normal = new Vector3(-1, 0, 0);
vertices[6].Normal = new Vector3(-1, 0, 0);
vertices[7].Normal = new Vector3(-1, 0, 0);

// ...

答案 1 :(得分:1)

看起来你有不正确的计算/没有法线。 Look at this example, specifically part 3.

法线是一个向量,用于描述光线与顶点/多边形相反的光线反射方向。

I like this picture to demonstrate蓝线是曲线上每个特定点的法线方向。

在XNA中,您可以计算顶点为vert1,vert2和vert3的多边形的法线,如下所示:

Vector3 dir = Vector3.Cross(vert2 - vert1, vert3 - vert1);
Vector3 norm = Vector3.Normalize(dir);

在很多情况下,这是通过建模软件自动完成的,因此无需计算。如果您在代码中创建立方体,则可能需要执行该计算。