- Home /
Will this script that does drawmeshinstancedprocedural actually work?
I was trying to make script that, at runtime, finds any children gameobjects with mesh filter and mesh renderers and it then draws them using Graphics.DrawMeshInstancedProcedural. Will this actually work? It seems like such an easy solution to a common problem so I expect it not to work. The reason I haven't tested it is because I don't want to spend many, many hours implementing into my final project (because I will only see if this benefits performance when scaled to the size for my game) for it then not to work. That is why I'm asking here to see if somebody out there knows if this will work so I can know if it would work before committing a lot of time to implementing this into the project. Any insight would be greatly appreciated! Here is the code:
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class InstanceObjects : MonoBehaviour
{
public Transform hexGrid;
public List<ObjectData> Objects = new List<ObjectData>();
//public List<GameObject> Children = new List<GameObject>();
bool canGo = false;
public void Start()
{
canGo = false;
//Children.Clear();
foreach (Transform child in hexGrid)
{
//Children.Add(child.gameObject);
ObjectData n = new ObjectData();
n.pos = child.position;
n.rot = child.rotation;
n.scale = child.localScale;
n.rend = child.GetComponent<MeshRenderer>();
n.filter = child.GetComponent<MeshFilter>();
Objects.Add(n);
}
canGo = true;
}
private void Update()
{
if (canGo)
BatchAndRender();
}
private void BatchAndRender()
{
foreach(ObjectData obj in Objects)
{
for (int i = 0; i < obj.filter.sharedMesh.subMeshCount; i++)
{
Graphics.DrawMeshInstancedProcedural(obj.filter.sharedMesh, i, obj.rend.materials[i],obj.rend.bounds,1);
}
}
}
}
public class ObjectData
{
public Vector3 pos;
public Quaternion rot;
public Vector3 scale;
public MeshRenderer rend;
public MeshFilter filter;
/*
public Matrix4x4 renderData
{
get
{
return Matrix4x4.TRS(pos, rot, scale);
}
}
*/
}
Ok, I ran it and all it did was drop my fps to like 3fps. I'm assu$$anonymous$$g its got to do with iterating through thousands of trees or something...
Ok I fixed it! I just wasnt actually enabling the canGo boolean because it was being forced false from another script
Your answer
Follow this Question
Related Questions
How to move from Graphics.DrawMeshInstanced to Graphics.RenderMeshInstanced 1 Answer
Multiple scenes or one scene? 3 Answers
Not getting shadows for dynamic objects 0 Answers
Strange FPS drop 1 Answer
"Big" 3ds model gives performance issues 3 Answers