CRLF fixes, .gitattributes file updated
[xonotic/xonotic.git] / misc / tools / shadescript / shade.vbs
1 option explicit 
2
3
4 Const ForReading = 1
5 Const ForWriting = 2
6 Const ForAppending = 8
7
8 dim CRLF 
9
10 CRLF = Chr(13) & Chr(10)
11
12 dim scriptFullName, scriptPath 
13 scriptFullName = WScript.ScriptFullName
14 scriptPath = Left ( scriptFullName, InStrRev ( scriptFullName, WScript.ScriptName) - 1 )
15
16 dim objFSO, setfolder, re, shader, objShaderTemplate, shaderTemplate
17 Set objFSO = CreateObject("Scripting.FileSystemObject")
18 Set setfolder = objFSO.GetFolder(scriptPath)
19 Set re = New RegExp
20
21 re.IgnoreCase = True
22 re.Global = True
23 re.Pattern = ""
24
25 Set shader = objFSO.OpenTextFile (scriptPath & "\" & setfolder.name & ".shader", ForWriting, True)
26
27 Set objShaderTemplate = objFSO.OpenTextFile(scriptPath & "\template.shader", ForReading)
28 shaderTemplate = objShaderTemplate.ReadAll()
29
30 function filetitle(sfilename)
31         filetitle = Left(sfilename, len(sfilename) - 4)
32 end function 
33
34 dim noLightmap, isLiquid, isTransparent, bounceScale, shaderString
35 dim shaderHead, shaderTail, shaderQUI, shaderDiffuse, diffuseExtra
36 dim subfold, texfile, fn
37
38 For Each subfold in setfolder.Subfolders
39         ' shader.write "Folder: " & subfold.Name & CRLF
40         For Each texfile In subfold.Files
41                 'Defaults               
42                 noLightmap              = false
43                 isLiquid                = false
44                 isTransparent   = false
45                 bounceScale     = 1
46                 shaderString    = shaderTemplate
47                 
48                 shaderHead              = ""
49                 shaderTail              = ""
50                 shaderQUI               = ""
51                 shaderDiffuse   = ""
52                 diffuseExtra    = ""
53                 
54                 ' First ignore any extra map or Thumbs.db
55                 re.Pattern = "_bump.|_gloss.|_norm.|_glow.|Thumbs.db"
56                 if not re.test(texfile.name) then                       
57                         re.pattern = "decal"
58                         if re.test(texfile.name) then
59                                 noLightmap = true
60                         end if
61                         
62                         re.pattern = "water"
63                         if re.test(texfile.name) then
64                                 noLightmap = true
65                                 isLiquid = true
66                                 shaderHead = shaderHead & "     surfaceparm trans" & CRLF
67                                 shaderHead = shaderHead & "     surfaceparm water" & CRLF
68                                 shaderHead = shaderHead & "     qer_trans 20" & CRLF
69                                 diffuseExtra = "blendfunc blend"
70                         end if
71                         
72                         re.pattern = "slime"
73                         if re.test(texfile.name) then
74                                 noLightmap = true
75                                 isLiquid = true
76                                 shaderHead = shaderHead & "     surfaceparm trans" & CRLF
77                                 shaderHead = shaderHead & "     surfaceparm slime" & CRLF
78                                 shaderHead = shaderHead & "     qer_trans 20" & CRLF
79                         end if
80                         
81                         re.pattern = "lava"
82                         if re.test(texfile.name) then
83                                 noLightmap = true
84                                 isLiquid = true
85                                 shaderHead = shaderHead & "     surfaceparm trans" & CRLF
86                                 shaderHead = shaderHead & "     surfaceparm lava" & CRLF
87                                 shaderHead = shaderHead & "     qer_trans 20" & CRLF
88                                 diffuseExtra = "                blendfunc add"
89                         end if
90                         
91                         re.pattern = "glass"
92                         if re.test(texfile.name) then
93                                 noLightmap = true
94                                 shaderHead = shaderHead & "     surfaceparm trans" & CRLF
95                                 diffuseExtra = "                blendfunc add"
96                         end if
97                         
98                         re.pattern = "metal"
99                         if re.test(texfile.name) then
100                                 bounceScale = bounceScale + 0.25
101                                 shaderHead = shaderHead & "     surfaceparm metalsteps" & CRLF
102                         end if
103                         
104                         re.pattern = "grate"
105                         if re.test(texfile.name) then
106                                 bounceScale = bounceScale + 0.25
107                                 shaderHead = shaderHead & "     surfaceparm trans" & CRLF
108                                 diffuseExtra = "                blendfunc blend"
109                         end if
110                         
111                         re.pattern = "shiny"
112                         if re.test(texfile.name) then
113                                 bounceScale = bounceScale + 0.25
114                         end if
115                         
116                         re.pattern = "dirt|terrain|old"
117                         if re.test(texfile.name) then
118                                 bounceScale = bounceScale - 0.25
119                                 shaderHead = shaderHead & "     surfaceparm dust" & CRLF
120                         end if
121                         
122                         shaderDiffuse = "textures/" & setfolder.name & "/" & subfold.name & "/" & texfile.name
123                         
124                         fn = scriptPath & "/" & subfold.name & "/" & filetitle(texfile.name) & "_gloss.tga" 
125                         if objFSO.FileExists(fn) Then
126                                 bounceScale = bounceScale + 0.25
127                         end if
128                         
129                         fn = scriptPath & "/" & subfold.name & "/" & filetitle(texfile.name) & "_qei.tga" 
130                         if objFSO.FileExists(fn) Then
131                                 shaderQUI = "textures/" & setfolder.name & "/" & subfold.name & "/" & filetitle(texfile.name) & "_qei.tga" 
132                         else
133                                 shaderQUI = shaderDiffuse
134                         end if
135
136                         if not noLightmap then 
137                                 shaderTail = "  {" & CRLF & "           map $lightmap" & CRLF & "               rgbGen identity" & CRLF & "             tcGen lightmap" & CRLF & "              blendfunc filter" & CRLF & "    }"
138                         end if
139                         
140                         if not bounceScale = 1 then 
141                                 re.pattern = ","
142                                 shaderHead = shaderHead & "     q3map_bounceScale " & re.Replace(bounceScale, ".") & CRLF
143                         end if
144                                 
145                         re.Pattern = "%shader_name%"
146                         shaderString = re.Replace(shaderTemplate, "textures/" & setfolder.name & "/" & subfold.name & "-" & filetitle(texfile.name))
147
148                         re.Pattern = "%qei_name%"
149                         shaderString = re.Replace(shaderString, shaderQUI)
150
151                         re.Pattern = "%shader_head%"
152                         shaderString = re.Replace(shaderString, shaderHead)
153                         
154                         re.Pattern = "%diffuse_map%"
155                         shaderString = re.Replace(shaderString, shaderDiffuse)
156                         
157                         re.Pattern = "%diffuse_map_extra%"
158                         shaderString = re.Replace(shaderString, diffuseExtra)
159
160                         re.Pattern = "%shader_tail%"
161                         shaderString = re.Replace(shaderString, shaderTail)
162                         
163
164                         
165                         shader.write shaderString & CRLF & CRLF
166                 end if
167         Next
168 Next
169 shader.Close
170
171
172