lightmapper.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**************************************************************************/
  2. /* lightmapper.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 LIGHTMAPPER_H
  31. #define LIGHTMAPPER_H
  32. #include "core/object/ref_counted.h"
  33. class Image;
  34. #if !defined(__aligned)
  35. #if defined(_WIN32) && defined(_MSC_VER)
  36. #define __aligned(...) __declspec(align(__VA_ARGS__))
  37. #else
  38. #define __aligned(...) __attribute__((aligned(__VA_ARGS__)))
  39. #endif
  40. #endif
  41. class LightmapDenoiser : public RefCounted {
  42. GDCLASS(LightmapDenoiser, RefCounted)
  43. protected:
  44. static LightmapDenoiser *(*create_function)();
  45. public:
  46. virtual Ref<Image> denoise_image(const Ref<Image> &p_image) = 0;
  47. static Ref<LightmapDenoiser> create();
  48. };
  49. class LightmapRaycaster : public RefCounted {
  50. GDCLASS(LightmapRaycaster, RefCounted)
  51. protected:
  52. static LightmapRaycaster *(*create_function)();
  53. public:
  54. // Compatible with embree4 rays.
  55. struct __aligned(16) Ray {
  56. const static unsigned int INVALID_GEOMETRY_ID = ((unsigned int)-1); // from rtcore_common.h
  57. /*! Default construction does nothing. */
  58. _FORCE_INLINE_ Ray() :
  59. geomID(INVALID_GEOMETRY_ID) {}
  60. /*! Constructs a ray from origin, direction, and ray segment. Near
  61. * has to be smaller than far. */
  62. _FORCE_INLINE_ Ray(const Vector3 &p_org,
  63. const Vector3 &p_dir,
  64. float p_tnear = 0.0f,
  65. float p_tfar = INFINITY) :
  66. org(p_org),
  67. tnear(p_tnear),
  68. dir(p_dir),
  69. time(0.0f),
  70. tfar(p_tfar),
  71. mask(-1),
  72. u(0.0),
  73. v(0.0),
  74. primID(INVALID_GEOMETRY_ID),
  75. geomID(INVALID_GEOMETRY_ID),
  76. instID(INVALID_GEOMETRY_ID) {}
  77. /*! Tests if we hit something. */
  78. _FORCE_INLINE_ explicit operator bool() const {
  79. return geomID != INVALID_GEOMETRY_ID;
  80. }
  81. public:
  82. Vector3 org; //!< Ray origin + tnear
  83. float tnear; //!< Start of ray segment
  84. Vector3 dir; //!< Ray direction + tfar
  85. float time; //!< Time of this ray for motion blur.
  86. float tfar; //!< End of ray segment
  87. unsigned int mask; //!< used to mask out objects during traversal
  88. unsigned int id; //!< ray ID
  89. unsigned int flags; //!< ray flags
  90. Vector3 normal; //!< Not normalized geometry normal
  91. float u; //!< Barycentric u coordinate of hit
  92. float v; //!< Barycentric v coordinate of hit
  93. unsigned int primID; //!< primitive ID
  94. unsigned int geomID; //!< geometry ID
  95. unsigned int instID; //!< instance ID
  96. };
  97. virtual bool intersect(Ray &p_ray) = 0;
  98. virtual void intersect(Vector<Ray> &r_rays) = 0;
  99. virtual void add_mesh(const Vector<Vector3> &p_vertices, const Vector<Vector3> &p_normals, const Vector<Vector2> &p_uv2s, unsigned int p_id) = 0;
  100. virtual void set_mesh_alpha_texture(Ref<Image> p_alpha_texture, unsigned int p_id) = 0;
  101. virtual void commit() = 0;
  102. virtual void set_mesh_filter(const HashSet<int> &p_mesh_ids) = 0;
  103. virtual void clear_mesh_filter() = 0;
  104. static Ref<LightmapRaycaster> create();
  105. };
  106. class Lightmapper : public RefCounted {
  107. GDCLASS(Lightmapper, RefCounted)
  108. public:
  109. enum GenerateProbes {
  110. GENERATE_PROBES_DISABLED,
  111. GENERATE_PROBES_SUBDIV_4,
  112. GENERATE_PROBES_SUBDIV_8,
  113. GENERATE_PROBES_SUBDIV_16,
  114. GENERATE_PROBES_SUBDIV_32,
  115. };
  116. enum LightType {
  117. LIGHT_TYPE_DIRECTIONAL,
  118. LIGHT_TYPE_OMNI,
  119. LIGHT_TYPE_SPOT
  120. };
  121. enum BakeError {
  122. BAKE_OK,
  123. BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE,
  124. BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES,
  125. BAKE_ERROR_ATLAS_TOO_SMALL,
  126. };
  127. enum BakeQuality {
  128. BAKE_QUALITY_LOW,
  129. BAKE_QUALITY_MEDIUM,
  130. BAKE_QUALITY_HIGH,
  131. BAKE_QUALITY_ULTRA,
  132. };
  133. typedef Lightmapper *(*CreateFunc)();
  134. static CreateFunc create_custom;
  135. static CreateFunc create_gpu;
  136. static CreateFunc create_cpu;
  137. protected:
  138. public:
  139. typedef bool (*BakeStepFunc)(float, const String &, void *, bool); //step index, step total, step description, userdata
  140. struct MeshData {
  141. //triangle data
  142. Vector<Vector3> points;
  143. Vector<Vector2> uv2;
  144. Vector<Vector3> normal;
  145. Ref<Image> albedo_on_uv2;
  146. Ref<Image> emission_on_uv2;
  147. Variant userdata;
  148. };
  149. virtual void add_mesh(const MeshData &p_mesh) = 0;
  150. virtual void add_directional_light(bool p_static, const Vector3 &p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_angular_distance, float p_shadow_blur) = 0;
  151. virtual void add_omni_light(bool p_static, const Vector3 &p_position, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_size, float p_shadow_blur) = 0;
  152. virtual void add_spot_light(bool p_static, const Vector3 &p_position, const Vector3 p_direction, const Color &p_color, float p_energy, float p_indirect_energy, float p_range, float p_attenuation, float p_spot_angle, float p_spot_attenuation, float p_size, float p_shadow_blur) = 0;
  153. virtual void add_probe(const Vector3 &p_position) = 0;
  154. virtual BakeError bake(BakeQuality p_quality, bool p_use_denoiser, float p_denoiser_strength, int p_denoiser_range, int p_bounces, float p_bounce_indirect_energy, float p_bias, int p_max_texture_size, bool p_bake_sh, bool p_texture_for_bounces, GenerateProbes p_generate_probes, const Ref<Image> &p_environment_panorama, const Basis &p_environment_transform, BakeStepFunc p_step_function = nullptr, void *p_step_userdata = nullptr, float p_exposure_normalization = 1.0) = 0;
  155. virtual int get_bake_texture_count() const = 0;
  156. virtual Ref<Image> get_bake_texture(int p_index) const = 0;
  157. virtual int get_bake_mesh_count() const = 0;
  158. virtual Variant get_bake_mesh_userdata(int p_index) const = 0;
  159. virtual Rect2 get_bake_mesh_uv_scale(int p_index) const = 0;
  160. virtual int get_bake_mesh_texture_slice(int p_index) const = 0;
  161. virtual int get_bake_probe_count() const = 0;
  162. virtual Vector3 get_bake_probe_point(int p_probe) const = 0;
  163. virtual Vector<Color> get_bake_probe_sh(int p_probe) const = 0;
  164. static Ref<Lightmapper> create();
  165. Lightmapper();
  166. };
  167. #endif // LIGHTMAPPER_H