Best way to efficiently draw many lines?

Hey all,



I need to draw many 3D lines in my scene; thousands of them. I noticed in programs like AutoCad and Sketchup, they are able to do this quite efficiently. However, my current method in JME runs extremely slowly in comparison.



Currently I am doing it like this:

[java]

public static Geometry CreateLine(AssetManager assetManager, ArrayList<Vector3f> points, ColorRGBA color, boolean isClosedLoop, float lineWidth)

{

// Vertex positions in space

Vector3f[] vertices = new Vector3f[points.size()];

vertices = points.toArray(vertices);



// Indexes. We define the order in which mesh should be constructed

int numIndexes = 2 * vertices.length;

int numLines = numIndexes / 2;

int padding = 0;

if (!isClosedLoop)

{

padding = 1;

}

int[] indexes = new int[numIndexes];

for (int i = 0; i < numLines - padding; i++)

{

indexes[2 * i] = i;

indexes[2 * i + 1] = (i + 1) % numLines;

}



// Setting buffers

Mesh lineMesh = new Mesh();

lineMesh.setMode(Mesh.Mode.Lines);

lineMesh.setLineWidth(lineWidth);

lineMesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));

lineMesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));

lineMesh.updateBound();



Geometry lineGeom = new Geometry(“lineMesh”, lineMesh);

Material matWireframe = new Material(assetManager, “Common/MatDefs/Misc/Unshaded.j3md”);

matWireframe.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);

matWireframe.setColor(“Color”, color.clone());

matWireframe.getAdditionalRenderState().setWireframe(true);

lineGeom.setMaterial(matWireframe);

return lineGeom;

}

[/java]



I am currently making a line from 2 points every time I call this function. I am thinking about changing my way of calling this to include all of the points that go together in a single line segment, but before I do so (it’s a lot of work), I am wondering if the way I am doing it is the best way to draw lines.



Any help is appreciated

I’m confused. You say: “I am currently making a line from 2 points every time I call this function.” But your function seems to support creating a mesh containing many many lines.



One mesh with a thousand lines should be pretty fast. 1000 meshes with one line each will be very slow.

Thank you pspeed for knocking me straight! For some reason I was thinking when I am passing in the lines to the function (the lines come from an XML file) I had to make a new object for each segment I was reading in…



Obviously that’s not the case!



I made 1 mesh with all the lines and it is MUCH better :slight_smile: