jMonkeyEngine / jmonkeyengine

A complete 3-D game development suite written in Java.
http://jmonkeyengine.org
BSD 3-Clause "New" or "Revised" License
3.75k stars 1.12k forks source link

ClassCastException if MTL loader can not find material texture #1173

Open devent opened 4 years ago

devent commented 4 years ago

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

GiorgioPeng commented 4 years ago

Do u have any ideas? I have the same problem. By the way, I am not sure whether the problem is caused by the operating system. My friends can run the code on Windows, but I cannot run the code on macOS.

GiorgioPeng commented 4 years ago

I found the answer, the mtl file used the absolute path to find the texture, and I changed it to the relative path. Maybe you can check the attribute map_Kd in your mtl file.

devent commented 4 years ago

Actually, I'm using now the JMonkeyEngine IDE to import models and I'm using the GITF file format to export from Blender. The steps are: 1. Export model from Blender in GITF. 2. Import GITF file in JMonkeyEngine IDE. 3. Use the generated 3jo file in your game. I think it's better to just deprecate all the other import formats from JMonkeyEngine. https://wiki.jmonkeyengine.org/jme3/external/blender/blender_gltf.html

GiorgioPeng commented 4 years ago

Thank u, I got it. Now we plan to use j3o file to replace the obj file.

treecaptcha commented 1 year ago

I appear to be also having this issue:

f9cdc06f-e9ca-41dc-ba85-3a89765130b8.zip


        Spatial tray = assetManager.loadModel("Worlds/tray/f9cdc06f-e9ca-41dc-ba85-3a89765130b8.obj");
        rootNode.attachChild(tray);
Dec 05, 2022 5:39:44 PM com.jme3.system.JmeDesktopSystem initialize
INFO: Running on jMonkeyEngine 3.5.2-stable
 * Branch: HEAD
 * Git Hash: 8ab3d24
 * Build Date: 2022-04-21
Dec 05, 2022 5:39:45 PM com.jme3.system.lwjgl.LwjglContext printContextInitInfo
INFO: LWJGL 3.3.1 build 7 context running on thread jME3 Main
 * Graphics Adapter: GLFW 3.4.0 Wayland X11 GLX Null EGL OSMesa monotonic shared
Dec 05, 2022 5:39:45 PM com.jme3.renderer.opengl.GLRenderer loadCapabilitiesCommon
INFO: OpenGL Renderer Information
 * Vendor: NVIDIA Corporation
 * Renderer: NVIDIA GeForce RTX 2070/PCIe/SSE2

 * OpenGL Version: 4.6.0 NVIDIA 520.61.05
 * GLSL Version: 4.60 NVIDIA
 * Profile: Compatibility
[ALSOFT] (EE) Failed to set real-time priority for thread: Operation not permitted (1)
[ALSOFT] (EE) Failed to set real-time priority for thread: Operation not permitted (1)
Dec 05, 2022 5:39:45 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio Renderer Information
 * Device: OpenAL Soft
 * Vendor: OpenAL Community
 * Renderer: OpenAL Soft
 * Version: 1.1 ALSOFT 1.21.1
 * Supported channels: 64
 * ALC extensions: ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX ALC_EXT_thread_local_context ALC_SOFT_device_clock ALC_SOFT_HRTF ALC_SOFT_loopback ALC_SOFT_loopback_bformat ALC_SOFT_output_limiter ALC_SOFT_pause_device
 * AL extensions: AL_EXT_ALAW AL_EXT_BFORMAT AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW AL_EXT_MULAW_BFORMAT AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model AL_EXT_SOURCE_RADIUS AL_EXT_STEREO_ANGLES AL_LOKI_quadriphonic AL_SOFT_bformat_ex AL_SOFTX_bformat_hoa AL_SOFT_block_alignment AL_SOFTX_callback_buffer AL_SOFTX_convolution_reverb AL_SOFT_deferred_updates AL_SOFT_direct_channels AL_SOFT_direct_channels_remix AL_SOFT_effect_target AL_SOFT_events AL_SOFTX_filter_gain_ex AL_SOFT_gain_clamp_ex AL_SOFT_loop_points AL_SOFTX_map_buffer AL_SOFT_MSADPCM AL_SOFT_source_latency AL_SOFT_source_length AL_SOFT_source_resampler AL_SOFT_source_spatialize
Dec 05, 2022 5:39:45 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio effect extension version: 1.0
Dec 05, 2022 5:39:45 PM com.jme3.audio.openal.ALAudioRenderer initOpenAL
INFO: Audio max auxiliary sends: 2
Dec 05, 2022 5:39:46 PM com.jme3.scene.plugins.OBJLoader loadMtlLib
WARNING: Cannot locate teapot.mtl for model Models/teapot.obj
Dec 05, 2022 5:39:46 PM com.jme3.scene.plugins.MTLLoader loadTexture
WARNING: Cannot locate Worlds/tray/Wallpaper_Dog_Bone.jpg (Flipped) (Mipmapped) for material Worlds/tray/f9cdc06f-e9ca-41dc-ba85-3a89765130b8.mtl
Dec 05, 2022 5:39:46 PM com.jme3.app.LegacyApplication handleError
SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
java.lang.ClassCastException: class com.jme3.asset.AssetKey cannot be cast to class com.jme3.asset.TextureKey (com.jme3.asset.AssetKey and com.jme3.asset.TextureKey are in unnamed module of loader 'app')
    at com.jme3.texture.Texture.setKey(Texture.java:429)
    at com.jme3.scene.plugins.MTLLoader.loadTexture(MTLLoader.java:187)
    at com.jme3.scene.plugins.MTLLoader.readLine(MTLLoader.java:233)
    at com.jme3.scene.plugins.MTLLoader.load(MTLLoader.java:306)
    at com.jme3.asset.DesktopAssetManager.loadLocatedAsset(DesktopAssetManager.java:272)
    at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:388)
    at com.jme3.scene.plugins.OBJLoader.loadMtlLib(OBJLoader.java:351)
    at com.jme3.scene.plugins.OBJLoader.readLine(OBJLoader.java:397)
    at com.jme3.scene.plugins.OBJLoader.load(OBJLoader.java:596)
    at com.jme3.asset.DesktopAssetManager.loadLocatedAsset(DesktopAssetManager.java:272)
    at com.jme3.asset.DesktopAssetManager.loadAsset(DesktopAssetManager.java:388)
    at com.jme3.asset.DesktopAssetManager.loadModel(DesktopAssetManager.java:439)
    at com.jme3.asset.DesktopAssetManager.loadModel(DesktopAssetManager.java:444)
    at ml.treecaptcha.flowergate.Flowergate.simpleInitApp(Flowergate.java:73)
    at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:240)
    at com.jme3.system.lwjgl.LwjglWindow.initInThread(LwjglWindow.java:548)
    at com.jme3.system.lwjgl.LwjglWindow.run(LwjglWindow.java:662)
    at java.base/java.lang.Thread.run(Thread.java:833)
pspeed42 commented 1 year ago

The real problem: WARNING: Cannot locate Worlds/tray/Wallpaper_Dog_Bone.jpg (Flipped) (Mipmapped)

It cannot locate your texture.

The subsequent error is caused by the code that is attempting to load a stand-in texture for you... so even if that bug is fixed your model will still not be loading correctly.