jMonkeyEngine / jMonkeyEngine/jmonkeyengine

ClassCastException if MTL loader can not find material texture

Open
#1,173 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Contribution welcome defect
Dominant language
Java
Stars
4.3k
Forks
1.2k
Avg merge
4d 7h
Merged PRs (30d)
14

Description

JME throws a ClassCastException if the MTL loader can not find the material texture key. Code:

        this.box = assetManager.loadModel("assets/models/tiles/box-1.obj");
        Material mat1 = assetManager.loadMaterial("assets/materials/tiles/loam/loam-1-1.mtl")
        mat1.setTexture("textures/loam/loam-1-1.png", assetManager.loadTexture("assets/textures/tiles/loam/loam-1-1.png"));
        box.setMaterial(mat1);
        return box

See the code in https://github.com/jMonkeyEngine/jmonkeyengine/blob/v3.2.4-stable/jme3-core/src/plugins/java/com/jme3/scene/plugins/MTLLoader.java#L186

            logger.log(Level.WARNING, "Cannot locate {0} for material {1}", new Object[]{texKey, key});
            texture = new Texture2D(PlaceholderAssets.getPlaceholderImage(assetManager));
            texture.setWrap(WrapMode.Repeat);
            texture.setKey(key);

You set the MaterialKey to a texture object. It will throw a CCEx on https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-core/src/main/java/com/jme3/texture/Texture.java#L427

    public void setKey(AssetKey key){
        this.key = (TextureKey) key;
    }

Error:

java.lang.ClassCastException: class com.jme3.asset.MaterialKey cannot be cast to class com.jme3.asset.TextureKey (com.jme3.asset.MaterialKey and com.jme3.asset.TextureKey are in unnamed module of loader 'app')
	at com.jme3.texture.Texture.setKey(Texture.java:427)
	at com.jme3.scene.plugins.MTLLoader.loadTexture(MTLLoader.java:186)
	at com.jme3.scene.plugins.MTLLoader.readLine(MTLLoader.java:232)
	at com.jme3.scene.plugins.MTLLoader.load(MTLLoader.java:304)

Version: 3.2.4-stable

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in jme3-core/src/plugins/java/com/jme3/scene/plugins/MTLLoader.java at the missing-texture handling around line 186, then inspect com/jme3/texture/Texture.java at setKey. Reproduce loading an MTL file whose material texture cannot be found; done means this case no longer throws ClassCastException and the existing placeholder behavior remains usable.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
game-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.