Trouble with GeometryBatchFactory

Hi ,

I’m trying to use GeometryBatchFactory with Lines :

[java]
Material matRed = new Material(app.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);
matRed.setColor(“Color”, new ColorRGBA(ColorRGBA.Red.getRed(), ColorRGBA.Red.getGreen(), ColorRGBA.Red.getBlue(), 0.75f));
Material matBlue = new Material(app.getAssetManager(), “Common/MatDefs/Misc/Unshaded.j3md”);
matBlue.setColor(“Color”, new ColorRGBA(ColorRGBA.Blue.getRed(), ColorRGBA.Blue.getGreen(), ColorRGBA.Blue.getBlue(), 0.75f));
Node testNode = new Node(“test Node”);
Line line1 = new Line(
new Vector3f(0,0,0),
new Vector3f(100,0,0));
Geometry geomLine1 = new Geometry(“line1”, line1);
Line line2 = new Line(
new Vector3f(100,0,0),
new Vector3f(100,100,0));
Geometry geomLine2 = new Geometry(“line2”,line2);
Line line3 = new Line(
new Vector3f(100,100,0),
new Vector3f(200,200,0));
Geometry geomLine3 = new Geometry(“line3”,line3);
Line line4 = new Line(
new Vector3f(200,200,0),
new Vector3f(300,200,0));
Geometry geomLine4 = new Geometry(“line4”,line4);
Line line5 = new Line(
new Vector3f(300,200,0),
new Vector3f(300,300,0));
Geometry geomLine5 = new Geometry(“line5”,line5);
Line line6 = new Line(
new Vector3f(300,300,0),
new Vector3f(500,300,0));
Geometry geomLine6 = new Geometry(“line5”,line6);
geomLine1.setMaterial(matRed); geomLine2.setMaterial(matBlue); geomLine3.setMaterial(matRed); geomLine4.setMaterial(matBlue); geomLine5.setMaterial(matRed);geomLine6.setMaterial(matBlue);
testNode.attachChild(geomLine1); testNode.attachChild(geomLine2); testNode.attachChild(geomLine3); testNode.attachChild(geomLine4); testNode.attachChild(geomLine5);

Node finalNode = null;
try {
finalNode = (Node) GeometryBatchFactory.optimize(testNode);
}catch(Exception e) {
e.printStackTrace();
}
rootNode.attachChild(finalNode);

[/java]

i have this exception :
[java]
java.lang.UnsupportedOperationException: The geometry line4 (Geometry) buffer Index has different number of components than the rest of the meshes (this: 3, expected: 2)
[/java]

Any ideas to help me please ?

The index buffer has a different number of components and cannot be batched with the other meshes. Create the same buffers on both meshes.

First, thank you Normen for the fast answer.

Homewer, i have some difficulties to understand it. I’ve tried something but without positive result.

Have you some examples to link ? Now, i’m reading it :

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:custom_meshes

but i don’t realize what i want :

[java]
Mesh mesh = new Mesh();
mesh.setMode(Mesh.Mode.Lines);
Vector3f [] vertices = new Vector3f[2];
vertices[0] = new Vector3f(1,1,0);
vertices[1] = new Vector3f(4,1,0);

	int [] indexes = { 0,1,1, 1,1,0 }; //I don't want triangles but lines so ...		

	mesh.setBuffer(Type.Position, 3, com.jme3.util.BufferUtils.createFloatBuffer(vertices));
	mesh.setBuffer(Type.Index,    3, com.jme3.util.BufferUtils.createIntBuffer(indexes));
	mesh.updateBound();
	
	Geometry geo1 = new Geometry("Mesh1", mesh);
	geo1.setMaterial(matBlue);
	
	Geometry geo2 = new Geometry("Mesh2", mesh);
	geo2.setMaterial(matRed);

	testNode.attachChild(geo1); testNode.attachChild(geo2);
	GeometryBatchFactory.optimize(testNode);
	rootNode.attachChild(testNode);

[/java]

Than you again.

I checked the Line.java source

[java] protected void updateGeometry(Vector3f start, Vector3f end) {
this.start = start;
this.end = end;
setBuffer(Type.Position, 3, new float[]{start.x, start.y, start.z,
end.x, end.y, end.z,});

    setBuffer(Type.TexCoord, 2, new float[]{0, 0,
                                            1, 1});

    setBuffer(Type.Normal, 3, new float[]{0, 0, 1,
                                          0, 0, 1});

    setBuffer(Type.Index, 3, new short[]{0, 1});

    updateBound();
}[/java]

Should the number of index components be 3 here? or should it be 1? i’ve not dealt with lines, or custom meshes much, so don’t hate on me for my speculation!

According to the exception :
[java]
buffer Index has different number of components than the rest of the meshes (this: 3, expected: 2)
[/java]

The number of components expected is … 2 here.

But i don’t know how to create same buffers on both lines despite of my lot of tests. I certainly missed something …

<cite>@wezrule said:</cite> I checked the Line.java source

[java] protected void updateGeometry(Vector3f start, Vector3f end) {
this.start = start;
this.end = end;
setBuffer(Type.Position, 3, new float{start.x, start.y, start.z,
end.x, end.y, end.z,});

    setBuffer(Type.TexCoord, 2, new float[]{0, 0,
                                            1, 1});

    setBuffer(Type.Normal, 3, new float[]{0, 0, 1,
                                          0, 0, 1});

    setBuffer(Type.Index, 3, new short[]{0, 1});

    updateBound();
}[/java]

Should the number of index components be 3 here? or should it be 1? i’ve not dealt with lines, or custom meshes much, so don’t hate on me for my speculation!

It should be 2. I guess Line.java has a bug.

1 Like

I’ve just fixed it in last SVN

1 Like

The ‘bug’ is fixed. Thanks a lot !!

1 Like