I have create a billboard system, and I would like to get any improvements, because when I instantiate it it takes 5000-10000ms for the UpdateBillboard function, this script is attached to every gameobject, is there a way to do this in a shader instead, which should give better performance right?
NOTE: You manually assign quad and tree through the inspector and the billboard is generated by another script at runtime (Once for each tree type, so only 1-10 times or so)
Code:
public BillboardClass tree;
public float BillboardDistance = 250.0f;
private Renderer render;
private Renderer QuadRenderer;
private Collider col;
[Serializable]
public class BillboardClass
{
public GameObject tree;
public Texture2D billboard;
public GameObject Quad;
[HideInInspector]
public Mesh mesh;
[HideInInspector]
public Vector3 Scale;
public void UpdateSize()
{
mesh = new Mesh();
mesh = Quad.GetComponent().sharedMesh;
Bounds bb = tree.GetComponent().bounds;
Scale = new Vector3(bb.max.x - bb.min.x, bb.max.y - bb.min.y, 1);
}
}
void CreateQuad()
{
GameObject Quad = GameObject.Instantiate(tree.Quad); //Create quad (Used to display billboard)
Quad.GetComponent().mesh = tree.mesh; //Assign the custom sized mesh to it.
Quad.transform.parent = transform; //Make parent of the tree
Quad.transform.localPosition = Vector3.zero; //Reset position
Quad.transform.localScale = tree.Scale;
QuadRenderer = Quad.GetComponent(); //Cache the renderer for the update function
QuadRenderer.material.mainTexture = tree.billboard;
QuadRenderer.enabled = false;
}
void Start()
{
render = GetComponent();
col = GetComponent();
CreateQuad();
InvokeRepeating("UpdateBillboard", UnityEngine.Random.Range(0.0f,1.0f), 1.0f);
}
Transform target;
bool ShowTree;
void UpdateBillboard()
{
if (target == null)
{
if (Camera.main != null)
{
target = Camera.main.transform;
}
return;
}
ShowTree = false;
if (Vector3.Distance(transform.position, target.position) < BillboardDistance)
{
ShowTree = true;
}
QuadRenderer.enabled = !ShowTree;
render.enabled = ShowTree;
col.enabled = ShowTree; //Should I disable the collider or not
}
↧