Exemple #1
0
        public int CopyAndGetIndex(Texture texture, RenderTextureReadWrite readWrite)
        {
            if (texture == null)
            {
                return(-1);
            }

            var index = m_textures.IndexOf(texture);

            if (index == -1)
            {
                // ありえない?
                return(-1);
            }

#if UNITY_EDITOR
            if (!string.IsNullOrEmpty(UnityEditor.AssetDatabase.GetAssetPath(texture)))
            {
                m_exportTextures[index] = texture;
                return(index);
            }
#endif

            // ToDo: may already exists
            m_exportTextures[index] = TextureConverter.CopyTexture(texture, readWrite, null);

            return(index);
        }
Exemple #2
0
        /// <summary>
        /// sRGBなテクスチャーを処理し、index を確定させる
        /// </summary>
        /// <param name="src"></param>
        /// <returns></returns>
        public int ExportSRGB(Texture src)
        {
            if (src == null)
            {
                return(-1);
            }

            // cache
            if (m_exportMap.TryGetValue(new ExportKey(src, glTFTextureTypes.SRGB), out var index))
            {
                return(index);
            }

            // get Texture2D
            index = Exported.Count;
            var texture2D = src as Texture2D;

            if (UseAsset(texture2D))
            {
                // do nothing
            }
            else
            {
                texture2D = TextureConverter.CopyTexture(src, TextureImportTypes.sRGB, null);
            }
            Exported.Add(texture2D);
            m_exportMap.Add(new ExportKey(src, glTFTextureTypes.SRGB), index);

            return(index);
        }
Exemple #3
0
        public virtual (Byte[] bytes, string mine) GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
        {
#if UNITY_EDITOR
            var path = UnityPath.FromAsset(texture);
            if (path.IsUnderAssetsFolder)
            {
                var textureImporter = AssetImporter.GetAtPath(path.Value) as TextureImporter;
                var getSizeMethod   = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
                if (textureImporter != null && getSizeMethod != null)
                {
                    var args = new object[2] {
                        0, 0
                    };
                    getSizeMethod.Invoke(textureImporter, args);
                    var originalWidth  = (int)args[0];
                    var originalHeight = (int)args[1];

                    var originalSize    = Mathf.Max(originalWidth, originalHeight);
                    var requiredMaxSize = textureImporter.maxTextureSize;

                    // Resized exporting if MaxSize setting value is smaller than original image size.
                    if (originalSize > requiredMaxSize)
                    {
                        return
                            (
                            TextureConverter.CopyTexture(texture, GetColorSpace(textureType), null).EncodeToPNG(),
                            "image/png"
                            );
                    }
                }

                if (path.Extension == ".png")
                {
                    return
                        (
                        System.IO.File.ReadAllBytes(path.FullPath),
                        "image/png"
                        );
                }
                if (path.Extension == ".jpg")
                {
                    return
                        (
                        System.IO.File.ReadAllBytes(path.FullPath),
                        "image/jpeg"
                        );
                }
            }
#endif

            return
                (
                TextureConverter.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
                "image/png"
                );
        }
Exemple #4
0
 public static Texture2D Import(Texture2D metallicRoughnessTexture,
                                float metallicFactor, float roughnessFactor, Texture2D occlusionTexture)
 {
     if (metallicRoughnessTexture != null && occlusionTexture != null)
     {
         if (metallicRoughnessTexture == occlusionTexture)
         {
             var copyMetallicRoughness   = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
             var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32();
             for (int i = 0; i < metallicRoughnessPixels.Length; ++i)
             {
                 metallicRoughnessPixels[i] = ImportPixel(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, metallicRoughnessPixels[i]);
             }
             copyMetallicRoughness.SetPixels32(metallicRoughnessPixels);
             copyMetallicRoughness.Apply();
             copyMetallicRoughness.name = metallicRoughnessTexture.name;
             return(copyMetallicRoughness);
         }
         else
         {
             var copyMetallicRoughness   = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
             var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32();
             var copyOcclusion           = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
             var occlusionPixels         = copyOcclusion.GetPixels32();
             if (metallicRoughnessPixels.Length != occlusionPixels.Length)
             {
                 throw new NotImplementedException();
             }
             for (int i = 0; i < metallicRoughnessPixels.Length; ++i)
             {
                 metallicRoughnessPixels[i] = ImportPixel(metallicRoughnessPixels[i], metallicFactor, roughnessFactor, occlusionPixels[i]);
             }
             copyMetallicRoughness.SetPixels32(metallicRoughnessPixels);
             copyMetallicRoughness.Apply();
             copyMetallicRoughness.name = metallicRoughnessTexture.name;
             return(copyMetallicRoughness);
         }
     }
     else if (metallicRoughnessTexture != null)
     {
         var copyTexture = TextureConverter.CopyTexture(metallicRoughnessTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
         copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ImportPixel(x, metallicFactor, roughnessFactor, default)).ToArray());
         copyTexture.Apply();
         copyTexture.name = metallicRoughnessTexture.name;
         return(copyTexture);
     }
     else if (occlusionTexture != null)
     {
         throw new NotImplementedException("occlusion only");
     }
     else
     {
         throw new ArgumentNullException("no texture");
     }
 }
Exemple #5
0
        /// <summary>
        /// 画像のバイト列を得る
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="texture"></param>
        /// <returns></returns>
        static (byte[] bytes, string mine) GetBytesWithMime(Texture2D texture)
        {
#if UNITY_EDITOR
            var path = UnityPath.FromAsset(texture);
            if (path.IsUnderAssetsFolder)
            {
                if (path.Extension == ".png")
                {
                    return
                        (
                        System.IO.File.ReadAllBytes(path.FullPath),
                        "image/png"
                        );
                }
                if (path.Extension == ".jpg")
                {
                    return
                        (
                        System.IO.File.ReadAllBytes(path.FullPath),
                        "image/jpeg"
                        );
                }
            }
#endif

            try
            {
                var png = texture.EncodeToPNG();
                if (png != null)
                {
                    return(png, "image/png");
                }
            }
            catch (Exception ex)
            {
                // fail to EncodeToPng
                // System.ArgumentException: not readable, the texture memory can not be accessed from scripts. You can make the texture readable in the Texture Import Settings.

                Debug.LogWarning(ex);
            }

            {
                // try copy and EncodeToPng
                var copy = TextureConverter.CopyTexture(texture, TextureImportTypes.sRGB, null);
                var png  = copy.EncodeToPNG();
                UnityEngine.Object.DestroyImmediate(copy);

                return(png, "image/png");
            }
        }
Exemple #6
0
        private static (byte[] bytes, string mime) CopyTextureAndGetBytesWithMime(Texture2D texture, ColorSpace colorSpace)
        {
            var copiedTex = TextureConverter.CopyTexture(texture, colorSpace, null);
            var bytes     = copiedTex.EncodeToPNG();

            if (Application.isPlaying)
            {
                UnityEngine.Object.Destroy(copiedTex);
            }
            else
            {
                UnityEngine.Object.DestroyImmediate(copiedTex);
            }

            return(bytes, "image/png");
        }
Exemple #7
0
 public static Texture2D Export(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture)
 {
     if (metallicSmoothTexture != null && occlusionTexture != null)
     {
         if (metallicSmoothTexture == occlusionTexture)
         {
             var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
             copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, x)).ToArray());
             copyTexture.Apply();
             copyTexture.name = metallicSmoothTexture.name;
             return(copyTexture);
         }
         else
         {
             var copyMetallicSmooth   = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
             var metallicSmoothPixels = copyMetallicSmooth.GetPixels32();
             var copyOcclusion        = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
             var occlusionPixels      = copyOcclusion.GetPixels32();
             if (metallicSmoothPixels.Length != occlusionPixels.Length)
             {
                 throw new NotImplementedException();
             }
             for (int i = 0; i < metallicSmoothPixels.Length; ++i)
             {
                 metallicSmoothPixels[i] = ExportPixel(metallicSmoothPixels[i], smoothness, occlusionPixels[i]);
             }
             copyMetallicSmooth.SetPixels32(metallicSmoothPixels);
             copyMetallicSmooth.Apply();
             copyMetallicSmooth.name = metallicSmoothTexture.name;
             return(copyMetallicSmooth);
         }
     }
     else if (metallicSmoothTexture)
     {
         var copyTexture = TextureConverter.CopyTexture(metallicSmoothTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
         copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, default)).ToArray());
         copyTexture.Apply();
         copyTexture.name = metallicSmoothTexture.name;
         return(copyTexture);
     }
     else if (occlusionTexture)
     {
         var copyTexture = TextureConverter.CopyTexture(occlusionTexture, glTFTextureTypes.OcclusionMetallicRoughness, null);
         copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ExportPixel(default, smoothness, x)).ToArray());