Skip to content

jMonkeyEngine 3 Tutorial (3) - Hello Assets

In this tutorial we will learn to load 3D models and text into the scene graph, using the jME Asset Manager. You will also learn how to determine the correct paths, and which file formats to use.

beginner-assets-models.png

The original tutorial illustration; the current example is shown below.

Tip

Create a project with the Initializer, and include the glTF loader from jme3-plugins.

Code Sample

The complete HelloAssets.java example loads an official glTF sample over the network, applies a texture to a box, and displays text. The first download may take a while. In a game, package or cache assets and load network resources asynchronously.

import com.jme3.app.SimpleApplication;
import com.jme3.asset.plugins.UrlLocator;
import com.jme3.bounding.BoundingBox;
import com.jme3.font.BitmapText;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Box;

/** Hello Assets: load a glTF model, texture, and font with the AssetManager. */
public class HelloAssets extends SimpleApplication {
    public static void main(String[] args) {
        HelloAssets app = new HelloAssets();
        app.setShowSettings(false);
        app.start();
    }

    @Override
    public void simpleInitApp() {
        setDisplayStatView(false);
        setDisplayFps(false);
        viewPort.setBackgroundColor(new ColorRGBA(.12f, .16f, .22f, 1));
        cam.setLocation(new Vector3f(0, 1.5f, 8));
        cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);

        // The demo loads an official sample remotely; package assets in a game.
        assetManager.registerLocator(
                "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/refs/heads/main/",
                UrlLocator.class);
        Spatial model = assetManager.loadModel("Models/BoomBox/glTF-Binary/BoomBox.glb");
        model.updateGeometricState();
        BoundingBox bounds = new BoundingBox();
        bounds.mergeLocal(model.getWorldBound());
        float height = Math.max(0.001f, bounds.getYExtent() * 2f);
        model.setLocalScale(2.5f / height);
        rootNode.attachChild(model);

        // A textured box demonstrates loading an image from the classpath.
        Geometry wall = new Geometry("Wall", new Box(1, 1, .1f));
        Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        material.setTexture("ColorMap", assetManager.loadTexture("Common/Textures/dot.png"));
        wall.setMaterial(material);
        wall.setLocalTranslation(-2.5f, 0, -1);
        rootNode.attachChild(wall);

        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-1, -2, -1).normalizeLocal());
        sun.setColor(ColorRGBA.White);
        rootNode.addLight(sun);
        AmbientLight ambient = new AmbientLight();
        ambient.setColor(ColorRGBA.White.mult(.4f));
        rootNode.addLight(ambient);

        BitmapText text = new BitmapText(guiFont);
        text.setText("Hello Assets");
        text.setLocalTranslation(20, text.getLineHeight() + 20, 0);
        guiNode.attachChild(text);
    }
}

Run the application: you should see a Boom Box model, a textured box and the text “Hello Assets”. The model is loaded from the Khronos sample assets; the sample is CC0.

Hello Assets running on Linux

The screenshot shows the example on Linux using native OpenGL with software rendering.

The Asset Manager

Game assets include models, textures, materials, shaders, fonts, sounds and scenes. AssetManager loads these resources by asset key through registered loaders and locators. A locator determines where to look; a loader reads the format. loadModel, loadTexture and loadFont are common entry points.

In a project generated by the Initializer, shared resources live under game-assets/src/main/assets. Asset keys are relative to that resource root:

game-assets/src/main/assets/
  Interface/
  Materials/
  Models/
  Scenes/
  Sounds/
  Textures/

Important

The Gradle project packages its resources for distribution. Preserve relative paths and filename case. Android and iOS launchers may package assets differently; use the generated project structure and test the packaged build.

Applications can also register locators for external folders, ZIP files or URLs, as shown below.

Loading Textures

Place your textures under your project's resource directory, for example game-assets/src/main/assets/Textures/. Load the texture into the material before you set the material. The following code in simpleInitApp() creates a textured wall:

/* Create a wall (Box with material and texture included with jMonkeyEngine) */
Box box = new Box(2.5f,2.5f,1.0f);
Spatial wall = new Geometry("Box", box );
Material mat_brick = new Material( assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat_brick.setTexture("ColorMap", assetManager.loadTexture("Common/Textures/dot.png"));
wall.setMaterial(mat_brick);
wall.setLocalTranslation(2.0f,-2.5f,0.0f);
rootNode.attachChild(wall);

In this case, you create your own Material and apply it to a Geometry. You base Materials on default material descriptions (such as “Unshaded.j3md”), as shown in this example.

Loading Text and Fonts

This example displays the text “Hello World” in the default font at the bottom edge of the window. You attach text to the guiNode – this is a special node for flat (orthogonal) display elements. You display text to show the game score, player health, etc. The following code sample goes into the simpleInitApp() method.

/* Display a line of text (default font included with jMonkeyEngine) */
setDisplayStatView(false);
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
BitmapText helloText = new BitmapText(guiFont);
helloText.setSize(guiFont.getCharSet().getRenderedSize());
helloText.setText("Hello World");
helloText.setLocalTranslation(300, helloText.getLineHeight(), 0);
guiNode.attachChild(helloText);

Tip

You can use setDisplayStatView(false); to deactivate the HUD display statistics.

Loading a Model

Place a glTF .glb in your resource directory, for example game-assets/src/main/assets/Models/Character/character.glb, and load it by the corresponding asset key:

Spatial model = assetManager.loadModel("Models/Character/character.glb");
rootNode.attachChild(model);

Include jme3-plugins for the glTF loader. The model path is an example: supply your own file and preserve the location of any external textures and buffers.

Note

glTF can include its own materials. PBR models also need suitable lighting and an environment probe; see PBR and environment lighting.

Loading Assets From Custom Paths

Assets outside the classpath can be loaded by registering a locator. For example, load a glTF model from a ZIP on the filesystem:

assetManager.registerLocator("models.zip", ZipLocator.class);
Spatial model = assetManager.loadModel("Models/Character/character.glb");
rootNode.attachChild(model);

The key is the path inside the archive; the ZIP file itself must exist at the path supplied to ZipLocator. Register a FileLocator to load from an external directory:

assetManager.registerLocator("/path/to/assets", FileLocator.class);
Spatial model = assetManager.loadModel("Models/Character/character.glb");

HttpZipLocator can load an archive from an HTTPS URL; UrlLocator can load assets directly from a URL base, as the full sample demonstrates. These locators need a valid URL and network access. See the AssetManager reference for loader and locator behavior.

Tip

When multiple locators contain the same key, use clear, unique paths and check which resource is loaded.

Creating Models and Scenes

Create models with a tool that exports glTF 2.0. Export a .glb or a .gltf file with its external resources, copy the complete result into the shared resource directory, and load it using assetManager.loadModel. Check scale, orientation, material appearance and animation in the engine.

Unlike the old SDK-specific conversion workflow, the generated project can package and load glTF assets directly. The engine's .j3o format remains useful when explicitly needed, but converting every model to .j3o is not required for release. See Blender and glTF for the recommended workflow.

Exercise - How to Load Assets

Try the same glTF model through three locators:

  1. Put a .glb under game-assets/src/main/assets/Models/Character/ and load it by its asset key.
  2. Put the same relative path inside models.zip, register ZipLocator for the ZIP file, and load the key from inside the archive.
  3. Register UrlLocator for the Khronos sample asset repository, as in the complete example above, and load Models/BoomBox/glTF-Binary/BoomBox.glb.

Use unique asset keys when multiple locators can contain the same filename. A ZIP on the filesystem must be present at the path passed to ZipLocator; it is not automatically embedded in a packaged game.

Tip

For a real project, prefer packaged resources or a controlled cache. A remote server can be unavailable, and network access should not block the render thread.

Conclusion

You can now populate a scene with shapes and models and load assets using assetManager. Asset keys are relative to a registered resource root, not necessarily the project directory. Your application can package and load glTF models directly; converting them to .j3o is optional.

See also: