APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.graphics.g3d

public Class ModelCache

extends Object
implements Disposable, RenderableProvider
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.graphics.g3d.RenderableProvider, com.badlogic.gdx.utils.Disposable
Imports
java.util.Comparator, com.badlogic.gdx.graphics.Camera, .Mesh, .VertexAttributes, com.badlogic.gdx.graphics.g3d.model.MeshPart, com.badlogic.gdx.graphics.g3d.utils.MeshBuilder, .RenderableSorter, com.badlogic.gdx.utils.Array, .Disposable, .FlushablePool, .GdxRuntimeException, .Pool

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).
Author
Xoppa

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
ModelCache.MeshPool

Allows to reuse one or more meshes while avoiding creating new objects.

public static class
ModelCache.SimpleMeshPool

A basic MeshPool implementation that avoids creating new meshes at the cost of memory usage.

public static class
ModelCache.Sorter

A RenderableSorter that sorts by vertex attributes, material attributes and primitive types (in that order), so that meshes can be easily merged.

public static class
ModelCache.TightMeshPool

A tight MeshPool implementation, which is typically used for static meshes (create once, use many).

Field Summary

Modifier and TypeField and Description
private boolean
private Camera
private Array<Renderable>
private MeshBuilder
private FlushablePool<MeshPart>
private ModelCache.MeshPool
private Array<Renderable>
private FlushablePool<Renderable>
private RenderableSorter
private Array<Renderable>

Constructor Summary

AccessConstructor and Description
public
ModelCache()

Create a ModelCache using the default Sorter and the SimpleMeshPool implementation.

public
ModelCache(RenderableSorter sorter, ModelCache.MeshPool meshPool)

Create a ModelCache using the specified RenderableSorter and MeshPool implementation.

Method Summary

Modifier and TypeMethod and Description
public void
add(Renderable
The Renderable to add, should not change while the cache is needed.
renderable
)

Adds the specified Renderable to the cache.

public void
add(final RenderableProvider renderableProvider)

Adds the specified RenderableProvider to the cache, see add(Renderable).

public <T extends RenderableProvider> void
add(final Iterable<T> renderableProviders)

Adds the specified RenderableProviders to the cache, see add(Renderable).

public void
begin()

Begin creating the cache, must be followed by a call to end(), in between these calls one or more calls to one of the add(...) methods can be made.

public void
begin(Camera
The Camera that will passed to the RenderableSorter
camera
)

Begin creating the cache, must be followed by a call to end(), in between these calls one or more calls to one of the add(...) methods can be made.

public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

public void
end()

Finishes creating the cache, must be called after a call to begin(), only after this call the cache will be valid (until the next call to begin()).

public void
private Renderable
obtainRenderable(Material material, int primitiveType)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

buildingback to summary
private boolean building
cameraback to summary
private Camera camera
itemsback to summary
private Array<Renderable> items
meshBuilderback to summary
private MeshBuilder meshBuilder
meshPartPoolback to summary
private FlushablePool<MeshPart> meshPartPool
meshPoolback to summary
private ModelCache.MeshPool meshPool
renderablesback to summary
private Array<Renderable> renderables
renderablesPoolback to summary
private FlushablePool<Renderable> renderablesPool
sorterback to summary
private RenderableSorter sorter
tmpback to summary
private Array<Renderable> tmp

Constructor Detail

ModelCacheback to summary
public ModelCache()

Create a ModelCache using the default Sorter and the SimpleMeshPool implementation. This might not be the most optimal implementation for you use-case, but should be good to start with.

ModelCacheback to summary
public ModelCache(RenderableSorter sorter, ModelCache.MeshPool meshPool)

Create a ModelCache using the specified RenderableSorter and MeshPool implementation. The RenderableSorter implementation will be called with the camera specified in begin(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

addback to summary
public void add(Renderable renderable)

Adds the specified Renderable to the cache. Must be called in between a call to begin() and end(). All member objects might (depending on possibilities) be used by reference and should not change while the cache is used. If the Renderable#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 the Mesh#getVertexAttributes(), Renderable#material and primitiveType (in that order). The Renderable#environment, Renderable#shader and Renderable#userData values (if any) are removed.

Parameters
renderable:Renderable

The Renderable to add, should not change while the cache is needed.

addback to summary
public void add(final RenderableProvider renderableProvider)

Adds the specified RenderableProvider to the cache, see add(Renderable).

addback to summary
public <T extends RenderableProvider> void add(final Iterable<T> renderableProviders)

Adds the specified RenderableProviders to the cache, see add(Renderable).

beginback to summary
public void begin()

Begin creating the cache, must be followed by a call to end(), 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 to end() is made. Use one of the add methods (e.g. add(Renderable) or add(RenderableProvider)) to add renderables to the cache.

beginback to summary
public void begin(Camera camera)

Begin creating the cache, must be followed by a call to end(), 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 to end() is made. Use one of the add methods (e.g. add(Renderable) or add(RenderableProvider)) to add renderables to the cache.

Parameters
camera:Camera

The Camera that will passed to the RenderableSorter

disposeback to summary
public void dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Doc from com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

Annotations
@Override
endback to summary
public void end()

Finishes creating the cache, must be called after a call to begin(), only after this call the cache will be valid (until the next call to begin()). Calling this method will process all renderables added using one of the add(...) methods and will combine them if possible.

getRenderablesback to summary
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool)

Implements com.badlogic.gdx.graphics.g3d.RenderableProvider.getRenderables.

Doc from com.badlogic.gdx.graphics.g3d.RenderableProvider.getRenderables.

Returns Renderable instances. Renderables are obtained from the provided Pool and added to the provided array. The Renderables obtained using Pool#obtain() will later be put back into the pool, do not store them internally. The resulting array can be rendered via a ModelBatch.

Annotations
@Override
obtainRenderableback to summary
private Renderable obtainRenderable(Material material, int primitiveType)
com.badlogic.gdx.graphics.g3d back to summary

public Interface ModelCache.MeshPool

extends Disposable
Known Direct Implementers
com.badlogic.gdx.graphics.g3d.ModelCache.SimpleMeshPool, com.badlogic.gdx.graphics.g3d.ModelCache.TightMeshPool

Allows to reuse one or more meshes while avoiding creating new objects. Depending on the implementation it might add memory optimizations as well. Call the obtain(VertexAttributes, int, int) method to obtain a mesh which can at minimum the specified amount of vertices and indices. Call the flush() method to flush the pool ant release all previously obtained meshes.

Method Summary

Modifier and TypeMethod and Description
public void
flush()

Releases all previously obtained Meshes using the the obtain(VertexAttributes, int, int) method.

public Mesh

Returns:

the obtained Mesh, or null when no mesh could be obtained.
obtain
(VertexAttributes
the vertex attributes of the mesh to obtain
vertexAttributes
,
int
the minimum amount vertices the mesh should be able to store
vertexCount
,
int
the minimum amount of indices the mesh should be able to store
indexCount
)

Will try to reuse or, when not possible to reuse, optionally create a Mesh that meets the specified criteria.

Inherited from com.badlogic.gdx.utils.Disposable:
dispose

Method Detail

flushback to summary
public void flush()

Releases all previously obtained Meshes using the the obtain(VertexAttributes, int, int) method.

obtainback to summary
public Mesh obtain(VertexAttributes vertexAttributes, int vertexCount, int indexCount)

Will try to reuse or, when not possible to reuse, optionally create a Mesh that meets the specified criteria.

Parameters
vertexAttributes:VertexAttributes

the vertex attributes of the mesh to obtain

vertexCount:int

the minimum amount vertices the mesh should be able to store

indexCount:int

the minimum amount of indices the mesh should be able to store

Returns:Mesh

the obtained Mesh, or null when no mesh could be obtained.

com.badlogic.gdx.graphics.g3d back to summary

public Class ModelCache.SimpleMeshPool

extends Object
implements MeshPool
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool, com.badlogic.gdx.utils.Disposable

A basic MeshPool implementation that avoids creating new meshes at the cost of memory usage. It does this by making the mesh always the maximum (64k) size. Use this when for dynamic caching where you need to obtain meshes very frequently (typically every frame).
Author
Xoppa

Field Summary

Modifier and TypeField and Description
private Array<Mesh>
private Array<Mesh>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

public void
flush()

Implements com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.flush.

Releases all previously obtained Meshes using the the obtain(VertexAttributes, int, int) method.

public Mesh
obtain(VertexAttributes vertexAttributes, int vertexCount, int indexCount)

Implements com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.obtain.

Will try to reuse or, when not possible to reuse, optionally create a Mesh that meets the specified criteria.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

freeMeshesback to summary
private Array<Mesh> freeMeshes
usedMeshesback to summary
private Array<Mesh> usedMeshes

Constructor Detail

SimpleMeshPoolback to summary
public SimpleMeshPool()

Method Detail

disposeback to summary
public void dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Doc from com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

Annotations
@Override
flushback to summary
public void flush()

Implements com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.flush.

Doc from com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.flush.

Releases all previously obtained Meshes using the the obtain(VertexAttributes, int, int) method.

Annotations
@Override
obtainback to summary
public Mesh obtain(VertexAttributes vertexAttributes, int vertexCount, int indexCount)

Implements com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.obtain.

Doc from com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.obtain.

Will try to reuse or, when not possible to reuse, optionally create a Mesh that meets the specified criteria.

Annotations
@Override
com.badlogic.gdx.graphics.g3d back to summary

public Class ModelCache.Sorter

extends Object
implements RenderableSorter, Comparator<Renderable>
Class Inheritance
All Implemented Interfaces
java.util.Comparator, com.badlogic.gdx.graphics.g3d.utils.RenderableSorter

A RenderableSorter that sorts by vertex attributes, material attributes and primitive types (in that order), so that meshes can be easily merged.
Author
Xoppa

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public int
compare(Renderable arg0, Renderable arg1)

Implements java.util.Comparator.compare.

Compares its two arguments for order.

public void
sort(Camera camera, Array<Renderable> renderables)

Implements com.badlogic.gdx.graphics.g3d.utils.RenderableSorter.sort.

Sorts the array of Renderable instances based on some criteria, e.g. material, distance to camera etc.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Sorterback to summary
public Sorter()

Method Detail

compareback to summary
public int compare(Renderable arg0, Renderable arg1)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Annotations
@Override
sortback to summary
public void sort(Camera camera, Array<Renderable> renderables)

Implements com.badlogic.gdx.graphics.g3d.utils.RenderableSorter.sort.

Doc from com.badlogic.gdx.graphics.g3d.utils.RenderableSorter.sort.

Sorts the array of Renderable instances based on some criteria, e.g. material, distance to camera etc.

Annotations
@Override
com.badlogic.gdx.graphics.g3d back to summary

public Class ModelCache.TightMeshPool

extends Object
implements MeshPool
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool, com.badlogic.gdx.utils.Disposable

A tight MeshPool implementation, which is typically used for static meshes (create once, use many).
Author
Xoppa

Field Summary

Modifier and TypeField and Description
private Array<Mesh>
private Array<Mesh>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

public void
flush()

Implements com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.flush.

Releases all previously obtained Meshes using the the obtain(VertexAttributes, int, int) method.

public Mesh
obtain(VertexAttributes vertexAttributes, int vertexCount, int indexCount)

Implements com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.obtain.

Will try to reuse or, when not possible to reuse, optionally create a Mesh that meets the specified criteria.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

freeMeshesback to summary
private Array<Mesh> freeMeshes
usedMeshesback to summary
private Array<Mesh> usedMeshes

Constructor Detail

TightMeshPoolback to summary
public TightMeshPool()

Method Detail

disposeback to summary
public void dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Doc from com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

Annotations
@Override
flushback to summary
public void flush()

Implements com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.flush.

Doc from com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.flush.

Releases all previously obtained Meshes using the the obtain(VertexAttributes, int, int) method.

Annotations
@Override
obtainback to summary
public Mesh obtain(VertexAttributes vertexAttributes, int vertexCount, int indexCount)

Implements com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.obtain.

Doc from com.badlogic.gdx.graphics.g3d.ModelCache.MeshPool.obtain.

Will try to reuse or, when not possible to reuse, optionally create a Mesh that meets the specified criteria.

Annotations
@Override