Search code examples
unity-game-engineimagemagickunreal-engine4

Imagemagick flip channels and create an alpha channel (Unreal RMA to Unity RMA)


So I have bunch of 3D models with RMAO maps to define the PBR channels. In unreal engine this is done so that I have the Roughness, metalness and ambient occlusion channels assigned as this in a mask texture:

  • R = Ambient Occlusion
  • G = Roughness
  • B = Metalness

Now I want to transfer my assets to unity, but unity uses a different system and has the following setup:

  • R = Metallic
  • G = Ambient Occlusion
  • B = empty
  • A = Smoothness (Basically inverted roughness)

So basically this boils down to doing the following:

R <- B
G <- R
B <- pure black
A <- Inverted G

I've tried this command magick convert 'texture.png' -alpha opaque -separate -swap 0,2 -swap 1,2 -swap 3,2 -swap 2,2 -combine png32:texture_new.png but the result does not have a separate alpha channel, so the opacity is on the RGB channels.

So what I basically would want to do is to convert an RGB texture to RGBA texture, flip the channels around a bit and then invert the alpha channel. Is this possible with an imagemagick.


Solution

  • I don't use Unreal or Unity so I have no way of checking this but maybe it will help you see a way if it doesn't work.

    I am laying it out so each line should produce one of your required output channels:

    magick texture.png -write MPR:orig -channel B -separate \
        \( MPR:orig -channel R -separate \)                 \
        \( MPR:orig -channel R -separate -threshold 100% \) \
        \( MPR:orig -channel G -separate -negate \)         \
        -combine PNG32:result.png
    

    Note by the way that magick convert ... is pretty much always wrong.


    If it doesn't work, you can output each channel at each stage to see which is wrong like this:

    magick texture.png -write MPR:orig -channel B -separate -write newR.png    \
        \( MPR:orig -channel R -separate                    -write newG.png \) \
        \( MPR:orig -channel R -separate -threshold 100%    -write newB.png \) \
        \( MPR:orig -channel G -separate -negate            -write newA.png \) \
        -combine PNG32:result.png