• RuntimesUnity
  • How to Correctly Set Parameters for Repacking Normal Map and Light Mask?

Hi everyone!

I'm trying to follow the Mix and Match and this guide [https://esotericsoftware.com/spine-unity-main-components#Runtime-Repacking] to generate new textures at runtime. Everything works perfectly when I'm not including additional textures, but I encounter issues when adding a Normal Map and Light Mask.

The source data for my normal map is set to Texture Type: Normal Map, and the light mask's source data is a standard Sprite. I tried writing some parameters based on my understanding, but the newly generated normal map and light mask don't display correctly, and I got errors: "Graphics.CopyTexture called with incompatible formats."

Here’s my code—any guidance or corrections would be greatly appreciated! Thank you!

        public void OptimizeSkin()
        {
            // Create a repacked skin.
            Skin previousSkin = skeleton.Skin;
            // Note: materials and textures returned by GetRepackedSkin() behave like 'new Texture2D()' and need to be destroyed
            if (runtimeMaterial)
                Destroy(runtimeMaterial);
            if (runtimeAtlas)
                Destroy(runtimeAtlas);
            Texture2D[] additionalOutputTextures = null;
            int[] additionalTexturePropertyIDsToCopy = new int[] { Shader.PropertyToID("_BumpMap"), Shader.PropertyToID("_MaskTex") };
            TextureFormat[] additionalTextureFormats = new TextureFormat[] { TextureFormat.DXT5, TextureFormat.DXT5 };
            Skin repackedSkin = previousSkin.GetRepackedSkin
                (
                "Repacked skin", dataMatrix.fullStyleData.skeletonDataAsset.atlasAssets[0].PrimaryMaterial, 
                out runtimeMaterial, out runtimeAtlas, 2048, mipmaps:true, 
                additionalTexturePropertyIDsToCopy: additionalTexturePropertyIDsToCopy, 
                additionalOutputTextures: additionalOutputTextures
                , additionalTextureFormats: additionalTextureFormats
                );
            previousSkin.Clear();
            // Use the repacked skin.
            skeleton.Skin = repackedSkin;
            skeleton.SetSlotsToSetupPose();
        }
Related Discussions
...

To correctly set parameters for repacking a normal map and light mask, you need to ensure that the texture formats and properties are correctly specified. The error "Graphics.CopyTexture called with incompatible formats" suggests that there might be a mismatch in the texture formats or properties.

Here is a revised version of your code with some adjustments:

public void OptimizeSkin()
{
    // Create a repacked skin.
    Skin previousSkin = skeleton.Skin;
    // Note: materials and textures returned by GetRepackedSkin() behave like 'new Texture2D()' and need to be destroyed
    if (runtimeMaterial)
        Destroy(runtimeMaterial);
    if (runtimeAtlas)
        Destroy(runtimeAtlas);

    // Define additional texture properties and formats
    int[] additionalTexturePropertyIDsToCopy = new int[] { Shader.PropertyToID("_BumpMap"), Shader.PropertyToID("_MaskTex") };
    TextureFormat[] additionalTextureFormats = new TextureFormat[] { TextureFormat.RGBA32, TextureFormat.RGBA32 }; // Ensure compatible formats

    // Initialize additional output textures array
    Texture2D[] additionalOutputTextures = new Texture2D[additionalTexturePropertyIDsToCopy.Length];

    // Get the repacked skin
    Skin repackedSkin = previousSkin.GetRepackedSkin(
        "Repacked skin", 
        dataMatrix.fullStyleData.skeletonDataAsset.atlasAssets[0].PrimaryMaterial, 
        out runtimeMaterial, 
        out runtimeAtlas, 
        2048, 
        mipmaps: true, 
        additionalTexturePropertyIDsToCopy: additionalTexturePropertyIDsToCopy, 
        additionalOutputTextures: additionalOutputTextures, 
        additionalTextureFormats: additionalTextureFormats
    );

    // Clear the previous skin
    previousSkin.Clear();

    // Use the repacked skin
    skeleton.Skin = repackedSkin;
    skeleton.SetSlotsToSetupPose();
}

Key Adjustments:

  1. Texture Formats: Ensure that the texture formats are compatible. In this case, TextureFormat.RGBA32 is used for both the normal map and the light mask to avoid format incompatibility.
  2. Initialization of additionalOutputTextures: Ensure that the additionalOutputTextures array is initialized with the same length as additionalTexturePropertyIDsToCopy.

Important Notes:

  • Texture Import Settings: Ensure that the import settings for your normal map and light mask textures in Unity are correctly set. For the normal map, set the Texture Type to Normal Map. For the light mask, ensure it is set to a compatible format.
  • Shader Property Names: Ensure that the property names (_BumpMap and _MaskTex) match the property names used in your shader.

By following these adjustments, you should be able to correctly repack the normal map and light mask without encountering the "Graphics.CopyTexture called with incompatible formats" error.

Thank you, bot. I tried the code you suggested, but the issue still persists, and I’m getting 116 errors.

56 of the errors are as follows:
Graphics.CopyTexture called with incompatible formats (src=RGB Compressed DXT1|BC1 UNorm, dst=RGBA8 UNorm).
UnityEngine.Graphics:CopyTexture (UnityEngine.Texture,int,int,int,int,int,int,UnityEngine.Texture,int,int,int,int)
Spine.Unity.AttachmentTools.AtlasUtilities:CopyTexture (UnityEngine.Texture2D,UnityEngine.Rect,UnityEngine.Texture2D)
....

And another 56 errors are:
Graphics.CopyTexture called with incompatible formats (src=RGBA Compressed DXT5|BC3 UNorm, dst=RGBA8 UNorm).
UnityEngine.Graphics:CopyTexture (UnityEngine.Texture,int,int,int,int,int,int,UnityEngine.Texture,int,int,int,int)
Spine.Unity.AttachmentTools.AtlasUtilities:CopyTexture (UnityEngine.Texture2D,UnityEngine.Rect,UnityEngine.Texture2D)
....

Here are screenshots of my Normal Map and Light Mask settings. I appreciate any guidance you can provide! Thank you!

Harald Thank you, Harald! I only modified the albedo and forgot to check the options for the normal map and light mask. It’s now working perfectly 😃

  • Harald が「いいね」しました。

@wpw1221 Glad to hear it helped. 🙂