lightmapper.h 7.7 KB

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