[英]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). 我正在使用Perlin噪波生成纹理,并且我做到了,因此纹理只有2种颜色(如果该值超出限制,则其白色为黑色,否则为黑色)。 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 我在此处添加代码,因此您可以在任何统一项目中轻松检查出该代码,并且我还尝试发送图片: 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). 编辑:使图片为64x64,这样我可以更好地看到像素,并且我认为我发现了问题(有点)。 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: 现在纹理看起来像这样:
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. 默认情况下,它可能设置为TextureWrapMode.Repeat。 Not sure whether this is just a workaround or the actual solution though. 虽然不确定这只是解决方法还是实际解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.