24m tri 50m verts from 300 trees
hello.
So i wrote this script to spawn choppable trees from a prefab and finally got it working correctly. Now i am running into problems when trying to optimize the forest of only 300 trees. I checked static in the inspector and it did not help at all I am not saving anything while trying to batch them together. what else can i do to get these numbers down. I am currently getting only 3 to 6 fps when i look at a section of trees.
could it be the tree model that I am using to high of a poly count? 24m triangles and 50m verts are big numbers. ya im getting millions that's not a typo.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RandomizeObj : MonoBehaviour {
public Transform obj;
public Vector3 postiion;
public GameObject objspawned;
public int totalobjs;
public float ObjectsetWidth;
public float ObjectsetLength;
public float respawntime = 3600.0f;
public float respawn = 3600.0f;
//respawn object if destryed after 60minutes - 3600 Seconds - 3600000.0f milliseconds
void Start () {
int spawned = 0;
while (spawned <= totalobjs) {
Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (position, Vector3.down, out hit)) {
Transform newObj = (Transform) Instantiate (obj, hit.point, Quaternion.identity);
newObj.transform.SetParent (transform);
newObj.name = gameObject.name + "_Obj" + spawned;
}
spawned++;
}
}
void Update() {
respawntime -= Time.deltaTime;
if (respawntime < 0) {
CheckForTrees ();
}
}
void CheckForTrees() {
int i = 0;
while (i < totalobjs) {
Vector3 position = new Vector3 (transform.position.x + Random.value * ObjectsetWidth, transform.position.y, transform.position.z + Random.value * ObjectsetLength);
objspawned = GameObject.Find(gameObject.name + "_Obj" + i);
if (objspawned == null) {
RaycastHit hit = new RaycastHit ();
if (Physics.Raycast (position, Vector3.down, out hit)) {
Transform newObj = (Transform)Instantiate (obj, hit.point, Quaternion.identity);
newObj.transform.SetParent (transform);
newObj.name = gameObject.name + "_Obj" + i;
Debug.Log ("Not Found Object at " + i);
}
}
i++;
}
respawntime = respawn;
}
}
Answer by ReggieBeRetro · Jul 04, 2016 at 03:35 AM
Oh it is the forest for sure. when i run the game with no trees that is just the terrain and a single player and no other objects instantiated i get 100+fps then as soon as i add them back in and look towards them fps drops. Ill be on the look out for some low poly trees i guess. :/ Or have a crack at making my own low poly trees in blender. I was trying to get away from modeling and focus on getting the majority of the code done but it looks like ill have to spend some time in blender before i can go any farther. thx for the input.
Apologies, I misunderstood the root of your question to be "how do I get my framerate back?" but it seems you've already got that handled. If you're asking for tips on how to optimize your code for smoother results, I'm afraid I'm still somewhat of a newb at the whole C# thing myself (almost a decade since I did any serious coding, and that was college courses).
Something I can suggest would be to replace your "Tree" mesh with a simple cylinder placeholder. That way you can test better iterations of your code without wondering if it's the tree model impacting your framerate. A gain of 10fps in your code might not mean much with a 100+fps scene full of placeholders, but once you start adding real geometry back in, it might make a world of difference.
As for your trees themselves, I'd advise a cap of about 5-7.5k tri on them, and cut your "forest" count in half (150 trees or so). If that seems thin or sparse, you could intersperse your scene with lower-poly models & scrub / brush plants to help fill things out, without overloading the GPU with raw polygons.
Answer by KieveKRS · Jul 04, 2016 at 03:19 AM
That's 80,000 tri per tree. To put things in perspective, character models are typically the most complex geometry a game will handle and a quick search suggests AAA-models for recent games range between 10-40k. Keeping in mind, we're talking main characters, which the game is only expected to render a couple of at any given time.
So yes, it's extremely likely that a forest of 80k-tri trees is killing your framerate. If they're your model, consider some heavy optimization. If not, I might suggest looking for a substantially lighter mesh.
Your answer
![](https://koobas.hobune.stream/wayback/20220612084200im_/https://answers.unity.com/themes/thub/images/avi.jpg)