lightmap_gi.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**************************************************************************/
  2. /* lightmap_gi.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 LIGHTMAP_GI_H
  31. #define LIGHTMAP_GI_H
  32. #include "core/templates/local_vector.h"
  33. #include "scene/3d/light_3d.h"
  34. #include "scene/3d/lightmapper.h"
  35. #include "scene/3d/visual_instance_3d.h"
  36. class Sky;
  37. class CameraAttributes;
  38. class LightmapGIData : public Resource {
  39. GDCLASS(LightmapGIData, Resource);
  40. RES_BASE_EXTENSION("lmbake")
  41. Ref<TextureLayered> light_texture;
  42. bool uses_spherical_harmonics = false;
  43. bool interior = false;
  44. RID lightmap;
  45. AABB bounds;
  46. float baked_exposure = 1.0;
  47. struct User {
  48. NodePath path;
  49. int32_t sub_instance = 0;
  50. Rect2 uv_scale;
  51. int slice_index = 0;
  52. };
  53. Vector<User> users;
  54. void _set_user_data(const Array &p_data);
  55. Array _get_user_data() const;
  56. void _set_probe_data(const Dictionary &p_data);
  57. Dictionary _get_probe_data() const;
  58. void _set_light_textures_data(const Array &p_data);
  59. Array _get_light_textures_data() const;
  60. protected:
  61. static void _bind_methods();
  62. public:
  63. void add_user(const NodePath &p_path, const Rect2 &p_uv_scale, int p_slice_index, int32_t p_sub_instance = -1);
  64. int get_user_count() const;
  65. NodePath get_user_path(int p_user) const;
  66. int32_t get_user_sub_instance(int p_user) const;
  67. Rect2 get_user_lightmap_uv_scale(int p_user) const;
  68. int get_user_lightmap_slice_index(int p_user) const;
  69. void clear_users();
  70. void set_light_texture(const Ref<TextureLayered> &p_light_texture);
  71. Ref<TextureLayered> get_light_texture() const;
  72. void set_uses_spherical_harmonics(bool p_enable);
  73. bool is_using_spherical_harmonics() const;
  74. bool is_interior() const;
  75. float get_baked_exposure() const;
  76. void set_capture_data(const AABB &p_bounds, bool p_interior, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree, float p_baked_exposure);
  77. PackedVector3Array get_capture_points() const;
  78. PackedColorArray get_capture_sh() const;
  79. PackedInt32Array get_capture_tetrahedra() const;
  80. PackedInt32Array get_capture_bsp_tree() const;
  81. AABB get_capture_bounds() const;
  82. void clear();
  83. virtual RID get_rid() const override;
  84. LightmapGIData();
  85. ~LightmapGIData();
  86. };
  87. class LightmapGI : public VisualInstance3D {
  88. GDCLASS(LightmapGI, VisualInstance3D);
  89. public:
  90. enum BakeQuality {
  91. BAKE_QUALITY_LOW,
  92. BAKE_QUALITY_MEDIUM,
  93. BAKE_QUALITY_HIGH,
  94. BAKE_QUALITY_ULTRA,
  95. };
  96. enum GenerateProbes {
  97. GENERATE_PROBES_DISABLED,
  98. GENERATE_PROBES_SUBDIV_4,
  99. GENERATE_PROBES_SUBDIV_8,
  100. GENERATE_PROBES_SUBDIV_16,
  101. GENERATE_PROBES_SUBDIV_32,
  102. };
  103. enum BakeError {
  104. BAKE_ERROR_OK,
  105. BAKE_ERROR_NO_SCENE_ROOT,
  106. BAKE_ERROR_FOREIGN_DATA,
  107. BAKE_ERROR_NO_LIGHTMAPPER,
  108. BAKE_ERROR_NO_SAVE_PATH,
  109. BAKE_ERROR_NO_MESHES,
  110. BAKE_ERROR_MESHES_INVALID,
  111. BAKE_ERROR_CANT_CREATE_IMAGE,
  112. BAKE_ERROR_USER_ABORTED,
  113. };
  114. enum EnvironmentMode {
  115. ENVIRONMENT_MODE_DISABLED,
  116. ENVIRONMENT_MODE_SCENE,
  117. ENVIRONMENT_MODE_CUSTOM_SKY,
  118. ENVIRONMENT_MODE_CUSTOM_COLOR,
  119. };
  120. private:
  121. BakeQuality bake_quality = BAKE_QUALITY_MEDIUM;
  122. bool use_denoiser = true;
  123. int bounces = 3;
  124. float bias = 0.0005;
  125. int max_texture_size = 16384;
  126. bool interior = false;
  127. EnvironmentMode environment_mode = ENVIRONMENT_MODE_SCENE;
  128. Ref<Sky> environment_custom_sky;
  129. Color environment_custom_color = Color(1, 1, 1);
  130. float environment_custom_energy = 1.0;
  131. bool directional = false;
  132. GenerateProbes gen_probes = GENERATE_PROBES_SUBDIV_8;
  133. Ref<CameraAttributes> camera_attributes;
  134. Ref<LightmapGIData> light_data;
  135. struct LightsFound {
  136. Transform3D xform;
  137. Light3D *light = nullptr;
  138. };
  139. struct MeshesFound {
  140. Transform3D xform;
  141. NodePath node_path;
  142. int32_t subindex = 0;
  143. Ref<Mesh> mesh;
  144. int32_t lightmap_scale = 0;
  145. Vector<Ref<Material>> overrides;
  146. };
  147. void _find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes);
  148. void _assign_lightmaps();
  149. void _clear_lightmaps();
  150. struct BakeTimeData {
  151. String text;
  152. int pass = 0;
  153. uint64_t last_step = 0;
  154. };
  155. struct BSPSimplex {
  156. int vertices[4] = {};
  157. int planes[4] = {};
  158. };
  159. struct BSPNode {
  160. static const int32_t EMPTY_LEAF = INT32_MIN;
  161. Plane plane;
  162. int32_t over = EMPTY_LEAF;
  163. int32_t under = EMPTY_LEAF;
  164. };
  165. int _bsp_get_simplex_side(const Vector<Vector3> &p_points, const LocalVector<BSPSimplex> &p_simplices, const Plane &p_plane, uint32_t p_simplex) const;
  166. int32_t _compute_bsp_tree(const Vector<Vector3> &p_points, const LocalVector<Plane> &p_planes, LocalVector<int32_t> &planes_tested, const LocalVector<BSPSimplex> &p_simplices, const LocalVector<int32_t> &p_simplex_indices, LocalVector<BSPNode> &bsp_nodes);
  167. struct BakeStepUD {
  168. Lightmapper::BakeStepFunc func;
  169. void *ud = nullptr;
  170. float from_percent = 0.0;
  171. float to_percent = 0.0;
  172. };
  173. static bool _lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh);
  174. struct GenProbesOctree {
  175. Vector3i offset;
  176. uint32_t size = 0;
  177. GenProbesOctree *children[8] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  178. ~GenProbesOctree() {
  179. for (int i = 0; i < 8; i++) {
  180. if (children[i] != nullptr) {
  181. memdelete(children[i]);
  182. }
  183. }
  184. }
  185. };
  186. void _plot_triangle_into_octree(GenProbesOctree *p_cell, float p_cell_size, const Vector3 *p_triangle);
  187. void _gen_new_positions_from_octree(const GenProbesOctree *p_cell, float p_cell_size, const Vector<Vector3> &probe_positions, LocalVector<Vector3> &new_probe_positions, HashMap<Vector3i, bool> &positions_used, const AABB &p_bounds);
  188. protected:
  189. void _validate_property(PropertyInfo &p_property) const;
  190. static void _bind_methods();
  191. void _notification(int p_what);
  192. public:
  193. void set_light_data(const Ref<LightmapGIData> &p_data);
  194. Ref<LightmapGIData> get_light_data() const;
  195. void set_bake_quality(BakeQuality p_quality);
  196. BakeQuality get_bake_quality() const;
  197. void set_use_denoiser(bool p_enable);
  198. bool is_using_denoiser() const;
  199. void set_directional(bool p_enable);
  200. bool is_directional() const;
  201. void set_interior(bool p_interior);
  202. bool is_interior() const;
  203. void set_environment_mode(EnvironmentMode p_mode);
  204. EnvironmentMode get_environment_mode() const;
  205. void set_environment_custom_sky(const Ref<Sky> &p_sky);
  206. Ref<Sky> get_environment_custom_sky() const;
  207. void set_environment_custom_color(const Color &p_color);
  208. Color get_environment_custom_color() const;
  209. void set_environment_custom_energy(float p_energy);
  210. float get_environment_custom_energy() const;
  211. void set_bounces(int p_bounces);
  212. int get_bounces() const;
  213. void set_bias(float p_bias);
  214. float get_bias() const;
  215. void set_max_texture_size(int p_size);
  216. int get_max_texture_size() const;
  217. void set_generate_probes(GenerateProbes p_generate_probes);
  218. GenerateProbes get_generate_probes() const;
  219. void set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes);
  220. Ref<CameraAttributes> get_camera_attributes() const;
  221. AABB get_aabb() const override;
  222. BakeError bake(Node *p_from_node, String p_image_data_path = "", Lightmapper::BakeStepFunc p_bake_step = nullptr, void *p_bake_userdata = nullptr);
  223. virtual PackedStringArray get_configuration_warnings() const override;
  224. LightmapGI();
  225. };
  226. VARIANT_ENUM_CAST(LightmapGI::BakeQuality);
  227. VARIANT_ENUM_CAST(LightmapGI::GenerateProbes);
  228. VARIANT_ENUM_CAST(LightmapGI::BakeError);
  229. VARIANT_ENUM_CAST(LightmapGI::EnvironmentMode);
  230. #endif // LIGHTMAP_GI_H