Libgdx 3D Optimize??

No, not quite. If you look close, there’s a tab labeled LogCat in the lower left. That’s the one…

Sorry i am being a bit of a noob here
How would that help me figure out the problem?

The transparency in the batch may be an issue of the depth testing. Since your 3D scene is just billboarding sprites, you can disable depth testing I think.


glDisable(GL_DEPTH_TEST);

In the lower part of the screen, you have two tabs. Console and LogCat. You are looking at Console, which isn’t very helpful. Open LogCat and look for garbage collection messages from the system while your app is running. It will print out what it does and how long it takes.

Thanks for the reply but that didn’t do the trick

Here are some suggestions (which you can of course take or leave). It seems like you have a few different issues going on here - at minimum, some performance issues and some technical issues related to things like blending and so on.

Addressing these sorts of issues can be time-consuming and require meticulous work, but sometimes that’s just the way it is. In this case, I think you might benefit from starting from scratch and rebuilding things from the ground up. That way you can focus on one problem at a time, and fix each problem as it arises. So, to that end, some suggestions:

  • I’m assuming that, in addition to providing higher-level features, LibGDX allows you to work with OpenGL directly (as you’re attempting to do here) without getting in the way. If that’s not the case, it might be good to clarify that, as it would have some implications for what you’re doing.

  • Assuming LibGDX does allow you to work with OpenGL directly, I would try to get your own shaders working. That is, if you’re going to work with OpenGL directly, go all the way with it so that you can know what’s going on even at the shader level.

  • You probably don’t want any conditionals in your fragment shaders, I don’t think (I noticed there was a conditional in the shader you posted, but maybe that was just an example).

  • If you’re not using normals, you could drop them from your shaders and mesh geometry, which could reduce data transfer costs.

  • If your object count is in the hundreds, you probably don’t want to render each mesh with a separate draw call (not sure if that’s what you’re doing currently). Batching multiple meshes together might involve pre-transforming the geometry in some cases. You also might consider using static meshes where practical. For example, if the ‘plant’ objects in your screenshot never move, you could combine multiple plants into a single static mesh, thereby reducing draw calls and perhaps data transfer as well (depending on how you’re doing things).

  • Try to get all the basics working before worrying about performance. For example, if transparency isn’t working, get that fixed first, then move on to performance-related issues.

  • Once you get your own shaders working, if you’re still seeing performance issues, just as an experiment you might try some different precisions (e.g. lowp), just to see if that has any effect.

These are just suggestions, and no guarantee that they’re even good ones :slight_smile: It does seem though like you might benefit from regrouping and tackling these problems one at a time in an isolated context (that’s what I’d do at least).

And? Does the log output tells you something? Judging from your screenshot, it looks like as if you are creating instances of matrices for each object each frame…that’s most likely not a good idea. Try to avoid object creations in inner loops. The log output should show you this by multiple garbage collection messages while the game is running.

Thanks for the advice
however i did have transparency working perfect before i moved to ModelBatch and the reason why I move to ModelBatch is because my rendering method was optimized enough for example (it was calling each mesh binding and unbinding) which i didn’t want and non of the OpenGl stuff was working so i moved to ModelBatch…

http://www.java-gaming.org/topics/libgdx-mesh-render-optimization/36196/view.html <— Thats a post i made which no body commented on so as far as i know you cannot use pure open gl functions to render meshes

Take note I have only worked in Open Gl 3+ (Modern Open GL) and Libgdx doesnt use that so it is hard for me to get the grasp of this

All of the optimizations you have mentioned i have already considered and knew about but as said before Libgdx is either hiding stuff or it doesnt contain all of the Open Gl functions

I would like to start again but i dont really see a point as all of my code I have made so far is just taking Libgdx ways and turning them into a more easier system to use(My opinion) for example libgdx puts all of its vertices and texturecoords, normals basically any attribute and combines that data to the vertex data using offset which obviously improves the performance but it makes the meshes hard to read/write manually. Only simple things like that i have changed, I am not completely sure what to do at the moment because starting again would have a end result which is the same as what i have so far.
Any ideas?

I have a decent amount of experience with 3D in libgdx.

Probably the biggest cause of your slow downs is the high number of ModelInstances (you said you are using ModelBatch so I assume all those objects are separate model instances)

You should try to merge them together as much as possible. This is how I was able to get good performance in Kotlin Voxel and GDX-Proto

Have you checked the garbage thing? Your screen shot is full float[] creations. You might think that it’s not a big deal, but believe me…on Android, it really is!

Sorry I am not ignoring you and I am about to check that right now :slight_smile:
I am just interested if there is any other way in rendering your own meshes instead of using Renderable because i still don’t have transparency working…
Does anyone know a way of disabling ModelBatch transparency sorting?

I have changed the createTransformation and made a static variable instead of creating lots of them

I am not using ModelInstances I am using Renderable

Renderable creating

private TexturedModel textureModel;
	private Material material;
	private Renderable renderable;
	
	public RenderableModel(TexturedModel model) {
		this.textureModel = model;
		
		material = new Material();
		material.set(blendingAttribute);
		material.set(new TextureAttribute(TextureAttribute.Diffuse,model.getTexture()));
		
		renderable = new Renderable();
		renderable.mesh = model.getModel().getMesh().getMeshData();
		renderable.meshPartOffset = 0;
		renderable.meshPartSize = model.getModel().getMesh().getMeshData().getNumIndices();
		renderable.primitiveType = GL20.GL_TRIANGLES;
		renderable.material = material;
		renderable.worldTransform.set(model.getModel().getTransform().getTransformationMatrix());
	}

Renderable Updating and Rendering

private Camera camera;
	private ModelBatch batch;
	private Environment environment;
	private RenderableProvider provider;
	private Array<Entity> entities;
	private static int meshesRendered = 0;

	public EntityRenderer(Camera camera, ModelBatch batch, Environment e) {
		this.camera = camera;
		this.batch = batch;
		this.environment = e;
		provider = new RenderableProvider() {

			@Override
			public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) {
				meshesRendered = 0;
				for (int i = 0; i < entities.size; i++) {
					Entity e = entities.get(i);
					prepareEntity(e);
					if (isVisible(getCamera(), e) || !e.isUseFrustumCulling()) {
						meshesRendered++;
						Renderable r = pool.obtain();
						r.set(e.getRenderableModel().getRenderable());
						renderables.add(r);

						if (MasterRenderer.getRenderCollisionMesh()) {
							meshesRendered++;
							Renderable r1 = pool.obtain();
							r1.set(e.getCollisionMesh().getRenderable());
							renderables.add(r1);
						}
					}
				}

			}
		};
	}
	public static int getMeshesRendered() {
		return meshesRendered;
	}
	public Camera getCamera() {
		return camera;
	}

	public void prepareEntity(Entity e) {
		e.getRenderableModel().update(camera, environment);
		e.getCollisionMesh().update(camera, environment);
	}

	public boolean isVisible(final Camera cam, final Entity e) {
		return cam.frustum.pointInFrustum(e.getPosition());
	}

	public void render(Array<Entity> entites, Entity floor) {
		this.entities = entites;

		batch.begin(camera);
		batch.render(floor.getRenderableModel().getRenderable());
		batch.end();
		
		batch.begin(camera);
		batch.render(provider);
		batch.end();
	}

In addition, I would try some micro-optimizations for the inner loop like reducing method calls…for example like so:


int end=entities.size;
for (int i = 0; i < end; i++) {
     Entity e = entities.get(i);
     e.getRenderableModel().update(camera, environment); // Is this expensive?
     e.getCollisionMesh().update(camera, environment); // Is this expensive?
     boolean isVisible=camera.frustum.pointInFrustum(e.getPosition()) // Is this expensive?
     if (isVisible || !e.isUseFrustumCulling()) {
        meshesRendered++;
        Renderable r = pool.obtain();
        r.set(e.getRenderableModel().getRenderable());
        renderables.add(r);

        if (MasterRenderer.getRenderCollisionMesh()) {
           meshesRendered++;
           Renderable r1 = pool.obtain();
           r1.set(e.getCollisionMesh().getRenderable());
           renderables.add(r1);
        }
    }
}

Depending on the performance profile of your app, this might or might not help. I know that it helped me a lot in a similar task. I would also check if these update(…) or the pointInFrustum(…) calls are expensive. Maybe something like pointInFrustum can be optimized by adding some early out criteria to it.

I wouldn’t say the update method is expensive what do you think?

	public void update(Camera camera,Environment e){
		renderable.environment = e;
		renderable.worldTransform.set(textureModel.getModel().getTransform().getTransformationMatrix());
	}

Here is a questions lets say I have over 500 zombies on the map how would i smoothly render these on the screen as
on my android it is lagging still which is still very hard to even do anything

The zombie would contain a transformation matrix at the moment

No, it’s not expensive, but it could be inlined anyway.

All visible at the same time?

Depends what you mean?

I mean they will be on the map but depending if they are in the frustum then they will be rendered or not?

Also do you think that the Samsung Galaxy ace 2 will be able to handle a game like this?


<<— This is that game i am trying to recreate