creating prefabs from existing identical objects

I am interested in taking the translate, rotate, and scale numbers, copying them, and applying them to prefab components once in Unity. I was wondering if there was an easy way to do this for a few thousand objects.

The particular application is a treegen script from Maya, which places leaves quite well. I have around 1500 leaves, all identical, sharing the same texture, and could be made prefabs–if I could copy the data quickly.

I’m thinking of bringing in one prefab, then instantiating based on a spreadsheet table, which, hopefully i can figure out how to export to from maya (any help appreciated).

Ideas? :?

EDIT: I guess it makes more sense to bring them into unity, then write a script to strip the data from there, and instantiate based on the previous objects. I am no scripting genius, If anyone can think of anything that’s great, otherwise Ill give it a shot.

Having each leaf as a separate object is going to be a study in how to achieve horrible performance.

What you want to do is build one mesh containing a triangle at each leaf position.

What do you plan to do with these 1500 objects? Hopefully not render them in game.

Maybe use Meshcombiner on them and then export to an obj using this script?

Probably the easiest method would be to export the objects from maya in a 3D model and either just use the objects exported, or instantiate like this:

var leafPrefab : GameObject;
var newLeavesParent : Transform;
var currentLeavesParent : Transform;

function Start ()
{
	newLeavesParent.parent = currentLeavesParent.parent;
	newLeavesParent.rotation = currentLeavesParent.rotation;
	newLeavesParent.position = currentLeavesParent.position;
	newLeavesParent.localScale = currentLeavesParent.localScale;

	var i = 0;
	while(i < currentLeavesParent.childCount)
	{
		child = currentLeavesParent.GetChild(i);
		newLeaf = Instantiate(leafPrefab, child.position, child.rotation);
		newLeaf.transform.localScale = child.localScale;
		newLeaf.transform.parent = newLeavesParent;
		i++;
	}

	// Do something like add meshcombiner to newLeavesParent and then export to obj
}

nich beat me to it…

I was intending to apply a mesh combiner script (combine at runtime).

Does that nullify the effects of the prefabs (i.e. memory savings)?

Or would even a mesh combiner script deteriorate performance to an unwanted degree?

Obviously, otherwise I would just merge them in Maya, which is what I had done previously–but I’m looking to reduce project file size as much as possible–while keeping reasonable performance.

I might be misunderstanding the meshcombiner function, it kinda feels like I’m trying to get something for nothing.

The problem with performance here is 1500 draw calls. If you combine a bunch of meshes as the scene loads that means a lot longer load time. Seems like one tree mesh is not going to hurt the file size that much.

Got it.

General rule then, at what point does it make sense to start using prefabs? Only above the 1500-4000 poly ‘sweet spot?’

We are working on web downloaded material–and need to seriously optimize downloads with a secondary focus on speed.

I guess if the average (broadband) download speed is around 100kb/s–that has to balance with load time

so if a mesh combine script takes 10 seconds to run, but only saves 50kb, it’s not worth it, but one can envision a scenario where the script runs in 5 seconds, but saves a few Mb
The leaves were an extreme case
–this is where I am coming from.

There two things to optimize for. File size and Performance.

When optimizing for Performance what matters is that when Unity renders the object you have as few objects as possible with a good amount of polys eg. 1500-4000 polygons.

For file size, having lots of objects which are instantiated is best since it will share the meshes, thus there is one mesh and only the additional game objects transforms need to be stored but not the actual vertex data.

Combining objects at load time gives you the best of both at the cost of increased load time. So in a web player this is the best solution.

Now what would be most efficient would be to generate the positions/rotations/scale for the 1500 objects from a script procedurally. This way you don’t need to store any data per instance.

There are some script examples in the documentation that show how to do exactly this. At the bottom of the page there are two examples. One placing a bunch of objects in a ring and one placing them in a grid.
Start with those and once you got it to generate the right pattern of instantiated objects, start plugging in the mesh combiner in order to optimize for speed.

http://unity3d.com/Documentation/Manual/Instantiating%20Prefabs.html

Thanks, we talked about this in SF, but after thinking about it, I wasn’t totally clear on where the performance/speed trade off hit in terms of running a mesh combine script.

Once load time came into play, it made a lot more sense. Thanks for clearing that up. I’m looking into dynamically generating leaf position based on branching–but I’ve put it off, mostly because the branches I generate in Maya are far too complex for games, and i usually delete them after the leaves are completed.

So my script will be something like
-figure out where branches would go-if they existed
-place leaves according to invisible branches

Might be a little heavy.

Thanks for the help everyone

Hi,

Just FYI, try taking a look at the (currently) latest post in the “Procedural Trees” thread under “ShowCase”. I just updated my trees with leaves and support for texturing.
It still has a way to go, before it looks realistic, but maybe you can use the script for inspiration.

And when I crank up the recursion on the trees, it demonstrates the increased load time quite nicely :slight_smile:

Regarding instantion of prefabs, is there a way to

  1. Instantiate in a random pattern instead of circle or grid?

  2. Time offset each instance so animations would not be in sync?

Of course. Just use one of the random functions to create random points where you instantiate the prefab.

Use yield to delay instancing of each prefab.
If you only want to delay the animation, you could also just change the time of the animation state.

Whee! Here’s a procedural forest, made w/ CBlarsen’s cool procedural tree script:

http://greenpriest.com/procedural_forest.html

20 x 20 instantiated grid, w/ 2x recursion on the trees.

Just reading the forums and connecting the dots…:slight_smile:

cool. runs decent on my 733 g4. are you combining meshes? is that already in carsten’s script? haven’t looked.

nice! 50 fps on an intel imac. i’m curious if you’re combining meshes as well.

Not combining meshes; I’m not sure how to use that script with instantiated prefabs.

As Higgy said, I’d love to see all the L-System variables in this script, not that I have a clue about the math…

It’s an interesting exercise to setup and tweak something like this, to get a sense of the tradeoff between polygon count for individual objects and overall object count.

I was going to explain to you how to use prefabs, but then I just went ahead, and did it.

The new script can now take a prefab as argument, and will generate that instead of leaves.

In the example below, I simply took the default box from the standard assets, and made a prefab where it was scaled down a bit. It looks like that because the leaves on each branch are actually placed at the same point, just rotated differently.

No, the code is not very efficient right now. But maybe an enterprising soul can figure out how to run the meshcombiner on this.

Have fun.

32087–1171–$dynamictreeprefabs_604.js (10.2 KB)
32087--1172--$generateprefabs_192.png

And by the way, I forgot to say cool little forest, Polytropoi. It runs faster than I expected.

Awesome forest! Runs at a great 7-10 fps on my 1 Ghz eMac. Maybe I need an upgrade…

AGhost

(sniff) I love you, man.