Сгенерированная карта на основе шума ПерлинаC#

Место общения программистов C#
Anonymous
Сгенерированная карта на основе шума Перлина

Сообщение Anonymous »

Я пытаюсь реализовать ландшафт на основе карты шума. Код взят из Интернета, так что спасибо Себастьяну Лагу.
Все прошло хорошо, карта генерируется нормально, но я хотел увеличить ее. Учитывая тот факт, что Unity не допускает более ~65 000 ячеек/вершин, я сгенерировал карту шума, разделенную на 4 фрагмента карты.
Проблема в том, что, хотя я разделил на 4, когда я пытаюсь выровнять все фрагменты в представлении сцены, сетка отсутствует.
[img]https://i.sstatic. net/LwLqv.png[/img]

Я использовал тот же код для создания 2DNoiseMaps, и они идеально выровнены, так что это потому, что сетки. Есть ли способ как-то объединить сетки MapChunk?

Код: Выделить всё

Noise.cs:
using UnityEngine;
using System.Collections;

public static class Noise
{
public static float[,] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vector2 offset)
{
float[,] noiseMap = new float[mapWidth, mapHeight];

System.Random prng = new System.Random(seed);
Vector2[] octaveOffsets = new Vector2[octaves];

for (int i = 0; i < octaves; i++)
{
float offsetX = prng.Next(-100000, 100000) + offset.x;
float offsetY = prng.Next(-100000, 100000) + offset.y;
octaveOffsets[i] = new Vector2(offsetX, offsetY);
}

if (scale  maxNoiseHeight)
{
maxNoiseHeight = noiseHeight;
}
else if (noiseHeight < minNoiseHeight)
{
minNoiseHeight = noiseHeight;
}

noiseMap[x, y] = noiseHeight;
}
}

for (int y = 0; y < mapHeight; y++)
{
for (int x = 0; x < mapWidth; x++)
{
noiseMap[x, y] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[x, y]);
}
}

return noiseMap;
}
}

Код: Выделить всё

MeshGeneratorКлассы 
и MeshData

Код: Выделить всё

 public static class MeshGenerator
{

public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve _heightCurve, int levelOfDetail)
{
AnimationCurve heightCurve = new AnimationCurve(_heightCurve.keys);

int width = heightMap.GetLength(0);
int height = heightMap.GetLength(1);
float topLeftX = (width - 1) / -2f;
float topLeftZ = (height - 1) / 2f;

int meshSimplificationIncrement = (levelOfDetail == 0) ? 1 : levelOfDetail * 2;
int verticesPerLine = (width - 1) / meshSimplificationIncrement + 1;

MeshData meshData = new MeshData(verticesPerLine, verticesPerLine);
int vertexIndex = 0;

for (int y = 0; y < height; y += meshSimplificationIncrement)
{
for (int x = 0; x < width; x += meshSimplificationIncrement)
{
meshData.vertices[vertexIndex] = new Vector3(topLeftX + x, heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier, topLeftZ - y);
meshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)height);

if (x < width - 1 && y <  height - 1)
{
meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine);
meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1);
}

vertexIndex++;
}
}

return meshData;

}
}

public class MeshData
{
public Vector3[] vertices;
public int[] triangles;
public Vector2[] uvs;

int triangleIndex;

public MeshData(int meshWidth, int meshHeight)
{
vertices = new Vector3[meshWidth * meshHeight];
uvs = new Vector2[meshWidth * meshHeight];
triangles = new int[(meshWidth - 1) * (meshHeight - 1) * 6];
}

public void AddTriangle(int a, int b, int c)
{
triangles[triangleIndex] = a;
triangles[triangleIndex + 1] = b;
triangles[triangleIndex + 2] = c;
triangleIndex += 3;
}

public Mesh CreateMesh()
{
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.RecalculateNormals();
return mesh;
}

}
А также метод GenerateMap, который создаст все необходимое:

Код: Выделить всё

 public void GenerateMap()
{

MapDisplay display = FindObjectOfType();
meshHeightCurve = BrokenConstantCurve.GenerateCurveBasedOnBiomes(meshHeightCurve, regions);
meshHeightCurve = BrokenConstantCurve.CreateBrokenConstantCurve(meshHeightCurve);
float[,] noiseMap = Noise.GenerateNoiseMap(mapChunkSize * 2, mapChunkSize * 2, seed, noiseScale, octaves, persistance, lacunarity, offset);
float[,] upperLeft = new float[mapChunkSize, mapChunkSize];
float[,] upperRight = new float[mapChunkSize, mapChunkSize];
float[,] lowerLeft = new float[mapChunkSize, mapChunkSize];
float[,] lowerRight = new float[mapChunkSize, mapChunkSize];
List noiseMapChunks = new List() { upperLeft, upperRight, lowerLeft, lowerRight };
for (var x = 0; x < mapChunkSize; x++)
{
for (var y = 0; y < mapChunkSize; y++)
upperLeft[x, y] = noiseMap[x, y];
}
for (var x = mapChunkSize; x < mapChunkSize * 2; x++)
{
for (var y = 0; y < mapChunkSize; y++)
try
{
upperRight[x - mapChunkSize, y] = noiseMap[x, y];
}
catch (Exception ex)
{

}
}
for (var x = 0; x < mapChunkSize; x++)
{
for (var y = mapChunkSize; y < mapChunkSize * 2; y++)
lowerLeft[x, y - mapChunkSize] = noiseMap[x, y];
}
for (var x = mapChunkSize; x < mapChunkSize * 2; x++)
{
for (var y = mapChunkSize; y < mapChunkSize * 2; y++)
lowerRight[x- mapChunkSize, y - mapChunkSize] = noiseMap[x, y];
}
display.ClearMeshes();
for (var chunkIndex = 0; chunkIndex < noiseMapChunks.Count; chunkIndex++)
{

Color[] colourMap = new Color[mapChunkSize * mapChunkSize];
for (int y = 0; y < mapChunkSize; y++)
{
for (int x = 0; x < mapChunkSize; x++)
{
//if (useFalloff)
//{
//    chunk[x, y] = Mathf.Clamp01(chunk[x, y] - falloffMap[x, y]);
//}
float currentHeight = noiseMapChunks[chunkIndex][x, y];
for (int j = 0; j < regions.Length; j++)
{
if (currentHeight 

Подробнее здесь: [url]https://stackoverflow.com/questions/78179209/generated-map-based-on-perlin-noise[/url]

Вернуться в «C#»