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

对不起,我的英语不好..

这是一个截图,可以更好地理解我的问题。


这是我的代码绘制函数(带有顶点缓冲区的实例化模型)

    // 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

最佳答案

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

您将必须总共使用24个顶点(每侧4个),而不是8个顶点。每个角都有3个顶点,这些顶点具有相同的位置但法线不同,每个相邻的面对应一个顶点:



如果无法将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);

// ...

关于graphics - 如何在XNA中消除光影对我的模型的影响?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15099295/

10-09 18:18
查看更多