Пример #1
0
        ///// <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);
        }
Пример #2
0
        /// <summary>
        /// Compress / encode a multi layer map file to an image
        /// </summary>
        /// <param name="input">Multi layer map in format x,y,layer</param>
        /// <param name="imageName">Output image name - image image index and extension will be added</param>
        /// <param name="exportPNG">True if a png is wanted</param>
        /// <param name="exportJPG">True if a jpg is wanted</param>
        public static void CompressToMultiChannelFileImage(string imageName, HeightMap r, HeightMap g, HeightMap b, HeightMap a, TextureFormat imageStorageFormat, GaiaConstants.ImageFileType imageFileType)
        {
            int width  = 0;
            int height = 0;

            if (r != null)
            {
                width  = r.Width();
                height = r.Depth();
            }
            else if (g != null)
            {
                width  = g.Width();
                height = g.Depth();
            }
            else if (b != null)
            {
                width  = b.Width();
                height = b.Depth();
            }
            else if (a != null)
            {
                width  = a.Width();
                height = a.Depth();
            }

            if (string.IsNullOrEmpty(imageName))
            {
                Debug.LogError("Cannot write image - no name supplied!");
                return;
            }

            if (width == 0 || height == 0)
            {
                Debug.LogError("Cannot write image - invalid dimensions : " + width + ", " + height);
                return;
            }

            Texture2D exportTexture = new Texture2D(width, height, imageStorageFormat, true, false);
            Color     pixelColor    = new Color();

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    pixelColor.r = r != null ? r[x, y] : 0f;
                    pixelColor.g = g != null ? g[x, y] : 0f;
                    pixelColor.b = b != null ? b[x, y] : 0f;
                    pixelColor.a = a != null ? a[x, y] : 1f;
                    exportTexture.SetPixel(x, y, pixelColor);
                }
            }
            exportTexture.Apply();

            #if UNITY_2017_1_OR_NEWER
            switch (imageFileType)
            {
            case GaiaConstants.ImageFileType.Jpg:
                byte[] jpgBytes = ImageConversion.EncodeToJPG(exportTexture, 100);
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".jpg", jpgBytes);
                break;

            case GaiaConstants.ImageFileType.Png:
                byte[] pngBytes = ImageConversion.EncodeToPNG(exportTexture);
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".png", pngBytes);
                break;

            case GaiaConstants.ImageFileType.Exr:
                byte[] exrBytes = ImageConversion.EncodeToEXR(exportTexture, Texture2D.EXRFlags.CompressZIP);
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".exr", exrBytes);
                break;
            }
            #else
            switch (imageFileType)
            {
            case GaiaConstants.ImageFileType.Jpg:
                byte[] jpgBytes = exportTexture.EncodeToJPG();
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".jpg", jpgBytes);
                break;

            case GaiaConstants.ImageFileType.Png:
                byte[] pngBytes = exportTexture.EncodeToPNG();
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".png", pngBytes);
                break;

            case GaiaConstants.ImageFileType.Exr:
                byte[] exrBytes = exportTexture.EncodeToEXR(Texture2D.EXRFlags.CompressZIP);
                GaiaCommon1.Utils.WriteAllBytes(imageName + ".exr", exrBytes);
                break;
            }
            #endif

            #if UNITY_EDITOR
            AssetDatabase.Refresh();
            #endif

            //Lose the texture
            DestroyImmediate(exportTexture);
        }