【问题标题】:Multiple 3D objects with C# and WPF使用 C# 和 WPF 的多个 3D 对象
【发布时间】:2018-11-09 18:35:19
【问题描述】:

我正在尝试使用 C# 和 WPF 来渲染两个具有不同颜色的三角形网格对象,但我不知道如何使它工作。

如果我将 numObjects 设置为 1,它将显示一个应有的红色三角形。

但是,当我将 numObjects 设置为 2 时,不显示第一个红色三角形,只显示第二个绿色三角形。

我在这里做错了什么?

这是我的代码:

using System.Windows.Media.Media3D;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        // Declare scene objects.
        Viewport3D myViewport3D = new Viewport3D();
        Model3DGroup myModel3DGroup = new Model3DGroup();
        GeometryModel3D myGeometryModel = new GeometryModel3D();
        ModelVisual3D myModelVisual3D = new ModelVisual3D();
        // Defines the camera used to view the 3D object. In order to view the 3D object,
        // the camera must be positioned and pointed such that the object is within view 
        // of the camera.
        PerspectiveCamera myPCamera = new PerspectiveCamera();

        public MainWindow()
        {
            InitializeComponent();

            // Specify where in the 3D scene the camera is.
            myPCamera.Position = new Point3D(0, 0, 5);

            // Specify the direction that the camera is pointing.
            myPCamera.LookDirection = new Vector3D(0, 0, -1);

            // Define camera's horizontal field of view in degrees.
            myPCamera.FieldOfView = 60;

            // Asign the camera to the viewport
            myViewport3D.Camera = myPCamera;
            // Define the lights cast in the scene. Without light, the 3D object cannot 
            // be seen. Note: to illuminate an object from additional directions, create 
            // additional lights.
            DirectionalLight myDirectionalLight = new DirectionalLight();
            myDirectionalLight.Color = Colors.White;
            myDirectionalLight.Direction = new Vector3D(-0.61, -0.5, -0.61);
            myModel3DGroup.Children.Add(myDirectionalLight);

            int numObjects = 2;

            for(int i = 0; i < numObjects; i++)
            {
                BuildObject(i);
            }
            // Add the group of models to the ModelVisual3d.
            myModelVisual3D.Content = myModel3DGroup;

            // 
            myViewport3D.Children.Add(myModelVisual3D);

            // Apply the viewport to the page so it will be rendered.
            this.Content = myViewport3D;

        }
        private void BuildObject(int i)
        {



            // The geometry specifes the shape of the 3D plane. In this sample, a flat sheet 
            // is created.
            MeshGeometry3D myMeshGeometry3D = new MeshGeometry3D();

            // Create a collection of normal vectors for the MeshGeometry3D.
            Vector3DCollection myNormalCollection = new Vector3DCollection();
            myNormalCollection.Add(new Vector3D(0, 0, 1));
            myNormalCollection.Add(new Vector3D(0, 0, 1));
            myNormalCollection.Add(new Vector3D(0, 0, 1));
            myMeshGeometry3D.Normals = myNormalCollection;

            double basex = 0 + i * 1;

            // Create a collection of vertex positions for the MeshGeometry3D. 
            Point3DCollection myPositionCollection = new Point3DCollection();
            myPositionCollection.Add(new Point3D(basex + -0.5, -0.5, 0.5));
            myPositionCollection.Add(new Point3D(basex + 0.5, -0.5, 0.5));
            myPositionCollection.Add(new Point3D(basex + 0.5, 0.5, 0.5));

            myMeshGeometry3D.Positions = myPositionCollection;

            // Create a collection of texture coordinates for the MeshGeometry3D.
            PointCollection myTextureCoordinatesCollection = new PointCollection();
            myTextureCoordinatesCollection.Add(new Point(0, 0));
            myTextureCoordinatesCollection.Add(new Point(1, 0));
            myTextureCoordinatesCollection.Add(new Point(1, 1));
            myMeshGeometry3D.TextureCoordinates = myTextureCoordinatesCollection;

            // Create a collection of triangle indices for the MeshGeometry3D.
            Int32Collection myTriangleIndicesCollection = new Int32Collection();
            myTriangleIndicesCollection.Add(0);
            myTriangleIndicesCollection.Add(1);
            myTriangleIndicesCollection.Add(2);
            myMeshGeometry3D.TriangleIndices = myTriangleIndicesCollection;

            // Apply the mesh to the geometry model.
            myGeometryModel.Geometry = myMeshGeometry3D;

            // The material specifies the material applied to the 3D object. In this sample a  
            // linear gradient covers the surface of the 3D object.

            Color color = Color.FromArgb(255, 255, 0, 0);
            if(i == 1)
            {
                color = Color.FromArgb(255, 0, 255, 0);
            }
            SolidColorBrush solid_brush = new SolidColorBrush(color);
            DiffuseMaterial solid_material = new DiffuseMaterial(solid_brush);
            myGeometryModel.Material = solid_material;


            // Add the geometry model to the model group.
            myModel3DGroup.Children.Add(myGeometryModel);
            Console.WriteLine(myGeometryModel.ToString());


        }
    }
}

【问题讨论】:

    标签: c# wpf 3d


    【解决方案1】:

    您实例化并仅使用单个 GeometryModel3D 对象(由字段 myGeometryModel 引用)。所以,绿色三角形的数据实质上是替换了myGeometryModel中的红色三角形数据。

    要解决此问题,请删除 myGeometryModel 字段,并在 BuildObject 方法中为 每个 三角形创建一个 GeometryModel3D 对象:

    public partial class MainWindow : Window
    {
        Viewport3D myViewport3D = new Viewport3D();
        Model3DGroup myModel3DGroup = new Model3DGroup();
        ModelVisual3D myModelVisual3D = new ModelVisual3D();
        PerspectiveCamera myPCamera = new PerspectiveCamera();
    
        ...
    
        private void BuildObject(int i)
        {
            var myGeometryModel = new GeometryModel3D();
    
            ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多