lightmap_gi.h 10 KB

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