forked from H2xDev/GodotVMF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmt_loader.gd
More file actions
164 lines (118 loc) · 5.37 KB
/
Copy pathvmt_loader.gd
File metadata and controls
164 lines (118 loc) · 5.37 KB
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
@static_unload
class_name VMTLoader extends RefCounted
static func is_file_valid(path: String):
var import_path = path + ".import";
if not FileAccess.file_exists(import_path): return false;
var file = FileAccess.open(import_path, FileAccess.READ);
var is_valid = file.get_as_text().contains("valid=false");
file.close();
return not is_valid;
static func parse_transform(transform_data: String):
var transformRegex = RegEx.new();
transformRegex.compile('^"?center\\s+([0-9-.]+)\\s+([0-9-.]+)\\s+scale\\s+([0-9-.]+)\\s+([0-9-.]+)\\s+rotate\\s+([0-9-.]+)\\s+translate\\s+([0-9-.]+)\\s+([0-9-.]+)"?$')
var transformParams = transformRegex.search(transform_data);
var center = Vector2(float(transformParams.get_string(1)), float(transformParams.get_string(2)));
var scale = Vector2(float(transformParams.get_string(3)), float(transformParams.get_string(4)));
var rotate = float(transformParams.get_string(5));
var translate = Vector2(float(transformParams.get_string(6)), float(transformParams.get_string(7)));
return {
center = center,
scale = scale,
rotate = rotate,
translate = translate,
}
static func load(path: String):
var structure = VDFParser.parse(path, true);
var shader_name = structure.keys()[0];
var details = structure[shader_name];
var material = null;
var is_blend_texture = shader_name == "worldvertextransition";
# NOTE: CS:GO/L4D
if "insert" in details:
details.merge(details["insert"]);
if details.get(">=dx90_20b"):
details.merge(details['>=dx90_20b']);
if "$shader" in details:
var extension = ".gdshader" if not details["$shader"].get_extension() else "";
var shader_path = "res://" + details["$shader"] + extension;
material = VMTShaderBasedMaterial.load(shader_path);
else:
material = StandardMaterial3D.new() if not is_blend_texture else WorldVertexTransitionMaterial.new();
var transformer = VMTTransformer.new();
var extend_transformer = Engine.get_main_loop().root.get_node_or_null("VMTExtend");
var uniforms: Array = material.shader.get_shader_uniform_list() if material is ShaderMaterial else [];
if shader_name == "unlitgeneric":
material.shading_mode = 0
elif shader_name == "vertexlitgeneric":
material.shading_mode = 2
for key in details.keys():
var value = details[key];
var is_compile_key = key.begins_with("%");
key = key.replace('$', '').replace('%', '');
if is_compile_key and value and key != "keywords":
var compile_keys = material.get_meta("compile_keys", []);
compile_keys.append(key);
material.set_meta("compile_keys", compile_keys);
if material is ShaderMaterial:
var mat: ShaderMaterial = material;
if uniforms.find(key) > -1:
var is_texture = value.has("/");
mat.set_shader_parameter(key, VTFLoader.get_texture(value) if is_texture else value);
continue;
if extend_transformer and key in extend_transformer:
extend_transformer[key].call(material, value);
continue;
if key in transformer:
transformer[key].call(material, value);
material.set_meta("details", details);
return material;
static func normalize_path(path: String) -> String:
return path.replace('\\', '/').replace('//', '/').replace('res:/', 'res://');
static func has_material(material: String) -> bool:
var material_path = normalize_path(VMFConfig.materials.target_folder + "/" + material + ".tres").to_lower();
if not ResourceLoader.exists(material_path):
material_path = material_path.replace(".tres", ".vmt");
if not ResourceLoader.exists(material_path):
return false;
return true;
static func get_material(material: String) -> Material:
var cached_material = VMFCache.get_cached(material);
if cached_material:
return cached_material as Material;
var material_path = normalize_path(VMFConfig.materials.target_folder + "/" + material + ".tres").to_lower();
if not ResourceLoader.exists(material_path):
material_path = material_path.replace(".tres", ".vmt");
if not ResourceLoader.exists(material_path):
if not VMFCache.is_file_logged(material_path):
VMFLogger.warn("Material not found: " + material_path);
VMFCache.add_logged_file(material_path);
material_path = VMFConfig.materials.fallback_material
if not material_path or not ResourceLoader.exists(material_path): return null;
cached_material = ResourceLoader.load(material_path);
VMFCache.add_cached(material, cached_material);
return cached_material as Material;
static func get_texture_size(side_material: String) -> Vector2:
var default_texture_size: int = VMFConfig.materials.default_texture_size;
var cache_key = "texture_size_" + side_material;
var cached_value = VMFCache.get_cached(cache_key);
if cached_value:
return cached_value as Vector2;
var material = get_material(side_material);
if not material:
cached_value = Vector2(default_texture_size, default_texture_size);
VMFCache.add_cached(cache_key, cached_value);
return cached_value;
var texture = material.albedo_texture \
if material is BaseMaterial3D \
else material.get_shader_parameter('albedo_texture');
if not texture and (material is ShaderMaterial):
texture = material.get_shader_parameter('basetexture');
if not texture:
cached_value = Vector2(default_texture_size, default_texture_size);
VMFCache.add_cached(cache_key, cached_value);
return cached_value;
cached_value = texture.get_size() \
if texture \
else Vector2(default_texture_size, default_texture_size);
VMFCache.add_cached(cache_key, cached_value);
return cached_value;