- Home /
FPS drops when adding polygons
I'll spare the details of my game - but essentially, I'm trying to make a JezzBall clone. I have a class that generates the "fill area" as a polygon whenever I finish building a wall.
If you are unfamiliar with JezzBall, basically all I'm doing is adding polygons to my game area - these can be any shape really; rectangles, irregular, regular, you name it.
Here's my issue: every time I add a polygon, my FPS drops around 5-10, each time. As you can see, eventually my game will be unplayable. Actually, the drawing of my walls will slow down over time, which is NOT good! It should be the same speed always.
Here is the function I use to add a polygon to my game area:
public void GeneratePolygon(Polygon poly)
{
GameObject goPoly = new GameObject();
goPoly.transform.localPosition = new Vector3(goPoly.transform.localPosition.x,
goPoly.transform.localPosition.y, -1f);
Triangulator tr = new Triangulator(poly.Points);
int[] indices = tr.Triangulate();
// Create the Vector3 vertices
Vector3[] vertices = new Vector3[poly.Points.Length];
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = new Vector3(poly.Points[i].x, poly.Points[i].y, 0);
}
// Create the mesh
Mesh msh = new Mesh();
msh.vertices = vertices;
msh.triangles = indices;
msh.RecalculateNormals();
msh.RecalculateBounds();
// Set up game object with mesh;
goPoly.isStatic = true;
goPoly.AddComponent(typeof(MeshRenderer));
goPoly.renderer.sharedMaterial = Resources.Load("Gray") as Material;
goPoly.renderer.sortingLayerName = "Foreground";
goPoly.renderer.sortingOrder = 2;
MeshFilter filter = goPoly.AddComponent(typeof(MeshFilter)) as MeshFilter;
filter.mesh = msh;
}
And here is my Polygon
class just in case it is needed:
public class Polygon
{
private Vector2[] _points;
public Vector2[] Points
{
get
{
return _points;
}
}
public Vector2 this [int i]
{
get { return _points [i]; }
}
public int Length
{
get { return _points.Length; }
}
public Polygon()
{
}
public Polygon(Vector2[] points)
{
_points = points;
}
public Polygon(List<Vector2> points)
{
_points = points.ToArray();
}
public bool Contains(Vector2 point)
{
bool isInside = false;
for (int i = 0, j = _points.Length - 1; i < _points.Length; j = i++)
{
if (((_points [i].y > point.y) != (_points [j].y > point.y)) &&
(point.x < (_points [j].x - _points [i].x) * (point.y - _points [i].y) / (_points [j].y - _points [i].y) + _points [i].x))
{
isInside = !isInside;
}
}
return isInside;
}
public float Area
{
get
{
float a = _points[0].y * (_points[_points.Length - 1].x - _points[1].x);
for (int i = 1; i < _points.Length; i++)
{
a += _points[i].y * (_points[i - 1].x - _points[(i + 1) % _points.Length].x);
}
return Mathf.Abs(a / 2);
}
}
}
I will not list Triangulator
here, as I just found it online (here specifically)
I noticed if I disable my polygon game objects in the inspector (or delete the material) it will increase the FPS back to normal...
The material is just a Gray color "Sprites/Default" shader.
Any ideas?
Answer by ryrich · Mar 05, 2015 at 11:00 AM
I wouldn't say this is an exact solution, but upgrading to Unity 5 (with no additional code changes) seems to have fixed this issue! Not sure why.
Your answer
Follow this Question
Related Questions
Optimize mesh for runtime performance 0 Answers
Will my huge sprites for a 2d game create performance issues? 2 Answers
Performance Drop on Integrated GPU Computers - GPU (Other) appears to be the source of issue. 0 Answers
Unity5 Procedural meshing slower than in Unity4? 1 Answer
Procedural Mesh Editing Performance 1 Answer