baked_lightmap.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**************************************************************************/
  2. /* baked_lightmap.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 BAKED_LIGHTMAP_H
  31. #define BAKED_LIGHTMAP_H
  32. #include "core/local_vector.h"
  33. #include "multimesh_instance.h"
  34. #include "scene/3d/light.h"
  35. #include "scene/3d/lightmapper.h"
  36. #include "scene/3d/visual_instance.h"
  37. class BakedLightmapData : public Resource {
  38. GDCLASS(BakedLightmapData, Resource);
  39. RES_BASE_EXTENSION("lmbake")
  40. RID baked_light;
  41. AABB bounds;
  42. float energy;
  43. bool interior;
  44. int cell_subdiv;
  45. Transform cell_space_xform;
  46. struct User {
  47. NodePath path;
  48. struct {
  49. Ref<Texture> single;
  50. Ref<TextureLayered> layered;
  51. } lightmap;
  52. int lightmap_slice;
  53. Rect2 lightmap_uv_rect;
  54. int instance_index;
  55. };
  56. Vector<User> users;
  57. void _set_user_data(const Array &p_data);
  58. Array _get_user_data() const;
  59. protected:
  60. static void _bind_methods();
  61. public:
  62. void set_bounds(const AABB &p_bounds);
  63. AABB get_bounds() const;
  64. void set_octree(const PoolVector<uint8_t> &p_octree);
  65. PoolVector<uint8_t> get_octree() const;
  66. void set_cell_space_transform(const Transform &p_xform);
  67. Transform get_cell_space_transform() const;
  68. void set_cell_subdiv(int p_cell_subdiv);
  69. int get_cell_subdiv() const;
  70. void set_energy(float p_energy);
  71. float get_energy() const;
  72. void set_interior(bool p_interior);
  73. bool is_interior() const;
  74. void add_user(const NodePath &p_path, const Ref<Resource> &p_lightmap, int p_lightmap_slice, const Rect2 &p_lightmap_uv_rect, int p_instance);
  75. int get_user_count() const;
  76. NodePath get_user_path(int p_user) const;
  77. Ref<Resource> get_user_lightmap(int p_user) const;
  78. int get_user_lightmap_slice(int p_user) const;
  79. Rect2 get_user_lightmap_uv_rect(int p_user) const;
  80. int get_user_instance(int p_user) const;
  81. void clear_users();
  82. void clear_data();
  83. virtual RID get_rid() const;
  84. BakedLightmapData();
  85. ~BakedLightmapData();
  86. };
  87. class BakedLightmap : public VisualInstance {
  88. GDCLASS(BakedLightmap, VisualInstance);
  89. public:
  90. enum BakeQuality {
  91. BAKE_QUALITY_LOW,
  92. BAKE_QUALITY_MEDIUM,
  93. BAKE_QUALITY_HIGH,
  94. BAKE_QUALITY_ULTRA
  95. };
  96. enum BakeError {
  97. BAKE_ERROR_OK,
  98. BAKE_ERROR_NO_SAVE_PATH,
  99. BAKE_ERROR_NO_MESHES,
  100. BAKE_ERROR_CANT_CREATE_IMAGE,
  101. BAKE_ERROR_LIGHTMAP_SIZE,
  102. BAKE_ERROR_INVALID_MESH,
  103. BAKE_ERROR_USER_ABORTED,
  104. BAKE_ERROR_NO_LIGHTMAPPER,
  105. BAKE_ERROR_NO_ROOT,
  106. };
  107. enum EnvironmentMode {
  108. ENVIRONMENT_MODE_DISABLED,
  109. ENVIRONMENT_MODE_SCENE,
  110. ENVIRONMENT_MODE_CUSTOM_SKY,
  111. ENVIRONMENT_MODE_CUSTOM_COLOR
  112. };
  113. struct BakeStepUD {
  114. Lightmapper::BakeStepFunc func;
  115. void *ud;
  116. float from_percent;
  117. float to_percent;
  118. };
  119. struct LightsFound {
  120. Transform xform;
  121. Light *light;
  122. };
  123. struct MeshesFound {
  124. Transform xform;
  125. NodePath node_path;
  126. int32_t subindex;
  127. Ref<Mesh> mesh;
  128. int32_t lightmap_scale;
  129. Vector<Ref<Material>> overrides;
  130. bool cast_shadows;
  131. bool generate_lightmap;
  132. };
  133. private:
  134. Vector3 extents;
  135. float default_texels_per_unit;
  136. float bias;
  137. BakeQuality bake_quality;
  138. bool generate_atlas;
  139. int max_atlas_size;
  140. bool capture_enabled;
  141. int bounces;
  142. float bounce_indirect_energy;
  143. bool use_denoiser;
  144. bool use_hdr;
  145. bool use_color;
  146. EnvironmentMode environment_mode;
  147. Ref<Sky> environment_custom_sky;
  148. Vector3 environment_custom_sky_rotation_degrees;
  149. Color environment_custom_color;
  150. float environment_custom_energy;
  151. Color environment_min_light;
  152. BakeQuality capture_quality;
  153. float capture_propagation;
  154. float capture_cell_size;
  155. String image_path; // (Deprecated property)
  156. Ref<BakedLightmapData> light_data;
  157. void _assign_lightmaps();
  158. void _clear_lightmaps();
  159. void _get_material_images(const MeshesFound &p_found_mesh, Lightmapper::MeshData &r_mesh_data, Vector<Ref<Texture>> &r_albedo_textures, Vector<Ref<Texture>> &r_emission_textures);
  160. Ref<Image> _get_irradiance_from_sky(Ref<Sky> p_sky, float p_energy, Vector2i p_size);
  161. Ref<Image> _get_irradiance_map(Ref<Environment> p_env, Vector2i p_size);
  162. void _find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights);
  163. Vector2i _compute_lightmap_size(const MeshesFound &p_mesh);
  164. static bool _lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh);
  165. void _save_image(String &r_base_path, Ref<Image> p_img, bool p_use_srgb);
  166. protected:
  167. static void _bind_methods();
  168. void _validate_property(PropertyInfo &property) const;
  169. void _notification(int p_what);
  170. public:
  171. static Lightmapper::BakeStepFunc bake_step_function;
  172. static Lightmapper::BakeStepFunc bake_substep_function;
  173. static Lightmapper::BakeEndFunc bake_end_function;
  174. void set_light_data(const Ref<BakedLightmapData> &p_data);
  175. Ref<BakedLightmapData> get_light_data() const;
  176. void set_capture_cell_size(float p_cell_size);
  177. float get_capture_cell_size() const;
  178. void set_extents(const Vector3 &p_extents);
  179. Vector3 get_extents() const;
  180. void set_default_texels_per_unit(const float &p_extents);
  181. float get_default_texels_per_unit() const;
  182. void set_capture_propagation(float p_propagation);
  183. float get_capture_propagation() const;
  184. void set_capture_quality(BakeQuality p_quality);
  185. BakeQuality get_capture_quality() const;
  186. void set_bake_quality(BakeQuality p_quality);
  187. BakeQuality get_bake_quality() const;
  188. void set_generate_atlas(bool p_enabled);
  189. bool is_generate_atlas_enabled() const;
  190. void set_max_atlas_size(int p_size);
  191. int get_max_atlas_size() const;
  192. void set_capture_enabled(bool p_enable);
  193. bool get_capture_enabled() const;
  194. void set_image_path(const String &p_path);
  195. String get_image_path() const;
  196. void set_environment_mode(EnvironmentMode p_mode);
  197. EnvironmentMode get_environment_mode() const;
  198. void set_environment_custom_sky(const Ref<Sky> &p_sky);
  199. Ref<Sky> get_environment_custom_sky() const;
  200. void set_environment_custom_sky_rotation_degrees(const Vector3 &p_rotation);
  201. Vector3 get_environment_custom_sky_rotation_degrees() const;
  202. void set_environment_custom_color(const Color &p_color);
  203. Color get_environment_custom_color() const;
  204. void set_environment_custom_energy(float p_energy);
  205. float get_environment_custom_energy() const;
  206. void set_environment_min_light(Color p_min_light);
  207. Color get_environment_min_light() const;
  208. void set_use_denoiser(bool p_enable);
  209. bool is_using_denoiser() const;
  210. void set_use_hdr(bool p_enable);
  211. bool is_using_hdr() const;
  212. void set_use_color(bool p_enable);
  213. bool is_using_color() const;
  214. void set_bounces(int p_bounces);
  215. int get_bounces() const;
  216. void set_bounce_indirect_energy(float p_indirect_energy);
  217. float get_bounce_indirect_energy() const;
  218. void set_bias(float p_bias);
  219. float get_bias() const;
  220. AABB get_aabb() const;
  221. PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  222. BakeError bake(Node *p_from_node, String p_data_save_path = "");
  223. BakedLightmap();
  224. };
  225. VARIANT_ENUM_CAST(BakedLightmap::BakeQuality);
  226. VARIANT_ENUM_CAST(BakedLightmap::BakeError);
  227. VARIANT_ENUM_CAST(BakedLightmap::EnvironmentMode);
  228. #endif // BAKED_LIGHTMAP_H