/// <summary>
/// Generates a texture containing the given graph's noise output.
/// If this is being called very often, create a permanent render target and material and
/// use the other version of this method instead for much better performance.
/// If an error occurred, outputs to the Unity debug console and returns "null".
/// </summary>
/// <param name="outputComponents">
/// The texture output.
/// For example, pass "rgb" or "xyz" to output the noise into the red, green, and blue channels
/// but not the alpha channel.
/// </param>
/// <param name="defaultColor">
/// The color (generally 0-1) of the color components which aren't set by the noise.
/// </param>
public static Texture2D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height,
string outputComponents, float defaultColor,
TextureFormat format = TextureFormat.RGBAFloat)
{
//Generate a shader from the graph and have Unity compile it.
string shaderPath = Path.Combine(Application.dataPath, "gpuNoiseShaderTemp.shader");
Shader shader = SaveShader(g, shaderPath, "TempGPUNoiseShader", outputComponents, defaultColor);
if (shader == null)
{
return(null);
}
//Render the shader's output into a render texture and copy the data to a Texture2D.
RenderTexture target = new RenderTexture(width, height, 16, RenderTextureFormat.ARGBFloat);
target.Create();
Texture2D resultTex = new Texture2D(width, height, format, false, true);
//Create the material and set its parameters.
Material mat = new Material(shader);
c.SetParams(mat);
GraphUtils.GenerateToTexture(target, mat, resultTex);
//Clean up.
target.Release();
if (!AssetDatabase.DeleteAsset(StringUtils.GetRelativePath(shaderPath, "Assets")))
{
Debug.LogError("Unable to delete temp file: " + shaderPath);
}
return(resultTex);
}