///// <summary> ///// Outputs a Render texture as a .png file ///// </summary> ///// <param name="path">The path to write to, including filename ending in .exr</param> ///// <param name="sourceRenderTexture">The render texture to export</param> //public static void WriteRenderTexturePNG(string path, RenderTexture sourceRenderTexture, TextureFormat textureFormat = TextureFormat.RGBA32) //{ // RenderTexture origTex = RenderTexture.active; // RenderTexture.active = sourceRenderTexture; // Texture2D exportTexture = new Texture2D(RenderTexture.active.width, RenderTexture.active.height, textureFormat, false); // exportTexture.ReadPixels(new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height), 0, 0); // exportTexture.Apply(); // byte[] exrBytes = ImageConversion.EncodeToPNG(exportTexture); // PWCommon4.Utils.WriteAllBytes(path, exrBytes); // RenderTexture.active = origTex; //} /// <summary> /// Outputs a Render texture to different file formats (default: .exr file) /// </summary> /// <param name="path">The path to write to, without the extension. The correct extension will be added according to the chosen imageFileType.</param> /// <param name="sourceRenderTexture">The render texture to export</param> /// <param name="imageFileType">Image File Type: EXR, PNG, TGA or JPG</param> /// <param name="textureFormat">Texture color format for the temporary Texture 2D to read the render texture into for exporting.</param> /// <returns>The full path to the exported file.</returns> public static string WriteRenderTexture(string path, RenderTexture sourceRenderTexture, GaiaConstants.ImageFileType imageFileType = GaiaConstants.ImageFileType.Exr, TextureFormat textureFormat = TextureFormat.RGBAFloat, int jpgQuality = 75) { RenderTexture origTex = RenderTexture.active; RenderTexture.active = sourceRenderTexture; Texture2D exportTexture = new Texture2D(RenderTexture.active.width, RenderTexture.active.height, textureFormat, false); exportTexture.ReadPixels(new Rect(0, 0, RenderTexture.active.width, RenderTexture.active.height), 0, 0); exportTexture.Apply(); byte[] fileBytes = new byte[0]; string extension = ".file"; switch (imageFileType) { case GaiaConstants.ImageFileType.Exr: fileBytes = ImageConversion.EncodeToEXR(exportTexture, Texture2D.EXRFlags.CompressZIP); extension = ".exr"; break; case GaiaConstants.ImageFileType.Png: fileBytes = ImageConversion.EncodeToPNG(exportTexture); extension = ".png"; break; case GaiaConstants.ImageFileType.Tga: fileBytes = ImageConversion.EncodeToTGA(exportTexture); extension = ".tga"; break; case GaiaConstants.ImageFileType.Jpg: fileBytes = ImageConversion.EncodeToJPG(exportTexture, jpgQuality); extension = ".jpg"; break; } path += extension; PWCommon4.Utils.WriteAllBytes(path, fileBytes); RenderTexture.active = origTex; return(path); }
public static byte[] ByteArray(Texture2D texture, PictureExtension extension) { if (extension == PictureExtension.EXR) { return(ImageConversion.EncodeToEXR(texture)); } else if (extension == PictureExtension.JPG) { return(ImageConversion.EncodeToJPG(texture)); } else if (extension == PictureExtension.PNG) { return(ImageConversion.EncodeToPNG(texture)); } else if (extension == PictureExtension.TGA) { return(ImageConversion.EncodeToTGA(texture)); } else { Debug.LogError("Not possible to encode 'Texture2D' to byte array ... "); return(null); } }
static void ExportToTGA(string path, Texture2D tex) { System.IO.File.WriteAllBytes(path, ImageConversion.EncodeToTGA(tex)); }
/// <summary> /// Takes the actual screen shot when the key is pressed or takeshot is true /// </summary> private void LateUpdate() { if (Input.GetKeyDown(m_screenShotKey) || m_takeShot) { if (m_mainCamera == null) { m_mainCamera = GaiaUtils.GetCamera(true); } //Pick up and use the actual screen dimensions if (m_useScreenSize) { m_targetWidth = Screen.width; m_targetHeight = Screen.height; } m_refreshAssetDB = true; RenderTexture rt; if (m_imageFormat == GaiaConstants.ImageFileType.Exr) { rt = new RenderTexture(m_targetWidth, m_targetHeight, 24, DefaultFormat.HDR); } else { rt = new RenderTexture(m_targetWidth, m_targetHeight, 24, DefaultFormat.LDR); } m_mainCamera.targetTexture = rt; Texture2D screenShot; if (m_imageFormat == GaiaConstants.ImageFileType.Exr) { screenShot = new Texture2D(m_targetWidth, m_targetHeight, TextureFormat.RGBAFloat, false); } else { screenShot = new Texture2D(m_targetWidth, m_targetHeight, TextureFormat.RGB24, false); } bool allowHDR = m_mainCamera.allowHDR; if (m_imageFormat == GaiaConstants.ImageFileType.Exr) { m_mainCamera.allowHDR = true; } m_mainCamera.Render(); m_mainCamera.allowHDR = allowHDR; RenderTexture.active = rt; screenShot.ReadPixels(new Rect(0, 0, m_targetWidth, m_targetHeight), 0, 0); m_mainCamera.targetTexture = null; RenderTexture.active = null; // JC: added to avoid errors Destroy(rt); if (m_watermark != null) { Gaia.GaiaUtils.MakeTextureReadable(m_watermark); screenShot = AddWatermark(screenShot, m_watermark); } byte[] bytes = null; switch (m_imageFormat) { case GaiaConstants.ImageFileType.Exr: bytes = ImageConversion.EncodeToEXR(screenShot, Texture2D.EXRFlags.CompressZIP); break; case GaiaConstants.ImageFileType.Png: bytes = ImageConversion.EncodeToPNG(screenShot); break; case GaiaConstants.ImageFileType.Tga: bytes = ImageConversion.EncodeToTGA(screenShot); break; case GaiaConstants.ImageFileType.Jpg: bytes = ImageConversion.EncodeToJPG(screenShot, 100); break; } string filename = ScreenShotName(m_targetWidth, m_targetHeight); PWCommon4.Utils.WriteAllBytes(filename, bytes); m_takeShot = false; Debug.Log(string.Format("Took screenshot to: {0}", filename)); } }