-
Notifications
You must be signed in to change notification settings - Fork 2
/
sloth.py
executable file
·1274 lines (971 loc) · 40.9 KB
/
sloth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# Copyright 2014 Maximilian Stahlberg
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys, os, re, argparse, copy, configparser
from PIL import Image
defaultSuffixes = {
"diffuse": "_d",
"normal": "_n",
"normalheight": "_nh",
"height": "_h",
"physical": "_orm",
"specular": "_s",
"addition": "_a",
"preview": "_p",
}
class VfsPathBuilder():
def __init__(self, aliases, shader):
self.aliases = aliases
self.shader = shader
self.path = shader["relpath"]+"/"
self.ext = shader["ext"]
def getVfsPath(self, maptype):
basename = self.shader[maptype]
ext = self.ext[maptype]
name = basename+ext
if name in self.aliases.keys():
return os.path.splitext(self.aliases[name])[0]
else:
return self.path+basename
class ShaderGenerator(dict):
# valid color format
colorRE = re.compile("^[0-9a-f]{6}$")
binaryAlphaRE = re.compile(b"^[\x00\xff]*$")
# mapping from surfaceparm values to words that trigger their use when keyword guessing is enabled
surfaceParms = \
{
"donotenter": ["lava", "slime"],
"dust": ["sand", "dust"],
"flesh": ["flesh", "meat", "organ"],
"ladder": ["ladder"],
"lava": ["lava"],
"metalsteps": ["metal", "steel", "iron", "tread", "grate"],
"slick": ["ice"],
"slime": ["slime"],
"water": ["water"],
}
defaultRenderer = "xreal"
supportedRenderers = ("quake3", defaultRenderer, "daemon")
# extension for (per-shader) option files
slothFileExt = ".sloth"
# basename of the per-set option file
defaultSlothFile = "options"+slothFileExt
# common file extensions for image source
extBlackList = \
[
".ora",
".psd",
".xcf",
]
def __init__(self, verbosity = 0):
self.verbosity = verbosity
self.sets = dict() # set name -> shader name -> key -> value
self.header = "" # header to be prepended to output
self.suffixes = dict() # map type -> suffix
self.aliases = dict() # name → VFS path
self.setSuffixes()
# default options that can be overwritten on a per-directory/shader basis
self["options"] = dict()
self["options"]["lightColors"] = dict() # color name -> RGB color triple
self["options"]["customLights"] = dict() # intensity name -> intensity; for grayscale addition maps
self["options"]["predefLights"] = dict() # intensity name -> intensity; for non-grayscale addition maps
self["options"]["additionGrayscale"] = False # wether to consider the addition maps are gray without checking
self["options"]["precalcColors"] = False # whether to precalculate fixed light colors
self["options"]["guessKeywords"] = False # whether to try to guess additional keywords based on shader (meta)data
self["options"]["radToAddExp"] = 1.0 # exponent used to convert radiosity RGB values into addition map color modifiers
self["options"]["heightNormalsMod"] = 1.0 # modifier used when generating normals from height maps
self["options"]["editorOpacity"] = 1.0 # in-editor opacity of transparent shaders
self["options"]["alphaTest"] = None # whether to use an alphaFunc/alphaTest keyword or smooth blending (default)
self["options"]["alphaShadows"] = True # whether to add the alphashadows surfaceparm keyword to relevant shaders
self["options"]["renderer"] = self.defaultRenderer
#############
# DEBUGGING #
#############
def verbose(self, text):
if self.verbosity > 0:
print(text, file = sys.stderr)
def debug(self, text):
if self.verbosity > 1:
print(text, file = sys.stderr)
def error(self, text):
print("Error: "+text, file = sys.stderr)
##################
# GLOBAL OPTIONS #
##################
def setHeader(self, text):
"Sets a header text to be put at the top of the shader file."
self.header = text
def setSuffixes(self, diffuse = defaultSuffixes["diffuse"], normal = defaultSuffixes["normal"], normalheight = defaultSuffixes["normalheight"], height = defaultSuffixes["height"], physical = defaultSuffixes["physical"], specular = defaultSuffixes["specular"], addition = defaultSuffixes["addition"], preview = defaultSuffixes["preview"]):
"Sets the filename suffixes for the different texture map types."
self.suffixes["diffuse"] = diffuse
self.suffixes["normal"] = normal
self.suffixes["normalheight"] = normalheight
self.suffixes["height"] = height
self.suffixes["physical"] = physical
self.suffixes["specular"] = specular
self.suffixes["addition"] = addition
self.suffixes["preview"] = preview
def readConfig(self, fp):
self.debug("Parsing global options file...")
self.__parseSlothFile(self, fp)
######################
# PER-SHADER OPTIONS #
######################
def __setKeywordGuessing(self, value, shader = None):
if not shader:
shader = self
shader["options"]["guessKeywords"] = value
def setKeywordGuessing(self, value = True):
"Whether to try to guess additional keywords based on shader (meta)data"
self.__setKeywordGuessing(value)
def __setRadToAddExponent(self, value, shader = None):
if not shader:
shader = self
shader["options"]["radToAddExp"] = value
def setRadToAddExponent(self, value):
"Set the exponent used to convert radiosity RGB values into addition map color modifiers"
self.__setRadToAddExponent(value)
def __setHeightNormalsMod(self, value, shader = None):
if not shader:
shader = self
shader["options"]["heightNormalsMod"] = value
def setHeightNormalsMod(self, value):
"Set the modifier used when generating normals from height maps"
self.__setHeightNormalsMod(value)
def __setEditorOpacity(self, value, shader = None):
if not shader:
shader = self
if type(value) == float and 0 < value <= 1:
shader["options"]["editorOpacity"] = value
else:
self.error("Editor transparency must be a float in ]0,1].")
def setEditorOpacity(self, value):
"Set the in-editor opacity of transparent shaders"
self.__setEditorOpacity(value)
def __setAlphaTest(self, test, shader = None):
if not shader:
shader = self
if type(test) == float and 0 <= test <= 1:
shader["options"]["alphaTest"] = test
elif type(test) == str and test in ("GT0", "GE128", "LT128"):
shader["options"]["alphaTest"] = test
elif test == None:
shader["options"]["alphaTest"] = None
else:
shader["options"]["alphaTest"] = None
self.error("Alpha test must be either None, a valid string or a float in [0,1].")
def setAlphaTest(self, test):
"Set the alpha test method used, blend smoothly if None."
self.__setAlphaTest(test)
def __setAlphaShadows(self, value, shader = None):
if not shader:
shader = self
shader["options"]["alphaShadows"] = value
def setAlphaShadows(self, value = True):
"Whether to add the alphashadows surfaceparm keyword to relevant shaders"
self.__setAlphaShadows(value)
def __addLightColor(self, name, color, shader = None):
if not shader:
shader = self
if not self.colorRE.match(color):
self.error("Not a valid color: "+color+". Format is [0-9][a-f]{6}.")
return
r = int(color[0:2], 16)
g = int(color[2:4], 16)
b = int(color[4:6], 16)
if name in shader["options"]["lightColors"] and (r, g, b) != shader["options"]["lightColors"][name]:
self.verbose("Overwriting light color "+name+": "+"%02x%02x%02x" % shader["options"]["lightColors"][name]+\
" -> "+color)
shader["options"]["lightColors"][name] = (r, g, b)
def addLightColor(self, name, color):
"Adds a light color with a given name to be used for light emitting shaders."
self.__addLightColor(name, color)
def __addLightIntensity(self, intensity, custom, shader = None):
if not shader:
shader = self
intensity = int(intensity)
if intensity < 0:
self.verbose("Ignoring negative light intensity.")
return
if intensity >= 10000:
name = str(int(intensity / 1000)) + "k"
elif intensity == 0:
name = "norad"
else:
name = str(intensity)
if custom:
shader["options"]["customLights"][name] = intensity
else:
shader["options"]["predefLights"][name] = intensity
def addCustomLightIntensity(self, intensity):
"Adds a light intensity to be used for light emitting shaders with grayscale addition maps."
self.__addLightIntensity(intensity, True)
def addPredefLightIntensity(self, intensity):
"Adds a light intensity to be used for light emitting shaders with non-grayscale addition maps."
self.__addLightIntensity(intensity, False)
def __setAdditionGrayscale(self, value, shader = None):
if not shader:
shader = self
shader["options"]["additionGrayscale"] = value
def __setPrecalcColors(self, value, shader = None):
if not shader:
shader = self
shader["options"]["precalcColors"] = value
def setPrecalcColors(self, value = True):
"Whether to precalculate light colors for light emitting shaders with predefined colors."
self.__setPrecalcColors(value)
def __setRenderer(self, renderer, shader = None):
if not shader:
shader = self
if renderer in self.supportedRenderers:
shader["options"]["renderer"] = renderer
else:
self.error("Renderer "+renderer+" not supported. Supported renderers are "+str(self.supportedRenderers)+".")
def setRenderer(self, renderer):
self.__setRenderer(renderer)
#################
# FUNCTIONALITY #
#################
def __copyOptions(self, source, target):
"Copies initial shader options."
target["options"] = copy.deepcopy(source["options"])
def __parseSlothFile(self, shader, path):
"Parses a per-directory/shader options file. path can also be a file pointer."
config = configparser.ConfigParser(allow_no_value = True)
# be case sensitive
config.sectionsxform = lambda option: option
config.optionxform = lambda option: option
# parse file
try:
if hasattr(path, "read"):
config.read_string(path.read())
else:
with open(path, "r") as fp:
config.read_file(fp)
except IOError:
self.error("Couldn't read "+path+".")
return
except (configparser.ParsingError, configparser.DuplicateOptionError) as error:
self.error(str(error))
return
# parse options
for section in config:
options = config[section]
if section == "options":
for option in options:
if option == "colors":
shader["options"]["lightColors"].clear()
print(path)
for nameAndColor in options[option].split():
print(nameAndColor)
try:
name, color = nameAndColor.split(":")
except ValueError:
continue
self.__addLightColor(name, color, shader)
elif option == "addColors":
for nameAndColor in options[option].split():
try:
name, color = nameAndColor.split(":")
except ValueError:
continue
self.__addLightColor(name, color, shader)
elif option == "predefLights":
shader["options"][option].clear()
for intensity in options["predefLights"].split():
self.__addLightIntensity(int(intensity), False, shader)
elif option == "additionGrayscale":
self.__setAdditionGrayscale(options.getboolean(option), shader)
elif option == "precalcColors":
self.__setPrecalcColors(options.getboolean(option), shader)
elif option == "addPredefLights":
for intensity in options[option].split():
self.__addLightIntensity(int(intensity), False, shader)
elif option == "customLights" in options:
shader["options"][option].clear()
for intensity in options[option].split():
self.__addLightIntensity(int(intensity), True, shader)
elif option == "addCustomLights":
for intensity in options[option].split():
self.__addLightIntensity(int(intensity), True, shader)
elif option == "colorBlendExp":
self.__setRadToAddExponent(options.getfloat(option), shader)
elif option == "alphaFunc":
self.__setAlphaTest(options[option], shader)
elif option == "alphaTest":
self.__setAlphaTest(options.getfloat(option), shader)
elif option == "alphaShadows":
self.__setAlphaShadows(options.getboolean(option), shader)
elif option == "heightNormalsMod":
self.__setHeightNormalsMod(options.getfloat(option), shader)
elif option == "editorOpacity":
self.__setEditorOpacity(options.getfloat(option), shader)
elif option == "renderer":
self.__setRenderer(options[option], shader)
else:
self.error("Invalid option "+option+" in section "+section+".")
elif section in ("keywords", "addKeywords", "delKeywords"):
for key, value in config[section].items():
shader["options"].setdefault(section, dict())
if value:
shader["options"][section].setdefault(key, set())
shader["options"][section][key].update(value.split())
else:
shader["options"][section][key] = None
elif section != "DEFAULT":
self.error("Invalid section "+section+".")
def __analyzeMaps(self, shader):
"Retrieves metadata from a shader's maps, such as whether there's an alpha channel on the diffuse map."
# diffuse map
imgpath = shader["abspath"]+os.path.sep+shader["diffuse"]+shader["ext"]["diffuse"]
if os.path.isfile(imgpath):
img = Image.open(imgpath, "r")
# look for transparency
if img.mode in ("RGBA", "LA"):
if img.getextrema()[-1][0] == 255:
self.verbose("Found completely white alpha channel in "+img.filename+".")
shader["meta"]["diffuseAlpha"] = False
else:
shader["meta"]["diffuseAlpha"] = True
elif img.mode == "p" and "transparency" in img.info:
shader["meta"]["diffuseAlpha"] = True
else:
shader["meta"]["diffuseAlpha"] = False
# check if transparency is binary
shader["meta"]["diffuseAlphaBin"] = False
if shader["meta"]["diffuseAlpha"]:
alphaSequence = img.split()[-1].tobytes()
if self.binaryAlphaRE.match(alphaSequence):
shader["meta"]["diffuseAlphaBin"] = True
# addition map
if shader["addition"]:
if shader["options"]["additionGrayscale"]:
shader["meta"]["additionGrayscale"] = True
else:
imgpath = shader["abspath"]+os.path.sep+shader["addition"]+shader["ext"]["addition"]
if os.path.isfile(imgpath):
img = Image.open(imgpath, "r")
gray = ( img.mode in ("L", "LA") )
img = img.convert("RGB")
# check for RGB images with no actual non-gray color
if not gray:
colors = img.getcolors(maxcolors = 256)
if colors:
gray = True
for _, rgb in colors:
if not rgb[0] == rgb[1] == rgb[2]:
gray = False
break
shader["meta"]["additionGrayscale"] = gray
# get average color if needed
if shader["options"]["precalcColors"]:
value = channel = 0
average = [0, 0, 0]
for count in img.histogram():
average[channel] += count * ( value / 0xff )
value += 1
if value > 0xff:
average[channel] /= img.size[0] * img.size[1]
value = 0
channel += 1
if channel == 3:
break
if gray:
shader["meta"]["additionAverage"] = (average[0], average[0], average[0])
else:
shader["meta"]["additionAverage"] = tuple(average)
else:
shader["meta"]["additionGrayscale"] = False
def __addKeywords(self, shader):
"Adds keywords based on knowledge (and potentially assumptions) about the shader (meta)data. Doesn't overwrite existing keywords."
shader.setdefault("keywords", dict())
keywords = shader["keywords"]
options = shader["options"]
# handle transparent diffuse map
if shader["meta"]["diffuseAlpha"]:
keywords.setdefault("surfaceparm", set())
keywords["surfaceparm"].add("trans")
keywords["cull"] = {"none"}
if options["alphaShadows"]:
keywords["surfaceparm"].add("alphashadow")
# attempt to guess additional keywords
if options["guessKeywords"]:
for surfaceParm, words in self.surfaceParms.items():
for word in words:
if word in shader["name"]:
keywords.setdefault("surfaceparm", set())
keywords["surfaceparm"].add(surfaceParm)
# overlay keywords defined in options, overwrite on conflict
if "keywords" in options:
for key, value in options["keywords"].items():
keywords[key] = value
# overlay keywords defined in options, if possible extend on conflict
if "addKeywords" in options:
for key, value in options["addKeywords"].items():
if not key in keywords:
keywords[key] = value
else:
keywords[key].update(value)
# delete specified keywords
if "delKeywords" in options:
for key, value in options["delKeywords"].items():
if key in keywords:
if value == None:
keywords.pop(key)
else:
keywords[key].difference_update(value)
if len(keywords[key]) == 0:
keywords.pop(key)
def __expandLightShaders(self, setname):
"Replaces every shader with an addition map with a set of shaders for each light color/intensity combination "
"(only intensity for non-grayscale addition maps) as well as a not glowing version."
newShaders = dict()
delNames = set()
for shadername in self.sets[setname]:
shader = self.sets[setname][shadername]
if shader["addition"]:
# mark original shader for deletion
delNames.add(shadername)
if shader["meta"]["additionGrayscale"]:
# the addition map is grayscale, use custom light colors
for colorName, (r, g, b) in shader["options"]["lightColors"].items():
for intensityName, intensity in shader["options"]["customLights"].items():
newShader = copy.deepcopy(shader)
newShader["meta"]["lightIntensity"] = intensity
newShader["meta"]["lightColor"] = {"r": r, "g": g, "b": b}
newShaders[shadername+"_"+colorName+"_"+intensityName] = newShader
else:
for intensityName, intensity in shader["options"]["predefLights"].items():
newShader = copy.deepcopy(shader)
newShader["meta"]["lightIntensity"] = intensity
newShaders[shadername+"_"+intensityName] = newShader
# remove addition map from original shader and append "_off" to its name
shader["addition"] = None
newShaders[shadername+"_off"] = shader
# delete old reference to the original
for shadername in delNames:
self.sets[setname].pop(shadername)
# add new shaders (adds back original shader under new name, without addition map)
self.sets[setname].update(newShaders)
def __readAliases(self, path):
filepath = os.path.join(path,"sloth-alias.txt")
if os.path.isfile(filepath):
with open(filepath) as f:
for line in f.readlines():
name, vfspath = line.strip("\n").split(" ")
self.aliases[name] = vfspath
def generateSet(self, path, setname = None, cutextension = None):
"Generates shader data for a given texture source folder."
abspath = os.path.abspath(path)
root = os.path.basename(os.path.abspath(path+os.path.sep+os.path.pardir))
relpath = root+"/"+os.path.basename(abspath)
filelist = os.listdir(abspath)
mapsbytype = dict() # map type -> set of filenames without extentions
mapext = dict() # map name (no extension) -> map filename (with extension)
slothfiles = set() # sloth per-shader config file names (no extension)
self.__readAliases(abspath)
# retrieve all maps by type
for filename in filelist + list(self.aliases):
mapname, ext = os.path.splitext(filename)
# check if this file extension is not among known unsupported ones
if ext.lower() in self.extBlackList:
self.verbose(filename + ": Unsupported format")
continue
if ext == self.slothFileExt:
slothfiles.add(mapname)
else:
for (maptype, suffix) in self.suffixes.items():
mapsbytype.setdefault(maptype, set())
if mapname.endswith(suffix):
mapext[mapname] = ext
mapsbytype[maptype].add(mapname)
# add a new set or extend the current one
if not setname:
if cutextension and len(cutextension) > 0:
setname = relpath.rsplit(cutextension, 1)[0]
else:
setname = relpath
self.sets.setdefault(setname, dict())
# parse per-directory options
options = dict()
self.__copyOptions(self, options)
if self.defaultSlothFile in filelist:
self.debug("Parsing per-directory options file for "+relpath+"...")
self.__parseSlothFile(options, abspath+os.path.sep+self.defaultSlothFile)
# add a shader for each diffuse map
for diffusename in mapsbytype["diffuse"]:
shadername = diffusename.rsplit(self.suffixes["diffuse"], 1)[0]
# add a new shader
shader = self.sets[setname][shadername] = dict()
# copy default options
self.__copyOptions(options, shader)
# init shader data
shader["name"] = shadername
shader["relpath"] = relpath
shader["abspath"] = abspath
shader["diffuse"] = diffusename
shader["ext"] = {"diffuse": mapext[diffusename]}
shader["meta"] = dict()
# attempt to find per-prefix/shader options file
slothname = ""
for pos in range(len(shadername)):
slothname += shadername[pos]
if slothname in slothfiles:
if slothname != shadername:
self.debug("Parsing per-prefix options file for "+shadername+" ("+slothname+"*)...")
else:
self.debug("Parsing per-shader options file for "+shadername+"...")
self.__parseSlothFile(shader, abspath+os.path.sep+slothname+self.slothFileExt)
# attempt to find a map of every known non-diffuse type
# assumes that non-diffuse map names form the start of diffuse map names
for maptype, suffix in self.suffixes.items():
basename = shadername
while basename != "":
mapname = basename+suffix
if mapname in mapsbytype[maptype]:
shader[maptype] = mapname
shader["ext"][maptype] = mapext[mapname]
break
basename = basename[:-1]
if basename == "": # no map of this type found
shader[maptype] = None
shader["ext"][maptype] = None
# retrieve more metadata from the maps
self.__analyzeMaps(shader)
# now that we have enough knowledge about the shader, add keywords
self.__addKeywords(shader)
numVariants = str(len(self.sets[setname]))
# expand relevant shaders into multiple light emitting ones
self.__expandLightShaders(setname)
numShaders = str(len(self.sets[setname]))
self.verbose(setname+": Added "+numShaders+" shaders for "+numVariants+" texture variants.")
def clearSets(self):
"Forgets about all shader data that has been generated."
self.sets.clear()
self.verbose("Cleared all sets.")
def __radToAdd(self, shader, r, g = None, b = None):
"Given light colors, return modified colors to be used in the blend phase of the addition map."
exp = shader["options"]["radToAddExp"]
if g and b:
return (r**exp, g**exp, b**exp)
else:
return r**exp
def getShader(self, setname = None, shadername = None):
"Assembles and returns the shader file content."
content = ""
for line in self.header.splitlines():
if line.startswith("//"):
content += line+"\n"
elif line == "":
content += "//\n"
else:
content += "// "+line+"\n"
if setname:
if setname in self.sets:
setnames = (setname, )
else:
self.error("Unknown set "+str(setname)+".")
return
else:
setnames = self.sets.keys()
for setname in setnames:
if shadername:
if shadername in self.sets[setname]:
names = (shadername, )
else:
continue
else:
content += "\n"+\
"// "+"-"*len(setname)+"\n"+\
"// "+setname+"\n"+\
"// "+"-"*len(setname)+"\n"
names = sorted(self.sets[setname].keys())
# is a stage currently opened?
in_stage = False
for shadername in names:
# prepare content
shader = self.sets[setname][shadername]
stage_keys = ""
vfsPathBuilder = VfsPathBuilder(self.aliases, shader)
getVfsPath = vfsPathBuilder.getVfsPath
# decide on a preview image
if shader["preview"]:
preview = "preview"
elif shader["diffuse"]:
preview = "diffuse"
else:
preview = None
# extract light color if available
if "lightColor" in shader["meta"]:
r = shader["meta"]["lightColor"]["r"] / 0xff
g = shader["meta"]["lightColor"]["g"] / 0xff
b = shader["meta"]["lightColor"]["b"] / 0xff
content += "\n"+setname+"/"+shadername+"\n{\n"
# preview image
if preview:
content += "\tqer_editorImage "+getVfsPath(preview)+"\n"
# in-editor transparency
if shader["meta"]["diffuseAlpha"] and shader["options"]["editorOpacity"] < 1:
content += "\tqer_trans "+"%.2f"%shader["options"]["editorOpacity"]+"\n"
content += "\n"
# keywords
if "keywords" in shader and len(shader["keywords"]) > 0:
for key, value in sorted(shader["keywords"].items()):
if type(value) != str and hasattr(value, "__iter__"):
for value in sorted(value):
content += "\t"+key+" "*max(1, 20-len(key))+str(value)+"\n"
elif value == None:
content += "\t"+key+"\n"
else:
content += "\t"+key+" "*max(1, 20-len(key))+str(value)+"\n"
content += "\n"
# surface light
if "lightIntensity" in shader["meta"] and shader["meta"]["lightIntensity"] > 0:
# intensity
content += "\tq3map_surfaceLight "+"%d" % shader["meta"]["lightIntensity"]+"\n"
# color
if "lightColor" in shader["meta"]:
content += "\tq3map_lightRGB "+"%.3f %.3f %.3f" % (r, g, b)+"\n\n"
elif "additionAverage" in shader["meta"]:
content += "\tq3map_lightRGB "+"%.3f %.3f %.3f" % shader["meta"]["additionAverage"]+"\n\n"
elif shader["addition"]:
content += "\tq3map_lightImage "+getVfsPath("addition")+"\n\n"
elif shader["diffuse"]:
content += "\tq3map_lightImage "+getVfsPath("diffuse")+"\n\n"
else:
content += "\tq3map_lightRGB 1.000 1.000 1.000\n\n"
# diffuse map
if shader["diffuse"]:
if shader["options"]["renderer"] == "daemon":
if not in_stage:
content += "\t{\n"
in_stage = True
content += "\t\tdiffuseMap "+getVfsPath("diffuse")+"\n"
# with alpha channel
if shader["meta"]["diffuseAlpha"]:
if shader["options"]["renderer"] != "daemon":
content += "\t{\n"+\
"\t\tmap "+getVfsPath("diffuse")+"\n"
if shader["options"]["renderer"] != "quake3":
content += "\t\tstage diffuseMap\n"
# alphatest forced
if shader["options"]["alphaTest"]:
if type(shader["options"]["alphaTest"]) == str:
stage_keys += "\t\talphaFunc "+shader["options"]["alphaTest"]+"\n"
else:
stage_keys += "\t\talphaTest "+"%.2f"%shader["options"]["alphaTest"]+"\n"
# alphatest implied by binary alpha values
elif shader["meta"]["diffuseAlphaBin"]:
stage_keys += "\t\talphaFunc GE128\n"
# smooth blending
else:
stage_keys += "\t\tblend blend\n"
if shader["options"]["renderer"] != "daemon":
content += stage_keys;
content += "\t}\n"
# without alpha channel
elif shader["options"]["renderer"] == "xreal":
content += "\tdiffuseMap "+getVfsPath("diffuse")+"\n"
elif shader["options"]["renderer"] == "quake3":
content += "\t{\n"+\
"\t\tmap "+getVfsPath("diffuse")+"\n"+\
"\t}\n"
# normal & height map
if shader["normal"]:
if shader["options"]["renderer"] == "daemon":
if not in_stage:
content += "\t{\n"
in_stage = True
content += "\t\tnormalMap "+getVfsPath("normal")+"\n"
elif shader["options"]["renderer"] == "xreal":
if shader["height"] and shader["options"]["heightNormalsMod"] > 0:
content += "\tnormalMap addnormals ( "+getVfsPath("normal")+\
", heightmap ( "+getVfsPath("height")+", "+\
"%.2f" % shader["options"]["heightNormalsMod"]+" ) )\n"
else:
content += "\tnormalMap "+getVfsPath("normal")+"\n"
if not shader["normal"] and shader["height"] and shader["options"]["heightNormalsMod"] > 0 and shader["options"]["renderer"] == "xreal":
content += "\tnormalMap heightmap ( "+getVfsPath("height")+", "+\
"%.2f" % shader["options"]["heightNormalsMod"]+" )\n"
if shader["normalheight"]:
if shader["options"]["renderer"] == "daemon":
if not in_stage:
content += "\t{\n"
in_stage = True
content += "\t\tnormalHeightMap "+getVfsPath("normalheight")+"\n"
if shader["height"]:
if shader["options"]["renderer"] == "daemon":
if not in_stage:
content += "\t{\n"
in_stage = True
content += "\t\theightMap "+getVfsPath("height")+"\n"
# physical map
if shader["physical"]:
if shader["options"]["renderer"] == "daemon":
if not in_stage:
content += "\t{\n"
in_stage = True
content += "\t\tphysicalMap "+getVfsPath("physical")+"\n"
# specular map
if shader["specular"]:
if shader["options"]["renderer"] == "daemon":
if not in_stage:
content += "\t{\n"
in_stage = True
content += "\t\tspecularMap "+getVfsPath("specular")+"\n"
elif shader["options"]["renderer"] == "xreal":
content += "\tspecularMap "+getVfsPath("specular")+"\n"
# addition map
if shader["addition"]:
has_light_color = "lightColor" in shader["meta"] and r + g + b < 3.0
if shader["options"]["renderer"] == "daemon" and not has_light_color:
if not in_stage:
content += "\t{\n"
in_stage = True
content += "\t\tglowMap "+getVfsPath("addition")+"\n"
elif shader["options"]["renderer"] == "xreal" and not has_light_color:
content += "\tglowMap "+getVfsPath("addition")+"\n"
elif shader["options"]["renderer"] == "quake3"\
or (shader["options"]["renderer"] == "daemon" and has_light_color)\
or (shader["options"]["renderer"] == "xreal" and has_light_color):
if in_stage:
content += stage_keys
content += "\t}\n"
in_stage = False
stage_keys += "\t{\n"+\
"\t\tmap "+getVfsPath("addition")+"\n"+\
"\t\tblend add\n"
in_stage = True
if shader["options"]["renderer"] in ["daemon", "xreal", "quake3"] and has_light_color:
stage_keys += \
"\t\tred "+"%.3f" % self.__radToAdd(shader, r)+"\n"+\
"\t\tgreen "+"%.3f" % self.__radToAdd(shader, g)+"\n"+\
"\t\tblue "+"%.3f" % self.__radToAdd(shader, b)+"\n"
if shader["options"]["renderer"] in ["daemon", "xreal", "quake3"]:
content += stage_keys;
stage_keys = ""
if in_stage:
if shader["options"]["renderer"] in ["daemon"]:
content += stage_keys
content += "\t}\n"
in_stage = False
content += "}\n"
return content