Package com.badlogic.gdx.graphics.g3d
Class ModelCache
- java.lang.Object
-
- com.badlogic.gdx.graphics.g3d.ModelCache
-
- All Implemented Interfaces:
RenderableProvider,Disposable
public class ModelCache extends java.lang.Object implements Disposable, RenderableProvider
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 resultingRenderable.worldTransformmight not be suitable for sorting anymore (such as the default sorter of ModelBatch does).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceModelCache.MeshPoolAllows to reuse one or more meshes while avoiding creating new objects.static classModelCache.SimpleMeshPoolA basicModelCache.MeshPoolimplementation that avoids creating new meshes at the cost of memory usage.static classModelCache.SorterARenderableSorterthat sorts by vertex attributes, material attributes and primitive types (in that order), so that meshes can be easily merged.static classModelCache.TightMeshPoolA tightModelCache.MeshPoolimplementation, which is typically used for static meshes (create once, use many).
-
Constructor Summary
Constructors Constructor Description ModelCache()Create a ModelCache using the defaultModelCache.Sorterand theModelCache.SimpleMeshPoolimplementation.ModelCache(RenderableSorter sorter, ModelCache.MeshPool meshPool)Create a ModelCache using the specifiedRenderableSorterandModelCache.MeshPoolimplementation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(Renderable renderable)Adds the specifiedRenderableto the cache.voidadd(RenderableProvider renderableProvider)Adds the specifiedRenderableProviderto the cache, seeadd(Renderable).<T extends RenderableProvider>
voidadd(java.lang.Iterable<T> renderableProviders)Adds the specifiedRenderableProviders to the cache, seeadd(Renderable).voidbegin()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.voidbegin(Camera camera)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.voiddispose()Releases all resources of this object.voidend()voidgetRenderables(Array<Renderable> renderables, Pool<Renderable> pool)ReturnsRenderableinstances.
-
-
-
Constructor Detail
-
ModelCache
public ModelCache()
Create a ModelCache using the defaultModelCache.Sorterand theModelCache.SimpleMeshPoolimplementation. This might not be the most optimal implementation for you use-case, but should be good to start with.
-
ModelCache
public ModelCache(RenderableSorter sorter, ModelCache.MeshPool meshPool)
Create a ModelCache using the specifiedRenderableSorterandModelCache.MeshPoolimplementation. TheRenderableSorterimplementation 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 Detail
-
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
public void begin(Camera camera)
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- TheCamerathat will passed to theRenderableSorter
-
end
public void end()
-
add
public void add(Renderable renderable)
Adds the specifiedRenderableto 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.bonesmember 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.materialand primitiveType (in that order). TheRenderable.environment,Renderable.shaderandRenderable.userDatavalues (if any) are removed.- Parameters:
renderable- TheRenderableto add, should not change while the cache is needed.
-
add
public void add(RenderableProvider renderableProvider)
Adds the specifiedRenderableProviderto the cache, seeadd(Renderable).
-
add
public <T extends RenderableProvider> void add(java.lang.Iterable<T> renderableProviders)
Adds the specifiedRenderableProviders to the cache, seeadd(Renderable).
-
getRenderables
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool)
Description copied from interface:RenderableProviderReturnsRenderableinstances. Renderables are obtained from the providedPooland 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:
getRenderablesin interfaceRenderableProvider- Parameters:
renderables- the output arraypool- the pool to obtain Renderables from
-
dispose
public void dispose()
Description copied from interface:DisposableReleases all resources of this object.- Specified by:
disposein interfaceDisposable
-
-