voxel_light_baker.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**************************************************************************/
  2. /* voxel_light_baker.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 VOXEL_LIGHT_BAKER_H
  31. #define VOXEL_LIGHT_BAKER_H
  32. #include "scene/3d/mesh_instance.h"
  33. #include "scene/resources/multimesh.h"
  34. class VoxelLightBaker {
  35. public:
  36. enum DebugMode {
  37. DEBUG_ALBEDO,
  38. DEBUG_LIGHT
  39. };
  40. enum BakeQuality {
  41. BAKE_QUALITY_LOW,
  42. BAKE_QUALITY_MEDIUM,
  43. BAKE_QUALITY_HIGH
  44. };
  45. enum BakeMode {
  46. BAKE_MODE_CONE_TRACE,
  47. BAKE_MODE_RAY_TRACE,
  48. };
  49. private:
  50. enum {
  51. CHILD_EMPTY = 0xFFFFFFFF
  52. };
  53. struct Cell {
  54. uint32_t children[8];
  55. float albedo[3]; //albedo in RGB24
  56. float emission[3]; //accumulated light in 16:16 fixed point (needs to be integer for moving lights fast)
  57. float normal[3];
  58. uint32_t used_sides;
  59. float alpha; //used for upsampling
  60. int level;
  61. Cell() {
  62. for (int i = 0; i < 8; i++) {
  63. children[i] = CHILD_EMPTY;
  64. }
  65. for (int i = 0; i < 3; i++) {
  66. emission[i] = 0;
  67. albedo[i] = 0;
  68. normal[i] = 0;
  69. }
  70. alpha = 0;
  71. used_sides = 0;
  72. level = 0;
  73. }
  74. };
  75. Vector<Cell> bake_cells;
  76. int cell_subdiv;
  77. struct Light {
  78. int x, y, z;
  79. float accum[6][3]; //rgb anisotropic
  80. float direct_accum[6][3]; //for direct bake
  81. int next_leaf;
  82. Light() {
  83. x = y = z = 0;
  84. for (int i = 0; i < 6; i++) {
  85. for (int j = 0; j < 3; j++) {
  86. accum[i][j] = 0;
  87. direct_accum[i][j] = 0;
  88. }
  89. }
  90. next_leaf = 0;
  91. }
  92. };
  93. int first_leaf;
  94. Vector<Light> bake_light;
  95. struct MaterialCache {
  96. //128x128 textures
  97. Vector<Color> albedo;
  98. Vector<Color> emission;
  99. };
  100. Map<Ref<Material>, MaterialCache> material_cache;
  101. int leaf_voxel_count;
  102. bool direct_lights_baked;
  103. AABB original_bounds;
  104. AABB po2_bounds;
  105. int axis_cell_size[3];
  106. Transform to_cell_space;
  107. int color_scan_cell_width;
  108. int bake_texture_size;
  109. float cell_size;
  110. float propagation;
  111. BakeQuality bake_quality;
  112. int max_original_cells;
  113. void _init_light_plot(int p_idx, int p_level, int p_x, int p_y, int p_z, uint32_t p_parent);
  114. Vector<Color> _get_bake_texture(Ref<Image> p_image, const Color &p_color_mul, const Color &p_color_add);
  115. MaterialCache _get_material_cache(Ref<Material> p_material);
  116. 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);
  117. void _fixup_plot(int p_idx, int p_level);
  118. void _debug_mesh(int p_idx, int p_level, const AABB &p_aabb, Ref<MultiMesh> &p_multimesh, int &idx, DebugMode p_mode);
  119. void _check_init_light();
  120. uint32_t _find_cell_at_pos(const Cell *cells, int x, int y, int z);
  121. public:
  122. void begin_bake(int p_subdiv, const AABB &p_bounds);
  123. void plot_mesh(const Transform &p_xform, Ref<Mesh> &p_mesh, const Vector<Ref<Material>> &p_materials, const Ref<Material> &p_override_material);
  124. void begin_bake_light(BakeQuality p_quality = BAKE_QUALITY_MEDIUM, float p_propagation = 0.85);
  125. void plot_light_directional(const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_energy, bool p_direct);
  126. void plot_light_omni(const Vector3 &p_pos, const Color &p_color, float p_energy, float p_indirect_energy, float p_radius, float p_attenutation, bool p_direct);
  127. void plot_light_spot(const Vector3 &p_pos, const Vector3 &p_axis, const Color &p_color, float p_energy, float p_indirect_energy, float p_radius, float p_attenutation, float p_spot_angle, float p_spot_attenuation, bool p_direct);
  128. void end_bake();
  129. struct LightMapData {
  130. int width;
  131. int height;
  132. PoolVector<float> light;
  133. };
  134. PoolVector<int> create_gi_probe_data();
  135. Ref<MultiMesh> create_debug_multimesh(DebugMode p_mode = DEBUG_ALBEDO);
  136. PoolVector<uint8_t> create_capture_octree(int p_subdiv);
  137. float get_cell_size() const;
  138. Transform get_to_cell_space_xform() const;
  139. VoxelLightBaker();
  140. };
  141. #endif // VOXEL_LIGHT_BAKER_H