Search code examples
pythonpanda3d

How do I change the texture of a particular Geom in a GeomNode?


Some context: I'm dynamically generating some tile-based graphics by creating a Geom for each tile (containing only a couple of tris), then combining all Geoms into a single GeomNode for the whole map.

According to the panda3d docs, one of the uses of a GeomNode is to be able to change the texture for individual Geoms. However, it doesn't explain how to do this.

Calling set_texture on the Geom itself doesn't work (presumably since the RenderState is in the GeomNode, not the Geom). I can get the RenderState corresponding to the nth Geom with geom_node.get_geom_state(n), and set it again with geom_node.set_geom_state(n, state), but I don't know how to change the RenderState so that it applies a texture, and from the API reference it looks very complicated.

How do I change the texture on an individual Geom in a GeomNode (and if the answer is "put each Geom in its own GeomNode", then is there ever a reason to have more than one Geom per GeomNode)?


Solution

  • Turns out if you read the documentation a little more closely, it says:

    You should not attempt to create or modify a RenderState object directly. Instead, call one of the make() functions to create one for you. And instead of modifying a RenderState object, create a new one.

    So to update the texture on Geom n, given some texture texture and a GeomNode node, you need to do something like this:

    attrib = TextureAttrib.make(texture)
    state = RenderState.make(attrib)
    node.set_geom_state(n, state)