为什么在这2种颜色的perlin噪声纹理中没有正确生成边框/边缘? (统一)

时间:2018-05-24 21:55:43

标签: c# unity3d

我使用perlin噪音生成纹理,并且我使它的纹理只有2种颜色(如果值高于限制,则其白色为黑色)。所以问题是边缘纹理的颜色有时不对,我似乎无法找出原因。我在这里添加代码,所以你可以在任何统一项目中轻松查看,我也尝试发送图片:borderproblem

public class TestScript : MonoBehaviour{
 public int mapWidth = 256;
 public int mapHeight = 256;
 public int xCount = 3;
 public int yCount = 3;

 void Start()
 {
     //create number of textures
     for (int y = 0; y < yCount; y++)
         for (int x = 0; x < xCount; x++)
         {
             CreateTexture(x * mapWidth, y * mapHeight);
         }
 }

 void CreateTexture(int posX, int posY)
 {
     //Creating gameObject to hold the texture
     GameObject gObject = new GameObject(posX + "; " + posY);
     gObject.transform.parent = this.transform;
     gObject.transform.localPosition = new Vector3(posX, posY);

     //adding SpriteRenderer and create and set a texture for it
     Texture2D mapTexture = new Texture2D(mapWidth, mapHeight);
     SpriteRenderer spriteRenderer = gObject.AddComponent<SpriteRenderer>();
     Vector2 mapSize = new Vector2(mapWidth, mapHeight);
     spriteRenderer.sprite = Sprite.Create(mapTexture, new Rect(Vector2.zero, mapSize), Vector2.zero, 1f);

     //generate texture values
     GenerateValues(mapTexture, posX, posY);
 }

 public void GenerateValues(Texture2D mapTexture, int posX, int posY)
 {
     //create a color array for the texture pixels
     Color32[] baseColors = new Color32[mapWidth * mapHeight];

     //iterate through all elements of color array
     for (int i = 0, y = 0; y < mapHeight; y++)
         for (int x = 0; x < mapWidth; x++, i++)
         {
             //calculate value
             bool currentValue = CalculateMapValue(x + posX, y + posY, 100000, 100000);
             //set color based on calculated value
             baseColors[y * mapWidth + x] = currentValue ? Color.black : Color.white;
         }

     // set colors and apply texture
     mapTexture.SetPixels32(baseColors);
     mapTexture.Apply();

 }

 bool CalculateMapValue(int x, int y, float offsetX, float offsetY)
 {
     float xCoord = (float)x / mapWidth * 5f + offsetX;
     float yCoord = (float)y / mapHeight * 5f + offsetY;

     float sample = Mathf.PerlinNoise(xCoord, yCoord);

     return sample <= 0.5f ? true : false;
 }

}

编辑: 制作图片64x64所以我可以更好地看到像素,我想我发现了问题(有点)。我稍微改变了颜色:

for (int i = 0, y = 0; y < mapHeight; y++)
        for (int x = 0; x < mapWidth; x++, i++)
        {
            //calculate value
            bool currentValue = CalculateMapValue(x + posX, y + posY, 100000, 100000);
            currentValue = y > x && x % 2 == 0;
            //set color based on calculated value
            baseColors[y * mapWidth + x] = currentValue ? Color.black : Color.white;
        }

    baseColors[mapWidth + 10] = Color.red;
    baseColors[0] = Color.red;

现在纹理看起来像这样:

newpic

single texture

所以我发现底部的像素在顶部可见(左侧和右侧相同),但我不知道为什么会这样,也不知道如何解决这个问题。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我只需要添加:

mapTexture.wrapMode = TextureWrapMode.Clamp;

默认情况下可能设置为TextureWrapMode.Repeat。不确定这只是一种解决方法还是实际的解决方案。