forked from Dukefarming/FS25-lua-scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVehicleMaterial.lua
More file actions
418 lines (317 loc) · 16.4 KB
/
VehicleMaterial.lua
File metadata and controls
418 lines (317 loc) · 16.4 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
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
---Stores data of vehicle materials (color, smoothness, clear coat, detail textures)
local VehicleMaterial_mt = Class(VehicleMaterial)
---
function VehicleMaterial.new(baseDirectory, customMt)
local self = setmetatable({}, customMt or VehicleMaterial_mt)
self.baseDirectory = baseDirectory
self.colorOnly = false
return self
end
---
function VehicleMaterial:clone()
local material = VehicleMaterial.new(self.baseDirectory)
material.targetMaterialSlotName = self.targetMaterialSlotName
material.templateName = self.templateName
material.materialTemplate = self.materialTemplate
material.colorScale = self.colorScale
material.smoothnessScale = self.smoothnessScale
material.metalnessScale = self.metalnessScale
material.clearCoatSmoothness = self.clearCoatSmoothness
material.clearCoatIntensity = self.clearCoatIntensity
material.porosity = self.porosity
material.detailDiffuse = self.detailDiffuse
material.detailNormal = self.detailNormal
material.detailSpecular = self.detailSpecular
material.diffuseMap = self.diffuseMap
material.normalMap = self.normalMap
material.specularMap = self.specularMap
return material
end
---
function VehicleMaterial:setColor(r, g, b)
if r == nil then
return
end
if type(r) == "table" then
self.colorScale = {r[1], r[2], r[3]}
else
self.colorScale = {r, g, b}
end
end
---
function VehicleMaterial:getBrightness()
if self.colorScale == nil then
return nil
end
return MathUtil.getBrightnessFromColor(self.colorScale[1], self.colorScale[2], self.colorScale[3])
end
---
function VehicleMaterial:setTemplateName(templateName, colorOnly, customEnvironment)
self.templateName = templateName
self.materialTemplate = g_vehicleMaterialManager:getMaterialTemplateByName(templateName, customEnvironment)
if self.materialTemplate ~= nil then
self.colorScale = self.materialTemplate.colorScale or self.materialTemplate.parentTemplate.colorScale
if self.colorScale == nil then
self.colorScale = {1, 1, 1}
end
if colorOnly then
return true
end
self.smoothnessScale = self.materialTemplate.smoothnessScale or self.materialTemplate.parentTemplate.smoothnessScale or 1
self.metalnessScale = self.materialTemplate.metalnessScale or self.materialTemplate.parentTemplate.metalnessScale or 1
self.clearCoatSmoothness = self.materialTemplate.clearCoatSmoothness or self.materialTemplate.parentTemplate.clearCoatSmoothness or 0
self.clearCoatIntensity = self.materialTemplate.clearCoatIntensity or self.materialTemplate.parentTemplate.clearCoatIntensity or 0
self.porosity = self.materialTemplate.porosity or self.materialTemplate.parentTemplate.porosity or 0
self.detailDiffuse = self.materialTemplate.detailDiffuse or self.materialTemplate.parentTemplate.detailDiffuse or "data/shared/detailLibrary/nonMetallic/default_diffuse.png"
self.detailNormal = self.materialTemplate.detailNormal or self.materialTemplate.parentTemplate.detailNormal or "data/shared/detailLibrary/nonMetallic/default_normal.png"
self.detailSpecular = self.materialTemplate.detailSpecular or self.materialTemplate.parentTemplate.detailSpecular or "data/shared/detailLibrary/nonMetallic/default_specular.png"
else
return false
end
return true
end
---
function VehicleMaterial:loadFromXML(xmlFile, key, customEnvironment)
self.targetMaterialSlotName = xmlFile:getValue(key .. "#materialSlotName")
local templateName = self.templateName or xmlFile:getValue(key .. "#materialTemplateName")
if templateName ~= nil and templateName ~= self.templateName then
local materialTemplateUseColorOnly = xmlFile:getValue(key .. "#materialTemplateUseColorOnly", false)
if not self:setTemplateName(templateName, materialTemplateUseColorOnly, customEnvironment) then
Logging.xmlWarning(xmlFile.xmlFile or xmlFile, "Unable to find material template '%s' in '%s'", templateName, key)
return false
end
end
local colorStr = xmlFile:getValue(key .. ".colorScale#value")
if colorStr ~= nil then
local colorTemplate = g_vehicleMaterialManager:getMaterialTemplateByName(colorStr, customEnvironment)
if colorTemplate ~= nil then
self.colorScale = colorTemplate.colorScale or colorTemplate.parentTemplate.colorScale
else
self.colorScale = string.getVector(colorStr, 3) or self.colorScale
end
end
self.smoothnessScale = xmlFile:getValue(key .. ".smoothness#value", self.smoothnessScale)
self.metalnessScale = xmlFile:getValue(key .. ".metalness#value", self.metalnessScale)
self.clearCoatSmoothness = xmlFile:getValue(key .. ".clearCoat#smoothness", self.clearCoatSmoothness)
self.clearCoatIntensity = xmlFile:getValue(key .. ".clearCoat#intensity", self.clearCoatIntensity)
local detailDiffuse = xmlFile:getValue(key .. ".detail#diffuse", nil, self.baseDirectory)
self.detailDiffuse = detailDiffuse or self.detailDiffuse
local detailNormal = xmlFile:getValue(key .. ".detail#normal", nil, self.baseDirectory)
self.detailNormal = detailNormal or self.detailNormal
local detailSpecular = xmlFile:getValue(key .. ".detail#specular", nil, self.baseDirectory)
self.detailSpecular = detailSpecular or self.detailSpecular
local diffuseMap = xmlFile:getValue(key .. ".textures#diffuse", nil, self.baseDirectory)
self.diffuseMap = diffuseMap or self.diffuseMap
local normalMap = xmlFile:getValue(key .. ".textures#normal", nil, self.baseDirectory)
self.normalMap = normalMap or self.normalMap
local specularMap = xmlFile:getValue(key .. ".textures#specular", nil, self.baseDirectory)
self.specularMap = specularMap or self.specularMap
return self.colorScale ~= nil
or self.smoothnessScale ~= nil
or self.metalnessScale ~= nil
or self.clearCoatSmoothness ~= nil
or self.clearCoatIntensity ~= nil
or self.porosity ~= nil
or self.detailDiffuse ~= nil
or self.detailNormal ~= nil
or self.detailSpecular ~= nil
or self.diffuseMap ~= nil
or self.normalMap ~= nil
or self.specularMap ~= nil
end
---
function VehicleMaterial:loadShortFromXML(xmlFile, key, customEnvironment)
self.targetMaterialSlotName = xmlFile:getValue(key .. "#materialSlotName")
local templateName = self.templateName or xmlFile:getValue(key .. "#materialTemplateName")
if templateName ~= nil then
local materialTemplateUseColorOnly = xmlFile:getValue(key .. "#materialTemplateUseColorOnly", false)
if not self:setTemplateName(templateName, materialTemplateUseColorOnly, customEnvironment) then
Logging.xmlWarning(xmlFile.xmlFile or xmlFile, "Unable to find material template '%s' in '%s'", templateName, key)
return false
end
local templateNameColor = xmlFile:getValue(key .. "#materialTemplateNameColor")
if templateNameColor ~= nil then
local materialTemplate = g_vehicleMaterialManager:getMaterialTemplateByName(templateNameColor, customEnvironment)
if materialTemplate ~= nil then
self.colorScale = materialTemplate.colorScale or materialTemplate.parentTemplate.colorScale
end
end
else
return false
end
return true
end
---
function VehicleMaterial:applyToVehicle(vehicle, targetMaterialSlotName)
local success = false
for i, component in ipairs(vehicle.components) do
success = self:apply(component.node, targetMaterialSlotName) or success
end
return success
end
---
function VehicleMaterial:apply(node, targetMaterialSlotName, colorOnly)
local success = false
targetMaterialSlotName = targetMaterialSlotName or self.targetMaterialSlotName
if getHasClassId(node, ClassIds.SHAPE) then
for i=1, getNumOfMaterials(node) do
local materialSlotName = getMaterialSlotName(node, i - 1)
if materialSlotName == targetMaterialSlotName or targetMaterialSlotName == nil then
self:applyToMaterial(node, i - 1, colorOnly)
success = true
end
end
end
for i=1, getNumOfChildren(node) do
success = self:apply(getChildAt(node, i - 1), targetMaterialSlotName, colorOnly) or success
end
return success
end
---
function VehicleMaterial:applyToMaterial(node, materialIndex, colorOnly)
if self.colorScale ~= nil then
setShaderParameter(node, "colorScale", self.colorScale[1], self.colorScale[2], self.colorScale[3], nil, false, materialIndex)
end
if colorOnly then
return
end
if self.smoothnessScale ~= nil then
setShaderParameter(node, "smoothnessScale", self.smoothnessScale, nil, nil, nil, false, materialIndex)
end
if self.metalnessScale ~= nil then
setShaderParameter(node, "metalnessScale", self.metalnessScale, nil, nil, nil, false, materialIndex)
end
if self.clearCoatSmoothness ~= nil then
setShaderParameter(node, "clearCoatSmoothness", self.clearCoatSmoothness, nil, nil, nil, false, materialIndex)
end
if self.clearCoatIntensity ~= nil then
setShaderParameter(node, "clearCoatIntensity", self.clearCoatIntensity, nil, nil, nil, false, materialIndex)
end
if self.porosity ~= nil then
setShaderParameter(node, "porosity", self.porosity, nil, nil, nil, false, materialIndex)
end
local materialId = getMaterial(node, materialIndex)
local newMaterialId = materialId
if self.detailDiffuse ~= nil then
newMaterialId = setMaterialCustomMapFromFile(newMaterialId, "detailDiffuse", self.detailDiffuse, false, true, false)
end
if self.detailNormal ~= nil then
newMaterialId = setMaterialCustomMapFromFile(newMaterialId, "detailNormal", self.detailNormal, false, false, false)
end
if self.detailSpecular ~= nil then
newMaterialId = setMaterialCustomMapFromFile(newMaterialId, "detailSpecular", self.detailSpecular, false, false, false)
end
if self.diffuseMap ~= nil then
newMaterialId = setMaterialDiffuseMapFromFile(newMaterialId, self.diffuseMap, false, true, false)
end
if self.normalMap ~= nil then
newMaterialId = setMaterialNormalMapFromFile(newMaterialId, self.normalMap, false, false, false)
end
if self.specularMap ~= nil then
newMaterialId = setMaterialGlossMapFromFile(newMaterialId, self.specularMap, false, false, false)
end
if newMaterialId ~= materialId then
setMaterial(node, newMaterialId, materialIndex)
end
end
---
function VehicleMaterial:getIsApplied(node, materialId, checkColor)
if checkColor ~= false then
local r, g, b, _ = getMaterialCustomParameter(materialId, "colorScale")
if self.colorScale ~= nil then
if r ~= self.colorScale[1] or g ~= self.colorScale[2] or b ~= self.colorScale[3] then
return false
end
else
if r ~= 1 or g ~= 1 or b ~= 1 then
return false
end
end
end
local smoothness = getMaterialCustomParameter(materialId, "smoothnessScale")
if self.smoothnessScale ~= nil and smoothness ~= self.smoothnessScale then
return false
elseif self.smoothnessScale == nil and smoothness ~= 1 then
return false
end
local metalness = getMaterialCustomParameter(materialId, "metalnessScale")
if self.metalnessScale ~= nil and metalness ~= self.metalnessScale then
return false
elseif self.metalnessScale == nil and metalness ~= 1 then
return false
end
local clearCoatSmoothness = getMaterialCustomParameter(materialId, "clearCoatSmoothness")
if self.clearCoatSmoothness ~= nil and clearCoatSmoothness ~= self.clearCoatSmoothness then
return false
elseif self.clearCoatSmoothness == nil and clearCoatSmoothness ~= 0 then
return false
end
local clearCoatIntensity = getMaterialCustomParameter(materialId, "clearCoatIntensity")
if self.clearCoatIntensity ~= nil and clearCoatIntensity ~= self.clearCoatIntensity then
return false
elseif self.clearCoatIntensity == nil and clearCoatIntensity ~= 0 then
return false
end
local porosity = getMaterialCustomParameter(materialId, "porosity")
if self.porosity ~= nil and porosity ~= self.porosity then
return false
elseif self.porosity == nil and porosity ~= 0 then
return false
end
local detailDiffuse = getMaterialCustomMapFilename(materialId, "detailDiffuse")
if self.detailDiffuse ~= nil and detailDiffuse ~= self.detailDiffuse then
return false
end
local detailNormal = getMaterialCustomMapFilename(materialId, "detailNormal")
if self.detailNormal ~= nil and detailNormal ~= self.detailNormal then
return false
end
local detailSpecular = getMaterialCustomMapFilename(materialId, "detailSpecular")
if self.detailSpecular ~= nil and detailSpecular ~= self.detailSpecular then
return false
end
local diffuseMap = getMaterialDiffuseMapFilename(materialId)
if self.diffuseMap ~= nil and diffuseMap ~= self.diffuseMap then
return false
end
local normalMap = getMaterialNormalMapFilename(materialId)
if self.normalMap ~= nil and normalMap ~= self.normalMap then
return false
end
local specularMap = getMaterialGlossMapFilename(materialId)
if self.specularMap ~= nil and specularMap ~= self.specularMap then
return false
end
return true
end
---
-- @param XMLSchema schema
-- @param string basePath
function VehicleMaterial.registerXMLPaths(schema, basePath)
schema:register(XMLValueType.STRING, basePath .. "#materialSlotName", "Material slot name in the i3d file")
schema:register(XMLValueType.STRING, basePath .. "#materialTemplateName", "Name of template to apply (all attributes will be used from template)")
schema:registerAutoCompletionDataSource(basePath .. "#materialTemplateName", "$data/shared/brandMaterialTemplates.xml", "templates.template#name")
schema:register(XMLValueType.BOOL, basePath .. "#materialTemplateUseColorOnly", "If 'true', only the color is used from the material template. The rest from the i3d file.", false)
schema:register(XMLValueType.STRING, basePath .. ".colorScale#value", "Material color if it should not be used from configuration (can also be a different material template, from which then ONLY the color is taken)")
schema:register(XMLValueType.FLOAT, basePath .. ".smoothness#value", "Smoothness value")
schema:register(XMLValueType.FLOAT, basePath .. ".metalness#value", "Metalness value")
schema:register(XMLValueType.FLOAT, basePath .. ".clearCoat#smoothness", "Smoothness of clear coat")
schema:register(XMLValueType.FLOAT, basePath .. ".clearCoat#intensity", "Intensity of clear coat")
schema:register(XMLValueType.FILENAME, basePath .. ".detail#diffuse", "Path to detail diffuse texture")
schema:register(XMLValueType.FILENAME, basePath .. ".detail#normal", "Path to detail normal texture")
schema:register(XMLValueType.FILENAME, basePath .. ".detail#specular", "Path to detail specular texture")
schema:register(XMLValueType.FILENAME, basePath .. ".textures#diffuse", "Path to diffuse texture")
schema:register(XMLValueType.FILENAME, basePath .. ".textures#normal", "Path to normal texture")
schema:register(XMLValueType.FILENAME, basePath .. ".textures#specular", "Path to specular texture")
end
---
-- @param XMLSchema schema
-- @param string basePath
function VehicleMaterial.registerShortXMLPaths(schema, basePath)
schema:register(XMLValueType.STRING, basePath .. "#materialSlotName", "Material slot name in the i3d file")
schema:register(XMLValueType.STRING, basePath .. "#materialTemplateName", "Name of template to apply (all attributes will be used from template)")
schema:registerAutoCompletionDataSource(basePath .. "#materialTemplateName", "$data/shared/brandMaterialTemplates.xml", "templates.template#name")
schema:register(XMLValueType.BOOL, basePath .. "#materialTemplateUseColorOnly", "If 'true', only the color is used from the material template. The rest from the i3d file.", false)
schema:register(XMLValueType.STRING, basePath .. "#materialTemplateNameColor", "Name of the material template that is used ONLY for the color")
schema:registerAutoCompletionDataSource(basePath .. "#materialTemplateNameColor", "$data/shared/brandMaterialTemplates.xml", "templates.template#name")
end