Change texture of loaded .obj in three.js at runtime

后端 未结 1 1035
梦如初夏
梦如初夏 2020-12-10 04:08

I\'m trying to swap image texture at runtime on a loaded three.js .obj. Here\'s the code straight from three.js examples with slight modification:

        va         


        
相关标签:
1条回答
  • 2020-12-10 05:02

    The problem here is that there are multiple child meshes in the object variable. By having only one myMesh global variable, you are storing only the last child mesh. Thus when you try to update the texture using this global variable, only one of the meshes gets a texture update, probably a small hidden part that is not clearly visible.

    The solution would be to either:

    • Store a myMeshes array global variable and push myMesh into it every time there is one. Then in your newTexture() function, iterate through all items in this array and update their maps/materials.
    • Or, store the object variable (the one returned from OBJLoader's callback) into one single global variable. Then in your newTexture() function, iterate through all the meshes like how it is done in the code (using traverse() and if statement), and update their maps/materials.

    Lastly, calling newTexture() somewhere in your code would also help ;)

    0 讨论(0)
提交回复
热议问题