void Update() { if (mat != null) { noiseSettings.SetupMaterial(mat); } }
private static void INTERNAL_Blit2D(NoiseSettings noise, RenderTexture dest, Material mat, int pass) { noise.SetupMaterial(mat); RenderTexture tempRT = RenderTexture.GetTemporary(dest.descriptor); RenderTexture prev = RenderTexture.active; RenderTexture.active = tempRT; Graphics.Blit(tempRT, mat, pass); RenderTexture.active = dest; // if(noise.filterSettings.filterStack != null) // { // noise.filterSettings.filterStack.Eval(tempRT, dest); // } // else { Graphics.Blit(tempRT, dest); } RenderTexture.active = prev; RenderTexture.ReleaseTemporary(tempRT); }
/// <summary> /// Bakes 3D noise defined by the given NoiseSettings instance into a Texture3D instance and returns /// a reference to it. /// </summary> /// <param name = "noise"> An instance of NoiseSettings defining the type of noise to bake </param> /// <param name = "width"> The width of the baked Texture3D </param> /// <param name = "height"> The height of the baked Texture3D </param> /// <param name = "depth"> The depth of the baked Texture3D </param> /// <param name = "format"> The GraphicsFormat for the baked Texture3D. In most cases, you will want to use GraphicsFormat.R16_UNorm </param> /// <param name = "flags"> TextureCreation flags for the baked Texture3D. </param> /// <returns> A reference to the baked Texture3D instance </returns> /// <remarks> /// Be careful when specifying TextureCreation flags. If you specify that mipmaps should be generated for /// a Texture3D, that will use a lot more memory than if you were generating mipmaps for a Texture2D. /// </remarks> public static Texture3D BakeToTexture3D(NoiseSettings noise, int width, int height, int depth, GraphicsFormat format = GraphicsFormat.R16_UNorm, TextureCreationFlags flags = TextureCreationFlags.None) { Material mat = GetDefaultBlitMaterial(noise); if (mat == null) { return(null); } RenderTexture sliceRT = RenderTexture.GetTemporary(width, height, 0, GraphicsFormat.R16_UNorm); Texture2D slice2D = new Texture2D(width, height, format, flags); Color[] colors = new Color[width * height * depth]; noise.SetupMaterial(mat); int pass = NoiseLib.GetNoiseIndex(noise); RenderTexture.active = sliceRT; List <Color[]> sliceColors = new List <Color[]>(depth); for (int i = 0; i < depth; ++i) { float uvy = ((float)i + 0.5f) / depth; mat.SetFloat("_UVY", uvy); Graphics.Blit(null, sliceRT, mat, pass * kNumBlitPasses + 1); slice2D.ReadPixels(new Rect(0, 0, width, height), 0, 0); sliceColors.Add(slice2D.GetPixels(0, 0, width, height)); } int pixPerSlice = width * height; for (int sliceID = 0; sliceID < sliceColors.Count; ++sliceID) { for (int pixelID = 0; pixelID < sliceColors[sliceID].Length; ++pixelID) { int pixel = (pixPerSlice * sliceID) + pixelID; colors[pixel] = sliceColors[sliceID][pixelID]; } } bool mipChain = ((int)flags & (int)TextureCreationFlags.MipChain) != 0; Texture3D texture = new Texture3D(width, height, depth, format, flags); texture.SetPixels(colors); texture.Apply(mipChain); RenderTexture.active = null; RenderTexture.ReleaseTemporary(sliceRT); return(texture); }