201 | function BaseMaterial:applyBaseMaterialConfiguration(xmlFile, configName, configId) |
202 | local spec = self.spec_baseMaterial |
203 | |
204 | local baseKey = string.format("vehicle.%sConfigurations", configName) |
205 | local i = 0 |
206 | |
207 | while true do |
208 | local key = string.format("%s.material(%d)", baseKey, i) |
209 | |
210 | if not hasXMLProperty(xmlFile, key) then |
211 | break |
212 | end |
213 | |
214 | local name = getXMLString(xmlFile, key .. "#name") |
215 | if not ClassUtil.getIsValidIndexName(name) then |
216 | g_logManager:xmlWarning(self.configFileName, "Given material name '%s' is not valid for material '%s'", name, key) |
217 | return false |
218 | end |
219 | |
220 | local shaderParameterName = getXMLString(xmlFile, key .. "#shaderParameter") |
221 | if not ClassUtil.getIsValidIndexName(shaderParameterName) then |
222 | g_logManager:xmlWarning(self.configFileName, "Given shader parameter '%s' is not valid for material '%s'", name, key) |
223 | return false |
224 | end |
225 | |
226 | local material = spec.nameToMaterial[name] |
227 | if material == nil then |
228 | g_logManager:xmlWarning(self.configFileName, "Given material name '%s' not found for material configuration '%s'", name, key) |
229 | return false |
230 | end |
231 | |
232 | local shaderParameter = material.nameToShaderParameter[shaderParameterName] |
233 | if shaderParameter == nil then |
234 | g_logManager:xmlWarning(self.configFileName, "Given shader parameter '%s' not found for material configuration '%s'", shaderParameterName, key) |
235 | return false |
236 | end |
237 | |
238 | local color |
239 | local colorStr = getXMLString(xmlFile, key.."#color") |
240 | if colorStr ~= nil then |
241 | color = g_brandColorManager:getBrandColorByName(colorStr) |
242 | if color == nil then |
243 | color = ConfigurationUtil.getColorFromString(colorStr) |
244 | end |
245 | else |
246 | color = ConfigurationUtil.getColorByConfigId(self, configName, configId) |
247 | if color == nil then |
248 | g_logManager(self.configFileName, "Color not found for configId '%d' for material configuration '%s'", configId, key) |
249 | return false |
250 | end |
251 | end |
252 | |
253 | local materialId = getXMLInt(xmlFile, key.."#material") |
254 | materialId = materialId or ConfigurationUtil.getMaterialByConfigId(self, configName, configId) |
255 | |
256 | shaderParameter.value[1] = color[1] |
257 | shaderParameter.value[2] = color[2] |
258 | shaderParameter.value[3] = color[3] |
259 | shaderParameter.value[4] = materialId or shaderParameter.value[4] |
260 | |
261 | if Utils.getNoNil(getXMLBool(xmlFile, key .. "#useContrastColor"), false) then |
262 | local brightness = MathUtil.getBrightnessFromColor(color[1], color[2], color[3]) |
263 | local threshold = getXMLFloat(xmlFile, key .. "#contrastThreshold") or 0.5 |
264 | if brightness > threshold then |
265 | brightness = 1 |
266 | else |
267 | brightness = 0 |
268 | end |
269 | |
270 | shaderParameter.value[1] = 1-brightness |
271 | shaderParameter.value[2] = 1-brightness |
272 | shaderParameter.value[3] = 1-brightness |
273 | end |
274 | |
275 | i = i + 1 |
276 | end |
277 | end |
80 | function BaseMaterial:loadBaseMaterialFromXML(xmlFile, key, material) |
81 | local name = getXMLString(xmlFile, key.."#name") |
82 | if name == nil then |
83 | g_logManager:xmlWarning(self.configFileName, "Missing material name for base material '%s'", key) |
84 | return false |
85 | end |
86 | if not ClassUtil.getIsValidIndexName(name) then |
87 | g_logManager:xmlWarning(self.configFileName, "Given material name '%s' is not valid for material '%s'", name, key) |
88 | return false |
89 | end |
90 | |
91 | local baseNode = I3DUtil.indexToObject(self.components, getXMLString(xmlFile, key.."#baseNode"), self.i3dMappings) |
92 | if baseNode == nil then |
93 | g_logManager:xmlWarning(self.configFileName, "Missing baseNode base material '%s'", key) |
94 | return false |
95 | else |
96 | if not getHasClassId(baseNode, ClassIds.SHAPE) then |
97 | g_logManager:xmlWarning(self.configFileName, "Material baseNode is not a shape '%s'", key) |
98 | return false |
99 | end |
100 | end |
101 | |
102 | material.name = name |
103 | material.baseNode = baseNode |
104 | material.materialId = getMaterial(baseNode, 0) |
105 | material.nameToShaderParameter = {} |
106 | material.shaderParameters = {} |
107 | |
108 | local i = 0 |
109 | while true do |
110 | local parameterKey = string.format("%s.shaderParameter(%d)", key, i) |
111 | if not hasXMLProperty(xmlFile, parameterKey) then |
112 | break |
113 | end |
114 | |
115 | local shaderParameter = {} |
116 | if self:loadBaseMaterialParameterFromXML(xmlFile, parameterKey, shaderParameter, material.baseNode) then |
117 | if material.nameToShaderParameter[shaderParameter.name] == nil then |
118 | material.nameToShaderParameter[shaderParameter.name] = shaderParameter |
119 | table.insert(material.shaderParameters, shaderParameter) |
120 | else |
121 | g_logManager:xmlWarning(self.configFileName, "shaderParameter '%s' already defined for material '%s'!", shaderParameter.name, key) |
122 | end |
123 | end |
124 | |
125 | i = i + 1 |
126 | end |
127 | |
128 | if #material.shaderParameters == 0 then |
129 | g_logManager:xmlWarning(self.configFileName, "Missing shaderParameters for base material '%s'", key) |
130 | return false |
131 | end |
132 | |
133 | return true |
134 | end |
138 | function BaseMaterial:loadBaseMaterialParameterFromXML(xmlFile, key, shaderParameter, node) |
139 | local name = getXMLString(xmlFile, key.."#name") |
140 | |
141 | if name == nil then |
142 | g_logManager:xmlWarning(self.configFileName, "Missing shader parameter name for base material '%s'", key) |
143 | return false |
144 | end |
145 | if not ClassUtil.getIsValidIndexName(name) then |
146 | g_logManager:xmlWarning(self.configFileName, "Given shader parameter name '%s' is not valid for base material '%s'", name, key) |
147 | return false |
148 | end |
149 | |
150 | local value = g_brandColorManager:loadColorAndMaterialFromXML(self.configFileName, node, name, xmlFile, key) |
151 | if value == nil then |
152 | g_logManager:xmlWarning(self.configFileName, "Failed to load shader parameter value or material for base material '%s'", key) |
153 | return false |
154 | end |
155 | |
156 | shaderParameter.name = name |
157 | shaderParameter.value = value |
158 | |
159 | return true |
160 | end |
45 | function BaseMaterial:onLoad(savegame) |
46 | local spec = self.spec_baseMaterial |
47 | |
48 | spec.baseMaterials = {} |
49 | spec.nameToMaterial = {} |
50 | |
51 | local i = 0 |
52 | while true do |
53 | local key = string.format("vehicle.baseMaterial.material(%d)", i) |
54 | if not hasXMLProperty(self.xmlFile, key) then |
55 | break |
56 | end |
57 | |
58 | local baseMaterial = {} |
59 | if self:loadBaseMaterialFromXML(self.xmlFile, key, baseMaterial) then |
60 | spec.nameToMaterial[baseMaterial.name] = baseMaterial |
61 | table.insert(spec.baseMaterials, baseMaterial) |
62 | end |
63 | |
64 | i = i + 1 |
65 | end |
66 | |
67 | if self.configurations["baseMaterial"] ~= nil then |
68 | self:applyBaseMaterialConfiguration(self.xmlFile, "baseMaterial", self.configurations["baseMaterial"]) |
69 | end |
70 | |
71 | if self.configurations["designMaterial"] ~= nil then |
72 | self:applyBaseMaterialConfiguration(self.xmlFile, "designMaterial", self.configurations["designMaterial"]) |
73 | end |
74 | |
75 | self:applyBaseMaterial() |
76 | end |
21 | function BaseMaterial.registerFunctions(vehicleType) |
22 | SpecializationUtil.registerFunction(vehicleType, "loadBaseMaterialFromXML", BaseMaterial.loadBaseMaterialFromXML) |
23 | SpecializationUtil.registerFunction(vehicleType, "loadBaseMaterialParameterFromXML", BaseMaterial.loadBaseMaterialParameterFromXML) |
24 | SpecializationUtil.registerFunction(vehicleType, "applyBaseMaterialConfiguration", BaseMaterial.applyBaseMaterialConfiguration) |
25 | SpecializationUtil.registerFunction(vehicleType, "applyBaseMaterial", BaseMaterial.applyBaseMaterial) |
26 | SpecializationUtil.registerFunction(vehicleType, "setBaseMaterial", BaseMaterial.setBaseMaterial) |
27 | SpecializationUtil.registerFunction(vehicleType, "setBaseMaterialColor", BaseMaterial.setBaseMaterialColor) |
28 | end |
175 | function BaseMaterial:setBaseMaterial(node, material) |
176 | if getHasClassId(node, ClassIds.SHAPE) then |
177 | local nodeMaterialId = getMaterial(node, 0) |
178 | if material.materialId == nodeMaterialId then |
179 | for i=#material.shaderParameters, 1, -1 do |
180 | local parameter = material.shaderParameters[i] |
181 | if getHasShaderParameter(node, parameter.name) then |
182 | setShaderParameter(node, parameter.name, parameter.value[1], parameter.value[2], parameter.value[3], parameter.value[4], false) |
183 | else |
184 | g_logManager:xmlWarning(self.configFileName, "ShaderParameter '%s' not found for material '%s'!", parameter.name, material.name) |
185 | table.remove(material.shaderParameters, i) |
186 | end |
187 | end |
188 | end |
189 | end |
190 | |
191 | local numChildren = getNumOfChildren(node) |
192 | if numChildren > 0 then |
193 | for i=0, numChildren-1 do |
194 | self:setBaseMaterial(getChildAt(node, i), material) |
195 | end |
196 | end |
197 | end |