x


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: alt text

Grid Texture: alt text

more ▼

asked May 10 '10 at 11:28 AM

EpikOwnage gravatar image

EpikOwnage
55 3 3 7

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.

May 10 '10 at 02:43 PM taoa

I have Indie so I can't. Anyway, speed is not a problem as it do this at loading, with alot of other calculatings :)

May 11 '10 at 02:11 PM EpikOwnage
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

more ▼

answered May 10 '10 at 12:28 PM

Mike 3 gravatar image

Mike 3
30.7k 10 67 256

Thank you, works greatly! :D For those who would like to use this, remember to set textures to ARGB32 and "Is readable"

May 10 '10 at 01:02 PM EpikOwnage
(comments are locked)
10|3000 characters needed characters left
more ▼

answered May 20 '10 at 04:00 PM

golgauth gravatar image

golgauth
12 1

works with Indie?

Jan 10 '11 at 12:15 AM cupsster
(comments are locked)
10|3000 characters needed characters left
more ▼

answered May 20 '10 at 04:02 PM

golgauth gravatar image

golgauth
12 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5276
x2279
x505
x370
x307

asked: May 10 '10 at 11:28 AM

Seen: 4875 times

Last Updated: May 10 '10 at 11:28 AM