lightmap_gi.h 12 KB

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