GPU texture compression for Unity using the Spark codecs.
Spark for Unity is a Unity package that exposes a subset of the Spark codecs through a simple and lightweight API. It enables the use of procedural textures and standard image formats in Unity applications, encoding them at runtime to native GPU formats like BC7, ASTC, and ETC2, using fast, high-quality GPU compute shaders.
This GitHub repository includes a Unity project with several examples:
Add the package to your project via the Unity Package Manager:
-
Package Manager → Add package from git URL... and enter:
https://github.com/ludicon/spark-unity.git?path=/Packages/com.ludicon.spark -
or add it directly to your project's
Packages/manifest.json:"com.ludicon.spark": "https://github.com/ludicon/spark-unity.git?path=/Packages/com.ludicon.spark"
The Spark for Unity package supports a subset of the Spark codecs at a fixed quality level.
The available formats are:
| Channels | BC Format | Mobile Format | BPP |
|---|---|---|---|
| RGB | BC1_RGB | ETC2_RGB | 4 |
| R | BC4_R | EAC_R | 4 |
| RG | BC5_RG | EAC_RG | 8 |
| RGB | BC7_RGB | ASTC_4x4_RGB | 8 |
| RGBA | BC7_RGBA | ASTC_4x4_RGBA | 8 |
Generic formats (SparkFormat.R, RG, RGB, RGBA) auto-resolve to the best format supported on the current GPU.
Spark for Unity has been tested on Metal (macOS, iOS), Vulkan (Android, Windows), OpenGL ES 3.1 (Android), and Direct3D (Windows). It has been tested on Unity versions 6.3 to 6.6, but it probably works on earlier versions as well.
The API may change, and it has not been tested thoroughly on all platforms and devices. If you encounter any issues, please report them at: https://github.com/Ludicon/spark-unity/issues
// Encode a texture with a specific format
Texture2D compressed = Spark.EncodeTexture(source, SparkFormat.BC7_RGB);
// Auto-select best format for current GPU
Texture2D compressed = Spark.EncodeTexture(source, SparkFormat.RGB);
// With sRGB options
Texture2D compressed = Spark.EncodeTexture(source, SparkFormat.RGB, srgb: true);
// Preload shaders to avoid first-encode hitch
Spark.Preload(SparkFormat.RGB, SparkFormat.RGBA);
// Release cached resources when done
Spark.ReleaseCache();A lower level EncodeTexture function is also available:
public static void EncodeTexture(CommandBuffer cmd, Texture source, Texture destination, SparkFormat format, int sourceMip = 0, int destMip = 0, int destX = 0, int destY = 0);It allows you provide a command buffer explicitly to batch-encode multiple textures using the same command buffer and to use the async compute queue. It also allows you to control the mipmap level and coordinates within the destination texture explicitly, letting you encode straight into a sub-region of an atlas.
The included SparkDemo scene showcases multiple use cases:
- Slideshow: Loads textures from
StreamingAssets/Textures/and compresses them on the fly. - Plasma: Displays a procedural plasma effect and compresses it in real-time.
- Mipmap: Loads a texture, generates mipmaps in the GPU and compresses them.
- glTF: Loads a glTF model using the glTFast importer and encodes its textures.
- Virtual Texture: Procedural virtual texture with compressed tiles to lower the memory footprint of the atlas texture from 64 MB to 16 MB.
- Benchmark: Measures the compression performance of every format.
Why not simply use Unity's Texture2D.Compress instead of Spark?
Texture2D.Compress is a built-in Unity function that compresses textures to DXT/BCn or ETC formats. It's orders of magnitude slower than Spark (from 10 to 1000x slower), produces lower quality results, and does not support BC7 and ASTC formats.
Spark for Unity is free for non-commercial use, or until your product reaches $100,000 in lifetime revenue.
- The C# code and Unity project files are released under the MIT license.
- Use of the Spark shaders is covered under the Spark EULA.
See ludicon.com/spark#licensing for the full licensing details.