gpt4 book ai didi

graphics - 如何在 XNA 中消除模型上的光/阴影效果?

转载 作者:行者123 更新时间:2023-12-03 06:24:08 24 4
gpt4 key购买 nike

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

抱歉我的英语不好..

这是一个屏幕截图,可以更好地理解我的问题。 enter image description here

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

    // 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 个位置相同但法线不同的顶点,每个相邻面都有一个顶点:

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);

// ...

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com