-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVehicleMaterialManager.lua
More file actions
332 lines (239 loc) · 13 KB
/
VehicleMaterialManager.lua
File metadata and controls
332 lines (239 loc) · 13 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
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
---This class handles the material templates that are used with the vehicle shader
local VehicleMaterialManager_mt = Class(VehicleMaterialManager, AbstractManager)
---
function VehicleMaterialManager:initDataStructures()
self.materialTemplates = {}
self.materialTemplatesByName = {}
self.modMaterialTemplatesToLoad = {}
end
---Load data on map load
-- @param integer xmlFile
-- @param table missionInfo
-- @param string baseDirectory
-- @return boolean true if loading was successful else false
function VehicleMaterialManager:loadMapData(xmlFile, missionInfo, baseDirectory)
self:loadMaterialTemplates(VehicleMaterialManager.DEFAULT_TEMPLATES_FILENAME)
self:loadMaterialTemplates(VehicleMaterialManager.DEFAULT_BRAND_TEMPLATES_FILENAME)
for i=#self.modMaterialTemplatesToLoad, 1, -1 do
local modMaterialTemplate = self.modMaterialTemplatesToLoad[i]
local xmlFile = XMLFile.load("ModFile", modMaterialTemplate.xmlFilename, g_modDescSchema)
if xmlFile ~= nil then
self:loadMaterialTemplatesFromXML(xmlFile, modMaterialTemplate.key, modMaterialTemplate.baseDirectory, modMaterialTemplate.customEnvironment, "metalPainted")
xmlFile:delete()
end
table.remove(self.modMaterialTemplatesToLoad, i)
end
end
---
function VehicleMaterialManager:addModMaterialTemplatesToLoad(xmlFilename, key, baseDirectory, customEnvironment)
table.insert(self.modMaterialTemplatesToLoad, {xmlFilename=xmlFilename, key=key, baseDirectory=baseDirectory, customEnvironment=customEnvironment})
end
---
function VehicleMaterialManager:loadMaterialTemplates(xmlFilename, baseDirectory, customEnvironment)
local xmlFile = XMLFile.load("templates", xmlFilename, VehicleMaterialManager.xmlSchema)
if xmlFile ~= nil then
self:loadMaterialTemplatesFromXML(xmlFile, "templates", baseDirectory, customEnvironment)
xmlFile:delete()
end
end
---
function VehicleMaterialManager:loadMaterialTemplatesFromXML(xmlFile, key, baseDirectory, customEnvironment, parentTemplateDefault)
parentTemplateDefault = xmlFile:getValue(key .. "#parentTemplateDefault", parentTemplateDefault)
xmlFile:iterate(key .. ".template", function(index, templateKey)
local name = xmlFile:getValue(templateKey .. "#name")
if name ~= nil then
if customEnvironment ~= nil then
name = string.upper(customEnvironment .. "." .. name)
else
name = string.upper(name)
end
local parentName = xmlFile:getValue(templateKey .. "#parentTemplate", parentTemplateDefault)
local parentTemplate
if parentName ~= nil then
parentTemplate = self.materialTemplatesByName[parentName:upper()]
if parentTemplate == nil then
Logging.xmlWarning(xmlFile, "Unable to find parent template '%s' for '%s'", parentName, templateKey)
return
end
end
local materialTemplate = self.materialTemplatesByName[name]
if materialTemplate == nil then
materialTemplate = {}
end
materialTemplate.name = name
materialTemplate.parentTemplate = parentTemplate or materialTemplate
materialTemplate.customEnvironment = customEnvironment
local brandName = xmlFile:getValue(templateKey .. "#brand")
if brandName ~= nil then
materialTemplate.brand = g_brandManager:getBrandByName(brandName)
if materialTemplate.brand == nil then
Logging.xmlWarning(xmlFile, "Unknown brand '%s' defined in material template '%s'", brandName, templateKey)
end
end
materialTemplate.titleL10N = xmlFile:getValue(templateKey .. "#title")
materialTemplate.colorScale = xmlFile:getValue(templateKey .. "#colorScale", nil, true)
materialTemplate.smoothnessScale = xmlFile:getValue(templateKey .. "#smoothnessScale")
materialTemplate.metalnessScale = xmlFile:getValue(templateKey .. "#metalnessScale")
materialTemplate.clearCoatSmoothness = xmlFile:getValue(templateKey .. "#clearCoatSmoothness")
materialTemplate.clearCoatIntensity = xmlFile:getValue(templateKey .. "#clearCoatIntensity")
materialTemplate.porosity = xmlFile:getValue(templateKey .. "#porosity")
materialTemplate.detailDiffuse = xmlFile:getValue(templateKey .. "#detailDiffuse")
if materialTemplate.detailDiffuse ~= nil then
materialTemplate.detailDiffuse = Utils.getFilename(materialTemplate.detailDiffuse, baseDirectory)
if not textureFileExists(materialTemplate.detailDiffuse) then
Logging.xmlWarning(xmlFile, "Unable to find detail texture '%s' in '%s'", materialTemplate.detailDiffuse, templateKey)
materialTemplate.detailDiffuse = nil
end
end
if materialTemplate.detailDiffuse == nil and materialTemplate.parentTemplate.detailDiffuse == nil then
Logging.xmlWarning(xmlFile, "Missing detail diffuse texture for '%s'", templateKey)
return
end
materialTemplate.detailNormal = xmlFile:getValue(templateKey .. "#detailNormal")
if materialTemplate.detailNormal ~= nil then
materialTemplate.detailNormal = Utils.getFilename(materialTemplate.detailNormal, baseDirectory)
if not textureFileExists(materialTemplate.detailNormal) then
Logging.xmlWarning(xmlFile, "Unable to find detail texture '%s' in '%s'", materialTemplate.detailNormal, templateKey)
materialTemplate.detailNormal = nil
end
end
if materialTemplate.detailNormal == nil and materialTemplate.parentTemplate.detailNormal == nil then
Logging.xmlWarning(xmlFile, "Missing detail normal texture for '%s'", templateKey)
return
end
materialTemplate.detailSpecular = xmlFile:getValue(templateKey .. "#detailSpecular")
if materialTemplate.detailSpecular ~= nil then
materialTemplate.detailSpecular = Utils.getFilename(materialTemplate.detailSpecular, baseDirectory)
if not textureFileExists(materialTemplate.detailSpecular) then
Logging.xmlWarning(xmlFile, "Unable to find detail texture '%s' in '%s'", materialTemplate.detailSpecular, templateKey)
materialTemplate.detailSpecular = nil
end
end
if materialTemplate.detailSpecular == nil and materialTemplate.parentTemplate.detailSpecular == nil then
Logging.xmlWarning(xmlFile, "Missing detail specular texture for '%s'", templateKey)
return
end
table.insert(self.materialTemplates, materialTemplate)
self.materialTemplatesByName[name] = materialTemplate
else
Logging.xmlWarning(xmlFile, "Missing name attribute for '%s'", templateKey)
end
end)
end
---
function VehicleMaterialManager:getMaterialTemplateByName(name, customEnvironment)
if name ~= nil then
if customEnvironment ~= nil then
local template = self.materialTemplatesByName[string.upper(customEnvironment .. "." .. name)]
if template ~= nil then
return template
end
end
return self.materialTemplatesByName[name:upper()]
end
return nil
end
---
function VehicleMaterialManager:getMaterialTemplateColorByName(name, customEnvironment)
if name ~= nil then
name = name:upper()
local materialTemplate = self:getMaterialTemplateByName(name, customEnvironment)
if materialTemplate ~= nil and materialTemplate.colorScale ~= nil then
return {materialTemplate.colorScale[1], materialTemplate.colorScale[2], materialTemplate.colorScale[3], 0}
end
end
return nil
end
---
function VehicleMaterialManager:getMaterialTemplateColorAndTitleByName(name, customEnvironment)
if name ~= nil then
name = name:upper()
local materialTemplate = self:getMaterialTemplateByName(name, customEnvironment)
if materialTemplate ~= nil then
local title
if materialTemplate.brand ~= nil then
title = materialTemplate.brand.title
end
if materialTemplate.titleL10N ~= nil then
if title ~= nil then
title = title .. " "
else
title = ""
end
title = title .. g_i18n:convertText(materialTemplate.titleL10N, materialTemplate.customEnvironment)
end
if materialTemplate.colorScale == nil then
return {1, 1, 1}, title
end
return table.clone(materialTemplate.colorScale), title
end
end
return nil, nil
end
---
function VehicleMaterialManager:getMaterialTemplateIndexByName(name)
if name ~= nil then
name = name:upper()
for i, materialTemplate in ipairs(self.materialTemplates) do
if materialTemplate.name == name then
return i
end
end
end
return 1
end
---
function VehicleMaterialManager:getMaterialTemplateNameByIndex(index)
local template = self.materialTemplates[index] or self.materialTemplates[1]
return template.name
end
---
function VehicleMaterialManager:getMaterialTemplateFinish(materialTemplateName)
local isMetallic, isMat = false, false
if materialTemplateName == nil then
return isMetallic, isMat
end
local materialTemplateNameLower = materialTemplateName:lower()
if materialTemplateNameLower:contains("silver")
or materialTemplateNameLower:contains("copper")
or materialTemplateNameLower:contains("gold")
or materialTemplateNameLower:contains("bronze")
or materialTemplateNameLower:contains("chrome")
or materialTemplateNameLower:contains("metallic") then
isMetallic = true
end
if materialTemplateNameLower:contains("matpaint") then
isMat = true
end
return isMetallic, isMat
end
---
function VehicleMaterialManager.registerXMLPaths(schema, basePath)
schema:register(XMLValueType.STRING, basePath .. "#id", "File Identifier")
schema:register(XMLValueType.STRING, basePath .. "#name", "File Name")
schema:register(XMLValueType.STRING, basePath .. "#parentTemplateDefault", "Name of default parent template")
schema:register(XMLValueType.STRING, basePath .. "#parentTemplateFilename", "Path to parent template file")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#name", "Name of template")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#title", "Name of the color to display in the shop")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#description", "Descrpition text of the template")
schema:register(XMLValueType.INT, basePath .. ".template(?)#usage", "Usage of the color")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#parentTemplate", "Name of parent template", "templates#parentTemplateDefault")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#brand", "Brand identifier")
schema:register(XMLValueType.VECTOR_3, basePath .. ".template(?)#colorScale", "Color values (sRGB)")
schema:register(XMLValueType.FLOAT, basePath .. ".template(?)#smoothnessScale")
schema:register(XMLValueType.FLOAT, basePath .. ".template(?)#metalnessScale")
schema:register(XMLValueType.FLOAT, basePath .. ".template(?)#clearCoatSmoothness")
schema:register(XMLValueType.FLOAT, basePath .. ".template(?)#clearCoatIntensity")
schema:register(XMLValueType.FLOAT, basePath .. ".template(?)#porosity")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#category", "Category name (Used by DCC Tool)")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#iconFilename", "Icon filename (Used by DCC Tool)")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#detailDiffuse", "Detail diffuse texture")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#detailNormal", "Detail normal texture")
schema:register(XMLValueType.STRING, basePath .. ".template(?)#detailSpecular", "Detail specular texture")
schema:register(XMLValueType.STRING, basePath .. ".template(?).colorScan#filename", "Path to scan reference")
schema:register(XMLValueType.BOOL, basePath .. ".template(?).colorScan#channelR", "Calibrate red channel", true)
schema:register(XMLValueType.BOOL, basePath .. ".template(?).colorScan#channelG", "Calibrate green channel", true)
schema:register(XMLValueType.BOOL, basePath .. ".template(?).colorScan#channelB", "Calibrate blue channel", true)
schema:register(XMLValueType.BOOL, basePath .. ".template(?).colorScan#channelSmoothness", "Calibrate smoothness", true)
schema:register(XMLValueType.BOOL, basePath .. ".template(?).colorScan#channelMetalness", "Calibrate metalness", true)
end