- Home /
Instantiate 697283 trees by script? Crash!
I'm trying to instantiate 1 tree (prefab) per red pixel of the second splat map (forest ground texture). The code works, as I tried some small lines to count the pixels that would allow the instance of the tree. The total count is 697283 pixels. That would mean at least that number of trees and more as for each square unit there should be like 10 trees for the forests to look nice(but that isn't coded yet). The splats are 2048x and the terrain size is 8192 units (squared).
The problem is unity crashes when I hit start (not so much crash as just stay there doing nothing, I just close it with the task manager). Is there a limit of allowed prefabs in the scene? Or is it just the computer dying because there's too much stuff?
Does anyone knows a nice solution?, other than painting them by hand (which is my last resort[the map is huge])
NOTE: the trees are a couple of planes put in cross with a texture, so they are really low poly. I even tried with untextured cubes to see if the crash was because of the trees but it still did. And btw, the mass placement of trees from the terrain tab doesn't help me as you can't choose the terrain type.
What happens if you complile your scene and open exe? Does it crash?
nice idea! I tried, and it works! But it lags horribly. So I think it's an optimization problem. $$anonymous$$aybe I should script a drawing distance, applied directly to the tree prefab and based on main camera distance. Also the game crashes when I go back with the cam to see the whole terrain at once.
But the views are amazing :))
Thanks for the tip! I just hope the optimization script will allow me to enter the test play in unity.
I tried to limit the draw distance to a 100 units from the camera but it crashed. And this time it crashed good, gives me a memory allocation error. With the build it doesn't even show any tree, yet it still lags.
What are you actually trying to accomplish, and why wouldn't the usual method of terrain-based trees work for you?
Usual method of terrain-based trees? Do you mean the "$$anonymous$$ass Place trees" in the "Terrain" tab? If so it is no use to me because you can't choose to place them only on certain terrains. In my case, the trees should be placed only where there is forest ground, which is in the second splat map, as red. So ins$$anonymous$$d of painting by hand, because the terrain is complex, (there are many small patches of forest ground ), I want to do it automatically. The trees have no real gameplay use, it's just decorative. But they must be there or it would look weird.
Answer by Eric5h5 · Dec 11, 2012 at 09:05 PM
You can't have anywhere near that many separate objects. Generally you should not have more than a few thousand, and fewer is preferable if possible. What you should do is use the terrain system, which is already optimized to handle large amounts of trees properly (it doesn't use separate objects for them). You have complete control over how you place trees by script; look up the TerrainData class in the docs. Otherwise you'd have to make your own tree system using the Mesh class.
Yes but you can't place trees with the terraindata. I'd have to use the brush, but it's a tedious process. And not efficient if I want more maps.
@SuIXo3:
Brush? I thought you talk about ingame creation?
http://docs.unity3d.com/Documentation/ScriptReference/TerrainData-treeInstances.html
Not efficient? I agree that the terrain engine is far from being perfect, but i guess it's like 100 times more efficient than what you've tried above ;)
I know it is efficient, the first thing I did with my tree model was to test it painting it with the brush, and it looks nice. But the .treeinstances...isn't it only to get the number of trees? How is it usable to place them at certain coordinates?
No, that wouldn't be very useful. Please read the docs for TerrainData.treeInstances and the TreeInstance struct.
I just found Terrain.AddTreeInstance. I guess I could use that. I'll try tomorrow. Thanks!
Answer by jRocket · Dec 11, 2012 at 09:08 PM
Make sure that they are set to static so Unity is batching them into a one draw call(Pro only). If that doesn't help or work, try making clusters of trees as one object and reduce the amount of tree objects actually being generated. If that doesn't work, you may have to make some kind of cell/grid based loading system, where only the trees in the nearest cell are loaded into memory at any one time.
I was thinking to do something like that. I guess I would have to code the generation of trees in the Update function, then within the pixels loop, set a conditional based on number of tree and some more stuff. I'll see if I think of something.
Have you read the DrawCallBatching page? The last paragraph at the very bottom describe an example with trees...
Like Eric said, seperate objects have quite an overhead (Gameobject, Transform, Renderer, $$anonymous$$esh). When Unity batches object it creates an additional combined version. Such a setup can't be handled by any game engine out there 500k objects and i guess each one has at least 100 tris? Event the triangle count would be crazy without some sort of LOD system.
looks nice but I don't have Pro. I wish I had, but 1500$ is too much for the moment :(, maybe one day :).
And no, the trees have 4 tris, 2 quads. They are just a couple of planes with texture. They will look small, as it's just decorative and has no impact on gameplay(it's a strategy game like...total war in the campaign map). I thought of a LOD system, but it's a pain to do. It there one in unity or do I have to do it myself? By changing the meshes based on distance and all I guess. Or maybe there is some script out there from which I can get ideas?
That still won't work with so many trees anyway. They really really should not use separate instances for each one.
Your answer
Follow this Question
Related Questions
Convert Terrain trees in to real trees 1 Answer
Terrain addTreeInstance crash 1 Answer
How do I instantiate the TerrainToolKit script? 1 Answer
Plant grass or tree with an image 0 Answers