RaenirSalazar

Untitled

Dec 31st, 2020
1,186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1.         public void Save2()
  2.         {
  3.             // Create a temporary render texture
  4.             RenderTexture temp =
  5.                 RenderTexture.GetTemporary(TextureLength, TextureLength, 0, RenderTextureFormat.ARGB32);
  6.             // Draw to the RT (Texture2D.whiteTexture is just a dummy src)
  7.             // mat is a material with the Turbulence Shader applied to it
  8.             Graphics.Blit(Texture2D.whiteTexture, temp, m_renderer.material);
  9.             // Copy the rendertexture to a regular texture
  10.             Texture2D tex = new Texture2D(TextureLength, TextureLength);
  11.             tex.ReadPixels(new Rect(0, 0, TextureLength, TextureLength), 0, 0, false);
  12.             tex.Apply();
  13.  
  14.             byte[] bytes = tex.EncodeToPNG();
  15.             File.WriteAllBytes(Application.dataPath + "/Temp/test.png", bytes);
  16.         }
  17.  
  18.         public void Save()
  19.         {
  20.            
  21.            
  22.             RenderTexture buffer = new RenderTexture(
  23.                                    TextureLength,
  24.                                    TextureLength,
  25.                                    0,                            // No depth/stencil buffer
  26.                                    RenderTextureFormat.ARGB32,   // Standard colour format
  27.                                    RenderTextureReadWrite.Linear
  28.                                    );
  29.                                         /*
  30.                             RenderTexture buffer = new RenderTexture(
  31.                                         TextureLength,
  32.                                         TextureLength,
  33.                                         0,                            // No depth/stencil buffer
  34.                                         RenderTextureFormat.ARGB32,   // Standard colour format
  35.                                         RenderTextureReadWrite.sRGB // No sRGB conversions
  36.                                     );
  37.                                     */
  38.             buffer.DiscardContents();
  39.  
  40.             texture = new Texture2D(TextureLength, TextureLength, TextureFormat.ARGB32, true);
  41.  
  42.             //MeshRenderer render = GetComponent<MeshRenderer>();
  43.             //texture = render.sharedMaterial.GetTexture("_MainTex") as Texture2D;
  44.             Material material = m_renderer.material;
  45.  
  46.             Graphics.Blit(null, buffer, material);
  47.             RenderTexture.active = buffer;           // If not using a scene camera
  48.  
  49.             texture.ReadPixels(
  50.               new Rect(0, 0, TextureLength, TextureLength), // Capture the whole texture
  51.               0, 0,                          // Write starting at the top-left texel
  52.               false);                          // No mipmaps
  53.             texture.Apply();
  54.             RenderTexture.active = null;
  55.             buffer.Release();
  56.             byte[] bytes = texture.EncodeToPNG();
  57.             File.WriteAllBytes(Application.dataPath + "/Temp/test.png", bytes);
  58.             // texture.Save();
  59.  
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment