程序图渲染优化:只绘制可见部分?
背景
假设我有数百万 个互连的 Node
实例,它们一起形成一个图。每个节点
都有一个2D位置。用户必须能够平移该图的程序渲染。每个Node
都有一个draw
方法,但是如果我每帧都draw
所有Node
,速度会非常慢。
由于用户通常不想看到整个图形而是放大图形,因此优化在于不绘制屏幕外的节点
。
我的方法
将 2D 世界空间划分为矩形段
。将每个 Node
分配给它们所在的任何 Segment
。绘图时,首先找出用户视图与哪一组 Segment
相交,然后 < code>仅绘制这些Segment
中的Node
。
现在我的实际问题是:
如何确定段
的最佳大小?(让它太大,这与绘图是一样的)让它太小,就会有太多的东西需要迭代。)
Background
Let's say I have millions of interconnected Node
instances which together form a graph. Each Node
has a 2D position. A user has to be able to pan through a procedural render of this graph. Each Node
has a draw
method, but if I draw
all Node
s every frame, it's very slow.
As the user does not usually want to see the whole graph but instead is zoomed in, the optimisation is in not drawing the Node
s that are off screen.
My approach
Divide the 2D world space into rectangular Segment
s. Assign each Node
to whatever Segment
they're in. When drawing, find out first which set of Segment
s the user's view intersects with and draw
only the Node
s in those Segment
s.
Now for my actual question:
How do I determine the optimal size of a Segment
? (Make it too big and it's the same thing as drawing everything. Make it too small and there are again too many to iterate through.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为四叉树在这里可能比划分为固定大小的矩形更有帮助。
I think Quad trees may be more helpful here than the division into fixed-size rectangles.
嗯,你总是可以通过反复试验来确定它。
顺便说一句,片段的大小可能取决于缩放级别。
另一种可能的解决方案是拥有两个排序的节点数组。一个数组
Node[] ax
按x
位置排序,另一个Node[] ay
按y
位置排序。如果要显示
(x1, y1) - (x2, y2)
中的节点,您将构建一组位于用边界过滤的排序数组的交集处的节点。Well, you can always determine it by trials-and-errors.
By the way, the size of the segment may depend on the zoom level.
Another possible solution is to have two sorted arrays of nodes. One array
Node[] ax
sorted by thex
position and the otherNode[] ay
by they
position.If you want to display nodes in
(x1, y1) - (x2, y2)
, you will build a set of nodes which are at the intersection of the sorted arrays filtered with the bounds.