Search Unity

Question AssetPostProcessor: how to set Normal map to uncompressed?

Discussion in 'Asset Importing & Exporting' started by adamgravois, Nov 11, 2022.

  1. adamgravois

    adamgravois

    Joined:
    Sep 11, 2017
    Posts:
    3
    Hey y'all,
    I'm building an AssetPostProcessor so I can drag-and-drop folders full of fbx & png files... I've got it working to set parameters like: normalmap, sRGBTexture, maxTextureSize, etc. :)

    I can't get it to set a normal map to "None" compression. :confused:
    Talking bout this setting:
    upload_2022-11-11_14-21-13.png

    I see there is a parameter, `TextureImporterCompression` with a property `Uncompressed`, but how is it used? Cant find an example showing usage. Here's my attempt:

    Code (CSharp):
    1. tImporter.ignorePngGamma = true;  // works
    2. tImporter.maxTextureSize = 4096;  // works
    3. tImporter.textureCompression = TextureImporterCompression.Uncompressed;  // doesnt work
    4.  

    Here's a link to my github for the script... https://github.com/adamgravois/foo/...018a7955b3da3653f1b7ea55/ClubAssetImporter.cs

    thanks!
     
  2. bastien_humeau

    bastien_humeau

    Unity Technologies

    Joined:
    Jun 14, 2017
    Posts:
    191
    Hi there,

    I'd try to use TextureImporter.SetPlatformTextureSettings (with the GetDefaultPlatformTextureSettings to get them first) rather than using the direct setter, I think it should be more reliable.
    Also, make sure that you don't have any overrides for the platform currently targeted on your project that would ignore your default setting.

    Lastly, if you're changing the settings in OnPostprocessAllAssets, you then should call EditorUtility.SetDirty(importer); followed by importer.SaveAndReimport(); to make sure your changes are saved and the asset re-imported with the proper setting.

    However, I think changing settings in OnPostprocessAllAssets is a bad idea. That is because Unity will always import the asset based on the settings in the meta file first, then run the OnPostprocessAllAssets, then see your changes and import a second time, which can take a long time on big projects.
    I'd rather use the specific preprocess methods for each asset you want to alter, like OnPreprocessTexture in that case so that the settings are being enforced at the beginning of the import and you don't have to import twice.
     
  3. adamgravois

    adamgravois

    Joined:
    Sep 11, 2017
    Posts:
    3
    thanks for hte reply! Much to think about... I did not consider
    OnPreProcessTexture
    as an option, but that makes sense. FWIW, I'm exporting these assets to another project (client's project) for use in-game. But I take your point about preprocess being preferable to re-re-importing.

    I still do not see an example for setting texture compression to uncompressed/none. Should mention we're using v2020.3. And I don't "know" C# in any meaningful way, lol.
    For TextureImporterCompression, I see that "Uncompressed" is a property, but how would I assign that in code? Should the example I posted work?
     
  4. bastien_humeau

    bastien_humeau

    Unity Technologies

    Joined:
    Jun 14, 2017
    Posts:
    191
    Hi @adamgravois,

    I think the example you posted should work. At least if it's working for the maxTextureSize, I see no reason why it wouldn't for the textureCompression.
    Maybe what you can try is to add the following code right after the
    tImporter.textureCompression = TextureImporterCompression.Uncompressed
    line:
    Code (CSharp):
    1. EditorUtility.SetDirty(tImporter);
    2. tImporter.SaveAndReimport();
    This should make sure that the changes made during OnPostprocessAllAsset are correctly picked up by the AssetDatabase at the end of the method and will trigger a new import of the texture with the meta file changed on disk.
    On a side note, if you're moving your code to use it from OnPreprocessTexture, then you don't have to call these two methods as the AssetDatabase doesn't need to reprocess the file because in that case the changes are done in place during the first import.
     
    adamgravois likes this.
  5. adamgravois

    adamgravois

    Joined:
    Sep 11, 2017
    Posts:
    3
    Dropping back in to say, this did work better when I moved the code to
    onPreProcessTexture


    Now looks like this:
    Code (CSharp):
    1.     void OnPreprocessTexture()
    2.     {
    3.         if (assetPath.Contains("-normal"))
    4.         {
    5.             Debug.Log($"  doing NORMAL preprocess");
    6.             TextureImporter textureImporter = (TextureImporter)assetImporter;
    7.             textureImporter.convertToNormalmap = true ;
    8.             textureImporter.textureType = TextureImporterType.NormalMap;
    9.             textureImporter.ignorePngGamma = true;
    10.             textureImporter.maxTextureSize = 4096;
    11.             textureImporter.textureCompression = TextureImporterCompression.Uncompressed;
    12.         }
    13.     }
    This produces a correct result with no warnings or error messages. Thanks for your help!
     
    bastien_humeau likes this.