• Unity
  • Using Your Own Mipmaps With Spine?

Hi All!

I'm a programmer creating a game with Unity. The Spine animator that I am working with is not familiar with mipmaps or how using different textures works in Unity. So! That is why I'm here! 8)

I can't use Unity's auto-generated Mipmaps unfortunately because the quality is not good enough.
How can I use my own Mipmaps for my Spine animations?
Do I have the ability to use a .DDS texture file or something similar?
Is there a better method that Spine natively offers?
If I want to use my own textures for this, should I receive a json or skel.bytes file from them?

I'm really just looking for any kind of direction as I'm completely oblivious.
Thank you so much to anyone taking the time to read this and assist! 🙂

Related Discussions
...

Thank you for replying to me!

Unfortunately, I didn’t understand that at all. There’s a link to a Japanese site that I cannot read.

Can you please explain to me how I can use my own mipmaps manually?

For example, you can replace your skeleton's mipmaps via the following steps:

1.Enable Advanced - Read/Write and Generate Mip Maps in your skeleton's texture Inspector.
2.Add textures to replace mipmaps to your Unity project, and enable Advanced - Read/Write of them.
3.Create a C# script with the following code:

using UnityEditor;
using UnityEngine;

public class MipMapReplacing : AssetPostprocessor
{
    private void OnPostprocessTexture( Texture2D texture )
    {
        // Loads textures to be set as mipmaps
        var tex1 = AssetDatabase.LoadAssetAtPath<Texture2D>( "Your texture's path1.png" );
        var tex2 = AssetDatabase.LoadAssetAtPath<Texture2D>( "Your texture's path2.png" );
        
// Set mipmap texture. The second argument is the MipMap level texture.SetPixels( tex1.GetPixels( 0 ), 1 ); texture.SetPixels( tex2.GetPixels( 0 ), 2 ); texture.Apply( false ); } }
  1. Right click your skeleton's texture and click Reimport in the menu.

If you try this and find line artifacts appear around the mipmap texture, please check the forum post I mentioned first.

Also as a side note: Note that you can also set the mipmap-bias. The Inspector of the Texture Atlas Asset provides a button to set the bias to -0.5 since it's common that the mipmaps appear too blurry:
spine-unity Runtime Documentation: Texture Atlas Asset

Regarding providing your own pre-authored mipmaps and loading dds files:
Texture access is provided by Unity, not by the spine-unity runtime, so if you should find any thirdparty asset which allows loading dds files to textures including mipmaps, these textures will then likely work with the spine-unity runtime as well.

Misaki wrote

For example, you can replace your skeleton's mipmaps via the following steps:

1.Enable Advanced - Read/Write and Generate Mip Maps in your skeleton's texture Inspector.
2.Add textures to replace mipmaps to your Unity project, and enable Advanced - Read/Write of them.
3.Create a C# script with the following code:

using UnityEditor;
using UnityEngine;

public class MipMapReplacing : AssetPostprocessor
{
    private void OnPostprocessTexture( Texture2D texture )
    {
        // Loads textures to be set as mipmaps
        var tex1 = AssetDatabase.LoadAssetAtPath<Texture2D>( "Your texture's path1.png" );
        var tex2 = AssetDatabase.LoadAssetAtPath<Texture2D>( "Your texture's path2.png" );
        
// Set mipmap texture. The second argument is the MipMap level texture.SetPixels( tex1.GetPixels( 0 ), 1 ); texture.SetPixels( tex2.GetPixels( 0 ), 2 ); texture.Apply( false ); } }
  1. Right click your skeleton's texture and click Reimport in the menu.

If you try this and find line artifacts appear around the mipmap texture, please check the forum post I mentioned first.

Okay, this makes much more sense to me! 8)

Though, I am unfamiliar with the OnPostprocessTexture() method. Once I create this script, where do I call it? Would I call this method in Start() for every single character?

May I please see an example of how you use it once the script is created? Thank you very much, Misaki! 🙂

Harald wrote

Regarding providing your own pre-authored mipmaps and loading dds files:
Texture access is provided by Unity, not by the spine-unity runtime, so if you should find any thirdparty asset which allows loading dds files to textures including mipmaps, these textures will then likely work with the spine-unity runtime as well.

Thanks for the reply, dude! 🙂
Though, I'm a bit confused by your response. What third party asset are you referring to?
Unity natively supports .DDS textures.

For example:
If I have a plane/quad in Unity, and I create a material with the following .DDS texture. Unity is automatically smart enough to know use the mipmaps provided in the .DDS file instead of creating its own.

That being said, I understand how mipmaps work in Unity. What I don't understand is how the Spine atlas uses the material. Does it work the same way any other model + material works? In other words, if I simply drag a .DDS file into the material like the example below, is anything unique happening behind the scenes, or will it allow Unity to use the mipmaps in the .DDS texture file?

I really feel like there should be any kind of documentation on this.
It is not simply a "Unity thing", because Unity supports .DDS files and custom mipmaps easily. 🙁

Though, I am unfamiliar with the OnPostprocessTexture() method. Once I create this script, where do I call it? Would I call this method in Start() for every single character?

May I please see an example of how you use it once the script is created? Thank you very much, Misaki! 🙂

No, you don’t need to call this method in Start(), just put the script in your project. Please see the Unity's documentation for more information:
https://docs.unity3d.com/ScriptReference/AssetPostprocessor.html
https://docs.unity3d.com/ScriptReference/AssetPostprocessor.OnPostprocessTexture.html

PayasoPrince wrote

Though, I'm a bit confused by your response. What third party asset are you referring to?
Unity natively supports .DDS textures.

Oh, thanks for the hint, somehow I missed that. Strangely it seems to have been supported for quite some time already. Then ignore what I said about thirdparty assets for importing if Unity already imports dds files including all mipmaps. If you are fine with the lossy compression of the mipmaps in the dds files, then there is no need to go down the path that Misaki mentioned above (loading mipmap levels from separate textures for each level).

PayasoPrince wrote

That being said, I understand how mipmaps work in Unity. What I don't understand is how the Spine atlas uses the material. Does it work the same way any other model + material works?

Yes. Just be sure that if special additional materials are generated next to each main material for e.g. blend modes (e.g. _Material-Multiply) or for rendering with masks (e.g. _Material_InsideMask), you need to assign your custom texture for each of the Materials.

PayasoPrince wrote

In other words, if I simply drag a .DDS file into the material like the example below, is anything unique happening behind the scenes, or will it allow Unity to use the mipmaps in the .DDS texture file?

The texture assigned at the Material is used, just drag the dds file onto your Material's texture property if you want to use it instead of the exported .png atlas texture.

PayasoPrince wrote

I really feel like there should be any kind of documentation on this.
It is not simply a "Unity thing", because Unity supports .DDS files and custom mipmaps easily. 🙁

Could you please describe what you think needs special documentation? Textures are used normally, so I'm unsure what of the standard behaviour we should document here. The spine-unity runtime uses normal MeshRenderer and MeshFilter components for rendering, just with some logic to bind the Material depending on the atlas page required, if multiple are needed.

Perhaps you are thinking about mipmaps the wrong way: note that to the graphics API behind the scenes, a texture resource is always a combination of all mipmap levels from 0 up to N (the pyramid up to level N), which is an inseparable unit. This texture resource is then bound before rendering, and the respective suitable mipmap levels' texels are accessed by the shader during a texture-sampling call. So anything that can be assigned as a Texture2D slot at your Material will be used normally, as it would when assigned at a cube or quad. Now you can use Misaki's mentioned code to fill the mipmap levels of a texture in an import-postprocessing step, or load it directly by using a dds file with pre-authored mipmaps.

Harald wrote

Could you please describe what you think needs special documentation?

I think this exact bit of information that you provided is perfect!:
"Textures are used normally, so anything that can be assigned as a Texture2D slot at your Material will be used normally, as it would when assigned at a cube or quad."

I read through the documentation, but it was a bit unclear to me what Spine did with the texture. Knowing this, it should literally be as easy as using a .DDS texture for the material. 🙂

With all of that being said, I am satisfied with the answers you both provided.
Though, I'm very interested in the solution that Misaki provided! :think:
I have never heard of doing it this way, and it seems like it would avoid the nasty compression issues that .DDS textures can create. I'm going to try Misaki's method, and I will follow up in this thread with the results. :nerd:

Thank you both! :grinteeth: