baked_lightmap.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*************************************************************************/
  2. /* baked_lightmap.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 BAKED_INDIRECT_LIGHT_H
  31. #define BAKED_INDIRECT_LIGHT_H
  32. #include "multimesh_instance.h"
  33. #include "scene/3d/light.h"
  34. #include "scene/3d/visual_instance.h"
  35. class BakedLightmapData : public Resource {
  36. GDCLASS(BakedLightmapData, Resource);
  37. RID baked_light;
  38. AABB bounds;
  39. float energy;
  40. int cell_subdiv;
  41. Transform cell_space_xform;
  42. struct User {
  43. NodePath path;
  44. Ref<Texture> lightmap;
  45. int instance_index;
  46. };
  47. Vector<User> users;
  48. void _set_user_data(const Array &p_data);
  49. Array _get_user_data() const;
  50. protected:
  51. static void _bind_methods();
  52. public:
  53. void set_bounds(const AABB &p_bounds);
  54. AABB get_bounds() const;
  55. void set_octree(const PoolVector<uint8_t> &p_octree);
  56. PoolVector<uint8_t> get_octree() const;
  57. void set_cell_space_transform(const Transform &p_xform);
  58. Transform get_cell_space_transform() const;
  59. void set_cell_subdiv(int p_cell_subdiv);
  60. int get_cell_subdiv() const;
  61. void set_energy(float p_energy);
  62. float get_energy() const;
  63. void add_user(const NodePath &p_path, const Ref<Texture> &p_lightmap, int p_instance = -1);
  64. int get_user_count() const;
  65. NodePath get_user_path(int p_user) const;
  66. Ref<Texture> get_user_lightmap(int p_user) const;
  67. int get_user_instance(int p_user) const;
  68. void clear_users();
  69. virtual RID get_rid() const;
  70. BakedLightmapData();
  71. ~BakedLightmapData();
  72. };
  73. class BakedLightmap : public VisualInstance {
  74. GDCLASS(BakedLightmap, VisualInstance);
  75. public:
  76. enum BakeQuality {
  77. BAKE_QUALITY_LOW,
  78. BAKE_QUALITY_MEDIUM,
  79. BAKE_QUALITY_HIGH
  80. };
  81. enum BakeMode {
  82. BAKE_MODE_CONE_TRACE,
  83. BAKE_MODE_RAY_TRACE,
  84. };
  85. enum BakeError {
  86. BAKE_ERROR_OK,
  87. BAKE_ERROR_NO_SAVE_PATH,
  88. BAKE_ERROR_NO_MESHES,
  89. BAKE_ERROR_CANT_CREATE_IMAGE,
  90. BAKE_ERROR_USER_ABORTED
  91. };
  92. typedef void (*BakeBeginFunc)(int);
  93. typedef bool (*BakeStepFunc)(int, const String &);
  94. typedef void (*BakeEndFunc)();
  95. private:
  96. float bake_cell_size;
  97. float capture_cell_size;
  98. Vector3 extents;
  99. float propagation;
  100. float energy;
  101. BakeQuality bake_quality;
  102. BakeMode bake_mode;
  103. bool hdr;
  104. String image_path;
  105. Ref<BakedLightmapData> light_data;
  106. struct PlotMesh {
  107. Ref<Material> override_material;
  108. Vector<Ref<Material> > instance_materials;
  109. Ref<Mesh> mesh;
  110. Transform local_xform;
  111. NodePath path;
  112. int instance_idx;
  113. };
  114. struct PlotLight {
  115. Light *light;
  116. Transform local_xform;
  117. };
  118. void _find_meshes_and_lights(Node *p_at_node, List<PlotMesh> &plot_meshes, List<PlotLight> &plot_lights);
  119. void _debug_bake();
  120. void _assign_lightmaps();
  121. void _clear_lightmaps();
  122. static bool _bake_time(void *ud, float p_secs, float p_progress);
  123. struct BakeTimeData {
  124. String text;
  125. int pass;
  126. uint64_t last_step;
  127. };
  128. protected:
  129. static void _bind_methods();
  130. void _notification(int p_what);
  131. public:
  132. static BakeBeginFunc bake_begin_function;
  133. static BakeStepFunc bake_step_function;
  134. static BakeEndFunc bake_end_function;
  135. void set_light_data(const Ref<BakedLightmapData> &p_data);
  136. Ref<BakedLightmapData> get_light_data() const;
  137. void set_bake_cell_size(float p_cell_size);
  138. float get_bake_cell_size() const;
  139. void set_capture_cell_size(float p_cell_size);
  140. float get_capture_cell_size() const;
  141. void set_extents(const Vector3 &p_extents);
  142. Vector3 get_extents() const;
  143. void set_propagation(float p_propagation);
  144. float get_propagation() const;
  145. void set_energy(float p_energy);
  146. float get_energy() const;
  147. void set_bake_quality(BakeQuality p_quality);
  148. BakeQuality get_bake_quality() const;
  149. void set_bake_mode(BakeMode p_mode);
  150. BakeMode get_bake_mode() const;
  151. void set_hdr(bool p_enable);
  152. bool is_hdr() const;
  153. void set_image_path(const String &p_path);
  154. String get_image_path() const;
  155. AABB get_aabb() const;
  156. PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  157. BakeError bake(Node *p_from_node, bool p_create_visual_debug = false);
  158. BakedLightmap();
  159. };
  160. VARIANT_ENUM_CAST(BakedLightmap::BakeQuality);
  161. VARIANT_ENUM_CAST(BakedLightmap::BakeMode);
  162. VARIANT_ENUM_CAST(BakedLightmap::BakeError);
  163. #endif // BAKED_INDIRECT_LIGHT_H