-
Notifications
You must be signed in to change notification settings - Fork 1
/
cvl_main_client.lua
413 lines (330 loc) · 10.3 KB
/
cvl_main_client.lua
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
local CONFIG_PATH = "cvl_config.xml"
local SHADER_PATH = "lights.fx"
--Vehicle streaming out distance is 300
local SHADER_MAX_DISTANCE = 350
local CORONA_OUTER_CONE = 180
local CORONA_INNER_CONE = 90
local vehicleModelDefaultDummiesNames = {
["headlights"] = "light_front_main",
["headlights2"] = "light_front_second",
["taillights"] = "light_rear_main",
["taillights2"] = "light_rear_second"
}
local vehicleModelDefaultDummiesRotation = {
["headlights"] = {0, 0, 0},
["headlights2"] = {0, 0, 0},
["taillights"] = {0, 0, 180},
["taillights2"] = {0, 0, 180},
}
CVL = {}
CVL.globalData = {}
CVL.modelsData = {}
CVL.vehiclesData = {}
function CVL.getDummyPosition(vehicle, dummy)
local defaultDummy = vehicleModelDefaultDummiesNames[dummy]
if defaultDummy then
return getVehicleModelDummyPosition(getElementModel(vehicle), defaultDummy)
end
return getVehicleComponentPosition(vehicle, dummy)
end
function CVL.getDummyRotation(vehicle, dummy)
local defaultDummy = vehicleModelDefaultDummiesNames[dummy]
if defaultDummy then
return unpack(vehicleModelDefaultDummiesRotation[dummy])
end
return getVehicleComponentRotation(vehicle, dummy, "root")
end
function CVL.buildShaderLightsPowerContainer(vehicle)
local lightsPower = (CVL.vehiclesData[vehicle] or {}).power or {}
local shaderLightsPower = {}
for i = 1, 16 do
local packed = 0
for p = 1, 3 do
packed = packed + math.floor((lightsPower[(i-1)*3+p] or 0)*255) * 256^(p-1)
end
shaderLightsPower[i] = packed
end
return shaderLightsPower
end
function CVL.applyShader(vehicle)
local vehicleData = CVL.vehiclesData[vehicle]
if not vehicleData then
vehicleData = {}
CVL.vehiclesData[vehicle] = vehicleData
end
if vehicleData.shader then return false end
local shader = dxCreateShader(SHADER_PATH, CVL.globalData.macros, 0, SHADER_MAX_DISTANCE, false, "vehicle")
dxSetShaderValue(shader, "sDataContainer1", CVL.buildShaderLightsPowerContainer(vehicle))
for i, textureName in ipairs(CVL.globalData.textures) do
engineApplyShaderToWorldTexture(shader, textureName, vehicle, false)
end
vehicleData.shader = shader
return true
end
function CVL.removeShader(vehicle)
local vehicleData = CVL.vehiclesData[vehicle]
if not vehicleData then return false end
local shader = vehicleData.shader
if not shader then return false end
if isElement(shader) then
destroyElement(shader)
end
vehicleData.shader = nil
return true
end
function CVL.createCoronas(vehicle)
local vehicleData = CVL.vehiclesData[vehicle]
if not vehicleData then
vehicleData = {}
CVL.vehiclesData[vehicle] = vehicleData
end
if vehicleData.coronas then return false end
local vehicleLightsData = vehicleData.lights or {}
local modelLightsData = (CVL.modelsData[getElementModel(vehicle)] or {}).lights or {}
local coronas = {}
for lightID, configLight in ipairs(CVL.globalData.lights) do
if configLight.dummy then
local x, y, z = CVL.getDummyPosition(vehicle, configLight.dummy)
if x then
local vehicleLight = vehicleLightsData[lightID] or {}
local modelLight = modelLightsData[lightID] or {}
local rot = vehicleLight.rot or modelLight.rot
if not rot then
rot = {}
local dx, dy, dz = CVL.getDummyPosition(vehicle, configLight.dummy.."_dir")
if dx then
rot[1], rot[2], rot[3] = getRotationFromDirection(dx - x, dy - y, dz - z)
else
rot[1], rot[2], rot[3] = CVL.getDummyRotation(vehicle, configLight.dummy)
end
end
local color = vehicleLight.color or modelLight.color or configLight.color
local size = vehicleLight.size or modelLight.size or configLight.size
local corona = createCustomCorona(
0, 0, 0,
0, 0, 0,
"directional",
size,
color[1]*0.5, color[2]*0.5, color[3]*0.5,
(vehicleLight.power or 0) * color[4])
setCustomCoronaDepthBias(corona, configLight.size/4)
setCustomCoronaLightCone(corona, CORONA_OUTER_CONE, CORONA_INNER_CONE)
attachCustomCorona(corona, vehicle,
x * (configLight.mirrored and -1 or 1), y, z,
rot[1], rot[2], rot[3] * (configLight.mirrored and -1 or 1))
coronas[lightID] = corona
end
end
end
vehicleData.coronas = coronas
return true
end
function CVL.removeCoronas(vehicle)
local vehicleData = CVL.vehiclesData[vehicle]
if not vehicleData then return false end
local coronas = vehicleData.coronas
if not coronas then return false end
vehicleData.coronas = nil
for lightID, corona in pairs(coronas) do
if isElement(corona) then
destroyElement(corona)
end
end
return true
end
function CVL.loadConfig()
local configNode = xmlLoadFile(CONFIG_PATH, true)
local texturesConfigNodeData = xmlNodeGetData(xmlNodeFindChild(configNode, "textures"))
local lightsConfigNodeData = xmlNodeGetData(xmlNodeFindChild(configNode, "lights"))
local modelsConfigNode = xmlNodeFindChild(configNode, "models")
local modelsConfigNodeData = modelsConfigNode and xmlNodeGetData(modelsConfigNode)
xmlUnloadFile(configNode)
local textures = {}
for i, nodeData in ipairs(texturesConfigNodeData.children) do
table.insert(textures, nodeData.attributes.name)
end
local macroFlagsArray = ""
local globalLights = {}
local ids = {}
for i, nodeData in ipairs(lightsConfigNodeData.children) do
local lightData = {
name = nodeData.attributes.name,
dummy = nodeData.attributes.dummy
}
if lightData.dummy then
lightData.mirrored = nodeData.attributes.mirrored == tostring(true)
lightData.size = tonumber(nodeData.attributes.size)
lightData.color = {}
local iterator = string.gmatch(nodeData.attributes.color, "%d+")
for c = 1, 4 do
lightData.color[c] = tonumber(iterator() or 255)
end
end
globalLights[i] = lightData
ids[lightData.name] = i
local flag = {}
local iterator = string.gmatch(nodeData.attributes.flag, "%d+")
for c = 1, 3 do
flag[c] = iterator() or 0
end
macroFlagsArray = macroFlagsArray..string.format("float3(%s),", table.concat(flag, ","))
end
local modelsData = {}
if modelsConfigNodeData then
for i, modelNodeData in ipairs(modelsConfigNodeData.children) do
local modelLights = {}
for j, lightNodeData in ipairs(modelNodeData.children) do
local lightData = {}
if lightNodeData.attributes.color then
lightData.color = {}
local iterator = string.gmatch(lightNodeData.attributes.color, "%d+")
for c = 1, 4 do
lightData.color[c] = tonumber(iterator() or 255)
end
end
if lightNodeData.attributes.size then
lightData.size = tonumber(lightNodeData.attributes.size)
end
if lightNodeData.attributes.rotation then
lightData.rot = {}
local iterator = string.gmatch(lightNodeData.attributes.rotation, "%d+")
for c = 1, 3 do
lightData.rot[c] = tonumber(iterator() or 0)
end
end
modelLights[ids[lightNodeData.attributes.name]] = lightData
end
modelsData[tonumber(modelNodeData.attributes.id)] = {
lights = modelLights
}
end
end
CVL.globalData = {
textures = textures,
lights = globalLights,
ids = ids,
macros = {
MACRO_LIGHTS_FLAGS_ARRAY_SIZE = #globalLights,
MACRO_LIGHTS_FLAGS_ARRAY = macroFlagsArray
}
}
CVL.modelsData = modelsData
return true
end
function CVL.updateAll()
for vehicle, data in pairs(CVL.vehiclesData) do
CVL.removeCoronas(vehicle)
CVL.removeShader(vehicle)
end
for i, vehicle in ipairs(getElementsByType("vehicle", root, true)) do
CVL.applyShader(vehicle)
CVL.createCoronas(vehicle)
end
return true
end
addEventHandler("onClientResourceStart", resourceRoot,
function()
CVL.loadConfig()
CVL.updateAll()
end
)
addEventHandler("onClientElementStreamIn", root,
function()
if getElementType(source) ~= "vehicle" then return end
CVL.applyShader(source)
CVL.createCoronas(source)
end
)
addEventHandler("onClientElementStreamOut", root,
function()
if getElementType(source) ~= "vehicle" then return end
CVL.removeCoronas(source)
CVL.removeShader(source)
end
)
addEventHandler("onClientElementDestroy", root,
function()
if not CVL.vehiclesData[source] then return end
CVL.removeCoronas(source)
CVL.removeShader(source)
CVL.vehiclesData[source] = nil
end
)
function CVL.setLightPower(vehicle, lightID, power)
local vehicleData = CVL.vehiclesData[vehicle]
if not vehicleData then
vehicleData = {}
CVL.vehiclesData[vehicle] = vehicleData
end
local lightsPower = vehicleData.power
if not lightsPower then
lightsPower = {}
vehicleData.power = lightsPower
end
lightsPower[lightID] = power
local shader = vehicleData.shader
if shader then
local shaderLightsPower = CVL.buildShaderLightsPowerContainer(vehicle)
dxSetShaderValue(shader, "sDataContainer1", shaderLightsPower)
end
local corona = (vehicleData.coronas or {})[lightID]
if corona then
local alpha = power * CVL.globalData.lights[lightID].color[4]
setCustomCoronaAlpha(corona, alpha)
end
return true
end
function CVL.setLightSize(vehicle, lightID, size)
local vehicleData = CVL.vehiclesData[vehicle]
if not vehicleData then
vehicleData = {}
CVL.vehiclesData[vehicle] = vehicleData
end
local lightsData = vehicleData.lights
if not lightsData then
lightsData = {}
vehicleData.lights = lightsData
end
local lightData = lightsData[lightID]
if not lightData then
lightData = {}
lightsData[lightID] = lightData
end
lightData.size = size
local corona = (vehicleData.coronas or {})[lightID]
if corona then
local modelSize = (((CVL.modelsData[getElementModel(vehicle)] or {}).lights or {})[lightID] or {}).size
setCustomCoronaSize(corona,
size or modelSize or CVL.globalData.lights[lightID].size)
end
return true
end
function CVL.setLightColor(vehicle, lightID, color)
local vehicleData = CVL.vehiclesData[vehicle]
if not vehicleData then
vehicleData = {}
CVL.vehiclesData[vehicle] = vehicleData
end
local lightsData = vehicleData.lights
if not lightsData then
lightsData = {}
vehicleData.lights = lightsData
end
local lightData = lightsData[lightID]
if not lightData then
lightData = {}
lightsData[lightID] = lightData
end
lightData.color = color
local corona = (vehicleData.coronas or {})[lightID]
if corona then
local modelColor = (((CVL.modelsData[getElementModel(vehicle)] or {}).lights or {})[lightID] or {}).color
color = color or modelColor or CVL.lightsData[lightID].color
setCustomCoronaColor(corona,
color[1] * 0.5,
color[2] * 0.5,
color[3] * 0.5,
((vehicleData.power or {})[lightID] or 0) * color[4])
end
return true
end