lightmap_gi.h 12 KB

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