package ethanjones.cubes.graphics.world;

import ethanjones.cubes.core.system.CubesException;
import ethanjones.cubes.core.system.Debug;

import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.Renderable;

import java.lang.reflect.Field;

public class CubesModelBatch extends ModelBatch {
  
  protected class CubesRenderablePool extends RenderablePool {
  
    @Override
    public Renderable obtain () {
      Renderable renderable = super.obtain();
      renderable.environment = null;
      renderable.material = null;
      renderable.meshPart.set("", null, 0, 0, 0);
      renderable.shader = null;
      
      // custom
      renderable.userData = null;
      renderable.worldTransform.idt();
      
      return renderable;
    }
  }
  
  public CubesModelBatch() {
    super(new WorldShaderProvider());
    
    try {
      Field field = ModelBatch.class.getDeclaredField("renderablesPool");
      field.setAccessible(true);
      field.set(this, new CubesRenderablePool());
      if (!(renderablesPool instanceof CubesRenderablePool)) throw new CubesException("Failed");
    } catch (Exception e) {
      Debug.crash(new CubesException("Failed to setup CubesRenderablePool", e));
    }
  }
}

@@ -474,32 +474,77 @@
 obtain();%0A      
+// super.obtain resets the following%0A      //
 renderable.envir
@@ -556,32 +556,34 @@
 t = null;%0A      
+//
 renderable.mater
@@ -592,32 +592,34 @@
 l = null;%0A      
+//
 renderable.meshP
@@ -644,32 +644,34 @@
 0, 0, 0);%0A      
+//
 renderable.shade
@@ -678,28 +678,24 @@
 r = null;%0A  
-    
 %0A      // cu
@@ -695,21 +695,8 @@
   //
- custom%0A     
  ren
@@ -716,24 +716,136 @@
 ata = null;%0A
+      // built in as of libgdx 1.9.6%0A      // https://github.com/libgdx/libgdx/pull/4550%0A      %0A      // custom%0A
       render
@@ -1372,9 +1372,8 @@
  %7D%0A  %7D%0A%7D
-%0A

Update CubesModelBatch.CubesRenderablePool due to libgdx/libgdx#4550