3D 模型作为 WPF 中的控件
在我最新的疯狂项目中,我试图为 3D 策略类游戏创建一个视图。它是 3D 的,因此视图的主要部分是 Viewport3D
。其中,有几个用于单元的 Model3D
。问题是,我想以交互方式使用 3D 视图(例如通过逐个单击来选择单元),因此我将模型包装在 ContainerUIElement3Ds 中。起初我的视图中只有几个模型,但后来可能会有更多,所以我不想将它们硬编码到视图中。我的第一种方法是创建一个 Control
来保存一个模型并处理其上的交互,但这个计划似乎失败了,因为 ContainerUIElement3D
是一个密封类,并且 Model3D 无法捕获我需要的事件。
那么我该如何解决这个问题呢?
In my latest crazy project I'm trying to create a view for a 3D strategy-like game. It's 3D, so the main part of the view is a Viewport3D
. In it, there are several Model3Ds
for the units. The thing is, I'd like to use the 3D view interactively (for example selecting units by clicking on them one by one), so I've wrapped my models in ContainerUIElement3Ds
. At first there are only a few models in my view, but later there can be more, so I don't want to hard code them into the view. My first approach was to create a Control
that holds one model and handles interactions on it, but this plan seemed to fail, since ContainerUIElement3D
is a sealed class, and Model3D
can't catch the events I need.
So how could I workaround this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以想到两种通用方法:
这两种方法都有价值,并且取决于您是否满足您的需求。
只要知道可以使用真正的交互元素作为一种先进技术,就是有价值的信息。这是您研究该领域的起点:
这篇文章描述了一个
ItemsControl
,但这比您问的更进一步。这些概念是对您来说重要的内容以及您可以在该网站上找到的其他文章链接。对图形元素进行命中测试并自行渲染所有交互性的强力方法可能听起来令人畏惧,但对于游戏来说,您通常希望完全控制元素。您可能有自己的计划,用抽象颜色或光环渲染选定的项目等。
按钮等基础设施并不那么复杂,您只需使用几个直接处理矩形或立方体的代码即可实现单击和鼠标悬停处理。如果这种“自己动手”的方法对您来说听起来不错,那么您所需要的就是 3D 命中测试。这里有一篇文章可以帮助您入门:
I can think of two general approaches:
Both approaches have value and it's up to you if either meets your needs.
Just to know that it's possible to use real interactive elements, as an advanced technique, is valuable information. Here's a starting point for your research into this area:
The article describes an
ItemsControl
but that is going even further that you asked. The concepts are what is important to you and the other article links you'll find on that site.The brute-force approach of hit-testing your graphical elements and rendering all the interactivity yourself might sound daunting, but for a game you often want complete control over the elements. You might have your own plan for rendering selected items, etc with abstract colors or halos.
There infrastructure of say, a button, is not that complicated and you can implement click and mouse over handling with just a few code working directly with rectangles or cubes. If this "do it yourself" approach sounds good to you, all you need is 3D hit-testing. Here's an article to get you started:
WPF 3D 不适合复杂的游戏。
但...
有两种方法(正如 Rick Sladkey 注意到的那样)获得交互行为:
因此,你的 Units 是此类的实例(p.1 或 2) )
交易的是你的单位的 3D 复杂性。
如果它们具有简单的网格和不太硬的材料,您将可以随意使用 1000-2000 甚至更多的单位数量。
简单材质、轻量网格并继承自 UIElement3D。
但是如果我们改变材质,场景会变得有点困难...
意见
继承来自 UIElement3D
建议
如果可能的话,不要使用 3party 库,用手在代码中编写 3D 对象。原因是其中一些部分性能不正确。
WPF 3D is not for complicated games.
But...
There are two ways (as Rick Sladkey noticed) to get interactive behavior:
So, your Units are instances of this classes (p.1 or 2)
The deal is 3D complexity of your Units.
If they have simple meshes and not so hard materials you will feel free with amount of units till 1000-2000 and even more.
Simple materials, light weight meshes and inherited from UIElement3D.
But scene become hard a bit if we change materials...
Opinion
Inherit from UIElement3D
Advice
if it is possible, do not use 3party libraries, write 3D objects in code by hand. The reason is the some of them is incorrect in part of performance.