💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
6.6 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "vegetation-system.hpp"
  20. #include "game/components/model-component.hpp"
  21. #include "game/components/transform-component.hpp"
  22. #include "scene/model-instance.hpp"
  23. #include "scene/lod-group.hpp"
  24. #include "scene/scene.hpp"
  25. #include "renderer/material.hpp"
  26. #include "geometry/aabb.hpp"
  27. #include "utility/fundamental-types.hpp"
  28. #include <cmath>
  29. using namespace ecs;
  30. vegetation_system::vegetation_system(entt::registry& registry):
  31. entity_system(registry),
  32. terrain_patch_size(1.0f),
  33. vegetation_patch_size(1.0f),
  34. vegetation_patch_columns(1),
  35. vegetation_patch_rows(1),
  36. vegetation_density(1.0f),
  37. vegetation_model(nullptr)
  38. {
  39. registry.on_construct<terrain_component>().connect<&vegetation_system::on_terrain_construct>(this);
  40. registry.on_destroy<terrain_component>().connect<&vegetation_system::on_terrain_destroy>(this);
  41. }
  42. vegetation_system::~vegetation_system()
  43. {}
  44. void vegetation_system::update(double t, double dt)
  45. {}
  46. void vegetation_system::set_terrain_patch_size(float size)
  47. {
  48. terrain_patch_size = size;
  49. vegetation_patch_size = terrain_patch_size / static_cast<float>(vegetation_patch_columns);
  50. }
  51. void vegetation_system::set_vegetation_patch_resolution(int subdivisions)
  52. {
  53. // Determine number of vegetation patch columns and rows per terrain patch
  54. vegetation_patch_columns = static_cast<int>(std::pow(2, subdivisions));
  55. vegetation_patch_rows = vegetation_patch_columns;
  56. vegetation_patch_size = terrain_patch_size / static_cast<float>(vegetation_patch_columns);
  57. }
  58. void vegetation_system::set_vegetation_density(float density)
  59. {
  60. vegetation_density = density;
  61. }
  62. void vegetation_system::set_vegetation_model(::model* model)
  63. {
  64. vegetation_model = model;
  65. }
  66. void vegetation_system::set_scene(::scene* scene)
  67. {
  68. this->scene = scene;
  69. }
  70. void vegetation_system::on_terrain_construct(entt::registry& registry, entt::entity entity, terrain_component& component)
  71. {
  72. // Find corner of terrain patch
  73. float terrain_patch_min_x = static_cast<float>(component.x) * terrain_patch_size - terrain_patch_size * 0.5f;
  74. float terrain_patch_min_z = static_cast<float>(component.z) * terrain_patch_size - terrain_patch_size * 0.5f;
  75. // Create vegetation patches
  76. for (int column = 0; column < vegetation_patch_columns; ++column)
  77. {
  78. for (int row = 0; row < vegetation_patch_rows; ++row)
  79. {
  80. /*
  81. // Create vegetation patch entity
  82. auto vegetation_patch_entity = registry.create();
  83. // Assign a transform component
  84. transform_component transform;
  85. transform.local = math::identity_transform<float>;
  86. transform.local.translation = float3{vegetation_patch_x, 0.0f, vegetation_patch_z};
  87. transform.warp = true;
  88. registry.assign_or_replace<transform_component>(vegetation_patch_entity, transform);
  89. // Assign a model component
  90. model_component model;
  91. model.model = vegetation_model;
  92. model.instance_count = 500;
  93. registry.assign_or_replace<model_component>(vegetation_patch_entity, model);
  94. */
  95. // Find patch translation
  96. float vegetation_patch_x = terrain_patch_min_x + vegetation_patch_size * static_cast<float>(column) + vegetation_patch_size * 0.5f;
  97. float vegetation_patch_z = terrain_patch_min_z + vegetation_patch_size * static_cast<float>(row) + vegetation_patch_size * 0.5f;
  98. float3 translation = {vegetation_patch_x, 0.0f, vegetation_patch_z};
  99. // Generate culling mask
  100. aabb<float>* culling_mask = new aabb<float>(vegetation_model->get_bounds());
  101. culling_mask->min_point.x = std::min<float>(culling_mask->min_point.x, translation.x - vegetation_patch_size * 0.5f);
  102. culling_mask->min_point.z = std::min<float>(culling_mask->min_point.z, translation.z - vegetation_patch_size * 0.5f);
  103. culling_mask->max_point.x = std::max<float>(culling_mask->max_point.x, translation.x + vegetation_patch_size * 0.5f);
  104. culling_mask->max_point.z = std::max<float>(culling_mask->max_point.z, translation.z + vegetation_patch_size * 0.5f);
  105. std::size_t lod_count = 4;
  106. std::size_t instance_count_lod0 = 500;
  107. std::size_t instance_count_lod1 = instance_count_lod0 / 2;
  108. std::size_t instance_count_lod2 = instance_count_lod1 / 2;
  109. // Generate LOD materials
  110. const material* lod0_material = (*vegetation_model->get_groups())[0]->get_material();
  111. material* lod1_material = new material(*lod0_material);
  112. static_cast<material_property<int>*>(lod1_material->get_property("instance_multiplier"))->set_value(2);
  113. material* lod2_material = new material(*lod0_material);
  114. static_cast<material_property<int>*>(lod2_material->get_property("instance_multiplier"))->set_value(4);
  115. // Create LOD 0
  116. model_instance* patch_lod0 = new model_instance();
  117. patch_lod0->set_model(vegetation_model);
  118. patch_lod0->set_translation(translation);
  119. patch_lod0->set_instanced(true, instance_count_lod0);
  120. patch_lod0->set_culling_mask(culling_mask);
  121. patch_lod0->update_tweens();
  122. // Create LOD 1
  123. model_instance* patch_lod1 = new model_instance();
  124. patch_lod1->set_model(vegetation_model);
  125. patch_lod1->set_material(0, lod1_material);
  126. patch_lod1->set_translation(translation);
  127. patch_lod1->set_instanced(true, instance_count_lod1);
  128. patch_lod1->set_culling_mask(culling_mask);
  129. patch_lod1->update_tweens();
  130. // Create LOD 2
  131. model_instance* patch_lod2 = new model_instance();
  132. patch_lod2->set_model(vegetation_model);
  133. patch_lod2->set_material(0, lod2_material);
  134. patch_lod2->set_translation(translation);
  135. patch_lod2->set_instanced(true, instance_count_lod2);
  136. patch_lod2->set_culling_mask(culling_mask);
  137. patch_lod2->update_tweens();
  138. // Create LOD group
  139. ::lod_group* lod_group = new ::lod_group(lod_count);
  140. lod_group->add_object(0, patch_lod0);
  141. lod_group->add_object(1, patch_lod1);
  142. lod_group->add_object(2, patch_lod2);
  143. lod_group->set_translation(translation);
  144. lod_group->update_tweens();
  145. // Add LOD group to scene
  146. scene->add_object(lod_group);
  147. }
  148. }
  149. }
  150. void vegetation_system::on_terrain_destroy(entt::registry& registry, entt::entity entity)
  151. {}