- Home /
Block-based terrain: WWW image problem and performance help?
I am making a terrain generator that instantiates a lot of Cube prefabs in specific positions according to the pixels of a image. I am using the WWW class to load the image since i want to be able to download levels in a future version. The code (javascript) of the generator is:
/*
FORMAT:
Red = Floor type
Green = Wall type - UNIMPLEMENTED
Blue = Height - UNIMPLEMENTED
Alpha = Prop/Character/NPC spawning - UNIMPLEMENTED
*/
var level : String;
var blocks : GameObject[] = new GameObject[256];
private var levelImage : Texture2D;
private var www : WWW;
function Start() {
levelImage = new Texture2D(64, 64, TextureFormat.DXT5, false);
level.Replace(" ","_");
level.Replace("?","");
level.Replace("/","");
level.Replace(".","");
level.Replace("&","");
level.Replace("%","");
var url : String = "file:///C:/levels/"+level+".png";
www = new WWW(url);
yield www;
www.LoadImageIntoTexture(levelImage);
levelImage.filterMode = FilterMode.Point;
var pixels : Color[] = levelImage.GetPixels(0, 0, 64, 64);
for (var xa : int = 0; xa < 64; xa++){
var x : int = xa*64;
for (var z : int = 0; z < 64; z++){
Instantiate(blocks[pixels[x+z].r],Vector3(xa,pixels[x+z].b,z),Quaternion.identity);
}
}
}
function OnGUI() {
if (www.isDone == false){
GUI.Box(Rect(Screen.width/2-200,Screen.height-50,400,30),"Loading...");
GUI.Box(Rect(Screen.width/2-200,Screen.height-50,400*www.progress,30),"");
}
}
This is the only code i am running to generate the level. the problem is that every cube is in the same height, generating a flat terrain, instead of rising the cubes respective to the blue value of the loaded image. With my current method, i don't even know if the image was loaded. This is my primary concern now, but i have another question: Is there a way to coroutinize this code so it does not hang Unity (or the application) when instantiating the cubes?
Comment