I totally love how libgdx brings a lot of interfaces for your application to connect to the graphics library. For instance the asset manager is really thought through well in particular. It has some really useful methods like Assets.manager.getProgress(). Especially since you can easily write your own asset loader and be able to utilize the loading progress in your app.
1) Simply add your loader to the asset manager at some point.
1 2 3 | AssetManager manager = new AssetManager(); manager.setLoader( StillModel.class, new StillModelAssetLoader( new InternalFileHandleResolver() ) ); manager.setLoader( ShaderProgram.class, new ShaderAssetLoader( new InternalFileHandleResolver() ) ); |
2) load your assets at required places, e.g. loading screen
(Note: SHADER_PHONG = “data/Graphics/Shader/Phong”; and MODEL_TEAPOT = “data/Graphics/Shader/teapot.obj”)
1 2 | manager.load( SHADER_PHONG, ShaderProgram.class ); manager.load( MODEL_TEAPOT, StillModel.class ); |
3) get your shader or model wherever you need them
1 2 | ShaderProgram phong = manager.get( SHADER_PHONG, ShaderProgram.class ); StillModel teapotMesh = manager.get( MODEL_TEAPOT, StillModel.class ); |
4) free the resource by unloading when you’re done with them (e.g. disposing your app; changing screens)
1 2 | manager.unload( SHADER_PHONG ); manager.unload( MODEL_TEAPOT); |
That’s the shader asset loader for loading custom shaders in libgdx. (Note: adapt your extensions)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class ShaderAssetLoader extends SynchronousAssetLoader<ShaderProgram, AssetLoaderParameters> { private static final String VERTEX_SHADER_EXTENSION = ".vsh"; private static final String FRAGMENT_SHADER_EXTENSION = ".fsh"; public ShaderAssetLoader ( final FileHandleResolver resolver ) { super( resolver ); } @Override public ShaderProgram load ( final AssetManager assetManager, final String fileName, final AssetLoaderParameters parameter ) { final ShaderProgram shader = new ShaderProgram( resolve( fileName + VERTEX_SHADER_EXTENSION ), resolve( fileName + FRAGMENT_SHADER_EXTENSION ) ); if ( shader.isCompiled() == false ) { throw new IllegalStateException( shader.getLog() ); } return shader; } @Override public Array getDependencies ( final String fileName, final AssetLoaderParameters parameter ) { return null; } } |
And here is the StillModel loader for loading 3D meshes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class StillModelAssetLoader extends SynchronousAssetLoader<StillModel, AssetLoaderParameters> { public StillModelAssetLoader ( FileHandleResolver resolver ) { super( resolver ); } @Override public StillModel load ( AssetManager assetManager, String fileName, AssetLoaderParameters parameter ) { return ModelLoaderRegistry.loadStillModel( resolve( fileName ) ); } @Override public Array getDependencies ( String fileName, AssetLoaderParameters parameter ) { return null; } } |
Conclusion: assets are handled well in libgdx :D
Leave a Reply