1
0

init.lua 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. local lighting_sections = {
  2. {n = "shadows", d = "Shadows",
  3. entries = {
  4. { n = "intensity", d = "Shadow Intensity", min = 0, max = 1 }
  5. }
  6. },
  7. {
  8. n = "exposure", d = "Exposure",
  9. entries = {
  10. {n = "luminance_min", d = "Minimum Luminance", min = -10, max = 10},
  11. {n = "luminance_max", d = "Maximum Luminance", min = -10, max = 10},
  12. {n = "exposure_correction", d = "Exposure Correction", min = -10, max = 10},
  13. {n = "speed_dark_bright", d = "Bright light adaptation speed", min = -10, max = 10, type="log2"},
  14. {n = "speed_bright_dark", d = "Dark scene adaptation speed", min = -10, max = 10, type="log2"},
  15. {n = "center_weight_power", d = "Power factor for center-weighting", min = 0.1, max = 10},
  16. }
  17. },
  18. {
  19. n = "bloom", d = "Bloom",
  20. entries = {
  21. {n = "intensity", d = "Intensity", min = 0, max = 1},
  22. {n = "strength_factor", d = "Strength Factor", min = 0.1, max = 10},
  23. {n = "radius", d = "Radius", min = 0.1, max = 8},
  24. },
  25. },
  26. {
  27. n = "volumetric_light", d = "Volumetric Lighting",
  28. entries = {
  29. {n = "strength", d = "Strength", min = 0, max = 1},
  30. },
  31. },
  32. }
  33. local function dump_lighting(lighting)
  34. local result = "{\n"
  35. local section_count = 0
  36. for _,section in ipairs(lighting_sections) do
  37. section_count = section_count + 1
  38. local parameters = section.entries or {}
  39. local state = lighting[section.n] or {}
  40. result = result.." "..section.n.." = {\n"
  41. local count = 0
  42. for _,v in ipairs(parameters) do
  43. count = count + 1
  44. result = result.." "..v.n.." = "..(math.floor(state[v.n] * 1000)/1000)
  45. if count < #parameters then
  46. result = result..","
  47. end
  48. result = result.."\n"
  49. end
  50. result = result.." }"
  51. if section_count < #lighting_sections then
  52. result = result..","
  53. end
  54. result = result.."\n"
  55. end
  56. result = result .."}"
  57. return result
  58. end
  59. core.register_chatcommand("set_lighting", {
  60. params = "",
  61. description = "Tune lighting parameters",
  62. func = function(player_name, param)
  63. local player = core.get_player_by_name(player_name);
  64. if not player then return end
  65. local lighting = player:get_lighting()
  66. local exposure = lighting.exposure or {}
  67. local content = {}
  68. local line = 1
  69. for _,section in ipairs(lighting_sections) do
  70. local parameters = section.entries or {}
  71. local state = lighting[section.n] or {}
  72. table.insert(content, "label[1,"..line..";"..section.d.."]")
  73. line = line + 1
  74. for _,v in ipairs(parameters) do
  75. table.insert(content, "label[2,"..line..";"..v.d.."]")
  76. table.insert(content, "scrollbaroptions[min=0;max=1000;smallstep=10;largestep=100;thumbsize=10]")
  77. local value = state[v.n]
  78. if v.type == "log2" then
  79. value = math.log(value or 1) / math.log(2)
  80. end
  81. local sb_scale = math.floor(1000 * (math.max(v.min, value or 0) - v.min) / (v.max - v.min))
  82. table.insert(content, "scrollbar[2,"..(line+0.7)..";12,1;horizontal;"..section.n.."."..v.n..";"..sb_scale.."]")
  83. line = line + 2.7
  84. end
  85. line = line + 1
  86. end
  87. local form = {
  88. "formspec_version[2]",
  89. "size[15,", line, "]",
  90. "position[0.99,0.15]",
  91. "anchor[1,0]",
  92. "padding[0.05,0.1]",
  93. "no_prepend[]",
  94. }
  95. table.insert_all(form, content)
  96. core.show_formspec(player_name, "lighting", table.concat(form))
  97. local debug_value = dump_lighting(lighting)
  98. local debug_ui = player:hud_add({type="text", position={x=0.1, y=0.3}, scale={x=1,y=1}, alignment = {x=1, y=1}, text=debug_value, number=0xFFFFFF})
  99. player:get_meta():set_int("lighting_hud", debug_ui)
  100. end
  101. })
  102. core.register_on_player_receive_fields(function(player, formname, fields)
  103. if formname ~= "lighting" then return end
  104. if not player then return end
  105. local hud_id = player:get_meta():get_int("lighting_hud")
  106. if fields.quit then
  107. player:hud_remove(hud_id)
  108. player:get_meta():set_int("lighting_hud", -1)
  109. return
  110. end
  111. local lighting = player:get_lighting()
  112. for _,section in ipairs(lighting_sections) do
  113. local parameters = section.entries or {}
  114. local state = (lighting[section.n] or {})
  115. lighting[section.n] = state
  116. for _,v in ipairs(parameters) do
  117. if fields[section.n.."."..v.n] then
  118. local event = core.explode_scrollbar_event(fields[section.n.."."..v.n])
  119. if event.type == "CHG" then
  120. local value = v.min + (v.max - v.min) * (event.value / 1000);
  121. if v.type == "log2" then
  122. value = math.pow(2, value);
  123. end
  124. state[v.n] = value;
  125. end
  126. end
  127. end
  128. end
  129. local debug_value = dump_lighting(lighting)
  130. player:hud_change(hud_id, "text", debug_value)
  131. player:set_lighting(lighting)
  132. end)