- Home /
Question by
StuwuStudio · Aug 11, 2017 at 07:47 PM ·
gameobjectmeshtree creatorprototypetree-creator
Generating a Mesh instead of a bunch of gameobject
I created a tree generator for my game. It uses unity cubes and pre-made leaves and assembles them to create a mesh. The problem? The tree is only 4.7k tris but it lowers the fps (from 113 fps to 78 fps). If was wondering if I could create the exact same tree but all in one mesh. The problem: I cannot scale/rotation a bunch of triangles to create this exact same tree:
The code:
case TreeCreatorTools.TreeShape.Round:
TreeHeight = Random.Range(LogMinHeight,LogMaxHeight);
TreeSize = Random.Range(LogMinSize,LogMaxSize);
DistanceBettwenBranch = Random.Range(MinDistanceBettwenBranch,MaxDistanceBettwenBranch);
CurrentHeight = 0;
CurrentScale = 1f;
beforeRadius = 0;
float BranchDistance = DistanceBettwenBranch;
float BranchDirection = Random.Range(0f,360f);
//GameObject Obj;
for(int tH = 0; tH < TreeHeight; tH++) { //Tree Height
Obj = (GameObject)Instantiate(ObjectTemplate,transform);
Obj.transform.localPosition = new Vector3(0f,CurrentHeight,0f);
CurrentScale = TreeSize * (logCurve.Evaluate((float)tH/TreeHeight)*logCurveInfluence);
Obj.transform.localScale = new Vector3(CurrentScale,CurrentScale,CurrentScale);
Obj.transform.GetChild(0).GetComponent<MeshRenderer>().material = LogMaterial;
if((float)tH/TreeHeight > HeightBeforeBranch) {
if(BranchDistance >= DistanceBettwenBranch) {
//Setup a branch
Obj = (GameObject)Instantiate(ObjectTemplate,transform);
Obj.transform.localPosition = new Vector3(0f,CurrentHeight,0f);
float THICCness = Random.Range(0.14987f,0.3756f);
Obj.transform.localScale = new Vector3(THICCness,RoundFoliage.Evaluate(((float)tH-beforeRadius)*(1f/(TreeHeight-beforeRadius)))*7.8f+Random.Range(MinBranchLenght*0.075f,MaxBranchLenght*0.075f),THICCness);
Obj.transform.eulerAngles = new Vector3(0f,BranchDirection,BranchRotation-100);
Obj.transform.GetChild(0).GetComponent<MeshRenderer>().material = LogMaterial;
Mesh UVEditor = Obj.GetComponentInChildren<MeshFilter>().mesh;
List<Vector2> UVs = new List<Vector2>();
for(int i = 0; i < UVEditor.uv.Length; i++) {
if(i < 4) {
UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
} else if(i < 6) { //8 -> 12
UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.x,UVEditor.uv[i].y*Obj.transform.localScale.z));
} else if(i < 8) {
UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
} else if(i < 10) {
UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.x,UVEditor.uv[i].y*Obj.transform.localScale.z));
} else if(i < 12) {
UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
} else if(i < 16) {
UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.x,UVEditor.uv[i].y*Obj.transform.localScale.z));
} else if(i < 20) {
UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
} else if(i < 24) {
UVs.Add(new Vector2(UVEditor.uv[i].x*Obj.transform.localScale.z,UVEditor.uv[i].y*Obj.transform.localScale.y));
}
}
Obj.GetComponentInChildren<MeshFilter>().mesh.uv = UVs.ToArray();
//Add the FLUFF at the end of the branch! (Also don't forget to do the prepared branch mode too)
if(Obj.transform.localScale.y < 1.2f) {
continue;
}
int FluffQ = Mathf.RoundToInt(Random.Range(RoundFluffMinSize*0.6f,RoundFluffMaxSize*0.6f));
GameObject Fluff;
for(int i = 0; i < FluffQ; i++) {
Fluff = (GameObject)Instantiate(FluffyTemplate,Obj.transform.GetChild(0));
Fluff.transform.localEulerAngles = Vector3.zero;
Fluff.transform.localPosition = new Vector3(0f,Random.Range(0.1f,0.5f),0f);
Fluff.transform.position += new Vector3(Random.Range(-RoundFluffMinSize,RoundFluffMinSize)*1f, 0f, Random.Range(RoundFluffMinSize,-RoundFluffMinSize)*1f);
float Size = Random.Range(8f,11f);
TreeCreatorTools.SetGlobalScale(Fluff.transform,new Vector3(Size,Size,Size));
Fluff.transform.GetChild(0).eulerAngles += new Vector3(Random.Range(-7f,7f),Random.Range(-7f,7f),Random.Range(-7f,7f));
Fluff.transform.GetChild(1).eulerAngles += new Vector3(Random.Range(-7f,7f),Random.Range(-7f,7f),Random.Range(-7f,7f));
Fluff.transform.GetChild(2).eulerAngles += new Vector3(Random.Range(-7f,7f),Random.Range(-7f,7f),Random.Range(-7f,7f));
Fluff.transform.GetChild(0).GetChild(0).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
Fluff.transform.GetChild(0).GetChild(1).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
Fluff.transform.GetChild(1).GetChild(0).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
Fluff.transform.GetChild(1).GetChild(1).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
Fluff.transform.GetChild(2).GetChild(0).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
Fluff.transform.GetChild(2).GetChild(1).GetComponent<MeshRenderer>().material = FluffLeavesMaterials;
}
BranchDistance = 0f;
BranchDirection += 150+Random.Range(-16.4f,16.4f);
} else {
BranchDistance += CurrentScale;
}
} else {
beforeRadius++;
}
CurrentHeight += CurrentScale;
}
break;
round-tree.png
(83.3 kB)
Comment
Your answer
Follow this Question
Related Questions
How do I make the all the leaves face one way ? 0 Answers
What is the best way to generate a 3D mesh from a number of planes? 0 Answers
Memory hike on combining meshes, how to solve? Crash in Hololens. 0 Answers
Carving the ground similar to the game "where's my water?" 0 Answers
question about invisible walls 2 Answers