younes noori younes noori - 6 months ago 24
Java Question

how can I use local image file as a Texture in JMonkeyEngine 3?

how can I use local image file as a Texture in JMonkeyEngine 3 ?

I try to use :

app.getAssetManager().registerLocator("D:\\yn\\", FileLocator.class);


and I have an image file "1.jpg" in yn directory. so it should work but not!

com.jme3.scene.shape.Box mesh = new com.jme3.scene.shape.Box(Vector3f.ZERO, 1, 1, 1);
Geometry geom = new Geometry("Box", mesh);

app.getAssetManager().registerLocator("D:\\yn\\", FileLocator.class);

Material mat = new Material(app.getAssetManager(), "1.jpg");

mat.setColor("Color", ColorRGBA.Green);
geom.setMaterial(mat);
geom.setLocalTranslation(1, 0, 0);
geom.addControl(new logo_anim(false));


and it shows this error:

SEVERE: Uncaught exception thrown in Thread[jME3 Main,5,main]
java.lang.ClassCastException: com.jme3.asset.AssetKey cannot be cast to com.jme3.asset.TextureKey

Answer

The String parameter in the Material constructor is the path to the material definition. You have to use a definition which supports textures. E.g. "Common/MatDefs/Misc/Unshaded.j3md"
Afterwards you can load and set the texture like this:

// If you copy the image file into the assets folder of your jME project
// you don't need to register a new locator.
assetManager.registerLocator("D:/yn/", FileLocator.class);

Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Texture tex = assetManager.loadTexture("1.jpg");
mat.setTexture("ColorMap", tex);