- Home /
Procedurally combine (merge) two Texture2D
Hello Unity users,
I am currently trying to combine two textures into one, by script.
The main texture is a procedurally generated texture (makes random pixels at random position, some kind of colored perlin noise). I've got another texture, which is a grid (represented by white pixels) on a black background. Let's say they're both 256*256.
- I through of getting every white pixels coordinate of the grid texture and reassigning them to the main texture, but It would be alot of work repeating 256 times an action, and I would like to chose the texture size as a variable. (maybe we could use "for"? but I really don't know how to use it so I gave up)
Another way I was thinking about was use of alpha maybe?
Could someone please help me? It would be great.
Regards -Alex
Examples:
Perlin Texture:
Grid Texture:
Are you sure you want to do this in a script? You could obtain the same result much faster using a RenderTexture (if you can, i.e. if you're on Pro) and your Graphics Card.
I have Indie so I can't. Anyway, speed is not a problem as it do this at loading, with alot of other calculatings :)
Answer by Mike 3 · May 10, 2010 at 12:28 PM
in js:
var cols1 = tex1.GetPixels();
var cols2 = tex2.GetPixels();
for(var i = 0; i < cols1.Length; ++i)
{
cols1[i] += cols2[i];
}
tex1.SetPixels(cols1);
tex1.Apply();
that will do an additive sum of both images (so will be white where the grid is white), then put it back into the first texture
it should be pretty easy to adapt it to any other style you like
Thank you, works greatly! :D For those who would like to use this, remember to set textures to ARGB32 and "Is readable"