- Home /
Random 2D Face Generator
Hello everyone.
I want to have a 2D Face Generator that will generate a face from a couple of different textures.
Something like: select a random face texture, then select a random hair texture, then select a random beard texture etc.
And eventually display it with GUI.DrawTexture.
My problem is not the code.
I'm worried about performance if I use about 5 GUI.DrawTexture just to draw the face.
Is there a way I can select my random texture and then combine them before display? (so I can use only one GUI.DrawTexture)
Thanks!
Answer by robertbu · Apr 12, 2013 at 03:55 PM
You could use GetPixels()/GetPixels32() and SetPixels()/SetPixels32() to create a new texture that combines the textures, but I think you are borrowing trouble where isn't any. When I have questions like this one, I usually write a bit of code that stresses the area I'm concerned about. So write some code that instead of displaying 5 textures, displays 25 textures. Deploy it on the target platform and see about performance.
It's never gonna be 25 (not in the near future anyways), so no need to over stresses it.
It's just that I already have like 700+ draw calls while in game and I want to try to save as many as possible.
I'm actually not sure if 700 is a lot, but I think it is :P
700 is a bunch of draw calls but the proof is always running the app on the target platform. You may want to take a look at a more general solution for your game...a texture atlas comes to $$anonymous$$d. A texture atlas would reduce your face drawing to a single draw call and could be used to help batch other draw calls.
The point is, say you test with 5 textures, and think "hmmm...it sort of seems maybe laggy; if I spin it this way feels sluggish, or am I imagining it... ."
There's a good chance that if you test with 25 noise textures, if will run perfectly fine. Then you'll have zero nagging doubts over whether you should have used only 4, or whether you can afford an extra scar texture.
Likewise, if 25 runs like molasses, you'll know for sure you weren't imagining that slowdown with 5, and every texture does count.
@robertbu "texture atlas" as in one texture to hold the pictures and then somehow only show the one you want?
I'm thinking about doing that regardless, I should help.
But back to the topic, you $$anonymous$$d showing me some example code of how to use the GetPixels()/GetPixels32() and SetPixels()/SetPixels32()?
Just in case i'll decide to use it :)
@Owen Reynolds "if I spin it this way feels sluggish" that's how I feel about it... so I'm just checking for other options.
In Unity, if two object are simple enough (i.e. have fewer than 900 vertex attributes), and if they share exactly the same material, they Unity can (and usually does) batch the call together. It can make a substantial performance improvement. You can read more about batching:
http://docs.unity3d.com/Documentation/$$anonymous$$anual/DrawCallBatching.html
The problem is that if a two materials are not the same if they have different textures. A way around this is to use a texture atlas. That is two object share the material including texture, but each displays a different part of the texture using the UV coordinates in the model.
So in the case of your faces, you could display the five different layers on five different planes, but since you would be using a texture atlas, the five planes would be batched together and only require one draw call.
Third-party tools make building projects using atlases simpler than doing them by hand.