- Home /
Combining textures - Flatten effect.
I'm trying to create a single, "flattened" texture from a set of others. Basically, a new texture is created, and each from the set is drawn onto it, so any transparent parts will show through to the texture behind it.
I keep getting "Can't Resize to a compressed texture format.", but I haven't been able to get rid of that error no matter what I do.
Might anyone have some suggestions?
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class TextureCombineUtility : MonoBehaviour {
//Call TextureCombineUtility.Flatten([textures]) in order to create a new, single texture from multiple layers.
public static Texture2D Flatten (Texture2D[] combines) {
//The new texture.
Texture2D newTexture = new Texture2D(0,0);
//Resize the new texture to match the largest texture.
foreach(Texture2D resizeTex in combines) {
if(resizeTex.width > newTexture.width) {
newTexture.Resize(resizeTex.width,newTexture.height);
}
if(resizeTex.height > newTexture.height) {
newTexture.Resize(newTexture.width,resizeTex.height);
}
}
//Resize each layer to match the new texture, and draw the layer onto the new texture.
int i = 0;
int l = combines.Length;
while(i < l) {
int xx = newTexture.width;
int yy = newTexture.height;
Texture2D addTex = new Texture2D(xx,yy,TextureFormat.DXT5,true);
addTex = combines[i];
addTex.Resize(xx,yy,TextureFormat.DXT5,true);
int x = 0;
int y = 0;
while(y < yy) {
while(x < xx) {
Color mainPixel = newTexture.GetPixel(x,y);
Color newPixel = addTex.GetPixel(x,y);
mainPixel = Color.Lerp(mainPixel,newPixel,newPixel.a);
newTexture.SetPixel(x,y,mainPixel);
++x;
}
++y;
}
++i;
}
return newTexture;
}
}
Answer by DaveA · May 08, 2011 at 05:56 PM
Try using TextureFormat.ARGB32 instead of DXT5. Then use Compress at the end
This led to the answer! What was also required was some repairs to my while statements; before ++y, x=0;, and before ++i,y=0;.
Thanks for your help!
Answer by Sa0Lin · Sep 24, 2012 at 07:42 PM
Hello. I used your script, and made an object with a MeshRenderer and changed it's material's texture to the return value of your Flatten function. I added 5 textures as parameters but the result is a texture created from other 5 textures that i have in the project.
public Texture2D[] tex;
//added 5 textures that are Imported using Advance setting and compressed using ARBG32, all the same size.
void Start(){
renderer.material.mainTexture = TextureCombineUtility.Flatten(tex);
}
//the result is a texture formed by other 5 textures from my project, and those are not passed as the params.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132625im_/https://answers.unity.com/themes/thub/images/avi.jpg)