利用 WPF 和 C# 绘制覆盖网格的 3D 外貌

打印 上一主题 下一主题

主题 929|帖子 929|积分 2787

此示例展示了如何利用 C# 代码和 XAML 绘制覆盖有网格的 3D 外貌。示例利用 WPF 和 C# 将纹理应用于三角形展示了如何将纹理应用于三角形。此示例只是利用该技术将包含大网格的位图应用于外貌。
在类级别,步伐利用以下代码来界说将点的 X 和 Z 坐标映射到 0.0 - 1.0 范围的比例因子。
  1. private const double texture_xscale = (xmax - xmin);
  2. private const double texture_zscale = (zmax - zmin);
复制代码
下面的代码表现了步伐如何向 3D 模子添加新点。

  1. // A dictionary to hold points for fast lookup.
  2. private Dictionary<Point3D, int> PointDictionary =
  3.     new Dictionary<Point3D, int>();
  4. // If the point already exists, return its index.
  5. // Otherwise create the point and return its new index.
  6. private int AddPoint(Point3DCollection points,
  7.     PointCollection texture_coords, Point3D point)
  8. {
  9.     // If the point is in the point dictionary,
  10.     // return its saved index.
  11.     if (PointDictionary.ContainsKey(point))
  12.         return PointDictionary[point];
  13.     // We didn't find the point. Create it.
  14.     points.Add(point);
  15.     PointDictionary.Add(point, points.Count - 1);
  16.     // Set the point's texture coordinates.
  17.     texture_coords.Add(
  18.         new Point(
  19.             (point.X - xmin) * texture_xscale,
  20.             (point.Z - zmin) * texture_zscale));
  21.     // Return the new point's index.
  22.     return points.Count - 1;
  23. }
复制代码
    与前面的示例一样,代码界说了一个字典来保存Point,以便可以快速查找它们。AddPoint方法查找一个点,假如该点尚不存在,则添加它。然后,它利用该点的 X 和 Z 坐标将该点映射到对象纹理利用的 UV 坐标的 0.0 - 1.0 范围。换句话说,具有最小 X/Z 坐标的点被映射到 (0, 0) 附近的 U/V 坐标,具有最大 X/Z 坐标的点被映射到 (1, 1) 附近的 U/V 坐标。
创建三角形后,步伐利用以下代码来创建其材质。

  1. // Make the surface's material using an image brush.
  2. ImageBrush grid_brush = new ImageBrush();
  3. grid_brush.ImageSource =
  4.     new BitmapImage(new Uri("Grid.png", UriKind.Relative));
  5. DiffuseMaterial grid_material = new DiffuseMaterial(grid_brush);
复制代码
文件 Grid.png 仅包含一个 513×513 像素的网格。或者,您也可以在代码中创建网格。第三种方法是利用偏导数来确定应该在那边绘制线条,然后利用细长的矩形或框将它们绘制在外貌上。(后面的帖子将解释如何绘制细长的框。)然而,这会需要做更多的工作。

我知道这些例子省略了大量细节。它们相互依存,以是您已经在之前的帖子中看到了关键点。细节也相当长,以是为了节省空间,我不会在每篇文章中都包含它们。



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

小小小幸运

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表