Replace travis with github actions (#9641)
[oweals/minetest.git] / util / generate-texture-normals.sh
1 #!/bin/bash
2
3 # This script generates normalmaps using The GIMP to do the heavy lifting.
4 # give any unrecognized switch (say, -h) for usage info.
5
6 rm /tmp/normals_filelist.txt
7
8 numprocs=6
9
10 skiptools=false
11 skipinventory=false
12 invresolution=64
13 dryrun=false
14 pattern="*.png *.jpg"
15
16 filter=0
17 scale=8
18 wrap=0
19 heightsource=0
20 conversion=0
21 invertx=0
22 inverty=0
23
24 while test -n "$1"; do
25         case "$1" in
26                 --scale|-s)
27                         if [ -z "$2" ] ; then echo "Missing scale parameter"; exit 1; fi
28                         scale=$2
29                         shift
30                         shift
31                         ;;
32                 --pattern|-p)
33                         if [ -z "$2" ] ; then echo "Missing pattern parameter"; exit 1; fi
34                         pattern=$2
35                         shift
36                         shift
37                         ;;
38                 --skiptools|-t)
39                         skiptools=true
40                         shift
41                         ;;
42                 --skipinventory|-i)
43                         if [[ $2 =~ ^[0-9]+$ ]]; then
44                                 invresolution=$2
45                                 shift
46                         fi
47                         skipinventory=true
48                         shift
49                         ;;
50                 --filter|-f)
51                         if [ -z "$2" ] ; then echo "Missing filter parameter"; exit 1; fi
52
53                         case "$2" in
54                                 sobel3|1)
55                                         filter=1
56                                         ;;
57                                 sobel5|2)
58                                         filter=2
59                                         ;;
60                                 prewitt3|3)
61                                         filter=3
62                                         ;;
63                                 prewitt5|4)
64                                         filter=4
65                                         ;;
66                                 3x3|5)
67                                         filter=5
68                                         ;;
69                                 5x5|6)
70                                         filter=6
71                                         ;;
72                                 7x7|7)
73                                         filter=7
74                                         ;;
75                                 9x9|8)
76                                         filter=8
77                                         ;;
78                                 *)
79                                         filter=0
80                                         ;;
81                         esac
82
83                         shift
84                         shift
85                         ;;
86                 --heightalpha|-a)
87                         heightsource=1
88                         shift
89                         ;;
90                 --conversion|-c)
91                                 if [ -z "$2" ] ; then echo "Missing conversion parameter"; exit 1; fi
92
93                                 case "$2" in
94                                         biased|1)
95                                                 conversion=1
96                                                 ;;
97                                         red|2)
98                                                 conversion=2
99                                                 ;;
100                                         green|3)
101                                                 conversion=3
102                                                 ;;
103                                         blue|4)
104                                                 conversion=4
105                                                 ;;
106                                         maxrgb|5)
107                                                 conversion=5
108                                                 ;;
109                                         minrgb|6)
110                                                 conversion=6
111                                                 ;;
112                                         colorspace|7)
113                                                 conversion=7
114                                                 ;;
115                                         normalize-only|8)
116                                                 conversion=8
117                                                 ;;
118                                         heightmap|9)
119                                                 conversion=9
120                                                 ;;
121                                         *)
122                                                 conversion=0
123                                                 ;;
124                         esac
125
126                         shift
127                         shift
128                         ;;
129                 --wrap|-w)
130                         wrap=1
131                         shift
132                         ;;
133                 --invertx|-x)
134                         invertx=1
135                         shift
136                         ;;
137                 --inverty|-y)
138                         inverty=1
139                         shift
140                         ;;
141                 --dryrun|-d)
142                         dryrun=true
143                         shift
144                         ;;
145                 *)
146                         echo -e "\nUsage:\n"
147                         echo "`basename $0` [--scale|-s <value>] [--filter|-f <string>]"
148                         echo " [--wrap|-w] [--heightalpha|-a] [--invertx|-x] [--inverty|-y]"
149                         echo " [--conversion|-c <string>] [--skiptools|-t] [--skipinventory|-i [<value>]]"
150                         echo " [--dryrun|-d] [--pattern|-p <pattern>]"
151                         echo -e "\nDefaults to a scale of 8, checking all files in the current directory, and not"
152                         echo "skipping apparent tools or inventory images.  Filter, if specified, may be one"
153                         echo "of: sobel3, sobel5, prewitt3, prewitt5, 3x3, 5x5, 7x7, or 9x9, or a value 1"
154                         echo "through 8 (1=sobel3, 2=sobel5, etc.). Defaults to 0 (four-sample).  The height"
155                         echo "source is taken from the image's alpha channel if heightalpha is specified.\n"
156                         echo ""
157                         echo "If inventory skip is specified, an optional resolution may also be included"
158                         echo "(default is 64).  Conversion can be one of: biased, red, green, blue, maxrgb,"
159                         echo "minrgb, colorspace, normalize-only, heightmap or a value from 1 to 9"
160                         echo "corresponding respectively to those keywords.  Defaults to 0 (simple"
161                         echo "normalize) if not specified.  Wrap, if specified, enables wrapping of the"
162                         echo "normalmap around the edges of the texture (defaults to no).  Invert X/Y"
163                         echo "reverses the calculated gradients  for the X and/or Y dimensions represented"
164                         echo "by the normalmap (both default to non-inverted)."
165                         echo ""
166                         echo "The pattern, can be an escaped pattern string such as \*apple\* or"
167                         echo "default_\*.png or similar (defaults to all PNG and JPG images in the current"
168                         echo "directory that do not contain \"_normal\" or \"_specular\" in their filenames)."
169                         echo ""
170                         echo "If set for dry-run, the actions this script will take will be printed, but no"
171                         echo "images will be generated.  Passing an invalid value to a switch will generally"
172                         echo "cause that switch to revert to its default value."
173                         echo ""
174                         exit 1
175                         ;;
176         esac
177 done
178
179 echo -e "\nProcessing files based on pattern \"$pattern\" ..."
180
181 normalMap()
182 {
183         out=`echo "$1" | sed 's/.png/_normal.png/' | sed 's/.jpg/_normal.png/'`
184
185         echo "Launched process to generate normalmap: \"$1\" --> \"$out\"" >&2
186
187         gimp -i -b "
188                 (define
189                         (normalMap-fbx-conversion fileName newFileName filter nscale wrap heightsource conversion invertx inverty)
190                         (let*
191                                 (
192                                         (image (car (gimp-file-load RUN-NONINTERACTIVE fileName fileName)))
193                                         (drawable (car (gimp-image-get-active-layer image)))
194                                         (drawable (car (gimp-image-flatten image)))
195                                 )
196                                 (if (> (car (gimp-drawable-type drawable)) 1)
197                                         (gimp-convert-rgb image) ()
198                                 )
199
200                                 (plug-in-normalmap 
201                                         RUN-NONINTERACTIVE
202                                         image
203                                         drawable
204                                         filter
205                                         0.0
206                                         nscale
207                                         wrap
208                                         heightsource
209                                         0
210                                         conversion
211                                         0
212                                         invertx
213                                         inverty
214                                         0
215                                         0.0
216                                         drawable)
217                                 (gimp-file-save RUN-NONINTERACTIVE image drawable newFileName newFileName)
218                                 (gimp-image-delete image)
219                         )
220                 )
221                 (normalMap-fbx-conversion \"$1\" \"$out\" $2 $3 $4 $5 $6 $7 $8)" -b '(gimp-quit 0)'
222 }
223
224 export -f normalMap
225
226 for file in `ls $pattern |grep -v "_normal.png"|grep -v "_specular"` ; do
227
228         invtest=`file "$file" |grep "$invresolution x $invresolution"`
229         if $skipinventory && [ -n "$invtest" ] ; then
230                 echo "Skipped presumed "$invresolution"px inventory image: $file" >&2
231                 continue
232         fi
233
234         tooltest=`echo "$file" \
235                 | grep -v "_tool" \
236                 | grep -v "_shovel" \
237                 | grep -v "_pick" \
238                 | grep -v "_axe" \
239                 | grep -v "_sword" \
240                 | grep -v "_hoe" \
241                 | grep -v "bucket_"`
242
243         if $skiptools && [ -z "$tooltest" ] ; then
244                 echo "Skipped presumed tool image: $file" >&2
245                 continue
246         fi
247
248         if $dryrun ; then
249                 echo "Would have generated a normalmap for $file" >&2
250                 continue
251         else
252                 echo \"$file\" $filter $scale $wrap $heightsource $conversion $invertx $inverty
253         fi
254 done | xargs -P $numprocs -n 8 -I{} bash -c normalMap\ \{\}\ \{\}\ \{\}\ \{\}\ \{\}\ \{\}\ \{\}\ \{\}
255