💿🐜 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.

179 lines
6.7 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.hpp"
  20. #include "entity/components/model.hpp"
  21. #include "entity/components/transform.hpp"
  22. #include "scene/model-instance.hpp"
  23. #include "scene/lod-group.hpp"
  24. #include "scene/collection.hpp"
  25. #include "renderer/material.hpp"
  26. #include "geom/aabb.hpp"
  27. #include "utility/fundamental-types.hpp"
  28. #include <cmath>
  29. namespace entity {
  30. namespace system {
  31. vegetation::vegetation(entity::registry& registry):
  32. updatable(registry),
  33. terrain_patch_size(1.0f),
  34. vegetation_patch_size(1.0f),
  35. vegetation_patch_columns(1),
  36. vegetation_patch_rows(1),
  37. vegetation_density(1.0f),
  38. vegetation_model(nullptr)
  39. {
  40. registry.on_construct<component::terrain>().connect<&vegetation::on_terrain_construct>(this);
  41. registry.on_destroy<component::terrain>().connect<&vegetation::on_terrain_destroy>(this);
  42. }
  43. vegetation::~vegetation()
  44. {}
  45. void vegetation::update(double t, double dt)
  46. {}
  47. void vegetation::set_terrain_patch_size(float size)
  48. {
  49. terrain_patch_size = size;
  50. vegetation_patch_size = terrain_patch_size / static_cast<float>(vegetation_patch_columns);
  51. }
  52. void vegetation::set_vegetation_patch_resolution(int subdivisions)
  53. {
  54. // Determine number of vegetation patch columns and rows per terrain patch
  55. vegetation_patch_columns = static_cast<int>(std::pow(2, subdivisions));
  56. vegetation_patch_rows = vegetation_patch_columns;
  57. vegetation_patch_size = terrain_patch_size / static_cast<float>(vegetation_patch_columns);
  58. }
  59. void vegetation::set_vegetation_density(float density)
  60. {
  61. vegetation_density = density;
  62. }
  63. void vegetation::set_vegetation_model(::model* model)
  64. {
  65. vegetation_model = model;
  66. }
  67. void vegetation::set_scene(scene::collection* collection)
  68. {
  69. this->scene_collection = collection;
  70. }
  71. void vegetation::on_terrain_construct(entity::registry& registry, entity::id entity_id, component::terrain& component)
  72. {
  73. // Find corner of terrain patch
  74. float terrain_patch_min_x = static_cast<float>(component.x) * terrain_patch_size - terrain_patch_size * 0.5f;
  75. float terrain_patch_min_z = static_cast<float>(component.z) * terrain_patch_size - terrain_patch_size * 0.5f;
  76. // Create vegetation patches
  77. for (int column = 0; column < vegetation_patch_columns; ++column)
  78. {
  79. for (int row = 0; row < vegetation_patch_rows; ++row)
  80. {
  81. /*
  82. // Create vegetation patch entity
  83. auto vegetation_patch_entity = registry.create();
  84. // Assign a transform component
  85. component::transform transform;
  86. transform.local = math::identity_transform<float>;
  87. transform.local.translation = float3{vegetation_patch_x, 0.0f, vegetation_patch_z};
  88. transform.warp = true;
  89. registry.assign_or_replace<component::transform>(vegetation_patch_entity, transform);
  90. // Assign a model component
  91. component::model model;
  92. model.model = vegetation_model;
  93. model.instance_count = 500;
  94. registry.assign_or_replace<component::model>(vegetation_patch_entity, model);
  95. */
  96. // Find patch translation
  97. float vegetation_patch_x = terrain_patch_min_x + vegetation_patch_size * static_cast<float>(column) + vegetation_patch_size * 0.5f;
  98. float vegetation_patch_z = terrain_patch_min_z + vegetation_patch_size * static_cast<float>(row) + vegetation_patch_size * 0.5f;
  99. float3 translation = {vegetation_patch_x, 0.0f, vegetation_patch_z};
  100. // Generate culling mask
  101. geom::aabb<float>* culling_mask = new geom::aabb<float>(vegetation_model->get_bounds());
  102. culling_mask->min_point.x = std::min<float>(culling_mask->min_point.x, translation.x - vegetation_patch_size * 0.5f);
  103. culling_mask->min_point.z = std::min<float>(culling_mask->min_point.z, translation.z - vegetation_patch_size * 0.5f);
  104. culling_mask->max_point.x = std::max<float>(culling_mask->max_point.x, translation.x + vegetation_patch_size * 0.5f);
  105. culling_mask->max_point.z = std::max<float>(culling_mask->max_point.z, translation.z + vegetation_patch_size * 0.5f);
  106. std::size_t lod_count = 4;
  107. std::size_t instance_count_lod0 = 500;
  108. std::size_t instance_count_lod1 = instance_count_lod0 / 2;
  109. std::size_t instance_count_lod2 = instance_count_lod1 / 2;
  110. // Generate LOD materials
  111. const material* lod0_material = (*vegetation_model->get_groups())[0]->get_material();
  112. material* lod1_material = new material(*lod0_material);
  113. static_cast<material_property<int>*>(lod1_material->get_property("instance_multiplier"))->set_value(2);
  114. material* lod2_material = new material(*lod0_material);
  115. static_cast<material_property<int>*>(lod2_material->get_property("instance_multiplier"))->set_value(4);
  116. // Create LOD 0
  117. scene::model_instance* patch_lod0 = new scene::model_instance();
  118. patch_lod0->set_model(vegetation_model);
  119. patch_lod0->set_translation(translation);
  120. patch_lod0->set_instanced(true, instance_count_lod0);
  121. patch_lod0->set_culling_mask(culling_mask);
  122. patch_lod0->update_tweens();
  123. // Create LOD 1
  124. scene::model_instance* patch_lod1 = new scene::model_instance();
  125. patch_lod1->set_model(vegetation_model);
  126. patch_lod1->set_material(0, lod1_material);
  127. patch_lod1->set_translation(translation);
  128. patch_lod1->set_instanced(true, instance_count_lod1);
  129. patch_lod1->set_culling_mask(culling_mask);
  130. patch_lod1->update_tweens();
  131. // Create LOD 2
  132. scene::model_instance* patch_lod2 = new scene::model_instance();
  133. patch_lod2->set_model(vegetation_model);
  134. patch_lod2->set_material(0, lod2_material);
  135. patch_lod2->set_translation(translation);
  136. patch_lod2->set_instanced(true, instance_count_lod2);
  137. patch_lod2->set_culling_mask(culling_mask);
  138. patch_lod2->update_tweens();
  139. // Create LOD group
  140. scene::lod_group* lod_group = new scene::lod_group(lod_count);
  141. lod_group->add_object(0, patch_lod0);
  142. lod_group->add_object(1, patch_lod1);
  143. lod_group->add_object(2, patch_lod2);
  144. lod_group->set_translation(translation);
  145. lod_group->update_tweens();
  146. // Add LOD group to scene
  147. scene_collection->add_object(lod_group);
  148. }
  149. }
  150. }
  151. void vegetation::on_terrain_destroy(entity::registry& registry, entity::id entity_id)
  152. {}
  153. } // namespace system
  154. } // namespace entity