Skip to content Skip to sidebar Skip to footer

How To Render 3d Object Within A Libgdx Actor?

Most of the Libgdx tutorials I found show how to add 2D elements in a 3D world, but I would like to know how to the the opposite, adding 3D elements in a 2D Stage. I tried adding

Solution 1:

I had the same problem but I needed to render 3d object only once so I came with an idea to render 3d model as a Sprite. In order to do that I rendered my model via modelBatch to frame buffer object instead of default screen buffer and then created a sprite from FBO color buffer.

Sample code below:

FrameBufferframeBuffer=newFrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true);

Sprite renderModel(ModelInstance modelInstance) {
    frameBuffer.begin(); //Capture rendering to frame buffer.

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT | (Gdx.graphics.getBufferFormat().coverageSampling ? GL20.GL_COVERAGE_BUFFER_BIT_NV : 0))
    modelBatch.begin(camera);
    modelBatch.render(modelInstance);
    modelBatch.end();

    frameBuffer.end();

    returnnewSprite(frameBuffer.getColorBufferTexture());
}

You can always update your sprite texture in a render loop with use of sprite.setTexture(); method. You can also create an Image from a texture -> new Image(frameBuffer.getColorBufferTexture()); and use it in Scene2d.

Post a Comment for "How To Render 3d Object Within A Libgdx Actor?"