I represent a scene with WPF 3D by creating MeshGeometry3D and adding vertices and normals to it. Everything looks good in the rendering scene (there is light, the material looks great), but where the grid triangles intersect, the triangle closer to the camera is not necessarily displayed on top. They seem to be drawn in random order. Is there a way to ensure that the mesh triangles appear in the βcorrectβ order?
Just in case this helps, here is my XAML:
<Viewport3D> <ModelVisual3D> <ModelVisual3D.Content> <Model3DGroup> <AmbientLight Color="#FF5A5A5A" /> <GeometryModel3D x:Name="geometryModel"> <GeometryModel3D.Material> <DiffuseMaterial Brush="DarkRed"/> </GeometryModel3D.Material> </GeometryModel3D> </Model3DGroup> </ModelVisual3D.Content> </ModelVisual3D> </Viewport3D>
and in the code I create a grid something like this:
var mesh = new MeshGeometry3D(); foreach (var item in objectsToTriangulate) { var triangles = item.Triangulate(); foreach (var triangle in triangles) { mesh.Positions.Add(triangle.Vertices[0]); mesh.Positions.Add(triangle.Vertices[1]); mesh.Positions.Add(triangle.Vertices[2]); mesh.Normals.Add(triangle.Normal); mesh.Normals.Add(triangle.Normal); mesh.Normals.Add(triangle.Normal); } } geometryModel.Geometry = mesh;
EDIT: None of the triangles intersect (except the edges), and sometimes the triangle that appears on top is actually WAY after another, so I donβt think this is an ambiguity in 3D sorting of triangles, as Ray Burns suggested.
Another behavior I noticed is that the order in which the triangles are displayed does not change when I move around the scene. That is, if I look at the problem area from the other side, the triangles are displayed in the same way, now the "correct" order.
wpf 3d
Hank
source share