forked from ZeroK-RTS/Zero-K
/
api_custom_unit_shaders.lua
648 lines (550 loc) · 20.8 KB
/
api_custom_unit_shaders.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
-- $Id$
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- author: jK
--
-- Copyright (C) 2008,2009,2010.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadget:GetInfo()
return {
name = "CustomUnitShaders",
desc = "allows to override the engine unit and feature shaders",
author = "jK, gajop",
date = "2008,2009,2010,2016",
license = "GNU GPL, v2 or later",
layer = 1,
enabled = Spring.Utilities.IsCurrentVersionNewerThan(100, 0) -- loaded by default?
}
end
if not Spring.Utilities.IsCurrentVersionNewerThan(100, 0) then
return
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Synced
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
if (gadgetHandler:IsSyncedCode()) then
return
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Unsynced
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
if not gl.CreateShader then
Spring.Log("CUS", LOG.WARNING, "Shaders not supported, disabling")
return
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
VFS.Include("luarules/utilities/unitrendering.lua", nil, VFS.MOD .. VFS.BASE)
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local shadows = false
local advShading = false
local normalmapping = false
local unitRendering = {
drawList = {},
materialInfos = {},
bufMaterials = {},
materialDefs = {},
loadedTextures = {},
spGetAllObjects = Spring.GetAllUnits,
spGetObjectPieceList = Spring.GetUnitPieceList,
spGetMaterial = Spring.UnitRendering.GetMaterial,
spSetMaterial = Spring.UnitRendering.SetMaterial,
spActivateMaterial = Spring.UnitRendering.ActivateMaterial,
spDeactivateMaterial = Spring.UnitRendering.DeactivateMaterial,
spSetObjectLuaDraw = Spring.UnitRendering.SetUnitLuaDraw,
spSetLODCount = Spring.UnitRendering.SetLODCount,
spSetPieceList = Spring.UnitRendering.SetPieceList,
DrawObject = "DrawUnit",
ObjectCreated = "UnitCreated",
ObjectDestroyed = "UnitDestroyed",
}
local featureRendering = {
drawList = {},
materialInfos = {},
bufMaterials = {},
materialDefs = {},
loadedTextures = {},
spGetAllObjects = Spring.GetAllFeatures,
spGetObjectPieceList = Spring.GetFeaturePieceList,
spGetMaterial = Spring.FeatureRendering.GetMaterial,
spSetMaterial = Spring.FeatureRendering.SetMaterial,
spActivateMaterial = Spring.FeatureRendering.ActivateMaterial,
spDeactivateMaterial = Spring.FeatureRendering.DeactivateMaterial,
spSetObjectLuaDraw = Spring.FeatureRendering.SetFeatureLuaDraw,
spSetLODCount = Spring.FeatureRendering.SetLODCount,
spSetPieceList = Spring.FeatureRendering.SetPieceList,
DrawObject = "DrawFeature",
ObjectCreated = "FeatureCreated",
ObjectDestroyed = "FeatureDestroyed",
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local _plugins = nil
local function InsertPlugin(str)
--str = str:upper()
return (_plugins and _plugins[str]) or ""
end
local function CompileShader(shader, definitions, plugins)
shader.vertexOrig = shader.vertex
shader.fragmentOrig = shader.fragment
shader.geometryOrig = shader.geometry
--// insert small pieces of code named `plugins`
--// this way we can use a basic shader and add some simple vertex animations etc.
do
if (plugins) then
_plugins = plugins
end
if (shader.vertex)
then shader.vertex = shader.vertex:gsub("%%%%([%a_]+)%%%%", InsertPlugin); end
if (shader.fragment)
then shader.fragment = shader.fragment:gsub("%%%%([%a_]+)%%%%", InsertPlugin); end
if (shader.geometry)
then shader.geometry = shader.geometry:gsub("%%%%([%a_]+)%%%%", InsertPlugin); end
_plugins = nil
end
--// append definitions at top of the shader code
--// (this way we can modularize a shader and enable/disable features in it)
if (definitions or shadows) then
definitions = definitions or {}
hasVersion = false
for _, def in pairs(definitions) do
hasVersion = hasVersion or string.sub(def,1,string.len("#version")) == "#version"
end
if not hasVersion then
table.insert(definitions, 1, "#version 130")
end
if (shadows) then
table.insert(definitions, "#define use_shadows")
end
definitions = table.concat(definitions, "\n") .. "\n"
if (shader.vertex)
then shader.vertex = definitions .. shader.vertex; end
if (shader.fragment)
then shader.fragment = definitions .. shader.fragment; end
if (shader.geometry)
then shader.geometry = definitions .. shader.geometry; end
end
local GLSLshader = gl.CreateShader(shader)
local errorLog = gl.GetShaderLog()
if not GLSLshader or errorLog ~= "" then
local function log (x)
Spring.Log("CUS", GLSLshader and LOG.NOTICE or LOG.ERROR, x)
end
if not GLSLshader then -- failing shaders are technically a runtime error
log("LUA_ERRSHADER") -- but LUA_ERRRUN gets picked up by crash reporter
end -- and we don't want to spam people with popups
-- since they aren't likely to lead to any change
log("CUS shader log -----------------------------\n" .. errorLog)
log("Vertex -------------------------------------\n" .. (shader.vertex or "nil"))
log("Fragment -----------------------------------\n" .. (shader.fragment or "nil"))
log("Geometry -----------------------------------\n" .. (shader.geometry or "nil"))
log("CUS end ------------------------------------")
end
shader.vertex = shader.vertexOrig
shader.fragment = shader.fragmentOrig
shader.geometry = shader.geometryOrig
return GLSLshader
end
local function _CompileMaterialShaders(rendering)
for _,mat_src in pairs(rendering.materialDefs) do
if (mat_src.shaderSource) then
local GLSLshader = CompileShader(mat_src.shaderSource, mat_src.shaderDefinitions, mat_src.shaderPlugins)
if (GLSLshader) then
if (mat_src.standardShader) then
gl.DeleteShader(mat_src.standardShader)
end
mat_src.standardShader = GLSLshader
mat_src.standardUniforms = {
cameraloc = gl.GetUniformLocation(GLSLshader, "camera"),
camerainvloc = gl.GetUniformLocation(GLSLshader, "cameraInv"),
cameraposloc = gl.GetUniformLocation(GLSLshader, "cameraPos"),
shadowmatrixloc = gl.GetUniformLocation(GLSLshader, "shadowMatrix"),
shadowparamsloc = gl.GetUniformLocation(GLSLshader, "shadowParams"),
sunposloc = gl.GetUniformLocation(GLSLshader, "sunPos"),
simframeloc = gl.GetUniformLocation(GLSLshader, "simFrame"),
}
end
end
if (mat_src.deferredSource) then
local GLSLshader = CompileShader(mat_src.deferredSource, mat_src.deferredDefinitions, mat_src.deferredPlugins)
if (GLSLshader) then
if (mat_src.deferredShader) then
gl.DeleteShader(mat_src.deferredShader)
end
mat_src.deferredShader = GLSLshader
mat_src.deferredUniforms = {
cameraloc = gl.GetUniformLocation(GLSLshader, "camera"),
camerainvloc = gl.GetUniformLocation(GLSLshader, "cameraInv"),
cameraposloc = gl.GetUniformLocation(GLSLshader, "cameraPos"),
shadowmatrixloc = gl.GetUniformLocation(GLSLshader, "shadowMatrix"),
shadowparamsloc = gl.GetUniformLocation(GLSLshader, "shadowParams"),
sunposloc = gl.GetUniformLocation(GLSLshader, "sunPos"),
simframeloc = gl.GetUniformLocation(GLSLshader, "simFrame"),
}
end
end
end
end
local function CompileMaterialShaders()
_CompileMaterialShaders(unitRendering)
_CompileMaterialShaders(featureRendering)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function GetObjectMaterial(rendering, objectDefID)
local mat = rendering.bufMaterials[objectDefID]
if mat then
return mat
end
local matInfo = rendering.materialInfos[objectDefID]
local mat = rendering.materialDefs[matInfo[1]]
if type(objectDefID) == "number" then
-- Non-number objectDefIDs are default material overrides. They will have
-- their textures defined in the unit materials files.
matInfo.UNITDEFID = objectDefID
matInfo.FEATUREDEFID = -objectDefID
end
--// find unitdef tex keyword and replace it
--// (a shader can be just for multiple unitdefs, so we support this keywords)
local texUnits = {}
for texid, tex in pairs(mat.texunits or {}) do
local tex_ = tex
for varname, value in pairs(matInfo) do
tex_ = tex_:gsub("%%"..tostring(varname),value)
end
texUnits[texid] = {tex=tex_, enable=false}
end
--// materials don't load those textures themselves
if (texUnits[1]) then
local texdl = gl.CreateList(function()
for _,tex in pairs(texUnits) do
local prefix = tex.tex:sub(1,1)
if (prefix~="%")
and(prefix~="#")
and(prefix~="!")
and(prefix~="$")
then
gl.Texture(tex.tex)
rendering.loadedTextures[#rendering.loadedTextures+1] = tex.tex
end
end
end)
gl.DeleteList(texdl)
end
local luaMat = rendering.spGetMaterial("opaque", {
standardshader = mat.standardShader,
deferredshader = mat.deferredShader,
standarduniforms = mat.standardUniforms,
deferreduniforms = mat.deferredUniforms,
usecamera = mat.usecamera,
culling = mat.culling,
texunits = texUnits,
prelist = mat.predl,
postlist = mat.postdl,
})
rendering.bufMaterials[objectDefID] = luaMat
return luaMat
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function ResetUnit(unitID)
local unitDefID = Spring.GetUnitDefID(unitID)
gadget:RenderUnitDestroyed(unitID, unitDefID)
Spring.UnitRendering.DeactivateMaterial(unitID, 3)
if not select(3,Spring.GetUnitIsStunned(unitID)) then --// inbuild?
gadget:UnitFinished(unitID,unitDefID)
end
end
local function ResetFeature(featureID)
gadget:FeatureDestroyed(featureID)
Spring.FeatureRendering.DeactivateMaterial(featureID, 3)
gadget:FeatureCreated(featureID)
end
local function ToggleShadows()
shadows = Spring.HaveShadows()
CompileMaterialShaders()
unitRendering.bufMaterials = {}
local units = Spring.GetAllUnits()
for _, unitID in pairs(units) do
ResetUnit(unitID)
end
featureRendering.bufMaterials = {}
local features = Spring.GetAllFeatures()
for _, featureID in pairs(features) do
ResetFeature(featureID)
end
end
local function ToggleAdvShading()
advShading = Spring.HaveAdvShading()
if (not advShading) then
--// unload all materials
unitRendering.drawList = {}
local units = Spring.GetAllUnits()
for _,unitID in pairs(units) do
ResetUnit(unitID)
end
featureRendering.drawList = {}
local features = Spring.GetAllFeatures()
for _, featureID in pairs(features) do
ResetFeature(featureID)
end
elseif (normalmapping) then
--// reinitializes all shaders
ToggleShadows()
end
end
function ToggleNormalmapping(_,newSetting,_, playerID)
if (playerID ~= Spring.GetMyPlayerID()) then
return
end
if newSetting and newSetting~="" then
normalmapping = (newSetting=="1")
elseif not newSetting or newSetting=="" then
normalmapping = not normalmapping
end
Spring.SetConfigInt("NormalMapping", (normalmapping and 1) or 0)
Spring.Echo("normalmapping is " .. (normalmapping and "enabled" or "disabled"))
if (not normalmapping) then
--// unload normalmapped materials
local units = Spring.GetAllUnits()
for _,unitID in pairs(units) do
local unitDefID = Spring.GetUnitDefID(unitID)
local unitMat = unitRendering.materialInfos[unitDefID]
if (unitMat) then
local mat = unitRendering.materialDefs[unitMat[1]]
if (not mat.force) then
gadget:RenderUnitDestroyed(unitID, unitDefID)
end
end
end
local features = Spring.GetAllFeatures()
for _,featureID in pairs(features) do
local featureDefID = Spring.GetFeatureDefID(featureID)
local featureMat = featureRendering.materialInfos[featureDefID]
if (featureMat) then
local mat = featureRendering.materialDefs[featureMat[1]]
if (not mat.force) then
gadget:FeatureDestroyed(featureID,featureDefID)
end
end
end
elseif (advShading) then
--// reinitializes all shaders
ToggleShadows()
end
end
local n = -1
function gadget:Update()
if (n<Spring.GetDrawFrame()) then
n = Spring.GetDrawFrame() + Spring.GetFPS()
if (advShading ~= Spring.HaveAdvShading()) then
ToggleAdvShading()
elseif (advShading)and(normalmapping)and(shadows ~= Spring.HaveShadows()) then
ToggleShadows()
end
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function GetShaderOverride(objectID, objectDefID)
if Spring.ValidUnitID(objectID) then
return Spring.GetUnitRulesParam(objectID, "comm_texture")
end
return false
end
function ObjectFinished(rendering, objectID, objectDefID)
if not advShading then
return
end
objectDefID = GetShaderOverride(objectID, objectDefID) or objectDefID
local objectMat = rendering.materialInfos[objectDefID]
if objectMat then
local mat = rendering.materialDefs[objectMat[1]]
if (normalmapping or mat.force) then
rendering.spActivateMaterial(objectID, 3)
rendering.spSetMaterial(objectID,3,"opaque", GetObjectMaterial(rendering, objectDefID))
for pieceID in ipairs(rendering.spGetObjectPieceList(objectID) or {}) do
rendering.spSetPieceList(objectID, 3, pieceID)
end
local DrawObject = mat[rendering.DrawObject]
local ObjectCreated = mat[rendering.ObjectCreated]
if DrawObject then
rendering.spSetObjectLuaDraw(objectID, true)
rendering.drawList[objectID] = mat
end
if ObjectCreated then
ObjectCreated(objectID, mat, 3)
end
end
end
end
function gadget:UnitFinished(unitID, unitDefID)
ObjectFinished(unitRendering, unitID, unitDefID)
end
function gadget:FeatureCreated(featureID)
ObjectFinished(featureRendering, featureID, Spring.GetFeatureDefID(featureID))
end
local function ObjectDestroyed(rendering, objectID, objectDefID)
rendering.spDeactivateMaterial(objectID, 3)
local mat = rendering.drawList[objectID]
if mat then
local _ObjectDestroyed = mat[rendering.ObjectDestroyed]
if _ObjectDestroyed then
_ObjectDestroyed(objectID, 3)
end
rendering.drawList[objectID] = nil
end
end
function gadget:RenderUnitDestroyed(unitID, unitDefID)
ObjectDestroyed(unitRendering, unitID, unitDefID)
end
function gadget:FeatureDestroyed(featureID)
ObjectDestroyed(featureRendering, featureID, Spring.GetFeatureDefID(featureID))
end
local function DrawObject(rendering, objectID, drawMode)
local mat = rendering.drawList[objectID]
if not mat then
return
end
local _DrawObject = mat[rendering.DrawObject]
if _DrawObject then
return _DrawObject(objectID, mat, drawMode)
end
end
---------------------------
-- DrawUnit(unitID,DrawMode)
-- With enum DrawMode {
-- notDrawing = 0,
-- normalDraw = 1,
-- shadowDraw = 2,
-- reflectionDraw = 3,
-- refractionDraw = 4,
-- luaMaterialDraw = 5,
-- };
-----------------
function gadget:DrawUnit(unitID, drawMode)
return DrawObject(unitRendering, unitID, drawMode)
end
function gadget:DrawFeature(featureID, drawMode)
return DrawObject(featureRendering, featureID, drawMode)
end
gadget.UnitReverseBuilt = gadget.RenderUnitDestroyed
gadget.UnitCloaked = gadget.RenderUnitDestroyed
gadget.UnitDecloaked = gadget.UnitFinished
-- NOTE: No feature equivalent (features can't change team)
function gadget:UnitGiven(unitID,...)
if not select(3, Spring.GetUnitIsStunned(unitID)) then
gadget:RenderUnitDestroyed(unitID, ...)
gadget:UnitFinished(unitID, ...)
end
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadget:GameFrame()
for _, uid in ipairs(Spring.GetAllUnits()) do
if not select(3,Spring.GetUnitIsStunned(uid)) then --// inbuild?
gadget:UnitFinished(uid, Spring.GetUnitDefID(uid), Spring.GetUnitTeam(uid))
end
end
for _, fid in ipairs(Spring.GetAllFeatures()) do
gadget:FeatureCreated(fid, Spring.GetFeatureDefID(fid), Spring.GetFeatureTeam(fid))
end
gadgetHandler:RemoveCallIn('GameFrame')
end
--// Workaround: unsynced LuaRules doesn't receive Shutdown events
Shutdown = Script.CreateScream()
local function _CleanupTextures(rendering)
for i = 1, #rendering.loadedTextures do
gl.DeleteTexture(rendering.loadedTextures[i])
end
for _, oid in ipairs(rendering.spGetAllObjects()) do
rendering.spSetLODCount(oid, 0)
end
end
Shutdown.func = function()
--// unload textures, so the user can do a `/luarules reload` to reload the normalmaps
_CleanupTextures(unitRendering)
_CleanupTextures(featureRendering)
end
local function _LoadMaterialConfigFiles(path)
local unitMaterialDefs = {}
local featureMaterialDefs = {}
local files = VFS.DirList(path)
table.sort(files)
for i = 1, #files do
local mats, unitMats = VFS.Include(files[i])
for k, v in pairs(mats) do
-- Spring.Echo(files[i],'is a feature?',v.feature)
local rendering
if v.feature then
rendering = featureRendering
else
rendering = unitRendering
end
if not rendering.materialDefs[k] then
rendering.materialDefs[k] = v
end
end
for k, v in pairs(unitMats) do
--// we check if the material is defined as a unit or as feature material (one namespace for both!!)
local materialDefs
if featureRendering.materialDefs[v[1]] then
materialDefs = featureMaterialDefs
else
materialDefs = unitMaterialDefs
end
if not materialDefs[k] then
materialDefs[k] = v
end
end
end
return unitMaterialDefs, featureMaterialDefs
end
local function _ProcessMaterials(rendering, materialDefs)
local engineShaderTypes = {"3do", "s3o", "obj", "ass"}
for _, mat_src in pairs(rendering.materialDefs) do
--mat_src = {shader = include("ModelMaterials/Shaders/default.lua") or "s3o"}
if mat_src.shader ~= nil and engineShaderTypes[mat_src.shader] == nil then
mat_src.shaderSource = mat_src.shader
mat_src.shader = nil
end
if mat_src.deferred ~= nil and engineShaderTypes[mat_src.deferred] == nil then
mat_src.deferredSource = mat_src.deferred
mat_src.deferred = nil
end
end
_CompileMaterialShaders(rendering)
for objectDefID, materialInfo in pairs(materialDefs) do
if (type(materialInfo) ~= "table") then
materialInfo = {materialInfo}
end
rendering.materialInfos[objectDefID] = materialInfo
end
end
function gadget:Initialize()
--// check user configs
shadows = Spring.HaveShadows()
advShading = Spring.HaveAdvShading()
normalmapping = (Spring.GetConfigInt("NormalMapping", 1)>0)
--// load the materials config files
local MATERIALS_DIR = "ModelMaterials/"
if not Spring.Utilities.IsCurrentVersionNewerThan(103, 0) then
MATERIALS_DIR = "ModelMaterials_103/"
end
local unitMaterialDefs, featureMaterialDefs = _LoadMaterialConfigFiles(MATERIALS_DIR)
--// process the materials (compile shaders, load textures, ...)
_ProcessMaterials(unitRendering, unitMaterialDefs)
_ProcessMaterials(featureRendering, featureMaterialDefs)
--// insert synced actions
gadgetHandler:AddSyncAction("unitshaders_reverse", UnitReverseBuilt)
gadgetHandler:AddChatAction("normalmapping", ToggleNormalmapping)
end