PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/mods/zk/LuaRules/Gadgets/CustomUnitShaders/UnitMaterials/1_normalmapping.lua

http://zero-k.googlecode.com/
Lua | 136 lines | 98 code | 23 blank | 15 comment | 25 complexity | 131ec04c00a7a812fb90081935406232 MD5 | raw file
Possible License(s): GPL-2.0, MIT, BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. -- $Id$
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. local materials = {
  5. normalMappedS3o = {
  6. shaderDefinitions = {
  7. "#define use_perspective_correct_shadows",
  8. "#define use_normalmapping",
  9. --"#define flip_normalmap",
  10. },
  11. shader = include(GADGET_DIR .. "UnitMaterials/Shaders/default.lua"),
  12. usecamera = false,
  13. culling = GL.BACK,
  14. predl = nil,
  15. postdl = nil,
  16. texunits = {
  17. [0] = '%%UNITDEFID:0',
  18. [1] = '%%UNITDEFID:1',
  19. [2] = '$shadow',
  20. [3] = '$specular',
  21. [4] = '$reflection',
  22. [5] = '%NORMALTEX',
  23. },
  24. },
  25. }
  26. --------------------------------------------------------------------------------
  27. --------------------------------------------------------------------------------
  28. -- Automated normalmap detection
  29. local unitMaterials = {}
  30. local function FindNormalmap(tex1, tex2)
  31. local normaltex
  32. --// check if there is a corresponding _normals.dds file
  33. if (VFS.FileExists(tex1)) then
  34. local basefilename = tex1:gsub("%....","")
  35. if (tonumber(basefilename:sub(-1,-1))) then
  36. basefilename = basefilename:sub(1,-2)
  37. end
  38. if (basefilename:sub(-1,-1) == "_") then
  39. basefilename = basefilename:sub(1,-2)
  40. end
  41. normaltex = basefilename .. "_normals.dds"
  42. if (not VFS.FileExists(normaltex)) then
  43. normaltex = nil
  44. end
  45. end --if FileExists
  46. if (not normaltex) and tex2 and (VFS.FileExists(tex2)) then
  47. local basefilename = tex2:gsub("%....","")
  48. if (tonumber(basefilename:sub(-1,-1))) then
  49. basefilename = basefilename:sub(1,-2)
  50. end
  51. if (basefilename:sub(-1,-1) == "_") then
  52. basefilename = basefilename:sub(1,-2)
  53. end
  54. normaltex = basefilename .. "_normals.dds"
  55. if (not VFS.FileExists(normaltex)) then
  56. normaltex = nil
  57. end
  58. end
  59. return normaltex
  60. end
  61. for i=1,#UnitDefs do
  62. local udef = UnitDefs[i]
  63. if (udef.customParams.normaltex and VFS.FileExists(udef.customParams.normaltex)) then
  64. unitMaterials[udef.name] = {"normalMappedS3o", NORMALTEX = udef.customParams.normaltex}
  65. elseif (udef.model.type == "s3o") then
  66. local modelpath = udef.model.path
  67. if (modelpath) then
  68. --// udef.model.textures is empty at gamestart, so read the texture filenames from the s3o directly
  69. local rawstr = VFS.LoadFile(modelpath)
  70. local header = rawstr:sub(1,60)
  71. local texPtrs = VFS.UnpackU32(header, 45, 2)
  72. local tex1,tex2
  73. if (texPtrs[2] > 0)
  74. then tex2 = "unittextures/" .. rawstr:sub(texPtrs[2]+1, rawstr:len()-1)
  75. else texPtrs[2] = rawstr:len() end
  76. if (texPtrs[1] > 0)
  77. then tex1 = "unittextures/" .. rawstr:sub(texPtrs[1]+1, texPtrs[2]-1) end
  78. -- output units without tex2
  79. if not tex2 then
  80. Spring.Log(gadget:GetInfo().name, LOG.WARNING, "CustomUnitShaders: " .. udef.name .. " no tex2")
  81. end
  82. local normaltex = FindNormalmap(tex1,tex2)
  83. if (normaltex and not unitMaterials[udef.name]) then
  84. unitMaterials[udef.name] = {"normalMappedS3o", NORMALTEX = normaltex}
  85. end
  86. end --if model
  87. elseif (udef.model.type == "obj") then
  88. local modelinfopath = udef.model.path
  89. if (modelinfopath) then
  90. modelinfopath = modelinfopath .. ".lua"
  91. if (VFS.FileExists(modelinfopath)) then
  92. local infoTbl = Include(modelinfopath)
  93. if (infoTbl) then
  94. local tex1 = "unittextures/" .. (infoTbl.tex1 or "")
  95. local tex2 = "unittextures/" .. (infoTbl.tex2 or "")
  96. -- output units without tex2
  97. if not tex2 then
  98. Spring.Log(gadget:GetInfo().name, LOG.WARNING, "CustomUnitShaders: " .. udef.name .. " no tex2")
  99. end
  100. local normaltex = FindNormalmap(tex1,tex2)
  101. if (normaltex and not unitMaterials[udef.name]) then
  102. unitMaterials[udef.name] = {"normalMappedS3o", NORMALTEX = normaltex}
  103. end
  104. end
  105. end
  106. end
  107. end --elseif
  108. end --for
  109. --------------------------------------------------------------------------------
  110. --------------------------------------------------------------------------------
  111. return materials, unitMaterials
  112. --------------------------------------------------------------------------------
  113. --------------------------------------------------------------------------------