簡體   English   中英

為什么在這兩種顏色的Perlin噪波紋理中無法正確生成邊界/邊緣? (統一)

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

我正在使用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

單一紋理

所以我發現底面的像素在頂面是可見的(與左側和右側相同),但是我不知道為什么這樣做,也不知道如何解決。 有任何想法嗎?

我只需要添加:

mapTexture.wrapMode = TextureWrapMode.Clamp;

默認情況下,它可能設置為TextureWrapMode.Repeat。 雖然不確定這只是解決方法還是實際解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM