- Home /
Filling area under linerenderer
I have a linerenderer which acts like a graph(i give it ~10-20 (x,y) positions) and I want to fill the area under it. Any idea how to achieve this ? Thanks a lot !
Answer by Harinezumi · Apr 19, 2018 at 02:19 PM
In my opinion a LineRenderer
is not very good for filling the area below a graph. Instead, you need a polygon whose lower vertices are aligned to the graph axes, and its upper vertices to the graph line.
In 3D you can use a dynamically created mesh, in 2D probably a sprite whose vertices you modify. Rough outline of 3D version:
public void CreateFilledGraphShape (Vector3[] linePoints) {
Vector3[] filledGraphPoints = new Vector3[linePoints.Length * 2]; // one point below each line point
for (int i = 0; i < linePoints.Length; ++i) {
filledGraphPoints[2 * i] = new Vector3(linePoints[i].x, 0, 0);
filledGraphPoints[2 * i + 1] = linePoints[i];
}
int numTriangles = (linePoints.Length -1) * 2;
int[] triangles = new int[numTriangles * 3];
int i = 0;
for (int t = 0; t < numTriangles; t += 2) {
// lower left triangle
triangles[i++] = 2 * t;
triangles[i++] = 2 * t +1
triangles[i++] = 2 * t +2;
// upper right triangle - you might need to experiment what are the correct indices
triangles[i++] = 2 * t + 1;
triangles[i++] = 2 * t + 2;
triangles[i++] = 2 * t + 3;
}
// create mesh
Mesh filledGraphMesh = new Mesh();
filledGraphMesh.vertices = filledGraphPoints;
filledGraphMesh.triangles = triangles;
// you might need to assign texture coordinates as well
// create game object and add renderer and mesh to it
GameObject filledGraph = new GameObject("Filled graph");
MeshRenderer renderer = filledGraph.AddComponent<MeshRenderer>();
renderer.mesh = mesh;
}
Answer by VDraks · Jun 25, 2020 at 10:01 AM
Maybe Sprite Shape can solve this problem
At least it helped me in a similar situation
intro-to-2d-world-building-with-sprite-shape
Your answer
Follow this Question
Related Questions
Generate a mesh around a LineRenderer 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers