x


Combine objects instantiated at runtime

I'd like to combine objects to reduce the drawcall amount. Currently it goes to thousands so it's really bad. I'm creating the terrain with the following basecode:

function Start() {
var width:int = 100;
var height:int = 100;
for(i = 0; i < width; i++) {
for(ii = 0; ii < height; ii++) {
Instantiate(Resources.Load("Ground"), Vector3(i,0,ii), Quaternion.Euler(0,0,0));
}

Should I combine the whole thing as one object? They're plain planes just next to each other. I'm planning on using a 1024 x 1024 texture with all the different terrain types and assign it to all planes. Will this approach be any good? 1024 x 1024 is needed considering the amount of different tiles I'll be using.

more ▼

asked Jan 15 '10 at 12:46 AM

Wizeon gravatar image

Wizeon
52 4 5 11

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

If you're using Unity 2.6+ there's a built-in function on Mesh called CombineMeshes that's probably preferable over the standard assets CombineChildren script. See the documentation here: http://unity3d.com/support/documentation/ScriptReference/Mesh.CombineMeshes.html

Some caveats of doing what you're suggesting though:

  • If your "Ground" object has collision, triggers, or really anything other than a mesh/mesh renderer on it, you'll probably need to keep the original game object you instantiated around with the mesh renderer disabled/destroyed.
  • [I may be wrong on this, but] Unity doesn't know when exactly it's safe to delete a mesh. Make sure you're not leaking meshes after creating them with CombineMeshes.
  • A single mesh in unity cannot have more than 65000 verts. If you're trying to combine an entire level made up of relatively small, detailed tiles, this might be an issue. You'll have to check manually to make sure your vert count is below that, and if it isn't generate multiple meshes.
  • If you can, make as many of your tiles use the same material as possible (i.e. atlas everything you can), so make it so that you have as few meshes as possible. CombineMeshes will make each mesh it's own submesh, but I don't know how efficient that is at combining draws of submeshes with the same material.
more ▼

answered Jan 15 '10 at 11:52 PM

Tetrad gravatar image

Tetrad
7.2k 27 37 89

(comments are locked)
10|3000 characters needed characters left

You better look at how to make a Mesh object as this will give way to many objects. You could try to merge them with the CombineChildren script from Standard Assets, but will probably give quite a delay. Also, instead of Resources.Load use a var Transform as global variable and drag the Ground prefab to it. If you use Resources.Load Unity won't know that you're actually using Ground in your project and if you don't have any other direct references to it, will not include it in the build of the project.

If you have only one Ground type it isn't to hard to generate the Mesh with some little coding, if you need help with that, let us know.

more ▼

answered Jan 15 '10 at 12:59 AM

Jaap Kreijkamp gravatar image

Jaap Kreijkamp
6.4k 20 26 70

Well to my understanding all assets included in the resources folder are included even if not referenced anywhere. And a build with that code works perfectly so that hasn't been an issue. I'll need the plane to have UV map so it can be assigned the same material but have different part of the texture render. Or can UV maps of runtime-created meshed be altered?

Jan 15 '10 at 01:12 AM Wizeon

You're right about the Resources thing, it's just that I did it wrong once with Shader.Find so I'm extra careful to ensure stuff is referred in the project. You can create Meshes runtime including its UV maps. Look at the Mesh class for this.

Jan 15 '10 at 08:39 AM Jaap Kreijkamp
(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:

x5061
x2079
x1669
x491
x110

asked: Jan 15 '10 at 12:46 AM

Seen: 6202 times

Last Updated: Jan 15 '10 at 12:46 AM