Multi-material objects in jME3

I have a single object consisting of four submeshes. Each submesh has its own material and texture file. In jME, I can only assign the entire mesh to a material with one texture file. How do I assign each submesh to a different texture?

You have to assign the material to the single Geometries, not to the super node. Open the model in the jMP SceneViewer to see its structure.

[snippet id=“13”]

Cheers,

Normen

I did as you said, and each submesh has the correct material, but the UV mapping is wrong for all of them. Am I missing something?



[java] Material guy0 = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

guy0.setTexture(“m_DiffuseMap”, assetManager.loadTexture(“guy/helmet.png”));

guy0.setFloat(“m_Shininess”, 10);



Material guy1 = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

guy1.setTexture(“m_DiffuseMap”, assetManager.loadTexture(“guy/shirt.png”));

guy1.setFloat(“m_Shininess”, 20);



Material guy2 = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

guy2.setTexture(“m_DiffuseMap”, assetManager.loadTexture(“guy/body.png”));

guy2.setFloat(“m_Shininess”, 50);



Material guy3 = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);

guy3.setTexture(“m_DiffuseMap”, assetManager.loadTexture(“guy/shorts.png”));

guy3.setFloat(“m_Shininess”, 20);



Node guy = (Node)assetManager.loadModel(“guy/guy.mesh.xml”);

((Geometry)guy.getChild(0)).setMaterial(guy0);

((Geometry)guy.getChild(1)).setMaterial(guy1);

((Geometry)guy.getChild(2)).setMaterial(guy2);

((Geometry)guy.getChild(3)).setMaterial(guy3);

// guy.setShadowMode(ShadowMode.CastAndReceive);

guy.setLocalScale(.1f);

rootNode.attachChild(guy);[/java]

Most likely you need to flip the texture, the OgreXML loader does this by default.

Why aren’t you using the ogre materials, but creating them manually for the ogre mesh?

Flipping the textures fixed the UV mapping problem, but I get a weird graphics error. It seems some faces aren’t displayed depending on where the camera is looking - different parts disappear as I pan the camera:







What’s the problem here?



Also, just adding the model makes it bright red. I have to add the textures manually. I’m not sure what “ogre materials” are.

Try

[java]TangentBinormalGenerator.generate(geometry.getMesh());[/java]

on the geometrys meshes.



@Momoko_Fan: Shouldnt this be default when there are no tangents?



Cheers,

Normen



P.S. All of this is much easier when using jMP to convert the model before :wink:

@Normen: I don’t think so, because OgreXML does not support normal mapping. You need to either create a J3M or manually apply normal mapping in code like is done above.