Merging Models from ModelBuilder in LibGDX

3d, java, libgdx

Solution

Instead of using createCylinder(), you can create 2 cylinders with the MeshBuilder class, and compose your final cylinder with part().

meshBuilder.begin();
meshBuilder.cylinder(4f, 6f, 4f, 16);
Mesh cylinder1 = meshBuilder.end();

meshBuilder.begin();
meshBuilder.cylinder(4f, 6f, 4f, 16);
Mesh cylinder2 = meshBuilder.end();


modelBuilder.begin();

modelBuilder.part("cylinder1", 
    cylinder1,
    Usage.Position | Usage.Normal | Usage.TextureCoordinates,
    new Material(
        TextureAttribute.createDiffuse(white), 
        ColorAttribute.createSpecular(1,1,1,1), 
        FloatAttribute.createShininess(8f)));

modelBuilder.part("cylinder2",
    cylinder2,
    Usage.Position | Usage.Normal | Usage.TextureCoordinates,
    new Material(
        TextureAttribute.createDiffuse(red), 
        ColorAttribute.createSpecular(1,1,1,1), 
        FloatAttribute.createShininess(8f)))
    .mesh.transform(new Matrix4().translate(0, 0, -2f));

Model finalCylinder = modelBuilder.end();

Problem

I'm a new to LibGDX 3D facilities and I'm wondering how I can merge two cylinders created using the ModelBuilder#createCylinder class.I have two ModelInstances : - The first one is a white cylinder, - The second a red cylinder with the same properties How can get only one cylinder to render (instance / model / object / whatever can be rendered) composed of the red above the white one (or vice versa). ``` Pixmap pixmap1 = new Pixmap(1, 1, Format.RGBA8888); pixmap1.setColor(Color.WHITE); pixmap1.fill(); Texture white = new Texture(pixmap1); //... Texture red = new Texture(pixmap2); model1 = modelBuilder.createCylinder(4f, 6f, 4f, 16, new Material( TextureAttribute.createDiffuse(white), ColorAttribute.createSpecular(1,1,1,1), FloatAttribute.createShininess(8f)) , Usage.Position | Usage.Normal | Usage.TextureCoordinates); model1I_white = new ModelInstance(model1, 0, 0, 0); //... model2I_red = new ModelInstance(model2, 0, 0, -2f); ``` Then I render ModelInstance with ModelBatch#render.

Original source