lightmapper.h 7.5 KB

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