简体   繁体   中英

Programmatically change model texture libgdx

I'm making a game and I want to change the texture on a model depending on associated data but no matter what I as soon as the texture should change the model becomes invisible the model is in g3dj format here is the materials portion

"materials":[
    {
        "id":"default",
        "diffuse": [0.640000, 0.640000, 0.640000],
    },
    {
        "id":"plant fibre",
        "textures":[
            {
                "id": "Texture.001",
                "filename": "graphics/plant fibre.png",
                "type": "DIFFUSE"
            }
        ]
    },
    {
        "id":"logs",
        "textures":[
            {
                "id": "Texture.001",
                "filename": "graphics/logs.png",
                "type": "DIFFUSE"
            }
        ]
    }
],

And here is my latest attempt

 self.getMaterial("default").clear();
 self.getMaterial("default").set(self.getMaterial(extra.getString("item")).get(TextureAttribute.Diffuse));

I have already tried everything I found by searching online and it all had the same problem

Turns out unused materials aren't passed to the modelInstance I just had to modify the model then create and render the modelInstance

Ps: this always happens I get so fed up with something that I post a question here and then solve the problem within 24 hours

// trees

val loader: ObjLoader = ObjLoader()
tree = loader.loadModel(Gdx.files.internal("data/tree2.obj"))
treeInstance = ModelInstance(tree)

for (m in treeInstance!!.materials) {
    println("material id: ${m.id}")
    m.remove(ColorAttribute.Emissive)
    if (m.id == "Leafs" || m.id == "Leafs2") {
        m.set(ColorAttribute.createDiffuse(Color.FOREST))
    } else {
        m.set(ColorAttribute.createDiffuse(Color.BROWN))
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM