AssetManager¶
Assets include models, materials, textures, scenes, shaders, audio and fonts. The AssetManager finds assets through locators, reads them through format-specific loaders, and caches eligible assets. Asset keys such as Models/Ship/ship.glb are relative to a locator's root, so the same code can load resources packaged with a project on different platforms.
The default manager includes a classpath locator and the engine's built-in assets. Additional locators can read from a directory, ZIP archive or URL. A locator finds a resource; a loader understands its file format. Include the relevant module for the loader, such as jme3-plugins for glTF.
Context¶
In a project created with the Initializer, keep shared resources under game-assets/src/main/assets:
game-assets/src/main/assets/
Interface/ # fonts and images
MatDefs/ # material definitions (.j3md)
Materials/ # material instances (.j3m)
Models/ # glTF, GLB, J3O, and associated resources
Scenes/ # scenes and associated resources
Shaders/ # shader sources
Sounds/ # audio
Textures/ # images
These directories are examples, not required names. Preserve the spelling and case of asset keys when reorganizing files. A .gltf may refer to external textures and buffers; keep those files at the relative paths recorded in the model. A .glb commonly packages them together. Resources in the classpath are packaged by the generated project; check each target's build output and its README.
Usage¶
Applications can use assetManager where it is provided, or obtain the manager through app.getAssetManager(). A key refers to the resource root, not the filesystem's project root:
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Textures/wall.png"));
Spatial model = assetManager.loadModel("Models/Ship/ship.glb");
rootNode.attachChild(model);
Provide those example files under the resource root. Built-in paths such as Common/MatDefs/... come from the engine. See Hello Assets for a complete runnable example.
Asset Directory¶
A project generated by the Initializer packages game-assets/src/main/assets for each selected platform: desktop uses resource JARs, Android uses application assets or an asset pack, and iOS copies files into the app bundle. Other project layouts can register different roots. Asset keys must match the packaged path, including letter case. If you rename a resource or move a glTF texture, update all references to it and rebuild the project.
Tip
Loading and reusing assets through the manager can avoid repeatedly loading the same resources. Asset loaders and cache behavior vary by asset type; consult the API if you need independent mutable instances or explicit cache control.
Example Code: Loading Assets¶
Create a material, load a texture, and load a font:
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setTexture("ColorMap", assetManager.loadTexture("Textures/wall.png"));
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
Load a glTF model from a ZIP file on the filesystem. The model key is relative to the ZIP archive, not the directory containing it:
assetManager.registerLocator("models.zip", ZipLocator.class);
Spatial scene = assetManager.loadModel("Scenes/house/house.glb");
rootNode.attachChild(scene);
For an external directory use FileLocator; for a URL base use UrlLocator. HttpZipLocator can read an archive from a remote URL. Supply a real path or URL and make sure the resources are present; loading from a network can fail or block and should be managed outside time-critical rendering work. See com.jme3.asset.plugins for the locator classes.
Important
An archive loaded by filesystem path must be present on the target device. It is not automatically made into a filesystem file just because it exists inside a JAR. Package resources on the classpath or explicitly distribute the archive and register its location.
Common AssetManager Tasks¶
The model paths below are illustrative; place matching files in your resource directory before running the snippets. A glTF asset does not need to be converted to .j3o to load it.
| Task? | Solution! |
|---|---|
Load a model with materials |
Use the asset manager’s |
Load a model without materials |
If you have a model without materials, you have to add a default material to make it visible. |
Load a scene |
You load scenes just like you load models: |
Cannot Locate a Resource?¶
If an asset works in your IDE but is missing in a packaged build, check the asset key, the registered locator and the actual contents of the distribution:
- Check the path and filename case. Asset keys are relative to their locator root.
- Make sure the file is under the generated project's resources directory or another root registered by a locator.
- If the model uses external textures or buffers, package those files at the relative paths it references.
- Check that the correct loader module is included on the target platform; glTF requires
jme3-plugins. - Inspect the built JAR or platform package and test it on the target. A ZIP used by
ZipLocatormust be available as a file at its configured path.
A project generated by the Initializer can package glTF directly. Conversion to .j3o is optional and is not a general prerequisite for a release. For a custom project, follow its build and resource-packaging configuration.
Asset Handling in Other IDEs¶
Gradle projects can be edited in different Java IDEs without creating a separate SDK project. If a custom build uses another resource root, configure that build to put the files on the runtime classpath or register an appropriate locator. The SDK may still be useful for asset inspection and scene editing; it is not required to load glTF in a game.
For explicit .j3o serialization, see Saving and Loading Nodes. Keep source assets in version control and verify the packaged result after changing asset paths.