2 years ago
#15339
Zacattack Space
cant make GPU generated terrain line up
story
I've been working on an open-world game we are using procedurally generated terrain to do most of the heavy lifting then we will go through and move stuff around as we want so far its been going great but we are now needing bigger terrains so I tried adding a neighboring terrain but they don't line up correctly, For your information, I'm using the GPU to generate the terrain for performance reasons I got the code from Sebastian league but I don't know anything about compute shaders
Problem
so what I suspect to be the problem is that there is no offset variable to be able to offset the Perlin noise for the 2nd terrain and I've tried to add one in but every time it just doesn't seem to do anything I've been able to do it for the CPU generated terrain but that's because I understand it
public float OffsetX;
public float OffsetZ;
I want to be able to control the offset with these thanks in advance
float[] GenerateHeightMapGPU (int mapSize) {
var prng = new System.Random (seed);
Vector2[] offsets = new Vector2[numOctaves];
for (int i = 0; i < numOctaves; i++) {
offsets[i] = new Vector2 (prng.Next (-10000, 10000), prng.Next (-10000, 10000));
}
ComputeBuffer offsetsBuffer = new ComputeBuffer (offsets.Length, sizeof (float) * 2);
offsetsBuffer.SetData (offsets);
heightMapComputeShader.SetBuffer (0, "offsets", offsetsBuffer);
int floatToIntMultiplier = 1000;
float[] map = new float[mapSize * mapSize];
ComputeBuffer mapBuffer = new ComputeBuffer (map.Length, sizeof (int));
mapBuffer.SetData (map);
heightMapComputeShader.SetBuffer (0, "heightMap", mapBuffer);
int[] minMaxHeight = { floatToIntMultiplier * numOctaves, 0 };
ComputeBuffer minMaxBuffer = new ComputeBuffer (minMaxHeight.Length, sizeof (int));
minMaxBuffer.SetData (minMaxHeight);
heightMapComputeShader.SetBuffer (0, "minMax", minMaxBuffer);
heightMapComputeShader.SetInt ("mapSize", mapSize);
heightMapComputeShader.SetInt ("octaves", numOctaves);
heightMapComputeShader.SetFloat ("lacunarity", lacunarity);
heightMapComputeShader.SetFloat ("persistence", persistence);
heightMapComputeShader.SetFloat ("scaleFactor", initialScale);
heightMapComputeShader.SetInt ("floatToIntMultiplier", floatToIntMultiplier);
heightMapComputeShader.Dispatch (0, Mathf.CeilToInt(map.Length/64f), 1, 1);
mapBuffer.GetData (map);
minMaxBuffer.GetData (minMaxHeight);
mapBuffer.Release ();
minMaxBuffer.Release ();
offsetsBuffer.Release ();
float minValue = (float) minMaxHeight[0] / (float) floatToIntMultiplier;
float maxValue = (float) minMaxHeight[1] / (float) floatToIntMultiplier;
for (int i = 0; i < map.Length; i++) {
map[i] = Mathf.InverseLerp (minValue, maxValue, map[i]);
}
return map;
}
c#
unity-game-engine
gpu
terrain
procedural-generation
0 Answers
Your Answer