Ejemplo n.º 1
0
        static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
        {
#if UNITY_EDITOR
            var path = UnityPath.FromAsset(texture);
            if (path.IsUnderAssetsFolder)
            {
                if (path.Extension == ".png")
                {
                    return(new BytesWithMime
                    {
                        Bytes = System.IO.File.ReadAllBytes(path.FullPath),
                        Mime = "image/png",
                    });
                }
                if (path.Extension == ".jpg")
                {
                    return(new BytesWithMime
                    {
                        Bytes = System.IO.File.ReadAllBytes(path.FullPath),
                        Mime = "image/jpeg",
                    });
                }
            }
#endif

            return(new BytesWithMime
            {
                Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
                Mime = "image/png",
            });
        }
Ejemplo n.º 2
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] = TextureItem.CopyTexture(texture, readWrite, null);

            return(index);
        }
Ejemplo n.º 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
                            (
                            TextureItem.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
                (
                TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
                "image/png"
                );
        }
Ejemplo n.º 4
0
        public static Texture2D Convert(Texture2D texture, glTFTextureTypes textureType, ColorConversion colorConversion, Material convertMaterial)
        {
            var copyTexture = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), convertMaterial);

            if (colorConversion != null)
            {
                copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => colorConversion(x)).ToArray());
                copyTexture.Apply();
            }
            copyTexture.name = texture.name;
            return(copyTexture);
        }
        public int CopyAndGetIndex(Texture texture, RenderTextureReadWrite readWrite)
        {
            if (texture == null)
            {
                return(-1);
            }

            var index = m_textures.IndexOf(texture);

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

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

            return(index);
        }
Ejemplo n.º 6
0
        static void Export_Color(Material m, List <Texture> textures, List <Texture> exportTextures, glTFMaterial material)
        {
            if (m.HasProperty("_Color"))
            {
                material.pbrMetallicRoughness.baseColorFactor = m.color.ToArray();
            }

            if (m.HasProperty("_MainTex"))
            {
                var index = textures.IndexOf(m.GetTexture("_MainTex"));
                if (index != -1 && m.mainTexture != null)
                {
                    exportTextures[index] = TextureItem.CopyTexture(m.mainTexture, RenderTextureReadWrite.sRGB, null);
                    material.pbrMetallicRoughness.baseColorTexture = new glTFMaterialBaseColorTextureInfo()
                    {
                        index = index,
                    };
                }
            }
        }
Ejemplo n.º 7
0
        static void Export_Emission(Material m, List <Texture> textures, List <Texture> exportTextures, glTFMaterial material)
        {
            if (m.HasProperty("_EmissionColor"))
            {
                var color = m.GetColor("_EmissionColor");
                material.emissiveFactor = new float[] { color.r, color.g, color.b };
            }

            if (m.HasProperty("_EmissionMap"))
            {
                var index = textures.IndexOf(m.GetTexture("_EmissionMap"));
                if (index != -1 && m.HasProperty("_EmissionMap"))
                {
                    exportTextures[index]    = TextureItem.CopyTexture(textures[index], RenderTextureReadWrite.sRGB, null);
                    material.emissiveTexture = new glTFMaterialEmissiveTextureInfo()
                    {
                        index = index,
                    };
                }
            }
        }
Ejemplo n.º 8
0
 static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
 {
     /*
      #if UNITY_EDITOR
      * var path = UnityPath.FromAsset(texture);
      * if (path.IsUnderAssetsFolder)
      * {
      *  if (path.Extension == ".png")
      *  {
      *      return new BytesWithMime
      *      {
      *          Bytes = System.IO.File.ReadAllBytes(path.FullPath),
      *          Mime = "image/png",
      *      };
      *  }
      * }
      #endif
      */
     return(new BytesWithMime
     {
         Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
         Mime = "image/png",
     });
 }
Ejemplo n.º 9
0
 public BytesWithPath(Texture texture)
 {
     Path  = "";
     Bytes = TextureItem.CopyTexture(texture).EncodeToPNG();
     Mime  = "image/png";
 }
Ejemplo n.º 10
0
 public BytesWithPath(Texture texture, bool isNormalMap)
 {
     //Path = "";
     Bytes = TextureItem.CopyTexture(texture, isNormalMap).EncodeToPNG();
     Mime  = "image/png";
 }