這就是我要做的,創建一個所有點的數組和它們的位置,讓我們來談談這個問題,就像你將以統一的全屏模式運行它,然後創建一個新的數組來保存所有點的位置,我們希望以像素爲單位的世界位置。現在代碼如下:
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();
最後一部分會添加一些精靈,它具有整個屏幕的大小並將其應用到它。
所有這些只是給你一個基本的想法,你將如何去解決它,所有這些代碼沒有經過測試,所以你會prolly發現它的一些錯誤,但我把這個工作留給你。如果您在這裏發現任何問題並發表評論,我會回覆,因爲您提出了一個有趣的問題,我非常樂意幫助您。這讓我想起使用不同顏色的光源渲染白色2D紋理(這是你的點),所以試着在網上找到人們如何編寫2D光渲染的腳本,你會prolly找到一些有趣和有用的東西那裏。
點是否會移動或改變其影響範圍?如果不是,則可以更高效地爲每個點生成一個相對於其直接相鄰點的影響多邊形,並根據需要更新每個多邊形的顏色。 – rutter
@rutter他們不會相對於彼此移動,他們的關係也不會改變。每個點生成一個多邊形不是我以前考慮過的。雖然回想起來,我對Wigner-seitz細胞的第一本能就是這樣,所以也許它有優點 –