Character color

My project has a built-in character editor. It allows the player to choose from various options to change the way their character looks, such as adding gloves, boots, helmets, etc. This is working fine (uses code to hide/show various parts of the model)

I want to see if it’s possible to allow the player to choose the color for the various parts . What would the approach be to do something like this? Right now the model just has a diffuse map it uses. What coding approaches are there in Panda to allow me to change the color of say a glove or boot dynamically?

The two simplest approaches might be the methods “setColor” and “setColorScale” of NodePath–if I’m not much mistaken, the former replaces the current (vertex, I think?) colour of the object, while the latter treats the given colour as a scalar: the colour of the NodePath is multiplied by the given colour. These should allow for a basic form of colourising; the latter might be best used with greyscale diffuse maps, allowing for a wider range of colourisations (I’m not sure about the proper use of the former, I’m afraid).

For example, presume that you have a model named “cat.egg”, which has a single, greyscale texture:

np = loader.loadModel("cat")

np.setColorScale(0, 1, 0, 1) # Make the cat green

This can, I think, be applied to separate sub-parts of a model; for example, if the “cat” model was composed of two sub-models named “paws” and “body”, we might do something like this:

np = loader.loadModel("cat")

paws = np.find("**/paws")

np.setColorScale(0, 1, 0, 1) # Make the cat green...
paws.setColorScale(0, 0, 1, 1) # ...but make its paws blue.

There may, however, be caveats with this approach that I’m not aware of.

More advanced colourisations might be implemented using a custom shader. For example, you might use vertex colours to specify “regions” of colour, and set the colours to apply to those regions via shader inputs; the shader might then interpolate fragment colours based on the vertex colour and shader input.

You could use the PNMImage class to directly manipulate the diffuse map for the model or make a custom one to save with that specific character once the player is satisfied it; you would obviously need a way to isolate the particular part of the map that covers the part you’re changing though (i.e. glove).

Thanks for the responses.
I think I tried the setColor option without results, but it was a while ago, so I’ll definitely try it again.

I’ll also look into PNMIMage.

If “setColor” doesn’t work, I recommend trying “setColorScale”.