title: How to render 3D object within a Libgdx Actor?
body: <p>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. </p>
  
  <p>I tried adding a background image to the <code>Stage</code>, then adding to the <code>Stage</code> an <code>Actor</code> that renders the model batch and the 3D instances in its draw() method.</p>
  
  <p>But instead, the image isn't drawn and part of the 3D object is hidden.</p>
  
  <p>SimpleGame class</p>
  
  <pre><code>public class SimpleGame extends ApplicationAdapter {
  
      Stage stage;
  
      @Override
      public void create () {
          stage = new Stage();
  
          InputMultiplexer im = new InputMultiplexer(stage);
          Gdx.input.setInputProcessor( im );
  
          Image background = new Image(new Texture("badlogic.jpg"));
          background.setSize(stage.getWidth(), stage.getHeight());
          stage.addActor(background);
  
          setup();
      }
  
      private void setup() {
          SimpleActor3D group = new SimpleActor3D();
          group.setSize(stage.getWidth(), stage.getHeight());
          group.setPosition(0, 0);
          stage.addActor(group);
      }
  
      @Override
      public void render () {
          stage.act();
  
          Gdx.gl.glClearColor(1, 1, 1, 1);
          Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
          Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
  
          stage.draw();
      }
  }
  </code></pre>
  
  <p>SimpleActor3D class</p>
  
  <pre><code>public class SimpleActor3D extends Actor {
  
      public Environment environment;
      public PerspectiveCamera camera;
  
      public ModelBatch modelBatch;
      public ModelInstance boxInstance;
  
      public SimpleActor3D() {
          environment = SimpleUtils.createEnvironment();
          camera = SimpleUtils.createCamera();
          boxInstance = SimpleUtils.createModelInstance(Color.GREEN);
  
          modelBatch = new ModelBatch();
      }
  
      @Override
      public void draw(Batch batch, float parentAlpha) {
          Gdx.gl.glViewport((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());
  
          modelBatch.begin(camera);
          modelBatch.render( boxInstance, environment );
          modelBatch.end();
  
          super.draw(batch, parentAlpha);
      }
  }
  </code></pre>
  
  <p>SimpleUtils class</p>
  
  <pre><code>public class SimpleUtils {
  
      public static Environment createEnvironment() {
          Environment environment = new Environment();
          environment.set( new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f) );
  
          DirectionalLight dLight = new DirectionalLight();
          Color lightColor = new Color(0.75f, 0.75f, 0.75f, 1);
          Vector3 lightVector = new Vector3(-1.0f, -0.75f, -0.25f);
          dLight.set( lightColor, lightVector );
          environment.add( dLight ) ;
  
          return environment;
      }
  
      public static PerspectiveCamera createCamera() {
          PerspectiveCamera camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
          camera.position.set(10f, 10f, 10f);
          camera.lookAt(0,0,0);
          camera.near = 1f;
          camera.far  = 300f;
          camera.update();
  
          return camera;
      }
  
      public static ModelInstance createModelInstance(Color color) {
          ModelBuilder modelBuilder = new ModelBuilder();
  
          Material boxMaterial = new Material();
          boxMaterial.set( ColorAttribute.createDiffuse(color) );
  
          int usageCode = VertexAttributes.Usage.Position + VertexAttributes.Usage.ColorPacked + VertexAttributes.Usage.Normal;
  
          Model boxModel = modelBuilder.createBox( 5f, 5f, 5f, boxMaterial, usageCode );
  
          return new ModelInstance(boxModel);
      }
  }
  </code></pre>
  
  <p>What I would like :</p>
  
  <p><a href="https://i.stack.imgur.com/ECnjL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ECnjL.png" alt="What I would like"></a></p>
  
  <p>What I have instead :</p>
  
  <p><a href="https://i.stack.imgur.com/pnm1A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pnm1A.png" alt="What I have instead"></a></p>
  
  <p>I have tried rendering the model batch directly in the <code>ApplicationAdapter</code> <code>render()</code> method and it works perfectly, so the problems must lie somewhere with the <code>Stage</code> but I can't find how.</p>