Skip to content

Conversation

@yahel-ck
Copy link

@yahel-ck yahel-ck commented Sep 17, 2024

Hey :)
This change adds an InstanceData field to Renderable and ModelInstance to allow rendering the same mesh with different InstanceData buffers or without instances.
Currently LibGDX only supports enabling instanced rendering on a Mesh instance (with enableInstancedRendering method) so it can only be rendered with it's own InstanceData buffer and can't be rendered regularly unless you unset it's instances property.

An example use-case for this feature (from my game) is rendering instanced "chunks" to optimize rendering by culling out chunks that are out of view, and still using instanced rendering to optimize rendering each chunk.
Without this change I'm pretty sure the only way to implement this is by duplicating the Mesh object, which is extremely messy.

Also I think it's more intuitive to have the instance data per ModelInstance rather than per Mesh.
Enabling instanced rendering on a ModelInstance would look something like this:

ModelInstance modelInstance1 = ...;
ModelInstance modelInstance2 = ...; // Same Mesh as modelInstance1
modelInstance1.setInstances(InstanceBufferObject(...));
modelInstance2.setInstances(InstanceBufferObject(...)); // Different data than modelInstance1

This change doesn't change/break the current flow of the rendering code unless the new instances property is set in the Renderable that's being drawn.
Using instance data on a Mesh still works.

These are the changes I made in each file:

  • Renderable - Added optional instances field.
  • ModelInstace - Added optional instances field, and changed getRenderable to use it.
  • Mesh - Add bind,unbind,render methods that receive an external InstanceData object.
  • MeshPart - Add render method that receives an external InstanceData object.
  • BaseShader - Change render,end,register methods to use the renderable's instances if set. Also add currentInstances property to hold the last renderable's instances to use similarly to currentMesh.

I tested this on an Android device with my own game, which uses both instanced rendering and regular rendering so I know this doesn't break any standard functionality.
If you think it's necessary I could come up with some minimal demo project for you guys to test it out yourself.

@yahel-ck yahel-ck changed the title Feature/seperate instance data from mesh Feature/separate instance data from mesh Sep 19, 2024
@yahel-ck yahel-ck marked this pull request as draft October 1, 2024 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant