我试图使用这个循环填充颜色数组然后用来设置像素,但由于某种原因我无法理解它只在某些时候工作。有时它会完美地产生纹理,有时它会混乱起来,我做错了什么?
void texRend()
{
GameObject parentGameObject = new GameObject("SpaceStation");
int tileRow = 0;
int tileCol = 0;
GameObject floorQuad = GameObject.CreatePrimitive(PrimitiveType.Quad);
floorQuad.transform.localScale = new Vector3(stationWidth*1.28f,stationHeight*1.28f,1);
floorQuad.transform.parent = parentGameObject.transform;
floorQuad.transform.position = new Vector3(0.0f, 0.0f, 0.0f);
Color transparent = new Color(0,0,0,0);
Texture2D floorTex = new Texture2D(stationWidth*256, stationHeight*256, TextureFormat.ARGB32, false);
Color[] floorArray = new Color[stationWidth*256*stationHeight*256];
int k = 0;
for(int row = 0; row < stationWidth*256; row++)
{
for(int col = 0; col < stationHeight*256; col++)
{
tileRow = Mathf.FloorToInt(row/256);
tileCol = Mathf.FloorToInt(col/256);
float prow = ((float)tileRow/(float)stationWidth)*scale;
float pcol = ((float)tileCol/(float)stationHeight)*scale;
if(roomType[tileRow, tileCol] != (int)room.None && roomType[tileRow, tileCol] != (int)room.Corridor)
{
int tileNumber = Mathf.Abs(Mathf.RoundToInt(Mathf.PerlinNoise(prow,pcol)*baseSprites.Length)%baseSprites.Length);
floorArray[k] = baseSprites[tileNumber].texture.GetPixel(row%256, col%256);
}
else if(roomType[tileRow, tileCol] == (int)room.Corridor)
{
if(connections[tileRow,tileCol] == (int)connect.NESW)
{
floorArray[k] = corridorFloorNESW.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.NS)
{
floorArray[k] = corridorFloorNS.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.EW)
{
floorArray[k] = corridorFloorEW.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.NE)
{
floorArray[k] = corridorFloorNE.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.NW)
{
floorArray[k] = corridorFloorNW.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.SE)
{
floorArray[k] = corridorFloorSE.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.SW)
{
floorArray[k] = corridorFloorSW.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.WNE)
{
floorArray[k] = corridorFloorWNE.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.NES)
{
floorArray[k] = corridorFloorNES.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.ESW)
{
floorArray[k] = corridorFloorESW.texture.GetPixel(row%256, col%256);
}
if(connections[tileRow,tileCol] == (int)connect.SWN)
{
floorArray[k] = corridorFloorSWN.texture.GetPixel(row%256, col%256);
}
}
k++;
}
}
floorTex.SetPixels(floorArray);
floorTex.Apply();
//attach texture to quad
floorQuad.GetComponent<Renderer>().material.mainTexture = floorTex;
floorQuad.GetComponent<Renderer>().material.shader = Shader.Find("Sprites/Diffuse");
byte[] bytes = floorTex.EncodeToPNG();
File.WriteAllBytes(Application.dataPath + "/../floorTexture.png", bytes);
}
好吧,经过这么多次运行后,它似乎只在纹理为正方形时产生正确的纹理。我想我需要某种动态偏移。
答案 0 :(得分:0)
原来我需要做的就是改变行和col for循环,它完美地工作!我不会期望纹理变得错误,就像那些不正确的那样。