如何在WPF中绘制3D管子?
假设我有一个 WPF 表单来输入管直径和管高度,并且我想可视化用户输入的管。
我找到了一些 3D 教程,但它们对我来说太复杂了,无法入门。希望有人可以启动我的 3D 理解。如果仅使用 XAML 就可以做到这一点,那就太好了...:)
Say I have a WPF form to enter a tube diameter and tube height, and I want to visualize the tube entered by the user.
I've found a few tutorials to 3D but they are too complex for me to get started. Hoping someone could kickstart my 3D understanding. It would be great if this was possible using only XAML... :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这主要可以使用 XAML 实现,您创建一个长度为 1、直径为 1 的管子对象,然后使用输入的值来适当缩放管子。如果您使用 MVVM 模式,则可以将用户用于输入直径和高度的文本框绑定到 ViewModel 中的属性,然后将缩放矩阵也绑定到这些值。
对于管子,您需要两组顶点,均位于圆上,例如 y=0 和 y=1。对于 0 到 360 度/2Pi 弧度之间的角度,x 和 z 坐标分别为 cos(角度) 和 sin(角度)。使用的值越多,管子就会显得越平滑,10 到 15 是一个很好的起点。为了使管子看起来更光滑,您可以使用共享法线,这将通过伪造照明来伪造更光滑的侧面。在上面的示例中,顶点的法线与管的底部相同:对于顶部和底部顶点,cos(角度)、0、sin(角度)。要创建描述此形状的 XAML,您最好阅读一些 XAML 3D 底漆,这样您就会了解它的作用。
This is possible mostly using XAML, you create an object that's a tube of length 1 and diameter 1, and then use the entered values to scale the tube appropriately. If you're using the MVVM pattern, you can bind the TextBoxes that the user uses to enter the diameter and height to properties in the ViewModel, and then bind the scaling matrices to those values too.
For the tube, you need two sets of vertices, both points on a circle, at e.g. y=0 and y=1. The x and z coordinates will be cos(angle) and sin(angle) respectively, for angle between 0 and 360 degrees/2Pi radians. The more values you use the smoother the tube will appear, 10 to 15 is a good starting point. To make the tube appear smoother you can use shared normals, which will fake smoother sides by fudging the lighting. The normals for a vertex in my example above are the same as the base of the tube: cos(angle), 0, sin(angle), for both the top and bottom vertices. To create the XAML that describes this shape you're best off reading some XAML 3D primers, that way you'll understand what it's doing.