- Home /
optimizing a forest.
So i'm trying to make a forest and iv been looking into combining the various tree meshes into one, but then i heard someone say something like: once they are combined, they will all want to render regardless of being on screen or not. because unity is going to think they are all one mesh now.
so now i'm totally lost, all i can do is fall back on my beginner knowledge and i make this script:
#pragma strict
var dist:float;
var player:Transform;
var trunk:Renderer;
var leaves:Renderer;
var trunkEnabled:boolean;
var leavesEnabled:boolean;
var leavesDist:float;
var trunkDist:float;
function Start ()
{
toggleTrunk();
toggleLeaves();
}
function FixedUpdate()
{
dist = Vector3.Distance(transform.position, player.position);
if(dist>trunkDist)
{
if(trunkEnabled)
{
toggleTrunk();
}
}
else if(dist>leavesDist)
{
if(leavesEnabled)
{
toggleLeaves();
}
if(!trunkEnabled)
{
toggleTrunk();
}
}
else
{
if(!leavesEnabled)
{
toggleLeaves();
}
if(!trunkEnabled)
{
toggleTrunk();
}
}
}
function toggleTrunk()
{
trunk.enabled = !trunk.enabled;
trunkEnabled = !trunkEnabled;
}
function toggleLeaves()
{
leaves.enabled = !leaves.enabled;
leavesEnabled = !leavesEnabled;
}
this script just makes the trunks and the leaves stop rendering after a certain distance (50 for trunks, and 40 for leaves). so far this seems to work alright, iv got around 3k trees packed pretty densely and i'm averaging around 260 draw calls and 150k verts on screen while looking out through the forest. it seems to run fine, i don't lag or anything. but it just feels wrong to me to have 3000 scripts all trying to get the players distance every fixed update. is this normal, or can i do something better? also, if anyone has an general advice on optimizing forest environments that would help me alot, because knowledge is half the battle, and i am ill equipped.
side note: my trees i made have 548 tris / 614 verts each.
Answer by cdrandin · Feb 04, 2015 at 08:09 PM
Why not use Unity's built in system for creating such terrains?
oh, i was under the impression that you couldn't use your own trees with that. ok, thanks i'll study up on terrains. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613182508im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tree Model Optimization 0 Answers
Placing tree objects with Terrain or Polybrush which one is useful ?? 0 Answers
Tree cards lie flat 0 Answers
Disabling colliders in Terrain Trees? 1 Answer
Multiple scenes or one scene? 3 Answers