I have to figures, a cone and a Box in libgdx:
ModelBuilder modelBuilder = new ModelBuilder();
Model cone= modelBuilder.createCone(40f,250f,40f,8,cMaterial, VertexAttributes.Usage.Position|VertexAttributes.Usage.Normal|VertexAttributes.Usage.TextureCoordinates);
Material tMaterial = new Material(ColorAttribute.createDiffuse(com.badlogic.gdx.graphics.Color.GRAY));
Texture troncoTexture = new Texture(Gdx.files.internal("t.jpg"));
TextureAttribute taTronco = new TextureAttribute(TextureAttribute.Diffuse,troncoTexture);
tMaterial.set(taTronco);
Model cylinder= modelBuilder.createCylinder(20, 320, 20, 12, boxMaterial, VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates);
Is there any way I can combine both models into a new model or modelInstance so when I apply a transformation both models move at the same time?
Example:
Model myCominedModel = new Model(cone,cylinder) //Something like this is possible?
myCombinedModel.rotate(1,1,1,60) // Rotate both models
You can use ModelCache
to merge one or more Models
. That will merge them into the same Mesh and render call where possible, which causes their individual transformation to be applied directly.
If you just want to add the Nodes of one model to the other then you could do that using model1.nodes.add(model2.nodes);
. Note however that this does not make model1
responsible for managing the resources of model2
, you still need to dispose both models when you don't need model1
anymore (but not before that). To fix that (and the references to the materials and such) you could use the static ModelBuilder.rebuildReferences(model1)
method, after which you should discard model2
, but not dispose it.
There are a few other options. E.g. to prevent having to deal with ownership of disposables you could combine the ModelInstance
instead of the Model
. You could also use ModelBuilder
to create a new model and add the nodes of both models to it.
That said; in your example code you are first creating two separate models using ModelBuilder
and then ask how you can combine them. You might as well build one model in the first place. Which would look something like (untested code):
ModelBuilder modelBuilder = new ModelBuilder();
modelBuilder.begin();
modelBuilder.part("cone", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, cMaterial)
.cone(40f,250f,40f,8);
modelBuilder.part("cylinder", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.TextureCoordinates, boxMaterial)
.cylinder(20, 320, 20, 12);
Model model = modelBuilder.end();
See this link for more information. Note that in your snippet you are creating a material without actually using it. You are also creating a Texture in there. Keep in mind that textures need to be disposed. If you want to make the model responsible for disposing it for you then you can call modelBuilder.manage(troncoTexture)
.
Also note that if you don't need a separate material for both the cone and the cylinder that you don't need to create a separate part()
for it. On the other hand, if you want to able to move them independently from each other within the model then you can call the node()
method before each part.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With