| 1 | /**************************************************************************/ |
| 2 | /* lightmap_gi.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 | |
| 31 | #ifndef LIGHTMAP_GI_H |
| 32 | #define LIGHTMAP_GI_H |
| 33 | |
| 34 | #include "core/templates/local_vector.h" |
| 35 | #include "scene/3d/light_3d.h" |
| 36 | #include "scene/3d/lightmapper.h" |
| 37 | #include "scene/3d/visual_instance_3d.h" |
| 38 | |
| 39 | class Sky; |
| 40 | class CameraAttributes; |
| 41 | |
| 42 | class LightmapGIData : public Resource { |
| 43 | GDCLASS(LightmapGIData, Resource); |
| 44 | RES_BASE_EXTENSION("lmbake" ) |
| 45 | |
| 46 | Ref<TextureLayered> light_texture; |
| 47 | |
| 48 | bool uses_spherical_harmonics = false; |
| 49 | bool interior = false; |
| 50 | |
| 51 | RID lightmap; |
| 52 | AABB bounds; |
| 53 | float baked_exposure = 1.0; |
| 54 | |
| 55 | struct User { |
| 56 | NodePath path; |
| 57 | int32_t sub_instance = 0; |
| 58 | Rect2 uv_scale; |
| 59 | int slice_index = 0; |
| 60 | }; |
| 61 | |
| 62 | Vector<User> users; |
| 63 | |
| 64 | void _set_user_data(const Array &p_data); |
| 65 | Array _get_user_data() const; |
| 66 | void _set_probe_data(const Dictionary &p_data); |
| 67 | Dictionary _get_probe_data() const; |
| 68 | void _set_light_textures_data(const Array &p_data); |
| 69 | Array _get_light_textures_data() const; |
| 70 | |
| 71 | protected: |
| 72 | static void _bind_methods(); |
| 73 | |
| 74 | public: |
| 75 | void add_user(const NodePath &p_path, const Rect2 &p_uv_scale, int p_slice_index, int32_t p_sub_instance = -1); |
| 76 | int get_user_count() const; |
| 77 | NodePath get_user_path(int p_user) const; |
| 78 | int32_t get_user_sub_instance(int p_user) const; |
| 79 | Rect2 get_user_lightmap_uv_scale(int p_user) const; |
| 80 | int get_user_lightmap_slice_index(int p_user) const; |
| 81 | void clear_users(); |
| 82 | |
| 83 | void set_light_texture(const Ref<TextureLayered> &p_light_texture); |
| 84 | Ref<TextureLayered> get_light_texture() const; |
| 85 | |
| 86 | void set_uses_spherical_harmonics(bool p_enable); |
| 87 | bool is_using_spherical_harmonics() const; |
| 88 | |
| 89 | bool is_interior() const; |
| 90 | float get_baked_exposure() const; |
| 91 | |
| 92 | void set_capture_data(const AABB &p_bounds, bool p_interior, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree, float p_baked_exposure); |
| 93 | PackedVector3Array get_capture_points() const; |
| 94 | PackedColorArray get_capture_sh() const; |
| 95 | PackedInt32Array get_capture_tetrahedra() const; |
| 96 | PackedInt32Array get_capture_bsp_tree() const; |
| 97 | AABB get_capture_bounds() const; |
| 98 | |
| 99 | void clear(); |
| 100 | |
| 101 | virtual RID get_rid() const override; |
| 102 | LightmapGIData(); |
| 103 | ~LightmapGIData(); |
| 104 | }; |
| 105 | |
| 106 | class LightmapGI : public VisualInstance3D { |
| 107 | GDCLASS(LightmapGI, VisualInstance3D); |
| 108 | |
| 109 | public: |
| 110 | enum BakeQuality { |
| 111 | BAKE_QUALITY_LOW, |
| 112 | BAKE_QUALITY_MEDIUM, |
| 113 | BAKE_QUALITY_HIGH, |
| 114 | BAKE_QUALITY_ULTRA, |
| 115 | }; |
| 116 | |
| 117 | enum GenerateProbes { |
| 118 | GENERATE_PROBES_DISABLED, |
| 119 | GENERATE_PROBES_SUBDIV_4, |
| 120 | GENERATE_PROBES_SUBDIV_8, |
| 121 | GENERATE_PROBES_SUBDIV_16, |
| 122 | GENERATE_PROBES_SUBDIV_32, |
| 123 | }; |
| 124 | |
| 125 | enum BakeError { |
| 126 | BAKE_ERROR_OK, |
| 127 | BAKE_ERROR_NO_SCENE_ROOT, |
| 128 | BAKE_ERROR_FOREIGN_DATA, |
| 129 | BAKE_ERROR_NO_LIGHTMAPPER, |
| 130 | BAKE_ERROR_NO_SAVE_PATH, |
| 131 | BAKE_ERROR_NO_MESHES, |
| 132 | BAKE_ERROR_MESHES_INVALID, |
| 133 | BAKE_ERROR_CANT_CREATE_IMAGE, |
| 134 | BAKE_ERROR_USER_ABORTED, |
| 135 | BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL, |
| 136 | }; |
| 137 | |
| 138 | enum EnvironmentMode { |
| 139 | ENVIRONMENT_MODE_DISABLED, |
| 140 | ENVIRONMENT_MODE_SCENE, |
| 141 | ENVIRONMENT_MODE_CUSTOM_SKY, |
| 142 | ENVIRONMENT_MODE_CUSTOM_COLOR, |
| 143 | }; |
| 144 | |
| 145 | private: |
| 146 | BakeQuality bake_quality = BAKE_QUALITY_MEDIUM; |
| 147 | bool use_denoiser = true; |
| 148 | int bounces = 3; |
| 149 | float bias = 0.0005; |
| 150 | int max_texture_size = 16384; |
| 151 | bool interior = false; |
| 152 | EnvironmentMode environment_mode = ENVIRONMENT_MODE_SCENE; |
| 153 | Ref<Sky> environment_custom_sky; |
| 154 | Color environment_custom_color = Color(1, 1, 1); |
| 155 | float environment_custom_energy = 1.0; |
| 156 | bool directional = false; |
| 157 | GenerateProbes gen_probes = GENERATE_PROBES_SUBDIV_8; |
| 158 | Ref<CameraAttributes> camera_attributes; |
| 159 | |
| 160 | Ref<LightmapGIData> light_data; |
| 161 | |
| 162 | struct LightsFound { |
| 163 | Transform3D xform; |
| 164 | Light3D *light = nullptr; |
| 165 | }; |
| 166 | |
| 167 | struct MeshesFound { |
| 168 | Transform3D xform; |
| 169 | NodePath node_path; |
| 170 | int32_t subindex = 0; |
| 171 | Ref<Mesh> mesh; |
| 172 | int32_t lightmap_scale = 0; |
| 173 | Vector<Ref<Material>> overrides; |
| 174 | }; |
| 175 | |
| 176 | void _find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes); |
| 177 | |
| 178 | void _assign_lightmaps(); |
| 179 | void _clear_lightmaps(); |
| 180 | |
| 181 | struct BakeTimeData { |
| 182 | String text; |
| 183 | int pass = 0; |
| 184 | uint64_t last_step = 0; |
| 185 | }; |
| 186 | |
| 187 | struct BSPSimplex { |
| 188 | int vertices[4] = {}; |
| 189 | int planes[4] = {}; |
| 190 | }; |
| 191 | |
| 192 | struct BSPNode { |
| 193 | static const int32_t EMPTY_LEAF = INT32_MIN; |
| 194 | Plane plane; |
| 195 | int32_t over = EMPTY_LEAF; |
| 196 | int32_t under = EMPTY_LEAF; |
| 197 | }; |
| 198 | |
| 199 | int _bsp_get_simplex_side(const Vector<Vector3> &p_points, const LocalVector<BSPSimplex> &p_simplices, const Plane &p_plane, uint32_t p_simplex) const; |
| 200 | int32_t _compute_bsp_tree(const Vector<Vector3> &p_points, const LocalVector<Plane> &p_planes, LocalVector<int32_t> &planes_tested, const LocalVector<BSPSimplex> &p_simplices, const LocalVector<int32_t> &p_simplex_indices, LocalVector<BSPNode> &bsp_nodes); |
| 201 | |
| 202 | struct BakeStepUD { |
| 203 | Lightmapper::BakeStepFunc func; |
| 204 | void *ud = nullptr; |
| 205 | float from_percent = 0.0; |
| 206 | float to_percent = 0.0; |
| 207 | }; |
| 208 | |
| 209 | static bool _lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh); |
| 210 | |
| 211 | struct GenProbesOctree { |
| 212 | Vector3i offset; |
| 213 | uint32_t size = 0; |
| 214 | GenProbesOctree *children[8] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr }; |
| 215 | ~GenProbesOctree() { |
| 216 | for (int i = 0; i < 8; i++) { |
| 217 | if (children[i] != nullptr) { |
| 218 | memdelete(children[i]); |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | void _plot_triangle_into_octree(GenProbesOctree *p_cell, float p_cell_size, const Vector3 *p_triangle); |
| 225 | void _gen_new_positions_from_octree(const GenProbesOctree *p_cell, float p_cell_size, const Vector<Vector3> &probe_positions, LocalVector<Vector3> &new_probe_positions, HashMap<Vector3i, bool> &positions_used, const AABB &p_bounds); |
| 226 | |
| 227 | protected: |
| 228 | void _validate_property(PropertyInfo &p_property) const; |
| 229 | static void _bind_methods(); |
| 230 | void _notification(int p_what); |
| 231 | |
| 232 | public: |
| 233 | void set_light_data(const Ref<LightmapGIData> &p_data); |
| 234 | Ref<LightmapGIData> get_light_data() const; |
| 235 | |
| 236 | void set_bake_quality(BakeQuality p_quality); |
| 237 | BakeQuality get_bake_quality() const; |
| 238 | |
| 239 | void set_use_denoiser(bool p_enable); |
| 240 | bool is_using_denoiser() const; |
| 241 | |
| 242 | void set_directional(bool p_enable); |
| 243 | bool is_directional() const; |
| 244 | |
| 245 | void set_interior(bool p_interior); |
| 246 | bool is_interior() const; |
| 247 | |
| 248 | void set_environment_mode(EnvironmentMode p_mode); |
| 249 | EnvironmentMode get_environment_mode() const; |
| 250 | |
| 251 | void set_environment_custom_sky(const Ref<Sky> &p_sky); |
| 252 | Ref<Sky> get_environment_custom_sky() const; |
| 253 | |
| 254 | void set_environment_custom_color(const Color &p_color); |
| 255 | Color get_environment_custom_color() const; |
| 256 | |
| 257 | void set_environment_custom_energy(float p_energy); |
| 258 | float get_environment_custom_energy() const; |
| 259 | |
| 260 | void set_bounces(int p_bounces); |
| 261 | int get_bounces() const; |
| 262 | |
| 263 | void set_bias(float p_bias); |
| 264 | float get_bias() const; |
| 265 | |
| 266 | void set_max_texture_size(int p_size); |
| 267 | int get_max_texture_size() const; |
| 268 | |
| 269 | void set_generate_probes(GenerateProbes p_generate_probes); |
| 270 | GenerateProbes get_generate_probes() const; |
| 271 | |
| 272 | void set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes); |
| 273 | Ref<CameraAttributes> get_camera_attributes() const; |
| 274 | |
| 275 | AABB get_aabb() const override; |
| 276 | |
| 277 | BakeError bake(Node *p_from_node, String p_image_data_path = "" , Lightmapper::BakeStepFunc p_bake_step = nullptr, void *p_bake_userdata = nullptr); |
| 278 | |
| 279 | virtual PackedStringArray get_configuration_warnings() const override; |
| 280 | |
| 281 | LightmapGI(); |
| 282 | }; |
| 283 | |
| 284 | VARIANT_ENUM_CAST(LightmapGI::BakeQuality); |
| 285 | VARIANT_ENUM_CAST(LightmapGI::GenerateProbes); |
| 286 | VARIANT_ENUM_CAST(LightmapGI::BakeError); |
| 287 | VARIANT_ENUM_CAST(LightmapGI::EnvironmentMode); |
| 288 | |
| 289 | #endif // LIGHTMAP_GI_H |
| 290 | |