C# 如何修复3D模型中这种奇怪的透明区域?
有些东西不能正常工作。如果你仔细看屏幕截图,你会发现结果很奇怪。展馆的地板渲染正确,但柱子有点透明,屋顶也很奇怪。我使用Assimp.NET从.obj文件导入他的网格。在其他引擎中,它看起来是正确的。另一件事是:如果我将CullMode设置为Back-它将剔除正面?!我认为可能有三件事:1,网格导入错误,或者z缓冲区不工作,或者我需要多个世界矩阵(我只使用一个) 也许有人知道这是什么吗 截图: 下面是一些代码: DepthBuffer/DepthStencilViewC# 如何修复3D模型中这种奇怪的透明区域?,c#,sharpdx,direct3d11,C#,Sharpdx,Direct3d11,有些东西不能正常工作。如果你仔细看屏幕截图,你会发现结果很奇怪。展馆的地板渲染正确,但柱子有点透明,屋顶也很奇怪。我使用Assimp.NET从.obj文件导入他的网格。在其他引擎中,它看起来是正确的。另一件事是:如果我将CullMode设置为Back-它将剔除正面?!我认为可能有三件事:1,网格导入错误,或者z缓冲区不工作,或者我需要多个世界矩阵(我只使用一个) 也许有人知道这是什么吗 截图: 下面是一些代码: DepthBuffer/DepthStencilView var depthBuf
var depthBufferDescription = new Texture2DDescription
{
Format = Format.D32_Float_S8X24_UInt,
ArraySize = 1,
MipLevels = 1,
Width = BackBuffer.Description.Width,
Height = BackBuffer.Description.Height,
SampleDescription = swapChainDescription.SampleDescription,
BindFlags = BindFlags.DepthStencil
};
var depthStencilViewDescription = new DepthStencilViewDescription
{
Dimension = SwapChain.Description.SampleDescription.Count > 1 || SwapChain.Description.SampleDescription.Quality > 0 ? DepthStencilViewDimension.Texture2DMultisampled : DepthStencilViewDimension.Texture2D
};
var depthStencilStateDescription = new DepthStencilStateDescription
{
IsDepthEnabled = true,
DepthComparison = Comparison.Always,
DepthWriteMask = DepthWriteMask.All,
IsStencilEnabled = false,
StencilReadMask = 0xff,
StencilWriteMask = 0xff,
FrontFace = new DepthStencilOperationDescription
{
Comparison = Comparison.Always,
PassOperation = StencilOperation.Keep,
FailOperation = StencilOperation.Keep,
DepthFailOperation = StencilOperation.Increment
},
BackFace = new DepthStencilOperationDescription
{
Comparison = Comparison.Always,
PassOperation = StencilOperation.Keep,
FailOperation = StencilOperation.Keep,
DepthFailOperation = StencilOperation.Decrement
}
};
加载网格文件:
public static Mesh Stadafax_ModelFromFile(string path)
{
if (_importContext.IsImportFormatSupported(Path.GetExtension(path)))
{
var imported = _importContext.ImportFile(path, PostProcessSteps.Triangulate | PostProcessSteps.FindDegenerates | PostProcessSteps.FindInstances | PostProcessSteps.FindInvalidData | PostProcessSteps.JoinIdenticalVertices | PostProcessSteps.OptimizeGraph | PostProcessSteps.ValidateDataStructure | PostProcessSteps.FlipUVs);
Mesh engineMesh = new Mesh();
Assimp.Mesh assimpMesh = imported.Meshes[0];
foreach(Face f in assimpMesh.Faces)
{
engineMesh.Structure.Faces.Add(new Rendering.Triangle((uint)f.Indices[0], (uint)f.Indices[1], (uint)f.Indices[2]));
}
List<Vector3D>[] uv = assimpMesh.TextureCoordinateChannels;
for(int i = 0; i < assimpMesh.Vertices.Count; i++)
{
engineMesh.Structure.Vertices.Add(new Vertex(new Vector4(assimpMesh.Vertices[i].X, assimpMesh.Vertices[i].Y, assimpMesh.Vertices[i].Z, 1), RenderColorRGBA.White, new Vector2(uv[0][i].X, uv[0][i].Y)));
}
return engineMesh;
}
else
NoëlEngine.Common.Output.Log("Model format not supported!", "Importeur", true); return null;
}
}
public静态网格Stadafax\u ModelFromFile(字符串路径)
{
如果(_importContext.IsImportFormatSupported(Path.GetExtension(Path)))
{
var imported=|u importContext.ImportFile(路径、后处理步骤.三角化|后处理步骤.FindDegenerations |后处理步骤.FindInstances |后处理步骤.FindInvalidData |后处理步骤.JoinIdenticalEvents |后处理步骤.OptimizeGraph |后处理步骤.ValidatedAtstructure |后处理步骤.FlipUVs);
网格引擎网格=新网格();
Assimp.Mesh assimpMesh=imported.Mesh[0];
foreach(辅助网格中的面f.Faces)
{
engineMesh.Structure.Faces.Add(新的渲染.三角形((uint)f.index[0],(uint)f.index[1],(uint)f.index[2]);
}
列表[]uv=assimpMesh.TextureCoordination通道;
对于(int i=0;i
如果有人对这可能是什么有一点想法,请写一条评论。你看到的实际上是在其他多边形后面的多边形,它们仍然在上面绘制 通过
depthstencilstatescription
配置深度缓冲区时,将depthscomparison
设置为比较。始终。这不是您想要的,您想要使用比较。Less
背后的逻辑是什么?检查像素的每个深度值是否可以写入深度缓冲区。此检查使用指定的比较配置
比较。始终
始终允许写入新值。因此,不管一个多边形实际上是在其他多边形后面,还是在其他多边形上面,或者其他什么,它都会覆盖(“在上面绘制”)已经存在的东西——即使它在空间上在后面
Comparison.Less
仅当值小于深度缓冲区中的当前值时才写入该值。不要忘记,较小的深度值更接近查看器。因此,靠近现有多边形的多边形将覆盖(“在上方绘制”)旧多边形。但如果它在它后面,它就不会画。那正是你想要的
您还可以猜测其他比较
枚举现在做了什么,并对它们进行处理:)没有太多地查看DX,但比较非常确定。始终和失败/通过都是“保留”使您的Z缓冲区变得无用。也许您是对的,但我该怎么更改它呢?该死的,我指的是DepthComparison属性,不是您如何配置模具缓冲区(尽管您也应该检查它,以防您要使用它)。您的DepthComparison
可能应该是Comparison.Less
。你可以在这里查看样本(虽然是C++,但是只看东西的名称就可以很容易地移植到SypPDX):DethToFrime=比较。非常感谢!!:)很高兴听到这个消息。我会把它写下来作为一个答案,这样你可以在几分钟内接受:)非常感谢!:)