/// <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> /// <param name="uvZ">The Z coordinate of the UVs, in case the graph uses it for 3D noise.</param> /// <param name="leaveReadable"> /// Whether the texture's pixel data can still be read from the CPU after this operation. /// </param> public static Texture2D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height, float uvZ, string outputComponents, float defaultColor, TextureFormat format = TextureFormat.RGBAFloat, bool leaveReadable = false) { //Generate a shader/material from the graph. Shader shader = ShaderUtil.CreateShaderAsset(g.GenerateShader("TempGPUNoiseShader", outputComponents, defaultColor)); if (shader == null) { return(null); } Material mat = new Material(shader); c.SetParams(mat); mat.SetFloat(GraphUtils.Param_UVz, uvZ); //Render the shader's output into a render texture and copy the data to a Texture2D. RenderTexture target = RenderTexture.GetTemporary(width, height, 16, RenderTextureFormat.ARGBFloat); Texture2D resultTex = new Texture2D(width, height, format, false, true); //Generate. GraphUtils.GenerateToTexture(target, mat, resultTex, leaveReadable); //Clean up. RenderTexture.ReleaseTemporary(target); return(resultTex); }
/// <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); }
/// <summary> /// Outputs noise into the given Texture2D. /// </summary> public void GenerateToTexture(Texture2D outTex) { RenderTexture tempTex = RenderTexture.GetTemporary(outTex.width, outTex.height); GraphUtils.GenerateToTexture(tempTex, GraphMat, outTex); RenderTexture.ReleaseTemporary(tempTex); }
/// <summary> /// Outputs noise into the given Texture2D. /// </summary> /// <param name="leaveReadable"> /// Whether to leave the texture data readable on the CPU after the operation. /// </param> public void GenerateToTexture(Texture2D outTex, bool leaveReadable = false) { UpdateAllParams(); RenderTexture tempTex = RenderTexture.GetTemporary(outTex.width, outTex.height); GraphUtils.GenerateToTexture(tempTex, GraphMat, outTex, leaveReadable); RenderTexture.ReleaseTemporary(tempTex); }
/// <summary> /// Generates a 3D texture containing the given graph's noise output. /// </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> /// <param name="useMipmaps">Whether the 3D texture object uses mipmapping.</param> /// <param name="leaveTextureReadable"> /// Whether to let the texture keep a CPU copy of its data on hand for later reading. /// </param> public static Texture3D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height, int depth, string outputComponents, float defaultColor, bool useMipmaps, bool leaveTextureReadable, TextureFormat format = TextureFormat.RGBA32) { //Generate a shader/material from the graph. Shader shader = ShaderUtil.CreateShaderAsset(g.GenerateShader("TempGPUNoiseShader", outputComponents, defaultColor)); if (shader == null) { return(null); } Material mat = new Material(shader); c.SetParams(mat); //For every Z layer in the texture, generate a 2D texture representing that layer. Color32[] finalPixels = new Color32[width * height * depth]; RenderTexture target = RenderTexture.GetTemporary(width, height, 16, RenderTextureFormat.ARGBFloat); Texture2D resultTex = new Texture2D(width, height, TextureFormat.RGBAFloat, false, true); for (int depthI = 0; depthI < depth; ++depthI) { //Get the UV.z coordinate. float uvZ = (float)depthI / depth; mat.SetFloat(GraphUtils.Param_UVz, uvZ); GraphUtils.GenerateToTexture(target, mat, resultTex, true); //Copy the resulting data into part of the 3D texture. Color32[] layerPixels = resultTex.GetPixels32(); int pixelOffset = depthI * (width * height); for (int pixelI = 0; pixelI < (width * height); ++pixelI) { finalPixels[pixelI + pixelOffset] = layerPixels[pixelI]; } } //Create the actual texture object. Texture3D finalTex = new Texture3D(width, height, depth, format, useMipmaps); finalTex.SetPixels32(finalPixels); finalTex.Apply(useMipmaps, !leaveTextureReadable); //Clean up. RenderTexture.ReleaseTemporary(target); return(finalTex); }
/// <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="gradientRampName">The name of the gradient ramp texture param.</param> public static Texture2D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height, Gradient gradientRamp, 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", "_MyGradientRamp14123"); if (shader == null) { return(null); } //Generate a texture from the gradient. Texture2D myRamp = new Texture2D(1024, 1, TextureFormat.RGBA32, false); Color[] cols = new Color[myRamp.width]; for (int i = 0; i < cols.Length; ++i) { cols[i] = gradientRamp.Evaluate((float)i / (float)(cols.Length - 1)); } myRamp.SetPixels(cols); myRamp.Apply(false, true); //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); mat.SetTexture("_MyGradientRamp14123", myRamp); 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); }
/// <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="gradientRampName">The name of the gradient ramp texture param.</param> /// <param name="uvZ">The Z coordinate of the UVs, in case the graph uses it for 3D noise.</param> /// <param name="leaveReadable"> /// Whether to leave the texture data readable on the CPU after the operation. /// </param> public static Texture2D GenerateToTexture(Graph g, GraphParamCollection c, int width, int height, float uvZ, Gradient gradientRamp, TextureFormat format = TextureFormat.RGBAFloat, bool leaveReadable = false) { //Generate a shader/material from the graph. Shader shader = ShaderUtil.CreateShaderAsset(g.GenerateShader("TempGPUNoiseShader", "_MyGradientRamp14123")); if (shader == null) { return(null); } Material mat = new Material(shader); c.SetParams(mat); mat.SetFloat(GraphUtils.Param_UVz, uvZ); //Generate a texture from the gradient. Texture2D myRamp = new Texture2D(1024, 1, TextureFormat.RGBA32, false); Color[] cols = new Color[myRamp.width]; for (int i = 0; i < cols.Length; ++i) { cols[i] = gradientRamp.Evaluate((float)i / (float)(cols.Length - 1)); } myRamp.SetPixels(cols); myRamp.Apply(false, true); mat.SetTexture("_MyGradientRamp14123", myRamp); //Render the shader's output into a render texture and copy the data to a Texture2D. RenderTexture target = RenderTexture.GetTemporary(width, height, 16, RenderTextureFormat.ARGBFloat); Texture2D resultTex = new Texture2D(width, height, format, false, true); //Generate. GraphUtils.GenerateToTexture(target, mat, resultTex, leaveReadable); //Clean up. RenderTexture.ReleaseTemporary(target); return(resultTex); }
/// <summary> /// Outputs noise into the given array. /// </summary> public void GenerateToArray(float[,] outData) { GraphUtils.GenerateToArray(outData, GraphMat); }
/// <summary> /// Outputs noise into the given RenderTexture. /// </summary> public void GenerateToFramebuffer(RenderTexture outTex) { GraphUtils.GenerateToTexture(outTex, GraphMat); }
/// <summary> /// Outputs noise into the screen or whatever RenderTexture is currently active. /// </summary> public void GenerateToCurrentFramebuffer() { GraphUtils.GenerateToTexture(RenderTexture.active, GraphMat); }
/// <summary> /// Outputs noise into the given RenderTexture. /// </summary> public void GenerateToFramebuffer(RenderTexture outTex) { UpdateAllParams(); GraphUtils.GenerateToTexture(outTex, GraphMat); }
/// <summary> /// Outputs noise into the given array. /// </summary> public void GenerateToArray(float[,] outData) { UpdateAllParams(); GraphUtils.GenerateToArray(outData, GraphMat); }