7533002638769c8566e9bd6bc95375d6d827d12c
[xonotic/xonotic.git] / misc / tools / shadescript / shade.sh
1 #!/bin/sh
2
3 case "$#" in
4         2)
5                 ;;
6         *)
7                 echo "Usage: $1 directory shader.template"
8                 exit 1
9                 ;;
10 esac
11
12 LF="
13 "
14
15 exec 3>"scripts/$1.shader"
16 template=`cat "$2"`
17
18 find "textures/$1" -type f -path "textures/*/*/*.*" -not -name '*_norm.*' -not -name '*_glow.*' -not -name '*_gloss.*' -not -name '*_reflect.*' -not -name '*.xcf' | while IFS= read -r F; do
19         F=${F%.*}
20
21         noLightmap=false
22         isLiquid=false
23         isTransparent=false
24         bounceScale=1.00
25         shaderString="$template"
26         shaderHead=
27         shaderTail=
28         shaderQUI=
29         shaderDiffuse=
30         diffuseExtra=
31
32         case "$F" in
33                 *decal*)
34                         noLightmap=true
35                         ;;
36         esac
37
38         # material type
39         case "$F" in
40                 *water*)
41                         noLightmap=true
42                         isLiquid=true
43                         shaderHead="$shaderHead surfaceparm trans\n     surfaceparm water\n     qer_trans 20\n"
44                         ;;
45                 *slime*)
46                         noLightmap=true
47                         isLiquid=true
48                         shaderHead="$shaderHead surfaceparm trans\n     surfaceparm slime\n     qer_trans 20\n"
49                         ;;
50                 *lava*)
51                         noLightmap=true
52                         isLiquid=true
53                         shaderHead="$shaderHead surfaceparm trans\n     surfaceparm lava\n      qer_trans 20\n"
54                         ;;
55                 *glass*)
56                         noLightmap=true
57                         shaderHead="$shaderHead surfaceparm trans\n"
58                         diffuseExtra="$diffuseExtra             blendfunc add\n"
59                         ;;
60                 *metal*)
61                         bounceScale=`echo "$bounceScale + 0.25" | bc -l`
62                         shaderHead="$shaderHead         surfaceparm metalsteps\n"
63                         ;;
64         esac
65
66         # what is it used for
67         case "$F" in
68                 *grate*)
69                         bounceScale=`echo "$bounceScale + 0.25" | bc -l`
70                         shaderHead="$shaderHead surfaceparm trans\n"
71                         diffuseExtra="$diffuseExtra             blendfunc blend\n"
72                         ;;
73         esac
74
75         # further properties
76         case "$F" in
77                 *shiny*)
78                         bounceScale=`echo "$bounceScale + 0.25" | bc -l`
79                         ;;
80         esac
81         case "$F" in
82                 *dirt*|*terrain*|*old*)
83                         bounceScale=`echo "$bounceScale - 0.25" | bc -l`
84                         shaderHead="$shaderHead surfaceparm dust\n"
85                         ;;
86         esac
87
88         shaderDiffuse="$F"
89         if [ -f "$F""_gloss.tga" ] || [ -f "$F""_gloss.jpg" ] || [ -f "$F""_gloss.png" ]; then
90                 bounceScale=`echo "$bounceScale - 0.25" | bc -l`
91         fi
92
93         if [ -f "$F""_qei.tga" ] || [ -f "$F""_qei.jpg" ] || [ -f "$F""_qei.png" ]; then
94                 shaderQUI="$F""_qei"
95         else
96                 shaderQUI="$F"
97         fi
98
99         if ! $noLightmap; then
100                 shaderTail="    {\n             map \$lightmap\n                rgbGen identity\n               tcGen lightmap\n                blendfunc filter\n      }"
101         fi
102         case "$bounceScale" in
103                 1|1.0|1.00)
104                         ;;
105                 *)
106                         shaderHead="$shaderHead q3map_bouncescale $bounceScale\n"
107                         ;;
108         esac
109
110         shaderName="`echo "$F" | cut -d / -f 1-2`/`echo "$F" | cut -d / -f 3`-`echo "$F" | cut -d / -f 4`"
111         echo "$shaderString$LF$LF" | sed -e "
112                 s,%shader_name%,$shaderName,g;
113                 s,%qei_name%,$shaderQUI,g;
114                 s,%shader_head%,$shaderHead,g;
115                 s,%diffuse_map%,$shaderDiffuse,g;
116                 s,%diffuse_map_extra%,$diffuseExtra,g;
117                 s,%shader_tail%,$shaderTail,g;
118         " >&3
119 done