2
0

shadows_component.lua 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. --Minetest
  2. --Copyright (C) 2021-2 x2048
  3. --Copyright (C) 2022-3 rubenwardy
  4. --
  5. --This program is free software; you can redistribute it and/or modify
  6. --it under the terms of the GNU Lesser General Public License as published by
  7. --the Free Software Foundation; either version 2.1 of the License, or
  8. --(at your option) any later version.
  9. --
  10. --This program is distributed in the hope that it will be useful,
  11. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. --GNU Lesser General Public License for more details.
  14. --
  15. --You should have received a copy of the GNU Lesser General Public License along
  16. --with this program; if not, write to the Free Software Foundation, Inc.,
  17. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. local shadow_levels_labels = {
  19. fgettext("Disabled"),
  20. fgettext("Very Low"),
  21. fgettext("Low"),
  22. fgettext("Medium"),
  23. fgettext("High"),
  24. fgettext("Very High"),
  25. fgettext("Custom"),
  26. }
  27. local PRESET_DISABLED = 1
  28. local PRESET_CUSTOM = #shadow_levels_labels
  29. -- max distance, texture size, texture_32bit, filters, map color
  30. local shadow_presets = {
  31. [2] = { 62, 512, true, 0, false },
  32. [3] = { 93, 1024, true, 0, false },
  33. [4] = { 140, 2048, true, 1, false },
  34. [5] = { 210, 4096, true, 2, true },
  35. [6] = { 300, 8192, true, 2, true },
  36. }
  37. local function detect_mapping_idx()
  38. if not core.settings:get_bool("enable_dynamic_shadows", false) then
  39. return PRESET_DISABLED
  40. end
  41. local shadow_map_max_distance = tonumber(core.settings:get("shadow_map_max_distance"))
  42. local shadow_map_texture_size = tonumber(core.settings:get("shadow_map_texture_size"))
  43. local shadow_map_texture_32bit = core.settings:get_bool("shadow_map_texture_32bit", false)
  44. local shadow_filters = tonumber(core.settings:get("shadow_filters"))
  45. local shadow_map_color = core.settings:get_bool("shadow_map_color", false)
  46. for i = 2, 6 do
  47. local preset = shadow_presets[i]
  48. if preset[1] == shadow_map_max_distance and
  49. preset[2] == shadow_map_texture_size and
  50. preset[3] == shadow_map_texture_32bit and
  51. preset[4] == shadow_filters and
  52. preset[5] == shadow_map_color then
  53. return i
  54. end
  55. end
  56. return PRESET_CUSTOM
  57. end
  58. local function apply_preset(preset)
  59. if preset then
  60. core.settings:set_bool("enable_dynamic_shadows", true)
  61. core.settings:set("shadow_map_max_distance", preset[1])
  62. core.settings:set("shadow_map_texture_size", preset[2])
  63. core.settings:set_bool("shadow_map_texture_32bit", preset[3])
  64. core.settings:set("shadow_filters", preset[4])
  65. core.settings:set_bool("shadow_map_color", preset[5])
  66. else
  67. core.settings:set_bool("enable_dynamic_shadows", false)
  68. end
  69. end
  70. return {
  71. query_text = "Shadows",
  72. requires = {
  73. shaders = true,
  74. opengl = true,
  75. },
  76. get_formspec = function(self, avail_w)
  77. local labels = table.copy(shadow_levels_labels)
  78. local idx = detect_mapping_idx()
  79. -- Remove "custom" if not already selected
  80. if idx ~= PRESET_CUSTOM then
  81. table.remove(labels, PRESET_CUSTOM)
  82. end
  83. local fs =
  84. "label[0,0.2;" .. fgettext("Dynamic shadows") .. "]" ..
  85. "dropdown[0,0.4;3,0.8;dd_shadows;" .. table.concat(labels, ",") .. ";" .. idx .. ";true]" ..
  86. "label[0,1.5;" .. core.colorize("#bbb", fgettext("(The game will need to enable shadows as well)")) .. "]"
  87. return fs, 1.8
  88. end,
  89. on_submit = function(self, fields)
  90. if fields.dd_shadows then
  91. local old_shadow_level_idx = detect_mapping_idx()
  92. local shadow_level_idx = tonumber(fields.dd_shadows)
  93. if shadow_level_idx == nil or shadow_level_idx == old_shadow_level_idx then
  94. return false
  95. end
  96. if shadow_level_idx == PRESET_CUSTOM then
  97. core.settings:set_bool("enable_dynamic_shadows", true)
  98. return true
  99. end
  100. local preset = shadow_presets[shadow_level_idx]
  101. apply_preset(preset)
  102. return true
  103. end
  104. end,
  105. }