voxelizer.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**************************************************************************/
  2. /* voxelizer.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef VOXELIZER_H
  31. #define VOXELIZER_H
  32. #include "scene/resources/multimesh.h"
  33. class Voxelizer {
  34. public:
  35. enum BakeResult {
  36. BAKE_RESULT_OK,
  37. BAKE_RESULT_INVALID_PARAMETER,
  38. BAKE_RESULT_CANCELLED,
  39. };
  40. typedef bool (*BakeStepFunc)(int, int);
  41. private:
  42. enum : uint32_t {
  43. CHILD_EMPTY = 0xFFFFFFFF
  44. };
  45. struct Cell {
  46. uint32_t children[8];
  47. float albedo[3] = {}; //albedo in RGB24
  48. float emission[3] = {}; //accumulated light in 16:16 fixed point (needs to be integer for moving lights fast)
  49. float normal[3] = {};
  50. uint32_t used_sides = 0;
  51. float alpha = 0.0; //used for upsampling
  52. uint16_t x = 0;
  53. uint16_t y = 0;
  54. uint16_t z = 0;
  55. uint16_t level = 0;
  56. Cell() {
  57. for (int i = 0; i < 8; i++) {
  58. children[i] = CHILD_EMPTY;
  59. }
  60. }
  61. };
  62. Vector<Cell> bake_cells;
  63. int cell_subdiv = 0;
  64. struct CellSort {
  65. union {
  66. uint64_t key = 0;
  67. struct {
  68. uint64_t z : 16;
  69. uint64_t y : 16;
  70. uint64_t x : 16;
  71. uint64_t level : 16;
  72. };
  73. };
  74. int32_t index = 0;
  75. _FORCE_INLINE_ bool operator<(const CellSort &p_cell_sort) const {
  76. return key < p_cell_sort.key;
  77. }
  78. };
  79. struct MaterialCache {
  80. //128x128 textures
  81. Vector<Color> albedo;
  82. Vector<Color> emission;
  83. };
  84. HashMap<Ref<Material>, MaterialCache> material_cache;
  85. float exposure_normalization = 1.0;
  86. AABB original_bounds;
  87. AABB po2_bounds;
  88. int axis_cell_size[3] = {};
  89. Transform3D to_cell_space;
  90. int color_scan_cell_width = 4;
  91. int bake_texture_size = 128;
  92. float cell_size = 0.0;
  93. int max_original_cells = 0;
  94. int leaf_voxel_count = 0;
  95. Vector<Color> _get_bake_texture(Ref<Image> p_image, const Color &p_color_mul, const Color &p_color_add);
  96. MaterialCache _get_material_cache(Ref<Material> p_material);
  97. void _plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, const Vector3 *p_vtx, const Vector3 *p_normal, const Vector2 *p_uv, const MaterialCache &p_material, const AABB &p_aabb);
  98. void _fixup_plot(int p_idx, int p_level);
  99. void _debug_mesh(int p_idx, int p_level, const AABB &p_aabb, Ref<MultiMesh> &p_multimesh, int &idx);
  100. bool sorted = false;
  101. void _sort();
  102. public:
  103. void begin_bake(int p_subdiv, const AABB &p_bounds, float p_exposure_normalization);
  104. int get_bake_steps(Ref<Mesh> &p_mesh) const;
  105. BakeResult plot_mesh(const Transform3D &p_xform, Ref<Mesh> &p_mesh, const Vector<Ref<Material>> &p_materials, const Ref<Material> &p_override_material, BakeStepFunc p_bake_step_function);
  106. void end_bake();
  107. int get_voxel_gi_octree_depth() const;
  108. Vector3i get_voxel_gi_octree_size() const;
  109. int get_voxel_gi_cell_count() const;
  110. Vector<uint8_t> get_voxel_gi_octree_cells() const;
  111. Vector<uint8_t> get_voxel_gi_data_cells() const;
  112. Vector<int> get_voxel_gi_level_cell_count() const;
  113. BakeResult get_sdf_3d_image(Vector<uint8_t> &r_image, BakeStepFunc p_bake_step_function) const;
  114. Ref<MultiMesh> create_debug_multimesh();
  115. Transform3D get_to_cell_space_xform() const;
  116. Voxelizer();
  117. };
  118. #endif // VOXELIZER_H