sketchfab / Unity-glTF-Exporter

Unity editor wizard that exports to glTF Format
MIT License
117 stars 14 forks source link

PSD conversion fails (invalid texture format for texture2d.EncodeToPng()) #5

Closed AurL closed 7 years ago

AurL commented 7 years ago
Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats
UnityEngine.Texture2D:EncodeToPNG()
SceneToGlTFWiz:getBytesFromTexture(Texture2D&, Byte[]&, IMAGETYPE) (at Assets/Unity-glTF-Exporter/SceneToGlTFWiz.cs:1126)
SceneToGlTFWiz:convertTexture(Texture2D, String, String, IMAGETYPE) (at Assets/Unity-glTF-Exporter/SceneToGlTFWiz.cs:1182)
SceneToGlTFWiz:ExportTexture(Texture, String, Boolean, IMAGETYPE) (at Assets/Unity-glTF-Exporter/SceneToGlTFWiz.cs:1217)
SceneToGlTFWiz:unityToPBRMaterial(Material, GlTF_Material&, Boolean) (at Assets/Unity-glTF-Exporter/SceneToGlTFWiz.cs:907)
<Export>c__Iterator0:MoveNext() (at Assets/Unity-glTF-Exporter/SceneToGlTFWiz.cs:461)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
SceneToGlTFWiz:ExportCoroutine(String, Preset, Boolean, Boolean, Boolean) (at Assets/Unity-glTF-Exporter/SceneToGlTFWiz.cs:156)
ExporterSKFB:OnGUI() (at Assets/Unity-glTF-Exporter/ExporterSKFB.cs:445)
UnityEditor.DockArea:OnGUI()

Issue with a PSD texture, it seems that the format is not correctly saves here

AurL commented 7 years ago

It's actually due to an update for Unity 5.5(.0f3) where textureFormat is replaced by textureCompression. To fix:

    private void getBytesFromTexture(ref Texture2D texture, out byte[] pixels, IMAGETYPE imageFormat)
    {
        //Make texture readable
        TextureImporter im = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter;
        bool readable = im.isReadable;
        TextureImporterCompression format = im.textureCompression;
        TextureImporterType type = im.textureType;

        if (!readable)
            im.isReadable = true;
        if (type != TextureImporterType.Default)
            im.textureType = TextureImporterType.Default;

        im.textureCompression = TextureImporterCompression.Uncompressed;
        im.SaveAndReimport();

        if (imageFormat == IMAGETYPE.RGBA)
            pixels = texture.EncodeToPNG();
        else
            pixels = texture.EncodeToJPG(imageFormat == IMAGETYPE.NORMAL_MAP ? jpgQualityNormalMap : jpgQuality);

        if (!readable)
            im.isReadable = false;
        if (type != TextureImporterType.Default)
            im.textureType = type;

        im.textureCompression = format;
        im.SaveAndReimport();
    }
AurL commented 7 years ago

Fixed in 1.0.0. Closing