lightmap_gi.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*************************************************************************/
  2. /* lightmap_gi.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 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_LIGHTMAPPER,
  106. BAKE_ERROR_NO_SAVE_PATH,
  107. BAKE_ERROR_NO_MESHES,
  108. BAKE_ERROR_MESHES_INVALID,
  109. BAKE_ERROR_CANT_CREATE_IMAGE,
  110. BAKE_ERROR_USER_ABORTED,
  111. };
  112. enum EnvironmentMode {
  113. ENVIRONMENT_MODE_DISABLED,
  114. ENVIRONMENT_MODE_SCENE,
  115. ENVIRONMENT_MODE_CUSTOM_SKY,
  116. ENVIRONMENT_MODE_CUSTOM_COLOR,
  117. };
  118. private:
  119. BakeQuality bake_quality = BAKE_QUALITY_MEDIUM;
  120. bool use_denoiser = true;
  121. int bounces = 1;
  122. float bias = 0.0005;
  123. int max_texture_size = 16384;
  124. bool interior = false;
  125. EnvironmentMode environment_mode = ENVIRONMENT_MODE_DISABLED;
  126. Ref<Sky> environment_custom_sky;
  127. Color environment_custom_color = Color(0.2, 0.7, 1.0);
  128. float environment_custom_energy = 1.0;
  129. bool directional = false;
  130. GenerateProbes gen_probes = GENERATE_PROBES_DISABLED;
  131. Ref<CameraAttributes> camera_attributes;
  132. Ref<LightmapGIData> light_data;
  133. struct LightsFound {
  134. Transform3D xform;
  135. Light3D *light = nullptr;
  136. };
  137. struct MeshesFound {
  138. Transform3D xform;
  139. NodePath node_path;
  140. int32_t subindex = 0;
  141. Ref<Mesh> mesh;
  142. int32_t lightmap_scale = 0;
  143. Vector<Ref<Material>> overrides;
  144. };
  145. void _find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes);
  146. void _assign_lightmaps();
  147. void _clear_lightmaps();
  148. struct BakeTimeData {
  149. String text;
  150. int pass = 0;
  151. uint64_t last_step = 0;
  152. };
  153. struct BSPSimplex {
  154. int vertices[4] = {};
  155. int planes[4] = {};
  156. };
  157. struct BSPNode {
  158. static const int32_t EMPTY_LEAF = INT32_MIN;
  159. Plane plane;
  160. int32_t over = EMPTY_LEAF;
  161. int32_t under = EMPTY_LEAF;
  162. };
  163. int _bsp_get_simplex_side(const Vector<Vector3> &p_points, const LocalVector<BSPSimplex> &p_simplices, const Plane &p_plane, uint32_t p_simplex) const;
  164. 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);
  165. struct BakeStepUD {
  166. Lightmapper::BakeStepFunc func;
  167. void *ud = nullptr;
  168. float from_percent = 0.0;
  169. float to_percent = 0.0;
  170. };
  171. static bool _lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh);
  172. struct GenProbesOctree {
  173. Vector3i offset;
  174. uint32_t size = 0;
  175. GenProbesOctree *children[8] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };
  176. ~GenProbesOctree() {
  177. for (int i = 0; i < 8; i++) {
  178. if (children[i] != nullptr) {
  179. memdelete(children[i]);
  180. }
  181. }
  182. }
  183. };
  184. void _plot_triangle_into_octree(GenProbesOctree *p_cell, float p_cell_size, const Vector3 *p_triangle);
  185. 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);
  186. protected:
  187. void _validate_property(PropertyInfo &p_property) const;
  188. static void _bind_methods();
  189. void _notification(int p_what);
  190. public:
  191. void set_light_data(const Ref<LightmapGIData> &p_data);
  192. Ref<LightmapGIData> get_light_data() const;
  193. void set_bake_quality(BakeQuality p_quality);
  194. BakeQuality get_bake_quality() const;
  195. void set_use_denoiser(bool p_enable);
  196. bool is_using_denoiser() const;
  197. void set_directional(bool p_enable);
  198. bool is_directional() const;
  199. void set_interior(bool p_interior);
  200. bool is_interior() const;
  201. void set_environment_mode(EnvironmentMode p_mode);
  202. EnvironmentMode get_environment_mode() const;
  203. void set_environment_custom_sky(const Ref<Sky> &p_sky);
  204. Ref<Sky> get_environment_custom_sky() const;
  205. void set_environment_custom_color(const Color &p_color);
  206. Color get_environment_custom_color() const;
  207. void set_environment_custom_energy(float p_energy);
  208. float get_environment_custom_energy() const;
  209. void set_bounces(int p_bounces);
  210. int get_bounces() const;
  211. void set_bias(float p_bias);
  212. float get_bias() const;
  213. void set_max_texture_size(int p_size);
  214. int get_max_texture_size() const;
  215. void set_generate_probes(GenerateProbes p_generate_probes);
  216. GenerateProbes get_generate_probes() const;
  217. void set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes);
  218. Ref<CameraAttributes> get_camera_attributes() const;
  219. AABB get_aabb() const override;
  220. BakeError bake(Node *p_from_node, String p_image_data_path = "", Lightmapper::BakeStepFunc p_bake_step = nullptr, void *p_bake_userdata = nullptr);
  221. LightmapGI();
  222. };
  223. VARIANT_ENUM_CAST(LightmapGI::BakeQuality);
  224. VARIANT_ENUM_CAST(LightmapGI::GenerateProbes);
  225. VARIANT_ENUM_CAST(LightmapGI::BakeError);
  226. VARIANT_ENUM_CAST(LightmapGI::EnvironmentMode);
  227. #endif // LIGHTMAP_GI_H