Rafael Adamy
Rafael Adamy

Reputation: 19

maxscript to automate rgb color conversion to gamma 2.2?

basically, I have many old 3d models, with materials made on a 1.0 gamma environment, and everytime I want to use one of them I have to manually convert all colors to match a 2.2 gamma equivalent.

the formula I use is 255*((old/255)^2.2), based on a tutorial by mintviz, but having to manually correct all colors is a tedious and time consuming process, and at least theoretically could be heavily automated.

what I'm thinking is a simple button that when you press, converts all existing colors, from the selected object only, to it's 2.2 equivalent.

I've done a little custom attributes interfacing in maxscript, but that's all, so I don't have a large experience with it, so I need some help here.

the script work on the materials of selected objects only, then it would either check each of them for existing colors assigned to the map slots, sub maps, etc, and apply the formula to all of them, or just brute force apply the formula to all possible slots.

what you think?

it should be simple enough, I just don't even know where to start

Upvotes: 1

Views: 546

Answers (1)

Swordslayer
Swordslayer

Reputation: 2091

You can try and get all the material and texturemap classes the selected object depends on and change all their properties that contain a color value:

fn gammaCorrectClr clrP4 gamma =
    (255 * [clrP4.x^gamma, clrP4.y^gamma, clrP4.z^gamma]) as Color

mapped fn gammaCorrectMaterial mat gamma =
(
    local props = getPropNames mat

    for prop in props where isKindOf (local val = getProperty mat prop) Color do
        setProperty mat prop (gammaCorrectClr (val as Point4) gamma)
)

for matClass in Material.classes do gammaCorrectMaterial (getClassInstances matClass target:selection[1]) 2.2
for texClass in TextureMap.classes do gammaCorrectMaterial (getClassInstances texClass target:selection[1]) 2.2

If you want to do the same only for the whole scene instead of a selected object, remove the target:selection[1] part from both getClassInstances calls.

Upvotes: 1

Related Questions