gi_probe.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*************************************************************************/
  2. /* gi_probe.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 GIPROBE_H
  31. #define GIPROBE_H
  32. #include "multimesh_instance.h"
  33. #include "scene/3d/visual_instance.h"
  34. class GIProbeData : public Resource {
  35. GDCLASS(GIProbeData, Resource);
  36. RID probe;
  37. protected:
  38. static void _bind_methods();
  39. public:
  40. void set_bounds(const Rect3 &p_bounds);
  41. Rect3 get_bounds() const;
  42. void set_cell_size(float p_size);
  43. float get_cell_size() const;
  44. void set_to_cell_xform(const Transform &p_xform);
  45. Transform get_to_cell_xform() const;
  46. void set_dynamic_data(const PoolVector<int> &p_data);
  47. PoolVector<int> get_dynamic_data() const;
  48. void set_dynamic_range(int p_range);
  49. int get_dynamic_range() const;
  50. void set_propagation(float p_range);
  51. float get_propagation() const;
  52. void set_energy(float p_range);
  53. float get_energy() const;
  54. void set_bias(float p_range);
  55. float get_bias() const;
  56. void set_normal_bias(float p_range);
  57. float get_normal_bias() const;
  58. void set_interior(bool p_enable);
  59. bool is_interior() const;
  60. void set_compress(bool p_enable);
  61. bool is_compressed() const;
  62. virtual RID get_rid() const;
  63. GIProbeData();
  64. ~GIProbeData();
  65. };
  66. class GIProbe : public VisualInstance {
  67. GDCLASS(GIProbe, VisualInstance);
  68. public:
  69. enum Subdiv {
  70. SUBDIV_64,
  71. SUBDIV_128,
  72. SUBDIV_256,
  73. SUBDIV_512,
  74. SUBDIV_MAX
  75. };
  76. typedef void (*BakeBeginFunc)(int);
  77. typedef void (*BakeStepFunc)(int, const String &);
  78. typedef void (*BakeEndFunc)();
  79. private:
  80. //stuff used for bake
  81. struct Baker {
  82. enum {
  83. CHILD_EMPTY = 0xFFFFFFFF
  84. };
  85. struct Cell {
  86. uint32_t childs[8];
  87. float albedo[3]; //albedo in RGB24
  88. float emission[3]; //accumulated light in 16:16 fixed point (needs to be integer for moving lights fast)
  89. float normal[3];
  90. uint32_t used_sides;
  91. float alpha; //used for upsampling
  92. int level;
  93. Cell() {
  94. for (int i = 0; i < 8; i++) {
  95. childs[i] = CHILD_EMPTY;
  96. }
  97. for (int i = 0; i < 3; i++) {
  98. emission[i] = 0;
  99. albedo[i] = 0;
  100. normal[i] = 0;
  101. }
  102. alpha = 0;
  103. used_sides = 0;
  104. level = 0;
  105. }
  106. };
  107. Vector<Cell> bake_cells;
  108. int cell_subdiv;
  109. struct MaterialCache {
  110. //128x128 textures
  111. Vector<Color> albedo;
  112. Vector<Color> emission;
  113. };
  114. Vector<Color> _get_bake_texture(Ref<Image> p_image, const Color &p_color);
  115. Map<Ref<Material>, MaterialCache> material_cache;
  116. MaterialCache _get_material_cache(Ref<Material> p_material);
  117. int leaf_voxel_count;
  118. Rect3 po2_bounds;
  119. int axis_cell_size[3];
  120. struct PlotMesh {
  121. Ref<Material> override_material;
  122. Vector<Ref<Material> > instance_materials;
  123. Ref<Mesh> mesh;
  124. Transform local_xform;
  125. };
  126. Transform to_cell_space;
  127. List<PlotMesh> mesh_list;
  128. };
  129. Ref<GIProbeData> probe_data;
  130. RID gi_probe;
  131. Subdiv subdiv;
  132. Vector3 extents;
  133. int dynamic_range;
  134. float energy;
  135. float bias;
  136. float normal_bias;
  137. float propagation;
  138. bool interior;
  139. bool compress;
  140. int color_scan_cell_width;
  141. int bake_texture_size;
  142. Vector<Color> _get_bake_texture(Ref<Image> p_image, const Color &p_color);
  143. Baker::MaterialCache _get_material_cache(Ref<Material> p_material, Baker *p_baker);
  144. void _plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, const Vector3 *p_vtx, const Vector2 *p_uv, const Baker::MaterialCache &p_material, const Rect3 &p_aabb, Baker *p_baker);
  145. void _plot_mesh(const Transform &p_xform, Ref<Mesh> &p_mesh, Baker *p_baker, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material);
  146. void _find_meshes(Node *p_at_node, Baker *p_baker);
  147. void _fixup_plot(int p_idx, int p_level, int p_x, int p_y, int p_z, Baker *p_baker);
  148. void _debug_mesh(int p_idx, int p_level, const Rect3 &p_aabb, Ref<MultiMesh> &p_multimesh, int &idx, Baker *p_baker);
  149. void _create_debug_mesh(Baker *p_baker);
  150. void _debug_bake();
  151. protected:
  152. static void _bind_methods();
  153. public:
  154. static BakeBeginFunc bake_begin_function;
  155. static BakeStepFunc bake_step_function;
  156. static BakeEndFunc bake_end_function;
  157. void set_probe_data(const Ref<GIProbeData> &p_data);
  158. Ref<GIProbeData> get_probe_data() const;
  159. void set_subdiv(Subdiv p_subdiv);
  160. Subdiv get_subdiv() const;
  161. void set_extents(const Vector3 &p_extents);
  162. Vector3 get_extents() const;
  163. void set_dynamic_range(int p_dynamic_range);
  164. int get_dynamic_range() const;
  165. void set_energy(float p_energy);
  166. float get_energy() const;
  167. void set_bias(float p_bias);
  168. float get_bias() const;
  169. void set_normal_bias(float p_normal_bias);
  170. float get_normal_bias() const;
  171. void set_propagation(float p_propagation);
  172. float get_propagation() const;
  173. void set_interior(bool p_enable);
  174. bool is_interior() const;
  175. void set_compress(bool p_enable);
  176. bool is_compressed() const;
  177. void bake(Node *p_from_node = NULL, bool p_create_visual_debug = false);
  178. virtual Rect3 get_aabb() const;
  179. virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  180. GIProbe();
  181. ~GIProbe();
  182. };
  183. VARIANT_ENUM_CAST(GIProbe::Subdiv)
  184. #endif // GIPROBE_H