Hello. So I’m generating terrain using Perlin Noise and tessellating it in a shader. I can correctly displace the mesh vertices on the CPU but tessellation is not increasing the mesh detail since the new vertices are interpolated from the old ones. I want to expand my approach to calculate the noise into a height map texture and sample it in my tessellation shader so that the new vertices can contribute to the detail of the mesh. But when I try to displace the vertices they all end up at the same y value of around 0.8 and the mesh is flat. There should be no issue with my shader code because when I use “SetTexture” with a Texture2D of normal image via the Inspector it works correctly. So there seems to be an issue with the GeneratedHeightMap itself. I have spent hours trying to find a solution.
Texture2D GeneratedHeightMap = new Texture2D((int)size, (int)size);
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
float2 uv = float2(x, y) / (float)size;
float freq = frequency;
float noise = 0f;
float AmplitudeSum = 0f;
float Amp = 1f;
for (int o = 0; o < octaves; o++){
noise += Amp * saturate(Mathf.PerlinNoise(uv.x * freq, uv.y * freq));
AmplitudeSum += Amp;
Amp *= persistence;
freq *= lacunarity;
}
noise /= AmplitudeSum;
GeneratedHeightMap.SetPixel(x, y, new Color(noise, noise, noise, 1f));
}
}
this.GetComponent<Renderer>().material.SetTexture("_HeightMap", GeneratedHeightMap);