这就是我要做的,创建一个包含所有点及其位置的数组,让我们来讨论一下,就像你将在全屏中统一运行它一样,然后创建一个保存所有点位置的新数组,但不是世界位置我们想要以像素为单位的位置。现在这是代码中的那部分:
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 光渲染脚本,你会发现一些有趣和有用的东西在那里。