简体   繁体   中英

Why are the borders/edges not generated properly in this 2 color perlin noise texture? (Unity)

I am generating textures using perlin noise, and i make it so the texture has only 2 colors (if the value is above a limit its white else its black). So the problem is that the color of the texture on the edges is sometimes not right, and i cant seem to find out why. I am adding the code here, so u can check it out easily in any unity project, and i also try to send a picture: 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;
 }

}

EDIT: made pictures 64x64 so i can see the pixels better, and i think i found the problem (sort of). i changed the colorization a bit:

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;

And now the textures look like this:

newpic

single texture

So what i found is that pixels on the bottom side are visible on the top side (the same with left and right side), but i have no idea why is that, nor how i could fix that. Any ideas?

I just had to add:

mapTexture.wrapMode = TextureWrapMode.Clamp;

It was probably set to TextureWrapMode.Repeat by default. Not sure whether this is just a workaround or the actual solution though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM