lightmap_gi.h 11 KB

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