generate-texture-normals.sh 5.8 KB

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