graphics_settings.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #pragma once
  2. #include <cstdint>
  3. namespace Render {
  4. enum class GraphicsQuality : uint8_t {
  5. Low = 0,
  6. Medium = 1,
  7. High = 2,
  8. Ultra = 3
  9. };
  10. struct LODMultipliers {
  11. float humanoid_full;
  12. float humanoid_reduced;
  13. float humanoid_minimal;
  14. float humanoid_billboard;
  15. float horse_full;
  16. float horse_reduced;
  17. float horse_minimal;
  18. float horse_billboard;
  19. float shadow_distance;
  20. bool enable_shadows;
  21. };
  22. struct GraphicsFeatures {
  23. bool enable_facial_hair;
  24. bool enable_mane_detail;
  25. bool enable_tail_detail;
  26. bool enable_armor_detail;
  27. bool enable_equipment_detail;
  28. bool enable_ground_shadows;
  29. bool enable_pose_cache;
  30. };
  31. struct BatchingConfig {
  32. bool force_batching;
  33. bool never_batch;
  34. int batching_unit_threshold;
  35. float batching_zoom_start;
  36. float batching_zoom_full;
  37. };
  38. class GraphicsSettings {
  39. public:
  40. static auto instance() noexcept -> GraphicsSettings & {
  41. static GraphicsSettings inst;
  42. return inst;
  43. }
  44. [[nodiscard]] auto quality() const noexcept -> GraphicsQuality {
  45. return m_quality;
  46. }
  47. void setQuality(GraphicsQuality q) noexcept {
  48. m_quality = q;
  49. apply_preset(q);
  50. }
  51. [[nodiscard]] auto
  52. lod_multipliers() const noexcept -> const LODMultipliers & {
  53. return m_lod_multipliers;
  54. }
  55. [[nodiscard]] auto features() const noexcept -> const GraphicsFeatures & {
  56. return m_features;
  57. }
  58. [[nodiscard]] auto
  59. batching_config() const noexcept -> const BatchingConfig & {
  60. return m_batching_config;
  61. }
  62. [[nodiscard]] auto
  63. calculate_batching_ratio(int visible_units,
  64. float camera_height) const noexcept -> float {
  65. if (m_batching_config.never_batch) {
  66. return 0.0F;
  67. }
  68. if (m_batching_config.force_batching) {
  69. return 1.0F;
  70. }
  71. float unit_factor = 0.0F;
  72. if (visible_units > m_batching_config.batching_unit_threshold) {
  73. int excess = visible_units - m_batching_config.batching_unit_threshold;
  74. int range = m_batching_config.batching_unit_threshold * 3;
  75. unit_factor = static_cast<float>(excess) / static_cast<float>(range);
  76. unit_factor =
  77. unit_factor < 0.0F ? 0.0F : (unit_factor > 1.0F ? 1.0F : unit_factor);
  78. }
  79. float zoom_factor = 0.0F;
  80. if (camera_height > m_batching_config.batching_zoom_start) {
  81. float range = m_batching_config.batching_zoom_full -
  82. m_batching_config.batching_zoom_start;
  83. if (range > 0.0F) {
  84. zoom_factor =
  85. (camera_height - m_batching_config.batching_zoom_start) / range;
  86. zoom_factor = zoom_factor < 0.0F
  87. ? 0.0F
  88. : (zoom_factor > 1.0F ? 1.0F : zoom_factor);
  89. }
  90. }
  91. return unit_factor > zoom_factor ? unit_factor : zoom_factor;
  92. }
  93. [[nodiscard]] auto humanoid_full_detail_distance() const noexcept -> float {
  94. return kBaseHumanoidFull * m_lod_multipliers.humanoid_full;
  95. }
  96. [[nodiscard]] auto
  97. humanoid_reduced_detail_distance() const noexcept -> float {
  98. return kBaseHumanoidReduced * m_lod_multipliers.humanoid_reduced;
  99. }
  100. [[nodiscard]] auto
  101. humanoid_minimal_detail_distance() const noexcept -> float {
  102. return kBaseHumanoidMinimal * m_lod_multipliers.humanoid_minimal;
  103. }
  104. [[nodiscard]] auto humanoid_billboard_distance() const noexcept -> float {
  105. return kBaseHumanoidBillboard * m_lod_multipliers.humanoid_billboard;
  106. }
  107. [[nodiscard]] auto horse_full_detail_distance() const noexcept -> float {
  108. return kBaseHorseFull * m_lod_multipliers.horse_full;
  109. }
  110. [[nodiscard]] auto horse_reduced_detail_distance() const noexcept -> float {
  111. return kBaseHorseReduced * m_lod_multipliers.horse_reduced;
  112. }
  113. [[nodiscard]] auto horse_minimal_detail_distance() const noexcept -> float {
  114. return kBaseHorseMinimal * m_lod_multipliers.horse_minimal;
  115. }
  116. [[nodiscard]] auto horse_billboard_distance() const noexcept -> float {
  117. return kBaseHorseBillboard * m_lod_multipliers.horse_billboard;
  118. }
  119. [[nodiscard]] auto shadow_max_distance() const noexcept -> float {
  120. return m_lod_multipliers.shadow_distance;
  121. }
  122. [[nodiscard]] auto shadows_enabled() const noexcept -> bool {
  123. return m_lod_multipliers.enable_shadows;
  124. }
  125. private:
  126. GraphicsSettings() { setQuality(GraphicsQuality::Ultra); }
  127. void apply_preset(GraphicsQuality q) noexcept {
  128. switch (q) {
  129. case GraphicsQuality::Low:
  130. m_lod_multipliers = {.humanoid_full = 0.8F,
  131. .humanoid_reduced = 0.8F,
  132. .humanoid_minimal = 0.8F,
  133. .humanoid_billboard = 0.8F,
  134. .horse_full = 0.8F,
  135. .horse_reduced = 0.8F,
  136. .horse_minimal = 0.8F,
  137. .horse_billboard = 0.8F,
  138. .shadow_distance = 25.0F,
  139. .enable_shadows = true};
  140. m_features = {.enable_facial_hair = false,
  141. .enable_mane_detail = false,
  142. .enable_tail_detail = false,
  143. .enable_armor_detail = true,
  144. .enable_equipment_detail = true,
  145. .enable_ground_shadows = true,
  146. .enable_pose_cache = true};
  147. m_batching_config = {.force_batching = true,
  148. .never_batch = false,
  149. .batching_unit_threshold = 0,
  150. .batching_zoom_start = 0.0F,
  151. .batching_zoom_full = 0.0F};
  152. break;
  153. case GraphicsQuality::Medium:
  154. m_lod_multipliers = {.humanoid_full = 1.0F,
  155. .humanoid_reduced = 1.0F,
  156. .humanoid_minimal = 1.0F,
  157. .humanoid_billboard = 1.0F,
  158. .horse_full = 1.0F,
  159. .horse_reduced = 1.0F,
  160. .horse_minimal = 1.0F,
  161. .horse_billboard = 1.0F,
  162. .shadow_distance = 40.0F,
  163. .enable_shadows = true};
  164. m_features = {.enable_facial_hair = true,
  165. .enable_mane_detail = true,
  166. .enable_tail_detail = true,
  167. .enable_armor_detail = true,
  168. .enable_equipment_detail = true,
  169. .enable_ground_shadows = true,
  170. .enable_pose_cache = true};
  171. m_batching_config = {.force_batching = false,
  172. .never_batch = false,
  173. .batching_unit_threshold = 30,
  174. .batching_zoom_start = 60.0F,
  175. .batching_zoom_full = 90.0F};
  176. break;
  177. case GraphicsQuality::High:
  178. m_lod_multipliers = {.humanoid_full = 2.0F,
  179. .humanoid_reduced = 2.0F,
  180. .humanoid_minimal = 2.0F,
  181. .humanoid_billboard = 2.0F,
  182. .horse_full = 2.0F,
  183. .horse_reduced = 2.0F,
  184. .horse_minimal = 2.0F,
  185. .horse_billboard = 2.0F,
  186. .shadow_distance = 80.0F,
  187. .enable_shadows = true};
  188. m_features = {.enable_facial_hair = true,
  189. .enable_mane_detail = true,
  190. .enable_tail_detail = true,
  191. .enable_armor_detail = true,
  192. .enable_equipment_detail = true,
  193. .enable_ground_shadows = true,
  194. .enable_pose_cache = true};
  195. m_batching_config = {.force_batching = false,
  196. .never_batch = false,
  197. .batching_unit_threshold = 50,
  198. .batching_zoom_start = 80.0F,
  199. .batching_zoom_full = 120.0F};
  200. break;
  201. case GraphicsQuality::Ultra:
  202. m_lod_multipliers = {.humanoid_full = 100.0F,
  203. .humanoid_reduced = 100.0F,
  204. .humanoid_minimal = 100.0F,
  205. .humanoid_billboard = 100.0F,
  206. .horse_full = 100.0F,
  207. .horse_reduced = 100.0F,
  208. .horse_minimal = 100.0F,
  209. .horse_billboard = 100.0F,
  210. .shadow_distance = 200.0F,
  211. .enable_shadows = true};
  212. m_features = {.enable_facial_hair = true,
  213. .enable_mane_detail = true,
  214. .enable_tail_detail = true,
  215. .enable_armor_detail = true,
  216. .enable_equipment_detail = true,
  217. .enable_ground_shadows = true,
  218. .enable_pose_cache = false};
  219. m_batching_config = {.force_batching = false,
  220. .never_batch = true,
  221. .batching_unit_threshold = 999999,
  222. .batching_zoom_start = 999999.0F,
  223. .batching_zoom_full = 999999.0F};
  224. break;
  225. }
  226. }
  227. static constexpr float kBaseHumanoidFull = 15.0F;
  228. static constexpr float kBaseHumanoidReduced = 35.0F;
  229. static constexpr float kBaseHumanoidMinimal = 60.0F;
  230. static constexpr float kBaseHumanoidBillboard = 100.0F;
  231. static constexpr float kBaseHorseFull = 20.0F;
  232. static constexpr float kBaseHorseReduced = 40.0F;
  233. static constexpr float kBaseHorseMinimal = 70.0F;
  234. static constexpr float kBaseHorseBillboard = 100.0F;
  235. GraphicsQuality m_quality{GraphicsQuality::Ultra};
  236. LODMultipliers m_lod_multipliers{};
  237. GraphicsFeatures m_features{};
  238. BatchingConfig m_batching_config{};
  239. };
  240. } // namespace Render