/// <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, ConvertTypes.None), out var index))
            {
                return(index);
            }

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

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

            return(index);
        }
示例#2
0
        /// <summary>
        /// Import glTF Metallic-Roughness texture to Unity Metallic-Smoothness-Occlusion texture.
        ///
        /// isLegacySquaredRoughness:
        ///     Before UniGLTF v0.69, roughness value in the texture was invalid squared value.
        /// </summary>
        public static Texture2D Import(Texture2D metallicRoughnessTexture,
                                       float metallicFactor, float roughnessFactor, Texture2D occlusionTexture, bool isLegacySquaredRoughness)
        {
            if (metallicRoughnessTexture == null && occlusionTexture == null)
            {
                throw new ArgumentNullException("no texture");
            }

            var src = metallicRoughnessTexture != null ? metallicRoughnessTexture : occlusionTexture;

            Importer.mainTexture = src;
            Importer.SetTexture("_GltfMetallicRoughnessTexture", metallicRoughnessTexture);
            Importer.SetTexture("_GltfOcclusionTexture", occlusionTexture);
            Importer.SetFloat("_GltfMetallicFactor", metallicFactor);
            Importer.SetFloat("_GltfRoughnessFactor", roughnessFactor);
            Importer.SetFloat("_IsLegacySquaredRoughness", isLegacySquaredRoughness ? 1 : 0);

            var dst = TextureConverter.CopyTexture(src, ColorSpace.Linear, true, Importer);

            Importer.mainTexture = null;
            Importer.SetTexture("_GltfMetallicRoughnessTexture", null);
            Importer.SetTexture("_GltfOcclusionTexture", null);
            Importer.SetFloat("_GltfMetallicFactor", 0);
            Importer.SetFloat("_GltfRoughnessFactor", 0);
            Importer.SetFloat("_IsLegacySquaredRoughness", 0);

            return(dst);
        }
        /// <summary>
        /// 画像のバイト列を得る
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="texture"></param>
        /// <returns></returns>
        public static (byte[] bytes, string mime) GetTextureBytesWithMime(Texture2D texture)
        {
            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");
            }
        }
示例#4
0
 public static Texture2D Export(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture)
 {
     // TODO: Replace with Shader implementation
     if (metallicSmoothTexture != null && occlusionTexture != null)
     {
         if (metallicSmoothTexture == occlusionTexture)
         {
             var dst           = TextureConverter.CreateEmptyTextureWithSettings(metallicSmoothTexture, ColorSpace.Linear, false);
             var linearTexture = TextureConverter.CopyTexture(metallicSmoothTexture, ColorSpace.Linear, true, null);
             dst.SetPixels32(linearTexture.GetPixels32().Select(x => ExportPixel(x, smoothness, x)).ToArray());
             dst.Apply();
             dst.name = metallicSmoothTexture.name;
             DestroyTexture(linearTexture);
             return(dst);
         }
         else
         {
             var dst = TextureConverter.CreateEmptyTextureWithSettings(metallicSmoothTexture, ColorSpace.Linear, false);
             var linearMetallicSmooth = TextureConverter.CopyTexture(metallicSmoothTexture, ColorSpace.Linear, true, null);
             var metallicSmoothPixels = linearMetallicSmooth.GetPixels32();
             var linearOcclusion      = TextureConverter.CopyTexture(occlusionTexture, ColorSpace.Linear, false, null);
             var occlusionPixels      = linearOcclusion.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]);
             }
             dst.SetPixels32(metallicSmoothPixels);
             dst.Apply();
             dst.name = metallicSmoothTexture.name;
             DestroyTexture(linearMetallicSmooth);
             DestroyTexture(linearOcclusion);
             return(dst);
         }
     }
     else if (metallicSmoothTexture)
     {
         var dst = TextureConverter.CreateEmptyTextureWithSettings(metallicSmoothTexture, ColorSpace.Linear, false);
         var linearMetallicSmooth = TextureConverter.CopyTexture(metallicSmoothTexture, ColorSpace.Linear, true, null);
         dst.SetPixels32(linearMetallicSmooth.GetPixels32().Select(x => ExportPixel(x, smoothness, default)).ToArray());
         dst.Apply();
         dst.name = metallicSmoothTexture.name;
         DestroyTexture(linearMetallicSmooth);
         return(dst);
     }
     else if (occlusionTexture)
     {
         var dst             = TextureConverter.CreateEmptyTextureWithSettings(occlusionTexture, ColorSpace.Linear, false);
         var linearOcclusion = TextureConverter.CopyTexture(occlusionTexture, ColorSpace.Linear, false, null);
         dst.SetPixels32(linearOcclusion.GetPixels32().Select(x => ExportPixel(default, smoothness, x)).ToArray());
示例#5
0
        public void CopyFromNonReadableSRgbDds()
        {
            var compressedTex = AssetDatabase.LoadAssetAtPath <Texture2D>($"{AssetPath}/4x4_non_readable_compressed.dds");

            Assert.False(compressedTex.isReadable);
            var copiedTex = TextureConverter.CopyTexture(compressedTex, ColorSpace.sRGB, true, null);
            var pixels    = copiedTex.GetPixels32(miplevel: 0);

            Assert.AreEqual(pixels.Length, DdsTextureValues.Length);
            for (var idx = 0; idx < pixels.Length; ++idx)
            {
                Assert.AreEqual(DdsTextureValues[idx], pixels[idx]);
            }
        }
示例#6
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, ColorSpace.Linear, 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, ColorSpace.Linear, null);
             var metallicRoughnessPixels = copyMetallicRoughness.GetPixels32();
             var copyOcclusion           = TextureConverter.CopyTexture(occlusionTexture, ColorSpace.Linear, 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, ColorSpace.Linear, 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)
     {
         var copyTexture = TextureConverter.CopyTexture(occlusionTexture, ColorSpace.Linear, null);
         copyTexture.SetPixels32(copyTexture.GetPixels32().Select(x => ImportPixel(default, metallicFactor, roughnessFactor, x)).ToArray());
示例#7
0
        public void CopyFromNonReadableSRgbPng()
        {
            var nonReadableTex = AssetDatabase.LoadAssetAtPath <Texture2D>($"{AssetPath}/4x4.png");

            Assert.False(nonReadableTex.isReadable);
            var copiedTex = TextureConverter.CopyTexture(nonReadableTex, ColorSpace.sRGB, null);
            var pixels    = copiedTex.GetPixels32(miplevel: 0);

            Assert.AreEqual(pixels.Length, PngTextureValues.Length);
            for (var idx = 0; idx < pixels.Length; ++idx)
            {
                Assert.AreEqual(PngTextureValues[idx], pixels[idx]);
            }
        }
示例#8
0
        private static (byte[] bytes, string mime) CopyTextureAndGetBytesWithMime(Texture2D texture, ColorSpace colorSpace)
        {
            var needsAlpha = texture.format != TextureFormat.RGB24;
            var copiedTex  = TextureConverter.CopyTexture(texture, colorSpace, needsAlpha, null);
            var bytes      = copiedTex.EncodeToPNG();

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

            return(bytes, "image/png");
        }
示例#9
0
        public void CopyAttributes()
        {
            var src = AssetDatabase.LoadAssetAtPath <Texture2D>($"{AssetPath}/4x4_non_readable.png");
            var dst = TextureConverter.CopyTexture(src, ColorSpace.sRGB, false, null);

            Assert.AreEqual(src.name, dst.name);
            Assert.AreEqual(src.anisoLevel, dst.anisoLevel);
            Assert.AreEqual(src.filterMode, dst.filterMode);
            Assert.AreEqual(src.mipMapBias, dst.mipMapBias);
            Assert.AreEqual(src.wrapMode, dst.wrapMode);
            Assert.AreEqual(src.wrapModeU, dst.wrapModeU);
            Assert.AreEqual(src.wrapModeV, dst.wrapModeV);
            Assert.AreEqual(src.wrapModeW, dst.wrapModeW);
            Assert.AreEqual(src.mipmapCount, dst.mipmapCount);
            Assert.AreEqual(src.width, dst.width);
            Assert.AreEqual(src.height, dst.height);
            Assert.AreEqual(src.format, dst.format);
            Assert.AreEqual(src.imageContentsHash, dst.imageContentsHash);
        }
示例#10
0
        public static Texture2D Export(Texture metallicSmoothTexture, float smoothness, Texture occlusionTexture)
        {
            if (metallicSmoothTexture == null && occlusionTexture == null)
            {
                throw new ArgumentNullException("no texture");
            }

            var src = metallicSmoothTexture != null ? metallicSmoothTexture : occlusionTexture;

            Exporter.mainTexture = src;
            Exporter.SetTexture("_UnityMetallicSmoothTexture", metallicSmoothTexture);
            Exporter.SetTexture("_UnityOcclusionTexture", occlusionTexture);
            Exporter.SetFloat("_UnitySmoothness", smoothness);

            var dst = TextureConverter.CopyTexture(src, ColorSpace.Linear, true, Exporter);

            Exporter.mainTexture = null;
            Exporter.SetTexture("_UnityMetallicSmoothTexture", null);
            Exporter.SetTexture("_UnityOcclusionTexture", null);
            Exporter.SetFloat("_UnitySmoothness", 0);

            return(dst);
        }
示例#11
0
 // Unity texture to GLTF data
 // ConvertToRawColorWhenNormalValueIsCompressed
 public static Texture2D Export(Texture texture)
 {
     return(TextureConverter.Convert(texture, ColorSpace.Linear, null, Decoder));
 }
示例#12
0
 // Unity texture to GLTF data
 public static Texture2D Export(Texture texture)
 {
     return(TextureConverter.CopyTexture(texture, ColorSpace.Linear, false, Exporter));
 }
 // Unity texture to GLTF data
 // ConvertToRawColorWhenNormalValueIsCompressed
 public static Texture2D Export(Texture texture)
 {
     return(TextureConverter.Convert(texture, TextureImportTypes.NormalMap, null, Decoder));
 }