c# 3D wpf黑色网格

本文关键字:网格 黑色 wpf 3D | 更新日期: 2023-09-27 18:07:15

c# 3D wpf visual studio 2010

我有一个场景,由我添加的一些三角形组成一个圆圈前面和后面都有网格

三角形显示在我的视图中,但它的内部和外部都是黑色的。我用这种方式添加素材

Material material = new DiffuseMaterial(new SolidColorBrush(Colors.Blue));

但是如果我把材质改成

Material material = new EmissiveMaterial(new SolidColorBrush(Colors.Red));

侧面开始在这种颜色中发光。

我有三个光源

< AmbientLight Color="White" / >
< DirectionalLight  x:Name="myDirectLight1" Color="White"/ >
< PointLight  x:Name="myPointLight1" Color="White" Position="0,0,0"/>

我也做了

myCamera.LookDirection = -(Vector3D)myCamera.Position; //always look to (0,0,0)
myDirectLight1.Direction = myCamera.LookDirection; //light comes from the camera

但是两边仍然是黑色的(除非我将材料改为emissiveemmaterial) ??

灯光是否有某种默认范围未设置为无穷大?

圆圈很大,直径约2000个单位,

添加

    private Model3DGroup CreateTriangleModel(Point3D p0, Point3D p1, Point3D p2, Material material, Material material2)
    {
        MeshGeometry3D mesh = null;
        GeometryModel3D model = null;
        Model3DGroup group = new Model3DGroup();
        //
        // Front side of jagged part
        //
        mesh = new MeshGeometry3D();
        mesh.Positions.Add(p0);
        mesh.Positions.Add(p1);
        mesh.Positions.Add(p2);
        mesh.TriangleIndices.Add(0);
        mesh.TriangleIndices.Add(1);
        mesh.TriangleIndices.Add(2);
        Vector3D normal = CalculateNormal(p0, p1, p2);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        //
        // Front side of the surface below the jagged edge
        //
        Point3D p3 = new Point3D(p1.X, p1.Y, bh);
        Point3D p4 = new Point3D(p2.X, p2.Y, bh);
        mesh.Positions.Add(p3);
        mesh.Positions.Add(p4);
        mesh.TriangleIndices.Add(1);
        mesh.TriangleIndices.Add(3);
        mesh.TriangleIndices.Add(4);
        normal = CalculateNormal(p1, p3, p4);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        mesh.TriangleIndices.Add(1);
        mesh.TriangleIndices.Add(4);
        mesh.TriangleIndices.Add(2);
        normal = CalculateNormal(p1, p4, p2);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        model = new GeometryModel3D(mesh, material);
        group.Children.Add(model);

        //
        // Back side of jagged part
        //
        mesh = new MeshGeometry3D();
        mesh.Positions.Add(p0);
        mesh.Positions.Add(p1);
        mesh.Positions.Add(p2);
        mesh.TriangleIndices.Add(0);
        mesh.TriangleIndices.Add(2);
        mesh.TriangleIndices.Add(1);
        normal = CalculateNormal(p0, p1, p2);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        //
        // Back side of the surface below the jagged edge
        //
        p3 = new Point3D(p1.X, p1.Y, bh);
        p4 = new Point3D(p2.X, p2.Y, bh);
        mesh.Positions.Add(p3);
        mesh.Positions.Add(p4);
        mesh.TriangleIndices.Add(1);
        mesh.TriangleIndices.Add(4);
        mesh.TriangleIndices.Add(3);
        normal = CalculateNormal(p1, p4, p3);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        mesh.TriangleIndices.Add(1);
        mesh.TriangleIndices.Add(2);
        mesh.TriangleIndices.Add(4);
        normal = CalculateNormal(p1, p2, p4);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        mesh.Normals.Add(normal);
        model = new GeometryModel3D(mesh, material2);
        group.Children.Add(model);

        return group;
    }
    private Vector3D CalculateNormal(Point3D p0, Point3D p1, Point3D p2)
    {
        Vector3D v0 = new Vector3D(
            p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z);
        Vector3D v1 = new Vector3D(
            p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z);
        return Vector3D.CrossProduct(v0, v1);
    }

c# 3D wpf黑色网格

错误是我的光源是在XAML代码中定义的,但我不小心删除了它们在源代码中导致了一个奇怪的结果,对象是"可见的"。但是都是黑色的,不管材质是什么