voxel_light_baker.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* voxel_light_baker.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. float energy;
  112. BakeQuality bake_quality;
  113. BakeMode bake_mode;
  114. int max_original_cells;
  115. void _init_light_plot(int p_idx, int p_level, int p_x, int p_y, int p_z, uint32_t p_parent);
  116. Vector<Color> _get_bake_texture(Ref<Image> p_image, const Color &p_color_mul, const Color &p_color_add);
  117. MaterialCache _get_material_cache(Ref<Material> p_material);
  118. 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);
  119. void _fixup_plot(int p_idx, int p_level);
  120. void _debug_mesh(int p_idx, int p_level, const AABB &p_aabb, Ref<MultiMesh> &p_multimesh, int &idx, DebugMode p_mode);
  121. void _check_init_light();
  122. uint32_t _find_cell_at_pos(const Cell *cells, int x, int y, int z);
  123. struct LightMap {
  124. Vector3 light;
  125. Vector3 pos;
  126. Vector3 normal;
  127. };
  128. void _plot_triangle(Vector2 *vertices, Vector3 *positions, Vector3 *normals, LightMap *pixels, int width, int height);
  129. _FORCE_INLINE_ void _sample_baked_octree_filtered_and_anisotropic(const Vector3 &p_posf, const Vector3 &p_direction, float p_level, Vector3 &r_color, float &r_alpha);
  130. _FORCE_INLINE_ Vector3 _voxel_cone_trace(const Vector3 &p_pos, const Vector3 &p_normal, float p_aperture);
  131. _FORCE_INLINE_ Vector3 _compute_pixel_light_at_pos(const Vector3 &p_pos, const Vector3 &p_normal);
  132. _FORCE_INLINE_ Vector3 _compute_ray_trace_at_pos(const Vector3 &p_pos, const Vector3 &p_normal);
  133. void _lightmap_bake_point(uint32_t p_x, LightMap *p_line);
  134. public:
  135. void begin_bake(int p_subdiv, const AABB &p_bounds);
  136. void plot_mesh(const Transform &p_xform, Ref<Mesh> &p_mesh, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material);
  137. void begin_bake_light(BakeQuality p_quality = BAKE_QUALITY_MEDIUM, BakeMode p_bake_mode = BAKE_MODE_CONE_TRACE, float p_propagation = 0.85, float p_energy = 1);
  138. void plot_light_directional(const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_energy, bool p_direct);
  139. 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);
  140. 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);
  141. void end_bake();
  142. struct LightMapData {
  143. int width;
  144. int height;
  145. PoolVector<float> light;
  146. };
  147. Error make_lightmap(const Transform &p_xform, Ref<Mesh> &p_mesh, LightMapData &r_lightmap, bool (*p_bake_time_func)(void *, float, float) = NULL, void *p_bake_time_ud = NULL);
  148. PoolVector<int> create_gi_probe_data();
  149. Ref<MultiMesh> create_debug_multimesh(DebugMode p_mode = DEBUG_ALBEDO);
  150. PoolVector<uint8_t> create_capture_octree(int p_subdiv);
  151. float get_cell_size() const;
  152. Transform get_to_cell_space_xform() const;
  153. VoxelLightBaker();
  154. };
  155. #endif // VOXEL_LIGHT_BAKER_H