C#3D wpf黑色网格

时间:2011-09-02 12:47:43

标签: c# wpf 3d geometry

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

但是两侧仍然是黑色的(除非我将材料更改为EmissiveMaterial)???

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

圆圈很大,直径约为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);
    }

1 个答案:

答案 0 :(得分:0)

错误是我的光源是在XAML代码中定义的,但我很容易删除 它们在源代码中导致奇怪的结果,其中对象是“可见的” 但无论材料如何,都是黑色的