Imported Object Textures

I have imported a .obj that has 4 textures on it. The textures are applied automatically through the .mtl file.



My Question is: How do i access those textures from within JME so that i can switch them out for other textures or manipulate them, etc.



Rob G

Actually I'm not sure if obj loads all 4 textures, but if (like e.g. hottbj, i think ogre as well) you can access them via the Objects texturestate.



Try it like this:

TextureState ts = (TextureState)node.getRenderState(StateType.Texture);
Texture t = ts.getTexture(0);



...change the id in getTexture to access the other textures

Works that way, but keep in mind that obj inflates the scengraph, by combining all meshes with the same material.

Empire Phoenix said:

Works that way


no it doesn't  :wink:

i guess he doesn't mean all 4 textures are in one TextureState?  :wink:

Well, since you cant know to which geometries the loader applied which textures, you should do it recursively:


  private void setThingsRec(Spatial s) {
    TextureState ts = (TextureState)s.getRenderState(RenderState.RS_TEXTURE);
    if (ts != null) {
      // set some texture properties
      ts.setCorrectionType(CorrectionType.Perspective);
      ts.getTexture().setMinificationFilter(MinificationFilter.Trilinear);
      ts.getTexture().setMagnificationFilter(MagnificationFilter.Bilinear);
      // save a  reference for later use
      if (!textures.contains(ts.getTexture())) {
        textures.add(ts.getTexture());
      } // if
    } // if
    // recurse
    if (s instanceof Node) {
      for (Spatial spat : ((Node)s).getChildren()) {
        setThingsRec(spat);
      } // for
    } // if
  } // setThingsRec



you can also access the image file name with ts.getTexture().getImageLocation()

gl&hf

Thanks guys! I understand now, can access all 4 textures. @Empire Phoenix: What do you mean it inflates the scene graph? Sorry don't know much about scene graph terminology.