/// <summary>
/// Uses the GPU to generate a RenderTexture where the pixels in the texture represent noise.
/// Set the octaves variable before calling this to a desired value.
/// </summary>
/// <returns>RenderTexture</returns>
/// <param name="width"> Width of the texture to generate. </param>
/// <param name="height"> Height of the texture to generate. </param>
/// <param name="noiseOffsetX"> X Coordinate of the offset for the noise on the texture. </param>
/// <param name="noiseOffsetY"> Y Coordinate of the offset for the noise on the texture. </param>
/// <param name="noiseScale"> Value to scale the noise coordinates by. </param>
/// <param name="normalize"> Whether or not to remap the noise from (-1, 1) to (0, 1). </param>
public static UnityEngine.RenderTexture GetNoiseRenderTexture(int width, int height, float noiseOffsetX = 0, float noiseOffsetY = 0, float noiseScale = 0.01f, bool normalize = true)
{
UnityEngine.RenderTexture retTex = new UnityEngine.RenderTexture(width, height, 0);
retTex.enableRandomWrite = true;
retTex.Create();
UnityEngine.ComputeShader shader = UnityEngine.Resources.Load(shaderPath) as UnityEngine.ComputeShader;
if (shader == null)
{
UnityEngine.Debug.LogError(noShaderMsg);
return(null);
}
int[] resInts = { width, height };
int kernel = shader.FindKernel("ComputeNoise");
shader.SetTexture(kernel, "Result", retTex);
SetShaderVars(shader, new UnityEngine.Vector2(noiseOffsetX, noiseOffsetY), normalize, noiseScale);
shader.SetInts("reses", resInts);
UnityEngine.ComputeBuffer permBuffer = new UnityEngine.ComputeBuffer(perm.Length, 4);
permBuffer.SetData(perm);
shader.SetBuffer(kernel, "perm", permBuffer);
shader.Dispatch(kernel, width / 14, height / 15, 1);
permBuffer.Release();
return(retTex);
}