Page 1 of 1

Combine Normal Maps

Posted: 2020-02-25T00:11:36-07:00
by nmrugg
Does anyone know how to combine normal maps in ImageMagick?

I found some tutorials showing how to do it in Photoshop, but I can't figure out how to do it with ImageMagick.

https://www.moddb.com/tutorials/photosh ... ormal-maps
https://www.youtube.com/watch?v=t8jvroWSCBU

Any help would be appreciated. :)

Re: Combine Normal Maps

Posted: 2020-02-25T15:47:18-07:00
by nmrugg
I figured out the first two steps

Code: Select all

convert "$IN" -channel B +level 0,0 -channel R -level 50%,100% +level 50%,100% -channel G -level 50%,100% +level 50%,100% add1.png
convert "$IN" -channel R -level 0%,50% +level 0%,50% -channel G -level 0%,50% +level 0%,50% sub1.png
What I can't figure out is the the channel mixing part. Is there a way to do that in ImageMagick?

Specifically this part:
Go to Image -> Adjustments -> Channel Mixer
Set the red channel to -100% RED, and it's constant to +50%
Set the green channel to -100% GREEN, and it's constant to +50%
Set the blue channel to -100% BLUE, and it's constant to 100%
(Also, I'm using ImageMagick 6.8.9-9, but I could change versions if necessary.)

Re: Combine Normal Maps

Posted: 2020-02-25T16:14:20-07:00
by fmw42
see -color-matrix

Unix syntax:

Code: Select all

convert image -color-matrix " \
-1 0 0 0, 0, 0.5 \
0 -1 0 0, 0, 0.5 \
0 0 -1 0, 0, 1 \
0 0  0 0, 0, 0 \
0 0  0 0, 0, 0 \
0 0  0 0, 0, 0" \
result
should reproduce your PS channel mixer result.

Re: Combine Normal Maps

Posted: 2020-02-25T20:23:15-07:00
by nmrugg
Thanks! That worked!

I think I got it all working now. Here's the bash script I came up with:

Code: Select all

IN1=$1
IN2=$2
OUT=${3:-'merged.png'}
TEMP_DIR=$(mktemp -d)

convert "$IN2" -channel B +level 0,0 -channel R -level 50%,100% +level 50%,100% -channel G -level 50%,100% +level 50%,100% $TEMP_DIR/add1.miff
convert "$IN2" -channel R -level 0%,50% +level 0%,50% -channel G -level 0%,50% +level 0%,50% $TEMP_DIR/sub1.miff

convert $TEMP_DIR/sub1.miff -color-matrix " \
-1 0 0 0, 0, 0.5 \
0 -1 0 0, 0, 0.5 \
0 0 -1 0, 0, 1 \
0 0  0 0, 0, 0 \
0 0  0 0, 0, 0 \
0 0  0 0, 0, 0" \
$TEMP_DIR/sub2.miff

convert $TEMP_DIR/add1.miff -color-matrix " \
1 0 0 0, 0, -0.5 \
0 1 0 0, 0, -0.5 \
0 0 0 0, 0, 0 \
0 0 0 0, 0, 0 \
0 0 0 0, 0, 0 \
0 0 0 0, 0, 0" \
$TEMP_DIR/add2.miff

convert $TEMP_DIR/sub2.miff -channel Red,Green -separate -background black -combine +channel $TEMP_DIR/subRG.miff
convert $TEMP_DIR/sub2.miff -channel Blue -separate -background black -combine +channel $TEMP_DIR/subB.miff

convert $TEMP_DIR/subRG.miff "$IN1" -compose difference -composite $TEMP_DIR/subB.miff -compose exclusion -composite $TEMP_DIR/add2.miff -compose linearDodge -composite $OUT

rm -R ${TEMP_DIR}

Re: Combine Normal Maps

Posted: 2020-02-25T22:29:26-07:00
by fmw42
You should consider combining your convert statements into fewer ones. This is untested.

I have save images to mpr: in-memory format for ease of use, since they can be named (as opposed to -clones). I have probably save too many, but it makes it easier to read. I have used parentheses, but some may not be needed. But again it makes it easier to read.

Code: Select all

convert "$IN2" +write mpr:in2 +delete \
\( mpr:in2 \
    -channel R -level 0%,50% +level 0%,50% \
    -channel G -level 0%,50% +level 0%,50% \
    -color-matrix " \
    -1 0 0 0, 0, 0.5 \
    0 -1 0 0, 0, 0.5 \
    0 0 -1 0, 0, 1 \
    0 0  0 0, 0, 0 \
    0 0  0 0, 0, 0 \
    0 0  0 0, 0, 0" \
    -write mpr:sub2 +delete \) \
\( mpr:sub2 \
    -channel Red,Green -separate -background black \
    -combine +channel +write mpr:subRG +delete \) \
\( mpr:sub2 \
    -channel Blue -separate -background black \
    -combine +channel +write mpr:subB +delete \) \
\( mpr:in2 \
    -channel B +level 0,0 \
    -channel R -level 50%,100% +level 50%,100% \
    -channel G -level 50%,100% +level 50%,100% \
    -color-matrix " \
    1 0 0 0, 0, -0.5 \
    0 1 0 0, 0, -0.5 \
    0 0 0 0, 0, 0 \
    0 0 0 0, 0, 0 \
    0 0 0 0, 0, 0 \
    0 0 0 0, 0, 0" \
    +write mpr:add2 +delete \) \
mpr:subRG "$IN1" -compose difference -composite \
    mpr:subB -compose exclusion -composite \
    mpr:add2 -compose linearDodge -composite \
$OUT
See

https://imagemagick.org/Usage/basics/#parenthesis
https://imagemagick.org/Usage/files/#mpr

Re: Combine Normal Maps

Posted: 2020-02-25T23:52:09-07:00
by nmrugg
Cool. Thanks. I didn't know about that.

The image that produces is not quite right, however. It's very green. I haven't had time to investigate what's wrong.

Re: Combine Normal Maps

Posted: 2020-02-26T10:36:21-07:00
by fmw42
Put in +write tmpX.png at various places, especially just before the closing parens to check if they produce the same results as your separate convert commands.