在网络中绘制点的影响(Draw influence of dots in network)

编程入门 行业动态 更新时间:2024-10-27 06:21:59
网络中绘制点的影响(Draw influence of dots in network)

我遇到了一个有点恼人的问题,如果不知道正确的词汇,很难解释,但下面的两个图像应该解释我在寻找什么。

我有一个连接点网络。 每个点都有一个颜色属性。

点网络

网络中的每个点代表一个位置,由于缺少更好的术语,该位置由相应的颜色“拥有”。 我现在想要通过生成类似于此的东西来正确地展示每个拥有点对其附近的影响,即颜色“领域”:

彩色网络

我无法看到一个简单的解决方案。 我能看到的最可能的解决方案是生成一个平面,将有关每个点的信息传递给适当的着色器,并使用它来计算平面的纹理。 然而,与此示例不同,不仅有10个点,而且可能介于1000-2000之间。

提出一些更具体的问题:

1)着色器可以处理点数(1000+)吗?

2)我不确定着色器必须具有哪种算法才能获得正确的结果。 迭代所有点并将它们与所有剩余点进行比较并产生某种重量似乎是次优的。 我的背景是物理学,第一直觉是产生类似于Wigner-seitz细胞的东西。 (我会提供一个图片链接来解释我的意思,但看起来我只能用新帐户发布2个链接。但是快速的谷歌图片搜索应该有很多好的结果)

3)最明显的一个问题:我是否缺少一个简单的解决方案?

I have run into a somewhat annoying problem which is difficult to explain without knowing the proper vocabulary, but the following two images should explain what I'm looking for.

I have a network of connected dots. Each dot has a color property.

Dot Network

Each dot in the network represents a location which, for the lack of a better term, is "owned" by the respective color. I would now like to properly showcase the influence each owned dot has on its vicinity, i.e. the colors "territory", by generating something akin to this:

Colored Dot Network

I cannot see a trivial solution to this. The most probable solution I can see is to generate a plane, pass information about each dot to an appropriate shader and use that to compute the texture of the plane. Unlike this example however, there are not just 10 dots, but likely somewhere between 1000-2000.

To pose some more specific questions:

1) Could a shader handle the amount of dots (1000+)?

2) I am not sure what kind of algorithm the shader would have to have in order to achieve the right result. Iterating through all dots and comparing them to all remaining dots and generating some sort of weight seems.. suboptimal. My background being in physics, the first instinct is something that generates something akin to a Wigner-seitz cell. (I would provide a link to an image explaining what I mean, but it appears I can only post 2 links with a fresh account. A quick google image search should however have plenty of good results)

3) And the most obvious one: Is there a trivial solution I'm missing?

最满意答案

这就是我要做的,创建一个包含所有点和它们位置的数组,让我们谈谈这个,就像你将在全屏统一运行它,然后创建一个新的数组,保持所有点的位置,但我们不是世界位置想要像素的位置。 现在这是代码中的那部分:

Transform[] dots; Vector3[] position; //Vector2 is more appropriate because of 2D space but most of the Unity functions support only Vector3 void Awake(){ for(int i =0; i < dots.Length; i++) position[i] = Camera.main.WorldToScreenPoint(dots[i].position); }

现在我们在屏幕上有所有点的位置,接下来的事情就是创建实际的纹理。

Texture2D outTex = new Texture2D (Screen.height, Screen.width, TextureFormat.RGB24, false);

现在我们应该遍历此纹理上的每个像素并创建特定的公式以确定该像素应该是什么颜色。

添加另一个Color32类型的数组变量,它将为每个点定义颜色(由谁“拥有”)。

int h = outTex.height; int w = outTex.width; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { //Formula for determining color is right here, here is my suggestion //(the most simplest one), check the color of each dot and based on the distance //between this pixel and dot's position determine how much influence that dot //has on this pixel, here is how it goes float red = 0; float green = 0; float blue = 0; float divider = 0; for(int c = 0; c < dots.Length; c++){ float curDist = Vector3.Distance(dots[c].position, new Vector3(j, i ,0); curDist = 1f/(curDist+1f); red += dotColors[c].r * curDist; green += dotColors[c].g * curDist; blue += dotColors[c].b * curDist; divider++; } outTex.SetPixel (j , i, new Color32(red/divider, green/divider, blue/divider, 255); } outTex.Apply();

最后一部分是添加一些具有整个屏幕大小的精灵并将此纹理应用于它。

所有这些只是为了让你基本了解你将如何解决它,所有这些代码都是未经测试的,因此你会在其中发现一些错误,但我将这项工作交给你。 如果您发现任何问题在这里发表评论并且我会回复,我非常愿意帮助您,因为您介绍了一个有趣的问题。 这一切都让我想起用它前面的不同颜色的光源渲染白色2D纹理(这是你的点),所以试着在网上找到人们如何为2D光渲染编写脚本,你会发现一些有趣和有用的东西那里。

Here is what I would do, create an array of all dots and their locations, let's talk about this like you will run this in fullscreen in unity, then create a new array that holds the position of all dots, but instead of world position we want position in pixels. Now here is that part in code:

Transform[] dots; Vector3[] position; //Vector2 is more appropriate because of 2D space but most of the Unity functions support only Vector3 void Awake(){ for(int i =0; i < dots.Length; i++) position[i] = Camera.main.WorldToScreenPoint(dots[i].position); }

Now we have position of all dots on screen, next thing would be creating actual texture.

Texture2D outTex = new Texture2D (Screen.height, Screen.width, TextureFormat.RGB24, false);

Now we should iterate through each pixel on this Texture and create specific formula to determine what color should that pixel be.

Add another array variable of type Color32, which would define color for each dot (by who is it "owned").

int h = outTex.height; int w = outTex.width; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { //Formula for determining color is right here, here is my suggestion //(the most simplest one), check the color of each dot and based on the distance //between this pixel and dot's position determine how much influence that dot //has on this pixel, here is how it goes float red = 0; float green = 0; float blue = 0; float divider = 0; for(int c = 0; c < dots.Length; c++){ float curDist = Vector3.Distance(dots[c].position, new Vector3(j, i ,0); curDist = 1f/(curDist+1f); red += dotColors[c].r * curDist; green += dotColors[c].g * curDist; blue += dotColors[c].b * curDist; divider++; } outTex.SetPixel (j , i, new Color32(red/divider, green/divider, blue/divider, 255); } outTex.Apply();

And the last part would be add some sprite that has the size of the whole screen and apply this texture to it.

All of this is just to give you basic idea of how would you go about to solve it, all of this code is untested so you will prolly find some errors in it, but I leave that work to you. If you find any problems put a comment here and I will respond, I am more than willing to help you since you introduced a interesting question. This all reminds me of rendering white 2D texture with light sources of different colors in front of it (this is your dots), so try finding online how people are writing a scripts for 2D light rendering, you will prolly find some interesting and useful things there.

更多推荐

本文发布于:2023-07-22 01:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215501.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网络   Draw   influence   dots   network

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!