简体   繁体   English

C#3D WPF黑色网格

[英]C# 3D wpf black meshes

C# 3D wpf visual studio 2010 C#3D WPF Visual Studio 2010

I have a scene consisting of a circle built up by a number triangles which I have added meshes to both backside and front. 我有一个场景,其中包括一个由数字三角形构成的圆,并在背面和正面都添加了网格。

The triangle shows up in my view but it sides are black, both inside and the outside. 在我看来,三角形出现了,但内部和外部均为黑色。 I add the material in this way 我以这种方式添加材料

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

but if I change the material to 但是如果我将材料更改为

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

The sides starts to shine in this colour. 侧面开始以这种颜色发光。

I have three lightsources 我有三个光源

< AmbientLight Color="White" / >

< DirectionalLight  x:Name="myDirectLight1" Color="White"/ >

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

Where I also do 我也在哪里

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

But the sides are still black (unless I change the material to EmissiveMaterial) ??? 但是侧面仍然是黑色的(除非我将材质更改为EmissiveMaterial)???

Do the lights have some sort of default range not set to infinity ? 灯光是否具有未设置为infinity的某种默认范围?

The circle is large, around 2000 units in diameter, 圆圈很大,直径约为2000个单位,

Regards 问候

Added 添加

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM