使用 WPF 和 C# 绘制覆盖网格的 3D 表面

此示例展示了如何使用 C# 代码和 XAML 绘制覆盖有网格的 3D 表面。示例使用 WPF 和 C# 将纹理应用于三角形展示了如何将纹理应用于三角形。此示例只是使用该技术将包含大网格的位图应用于表面。

在类级别,程序使用以下代码来定义将点的 X 和 Z 坐标映射到 0.0 - 1.0 范围的比例因子。

cs 复制代码
private const double texture_xscale = (xmax - xmin);
private const double texture_zscale = (zmax - zmin);

下面的代码显示了程序如何向 3D 模型添加新点。

cs 复制代码
// A dictionary to hold points for fast lookup.
private Dictionary<Point3D, int> PointDictionary =
    new Dictionary<Point3D, int>();

// If the point already exists, return its index.
// Otherwise create the point and return its new index.
private int AddPoint(Point3DCollection points,
    PointCollection texture_coords, Point3D point)
{
    // If the point is in the point dictionary,
    // return its saved index.
    if (PointDictionary.ContainsKey(point))
        return PointDictionary[point];

    // We didn't find the point. Create it.
    points.Add(point);
    PointDictionary.Add(point, points.Count - 1);

    // Set the point's texture coordinates.
    texture_coords.Add(
        new Point(
            (point.X - xmin) * texture_xscale,
            (point.Z - zmin) * texture_zscale));

    // Return the new point's index.
    return points.Count - 1;
}

与前面的示例一样,代码定义了一个字典来保存Point,以便可以快速查找它们。AddPoint方法查找一个点,如果该点尚不存在,则添加它。然后,它使用该点的 X 和 Z 坐标将该点映射到对象纹理使用的 UV 坐标的 0.0 - 1.0 范围。换句话说,具有最小 X/Z 坐标的点被映射到 (0, 0) 附近的 U/V 坐标,具有最大 X/Z 坐标的点被映射到 (1, 1) 附近的 U/V 坐标。

创建三角形后,程序使用以下代码来创建其材质。

cs 复制代码
// Make the surface's material using an image brush.
ImageBrush grid_brush = new ImageBrush();
grid_brush.ImageSource =
    new BitmapImage(new Uri("Grid.png", UriKind.Relative));
DiffuseMaterial grid_material = new DiffuseMaterial(grid_brush);

文件 Grid.png 仅包含一个 513×513 像素的网格。或者,您也可以在代码中创建网格。第三种方法是使用偏导数来确定应该在哪里绘制线条,然后使用细长的矩形或框将它们绘制在表面上。(后面的帖子将解释如何绘制细长的框。)然而,这会需要做更多的工作。

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

相关推荐
云飞云共享云桌面1 小时前
机械制造研发部门:一台SolidWorks云主机替代 10 台传统图形工作站
运维·服务器·网络·3d·自动化·制造
云飞云共享云桌面3 小时前
非标自动化工厂研发部门怎么实现一台服务器带10个SolidWorks设计并发
运维·服务器·人工智能·3d·自动化·电脑·制造
坚定信念,勇往无前5 小时前
unity-UMA 的完整落地方案(捏脸,换装)
3d
3D阅阅三维模型轻量化及浏览平台6 小时前
机加工 / 模具 / 钣金现场审图痛点:不用 UG/SW,手机、网页直接打开 STEP、DXF 轻量化审图工具(3D 阅阅)
人工智能·3d·机加工·钣金加工·模具加工
Leinwin7 小时前
Copilot Studio 实战:多模型选择、GitHub Copilot harness 计费与 Copilot Credits 额度管理
开发语言·c#
FuckPatience7 小时前
C# Winform 使用Label循环显示闪烁
开发语言·c#
念何架构之路7 小时前
OpenTelemetry核心概念
后端·目标跟踪·golang·c#·linq
开开心心就好8 小时前
电脑语音实时转文字工具,会议记录很省事
运维·服务器·前端·c#·ocr·perl·1024程序员节
波力海苔夹心脆6758 小时前
C# 固高 GTS 运动控制卡入门:伺服上下电、Jog 点动与二维插补(WinForms 实战)
服务器·开发语言·c#·运动控制·伺服电机·winforms·固高
何以解忧唯有撸码15 小时前
c# + YOLODotNet+OpenCVSharp 实现物品轨迹追踪
计算机视觉·c#·winform·yolov8·opencvsharp