- Home /
How to read texture in chunks instead of stripes.
So currently I am using a 1000 by 1000 texture
wallMap = new Texture2D(width, width, TextureFormat.ARGB32, false);
and then coloring in certain parts to create a maze type of thing. Then at the end I place prefabs based on colors in the textures. Here is what I am currently using to place the prefabs (width = 1000, scaleAmount = 10):
for (int x = 0; x < width; x++)
{
for (int z = 0; z < width; z++)
{
if (wallMap.GetPixel(x, z) == wallColor)
{
Instantiate(wall, new Vector3(x * scaleAmount, 0, z * scaleAmount), Quaternion.identity, parentTransform);
numOfObj++;
if (numOfObj > 2500)
{
numOfObj = 0;
CreateNewParent();
}
}
else if (wallMap.GetPixel(x, z) == playerSpawnColor)
{
wallPos.Add(new Vector2(x, z));
}
}
}
Every time the the number of objects is more than 2500 I create a new parent and set it equal to parentTranform. At the end all the parents combine the child meshes together. This currently works well but since it the objects are set to parents in stripes, looking one way will either lower or increase fps by a lot due to the clipping plane. What I want is to be able to group the objects by chunks. I tried to use something like this:
for (int i = 0; i < 19; i++)
{
for (int u = 0; u < 19; u++)
{
Color[] colors = wallMap.GetPixels(i * 20, u * 20, 500, 500);
for (int o = 0; o < colors.Length; o++)
{
if (colors[i] == wallColor)
{
Instantiate(wall, new Vector3(i * 20 + o, 0, u * 20 + o), Quaternion.identity, parentTransform);
numOfObj++;
if (numOfObj > 2500)
{
numOfObj = 0;
CreateNewParent();
}
}
}
}
}
My reasoning was that I could read 500 by 500 areas of the texture at a time and group those together. I had some relative success but it isn't really working, and I'm not sure how to get the location when using GetPixels. Thanks for any help.