-
Notifications
You must be signed in to change notification settings - Fork 11
/
material_converter.py
46 lines (41 loc) · 1.75 KB
/
material_converter.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
import sys
from pathlib import Path
from typing import Tuple, TypeVar, Type
from SourceIO.library.source1.vmt import VMT
from shader_converters.eyerefract import EyeRefract
from shader_converters.lightmappedgeneric import LightmappedGeneric
from shader_converters.shader_base import ShaderBase, GameType
from shader_converters.unlitgeneric import UnlitGeneric
from shader_converters.vertexlitgeneric import VertexLitGeneric
from utils import normalize_path
MaterialName = TypeVar('MaterialName', str, str)
CdPath = TypeVar('CdPath', str, str)
MaterialPath = TypeVar('MaterialPath', str, str)
Material = Tuple[MaterialName, CdPath, MaterialPath]
s1_to_s2_shader = {
"worldvertextransition": LightmappedGeneric,
"lightmappedgeneric": LightmappedGeneric,
"vertexlitgeneric": VertexLitGeneric,
"teeth": VertexLitGeneric,
"unlitgeneric": UnlitGeneric,
"eyes": EyeRefract,
"eyerefract": EyeRefract,
}
def convert_material(material: Material, s2_output_path: Path, game: GameType = GameType.CS2):
if not (material[0] and material[2]):
return False, f"Failed to open file {material[0]}"
vmt = VMT(material[2], material[0])
shader_converter: Type[ShaderBase] = s1_to_s2_shader.get(vmt.shader, None)
if shader_converter is None:
# sys.stderr.write(f'Unsupported shader: "{vmt.shader}"\n')
return False, f'Unsupported Source1 shader {vmt.shader}!'
mat_path = normalize_path(Path(material[1]) / material[0])
mat_name = mat_path.stem
mat_path = mat_path.parent
converter = shader_converter(mat_name, mat_path, vmt, s2_output_path, game)
try:
converter.convert()
except Exception as ex:
print(f'Failed to convert {material[2]} due to {ex}')
converter.write_vmat()
return True, vmt.shader