Package com.badlogic.gdx.graphics.g3d
Class ModelCache
java.lang.Object
com.badlogic.gdx.graphics.g3d.ModelCache
- All Implemented Interfaces:
RenderableProvider
,Disposable
ModelCache tries to combine multiple render calls into a single render call by merging them where possible. Can be used for
multiple type of models (e.g. varying vertex attributes or materials), the ModelCache will combine where possible. Can be used
dynamically (e.g. every frame) or statically (e.g. to combine part of scenery). Be aware that any combined vertices are
directly transformed, therefore the resulting
Renderable.worldTransform
might not be suitable for sorting anymore (such
as the default sorter of ModelBatch does).-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Allows to reuse one or more meshes while avoiding creating new objects.static class
A basicModelCache.MeshPool
implementation that avoids creating new meshes at the cost of memory usage.static class
ARenderableSorter
that sorts by vertex attributes, material attributes and primitive types (in that order), so that meshes can be easily merged.static class
A tightModelCache.MeshPool
implementation, which is typically used for static meshes (create once, use many). -
Constructor Summary
ConstructorsConstructorDescriptionCreate a ModelCache using the defaultModelCache.Sorter
and theModelCache.SimpleMeshPool
implementation.ModelCache
(RenderableSorter sorter, ModelCache.MeshPool meshPool) Create a ModelCache using the specifiedRenderableSorter
andModelCache.MeshPool
implementation. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(Renderable renderable) Adds the specifiedRenderable
to the cache.void
add
(RenderableProvider renderableProvider) Adds the specifiedRenderableProvider
to the cache, seeadd(Renderable)
.<T extends RenderableProvider>
voidAdds the specifiedRenderableProvider
s to the cache, seeadd(Renderable)
.void
begin()
Begin creating the cache, must be followed by a call toend()
, in between these calls one or more calls to one of the add(...) methods can be made.void
Begin creating the cache, must be followed by a call toend()
, in between these calls one or more calls to one of the add(...) methods can be made.void
dispose()
Releases all resources of this object.void
end()
void
getRenderables
(Array<Renderable> renderables, Pool<Renderable> pool) ReturnsRenderable
instances.
-
Constructor Details
-
ModelCache
public ModelCache()Create a ModelCache using the defaultModelCache.Sorter
and theModelCache.SimpleMeshPool
implementation. This might not be the most optimal implementation for you use-case, but should be good to start with. -
ModelCache
Create a ModelCache using the specifiedRenderableSorter
andModelCache.MeshPool
implementation. TheRenderableSorter
implementation will be called with the camera specified inbegin(Camera)
. By default this will be null. The sorter is important for optimizing the cache. For the best result, make sure that renderables that can be merged are next to each other.
-
-
Method Details
-
begin
public void begin()Begin creating the cache, must be followed by a call toend()
, in between these calls one or more calls to one of the add(...) methods can be made. Calling this method will clear the cache and prepare it for creating a new cache. The cache is not valid until the call toend()
is made. Use one of the add methods (e.g.add(Renderable)
oradd(RenderableProvider)
) to add renderables to the cache. -
begin
Begin creating the cache, must be followed by a call toend()
, in between these calls one or more calls to one of the add(...) methods can be made. Calling this method will clear the cache and prepare it for creating a new cache. The cache is not valid until the call toend()
is made. Use one of the add methods (e.g.add(Renderable)
oradd(RenderableProvider)
) to add renderables to the cache.- Parameters:
camera
- TheCamera
that will passed to theRenderableSorter
-
end
public void end() -
add
Adds the specifiedRenderable
to the cache. Must be called in between a call tobegin()
andend()
. All member objects might (depending on possibilities) be used by reference and should not change while the cache is used. If theRenderable.bones
member is not null then skinning is assumed and the renderable will be added as-is, by reference. Otherwise the renderable will be merged with other renderables as much as possible, depending on theMesh.getVertexAttributes()
,Renderable.material
and primitiveType (in that order). TheRenderable.environment
,Renderable.shader
andRenderable.userData
values (if any) are removed.- Parameters:
renderable
- TheRenderable
to add, should not change while the cache is needed.
-
add
Adds the specifiedRenderableProvider
to the cache, seeadd(Renderable)
. -
add
Adds the specifiedRenderableProvider
s to the cache, seeadd(Renderable)
. -
getRenderables
Description copied from interface:RenderableProvider
ReturnsRenderable
instances. Renderables are obtained from the providedPool
and added to the provided array. The Renderables obtained usingPool.obtain()
will later be put back into the pool, do not store them internally. The resulting array can be rendered via aModelBatch
.- Specified by:
getRenderables
in interfaceRenderableProvider
- Parameters:
renderables
- the output arraypool
- the pool to obtain Renderables from
-
dispose
public void dispose()Description copied from interface:Disposable
Releases all resources of this object.- Specified by:
dispose
in interfaceDisposable
-