fix Port-O-Launch
[divverent/nexuiz.git] / misc / autoshader.sh
1 #!/bin/sh
2
3 if ! [ -d "textures" ] && ! [ -d "env" ]; then
4         echo "Sorry, must be run from a directory with a textures subfolder. Giving up."
5         exit 1
6 fi
7
8 case "$0" in
9         */*)
10                 mydir=${0%/*}
11                 ;;
12         *)
13                 mydir=.
14                 ;;
15 esac
16
17 makeshader()
18 {
19         s=`texnormalize "$1"`
20         dir=${s#textures/}
21         dir=${dir%%/*}
22         echo scripts/$dir.shader
23         mkdir -p scripts
24         cat <<EOF >>"scripts/$dir.shader"
25 $s
26 {
27         qer_editorimage $1
28         qer_trans 0.5
29         // maybe: surfaceparm nonsolid
30         surfaceparm trans
31         surfaceparm alphashadow
32         surfaceparm nomarks
33         cull disable
34         {
35                 map $s
36                 blendfunc blend
37                 // or: alphafunc GE128
38         }
39         {
40                 map \$lightmap
41                 blendfunc filter
42                 rgbGen identity
43         }
44 }
45 EOF
46 }
47
48 makeskyshader()
49 {
50         coords=`sh "$mydir/brightspot.sh" "$1"`
51         s=`texnormalize "$1"`
52         case "$coords" in
53                 *\ *)
54                         ;;
55                 *)
56                         coords="-42 -42"
57                         echo >&2 "NOTE: brightspot tool did not work"
58                         ;;
59         esac
60         s=${s%_up}
61         s=${s#env/}
62         dir=${s%%/*}
63         echo >&2 "appending to scripts/$dir.shader"
64         echo scripts/$dir.shader
65         mkdir -p scripts
66         cat <<EOF >>"scripts/$dir.shader"
67 textures/$s
68 {
69         qer_editorimage $1
70         surfaceparm noimpact
71         surfaceparm nolightmap
72         surfaceparm sky
73         surfaceparm nomarks
74         q3map_sunExt .5 .5 .7 $coords 2 16 // red green blue intensity degrees elevation deviance samples
75         q3map_surfacelight 150 // intensity
76         skyparms env/$s - -
77 }
78 EOF
79 }
80
81 texnormalize()
82 {
83         echo "$1" | sed 's/\.[Jj][Pp][Gg]$\|\.[Tt][Gg][Aa]$\|\.[Pp][Nn][Gg]$//;'
84 }
85
86 allshadernames() # prints all shader names or texture names
87 {
88         cat scripts/*.shader 2>/dev/null | tr '\r' '\n' | {
89                 mode=root
90                 while IFS= read -r LINE; do
91                         LINE=`echo "$LINE" | sed 's,//.*,,; s/\s\+/ /g; s/^ //; s/ $//; s/"//g;'`
92                         [ -n "$LINE" ] || continue
93                         set -- $LINE
94                         case "$mode:$1" in
95                                 root:'{')
96                                         mode=shader
97                                         ;;
98                                 root:*)
99                                         texnormalize "$1"
100                                         ;;
101
102                                 shader:'{')
103                                         mode=stage
104                                         ;;
105                                 shader:'}')
106                                         mode=root
107                                         ;;
108                                 shader:skyparms)
109                                         echo "`texnormalize "$1"`_up"
110                                         ;;
111
112                                 stage:'}')
113                                         mode=shader
114                                         ;;
115                                 stage:map)
116                                         texnormalize "$2"
117                                         ;;
118                                 stage:clampmap)
119                                         texnormalize "$2"
120                                         ;;
121                                 stage:animmap)
122                                         shift
123                                         shift
124                                         for X in "$@"; do
125                                                 texnormalize "$X"
126                                         done
127                                         ;;
128                         esac
129                 done
130         }
131 }
132
133 allshaders=`allshadernames`
134 lf="
135 "
136
137 has_shader()
138 {
139         sh=`texnormalize "$1"`
140         case "$lf$allshaders$lf" in
141                 *"$lf$sh$lf"*)
142                         return 0
143                         ;;
144         esac
145         return 1
146 }
147
148 has_alpha()
149 {
150         convert "$1" -depth 8 RGBA:- | xxd -c 4 -g 1 | grep -v " ff  " >/dev/null
151 }
152
153 autoshaders()
154 {
155         {
156                 [ -d "textures" ] && find textures -type f \( -iname \*.tga -o -iname \*.png \) -print | while IFS= read -r TEX; do
157                         case `texnormalize "$TEX"` in
158                                 *_norm|*_shirt|*_pants|*_glow|*_gloss|*_bump)
159                                         # ignore these (they are used implicitly)
160                                         continue
161                                         ;;
162                         esac
163                         if has_shader "$TEX"; then
164                                 echo>&2 "    $TEX has an associated shader, ignoring."
165                         else
166                                 if has_alpha "$TEX"; then
167                                         echo>&2 "*** $TEX has alpha but no shader, creating default alpha shader."
168                                         makeshader "$TEX"
169                                 else
170                                         echo>&2 "    $TEX has no shader and no alpha, fine."
171                                 fi
172                         fi
173                 done
174                 [ -d "env" ] && find env -type f \( -iname \*_up.tga -o -iname \*_up.png -o -iname \*_up.jpg \) -print | while IFS= read -r TEX; do
175                         if has_shader "$TEX"; then
176                                 echo>&2 "    $TEX has an associated shader, ignoring."
177                         else
178                                 echo>&2 "*** $TEX is sky but has no shader, creating default sky shader."
179                                 makeskyshader "$TEX"
180                         fi
181                 done
182         } | sort -u
183 }
184
185 aashaders=`autoshaders`
186
187 if [ -n "$aashaders" ]; then
188         cat <<EOF
189 The following shader files have been automatically created or appended to:
190
191 $aashaders
192
193 Please edit them to your needs, and possibly rename them.
194 EOF
195 fi