| 1 | /**************************************************************************/ |
| 2 | /* lightmap_gi.cpp */ |
| 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 | #include "lightmap_gi.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/io/config_file.h" |
| 35 | #include "core/math/delaunay_3d.h" |
| 36 | #include "core/object/object.h" |
| 37 | #include "scene/3d/lightmap_probe.h" |
| 38 | #include "scene/3d/mesh_instance_3d.h" |
| 39 | #include "scene/resources/camera_attributes.h" |
| 40 | #include "scene/resources/environment.h" |
| 41 | #include "scene/resources/image_texture.h" |
| 42 | #include "scene/resources/sky.h" |
| 43 | |
| 44 | #include "modules/modules_enabled.gen.h" // For lightmapper_rd. |
| 45 | |
| 46 | void LightmapGIData::add_user(const NodePath &p_path, const Rect2 &p_uv_scale, int p_slice_index, int32_t p_sub_instance) { |
| 47 | User user; |
| 48 | user.path = p_path; |
| 49 | user.uv_scale = p_uv_scale; |
| 50 | user.slice_index = p_slice_index; |
| 51 | user.sub_instance = p_sub_instance; |
| 52 | users.push_back(user); |
| 53 | } |
| 54 | |
| 55 | int LightmapGIData::get_user_count() const { |
| 56 | return users.size(); |
| 57 | } |
| 58 | |
| 59 | NodePath LightmapGIData::get_user_path(int p_user) const { |
| 60 | ERR_FAIL_INDEX_V(p_user, users.size(), NodePath()); |
| 61 | return users[p_user].path; |
| 62 | } |
| 63 | |
| 64 | int32_t LightmapGIData::get_user_sub_instance(int p_user) const { |
| 65 | ERR_FAIL_INDEX_V(p_user, users.size(), -1); |
| 66 | return users[p_user].sub_instance; |
| 67 | } |
| 68 | |
| 69 | Rect2 LightmapGIData::get_user_lightmap_uv_scale(int p_user) const { |
| 70 | ERR_FAIL_INDEX_V(p_user, users.size(), Rect2()); |
| 71 | return users[p_user].uv_scale; |
| 72 | } |
| 73 | |
| 74 | int LightmapGIData::get_user_lightmap_slice_index(int p_user) const { |
| 75 | ERR_FAIL_INDEX_V(p_user, users.size(), -1); |
| 76 | return users[p_user].slice_index; |
| 77 | } |
| 78 | |
| 79 | void LightmapGIData::clear_users() { |
| 80 | users.clear(); |
| 81 | } |
| 82 | |
| 83 | void LightmapGIData::_set_user_data(const Array &p_data) { |
| 84 | ERR_FAIL_COND((p_data.size() % 4) != 0); |
| 85 | users.clear(); |
| 86 | for (int i = 0; i < p_data.size(); i += 4) { |
| 87 | add_user(p_data[i + 0], p_data[i + 1], p_data[i + 2], p_data[i + 3]); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | Array LightmapGIData::_get_user_data() const { |
| 92 | Array ret; |
| 93 | for (int i = 0; i < users.size(); i++) { |
| 94 | ret.push_back(users[i].path); |
| 95 | ret.push_back(users[i].uv_scale); |
| 96 | ret.push_back(users[i].slice_index); |
| 97 | ret.push_back(users[i].sub_instance); |
| 98 | } |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | void LightmapGIData::set_lightmap_textures(const TypedArray<TextureLayered> &p_data) { |
| 103 | storage_light_textures = p_data; |
| 104 | if (p_data.is_empty()) { |
| 105 | combined_light_texture = Ref<TextureLayered>(); |
| 106 | _reset_lightmap_textures(); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | if (p_data.size() == 1) { |
| 111 | combined_light_texture = p_data[0]; |
| 112 | } else { |
| 113 | Vector<Ref<Image>> images; |
| 114 | for (int i = 0; i < p_data.size(); i++) { |
| 115 | Ref<TextureLayered> texture = p_data[i]; |
| 116 | ERR_FAIL_COND_MSG(texture.is_null(), vformat("Invalid TextureLayered at index %d.", i)); |
| 117 | for (int j = 0; j < texture->get_layers(); j++) { |
| 118 | images.push_back(texture->get_layer_data(j)); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | Ref<Texture2DArray> combined_texture; |
| 123 | combined_texture.instantiate(); |
| 124 | |
| 125 | combined_texture->create_from_images(images); |
| 126 | combined_light_texture = combined_texture; |
| 127 | } |
| 128 | _reset_lightmap_textures(); |
| 129 | } |
| 130 | |
| 131 | TypedArray<TextureLayered> LightmapGIData::get_lightmap_textures() const { |
| 132 | return storage_light_textures; |
| 133 | } |
| 134 | |
| 135 | void LightmapGIData::set_shadowmask_textures(const TypedArray<TextureLayered> &p_data) { |
| 136 | storage_shadowmask_textures = p_data; |
| 137 | |
| 138 | if (p_data.is_empty()) { |
| 139 | combined_shadowmask_texture = Ref<TextureLayered>(); |
| 140 | _reset_shadowmask_textures(); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | if (p_data.size() == 1) { |
| 145 | combined_shadowmask_texture = p_data[0]; |
| 146 | |
| 147 | } else { |
| 148 | Vector<Ref<Image>> images; |
| 149 | for (int i = 0; i < p_data.size(); i++) { |
| 150 | Ref<TextureLayered> texture = p_data[i]; |
| 151 | ERR_FAIL_COND_MSG(texture.is_null(), vformat("Invalid TextureLayered at index %d.", i)); |
| 152 | for (int j = 0; j < texture->get_layers(); j++) { |
| 153 | images.push_back(texture->get_layer_data(j)); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | Ref<Texture2DArray> combined_texture; |
| 158 | combined_texture.instantiate(); |
| 159 | |
| 160 | combined_texture->create_from_images(images); |
| 161 | combined_shadowmask_texture = combined_texture; |
| 162 | } |
| 163 | |
| 164 | _reset_shadowmask_textures(); |
| 165 | } |
| 166 | |
| 167 | TypedArray<TextureLayered> LightmapGIData::get_shadowmask_textures() const { |
| 168 | return storage_shadowmask_textures; |
| 169 | } |
| 170 | |
| 171 | void LightmapGIData::clear_shadowmask_textures() { |
| 172 | RS::get_singleton()->lightmap_set_shadowmask_textures(lightmap, RID()); |
| 173 | storage_shadowmask_textures.clear(); |
| 174 | combined_shadowmask_texture.unref(); |
| 175 | } |
| 176 | |
| 177 | bool LightmapGIData::has_shadowmask_textures() { |
| 178 | return !storage_shadowmask_textures.is_empty() && combined_shadowmask_texture.is_valid(); |
| 179 | } |
| 180 | |
| 181 | RID LightmapGIData::get_rid() const { |
| 182 | return lightmap; |
| 183 | } |
| 184 | |
| 185 | void LightmapGIData::clear() { |
| 186 | users.clear(); |
| 187 | } |
| 188 | |
| 189 | void LightmapGIData::_reset_lightmap_textures() { |
| 190 | RS::get_singleton()->lightmap_set_textures(lightmap, combined_light_texture.is_valid() ? combined_light_texture->get_rid() : RID(), uses_spherical_harmonics); |
| 191 | } |
| 192 | |
| 193 | void LightmapGIData::_reset_shadowmask_textures() { |
| 194 | RS::get_singleton()->lightmap_set_shadowmask_textures(lightmap, combined_shadowmask_texture.is_valid() ? combined_shadowmask_texture->get_rid() : RID()); |
| 195 | } |
| 196 | |
| 197 | void LightmapGIData::set_uses_spherical_harmonics(bool p_enable) { |
| 198 | uses_spherical_harmonics = p_enable; |
| 199 | _reset_lightmap_textures(); |
| 200 | } |
| 201 | |
| 202 | bool LightmapGIData::is_using_spherical_harmonics() const { |
| 203 | return uses_spherical_harmonics; |
| 204 | } |
| 205 | |
| 206 | void LightmapGIData::_set_uses_packed_directional(bool p_enable) { |
| 207 | _uses_packed_directional = p_enable; |
| 208 | } |
| 209 | |
| 210 | bool LightmapGIData::_is_using_packed_directional() const { |
| 211 | return _uses_packed_directional; |
| 212 | } |
| 213 | |
| 214 | void LightmapGIData::update_shadowmask_mode(ShadowmaskMode p_mode) { |
| 215 | RS::get_singleton()->lightmap_set_shadowmask_mode(lightmap, (RS::ShadowmaskMode)p_mode); |
| 216 | } |
| 217 | |
| 218 | LightmapGIData::ShadowmaskMode LightmapGIData::get_shadowmask_mode() const { |
| 219 | return (ShadowmaskMode)RS::get_singleton()->lightmap_get_shadowmask_mode(lightmap); |
| 220 | } |
| 221 | |
| 222 | void LightmapGIData::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, uint32_t p_lightprobe_hash) { |
| 223 | if (p_points.size()) { |
| 224 | int pc = p_points.size(); |
| 225 | ERR_FAIL_COND(pc * 9 != p_point_sh.size()); |
| 226 | ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0); |
| 227 | ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0); |
| 228 | RS::get_singleton()->lightmap_set_probe_capture_data(lightmap, p_points, p_point_sh, p_tetrahedra, p_bsp_tree); |
| 229 | RS::get_singleton()->lightmap_set_probe_bounds(lightmap, p_bounds); |
| 230 | RS::get_singleton()->lightmap_set_probe_interior(lightmap, p_interior); |
| 231 | } else { |
| 232 | RS::get_singleton()->lightmap_set_probe_capture_data(lightmap, PackedVector3Array(), PackedColorArray(), PackedInt32Array(), PackedInt32Array()); |
| 233 | RS::get_singleton()->lightmap_set_probe_bounds(lightmap, AABB()); |
| 234 | RS::get_singleton()->lightmap_set_probe_interior(lightmap, false); |
| 235 | } |
| 236 | RS::get_singleton()->lightmap_set_baked_exposure_normalization(lightmap, p_baked_exposure); |
| 237 | baked_exposure = p_baked_exposure; |
| 238 | lightprobe_hash = p_lightprobe_hash; |
| 239 | interior = p_interior; |
| 240 | bounds = p_bounds; |
| 241 | } |
| 242 | |
| 243 | PackedVector3Array LightmapGIData::get_capture_points() const { |
| 244 | return RS::get_singleton()->lightmap_get_probe_capture_points(lightmap); |
| 245 | } |
| 246 | |
| 247 | PackedColorArray LightmapGIData::get_capture_sh() const { |
| 248 | return RS::get_singleton()->lightmap_get_probe_capture_sh(lightmap); |
| 249 | } |
| 250 | |
| 251 | PackedInt32Array LightmapGIData::get_capture_tetrahedra() const { |
| 252 | return RS::get_singleton()->lightmap_get_probe_capture_tetrahedra(lightmap); |
| 253 | } |
| 254 | |
| 255 | PackedInt32Array LightmapGIData::get_capture_bsp_tree() const { |
| 256 | return RS::get_singleton()->lightmap_get_probe_capture_bsp_tree(lightmap); |
| 257 | } |
| 258 | |
| 259 | uint32_t LightmapGIData::get_lightprobe_hash() const { |
| 260 | return lightprobe_hash; |
| 261 | } |
| 262 | |
| 263 | AABB LightmapGIData::get_capture_bounds() const { |
| 264 | return bounds; |
| 265 | } |
| 266 | |
| 267 | bool LightmapGIData::is_interior() const { |
| 268 | return interior; |
| 269 | } |
| 270 | |
| 271 | float LightmapGIData::get_baked_exposure() const { |
| 272 | return baked_exposure; |
| 273 | } |
| 274 | |
| 275 | void LightmapGIData::_set_probe_data(const Dictionary &p_data) { |
| 276 | ERR_FAIL_COND(!p_data.has("bounds")); |
| 277 | ERR_FAIL_COND(!p_data.has("points")); |
| 278 | ERR_FAIL_COND(!p_data.has("tetrahedra")); |
| 279 | ERR_FAIL_COND(!p_data.has("bsp")); |
| 280 | ERR_FAIL_COND(!p_data.has("sh")); |
| 281 | ERR_FAIL_COND(!p_data.has("interior")); |
| 282 | ERR_FAIL_COND(!p_data.has("baked_exposure")); |
| 283 | |
| 284 | uint32_t phash = 0; |
| 285 | if (p_data.has("lightprobe_hash")) { // Older versions will not have it. |
| 286 | phash = p_data["lightprobe_hash"]; |
| 287 | } |
| 288 | set_capture_data(p_data["bounds"], p_data["interior"], p_data["points"], p_data["sh"], p_data["tetrahedra"], p_data["bsp"], p_data["baked_exposure"], phash); |
| 289 | } |
| 290 | |
| 291 | Dictionary LightmapGIData::_get_probe_data() const { |
| 292 | Dictionary d; |
| 293 | d["bounds"] = get_capture_bounds(); |
| 294 | d["points"] = get_capture_points(); |
| 295 | d["tetrahedra"] = get_capture_tetrahedra(); |
| 296 | d["bsp"] = get_capture_bsp_tree(); |
| 297 | d["sh"] = get_capture_sh(); |
| 298 | d["interior"] = is_interior(); |
| 299 | d["baked_exposure"] = get_baked_exposure(); |
| 300 | d["lightprobe_hash"] = lightprobe_hash; |
| 301 | return d; |
| 302 | } |
| 303 | |
| 304 | #ifndef DISABLE_DEPRECATED |
| 305 | void LightmapGIData::set_light_texture(const Ref<TextureLayered> &p_light_texture) { |
| 306 | TypedArray<TextureLayered> arr = { p_light_texture }; |
| 307 | set_lightmap_textures(arr); |
| 308 | } |
| 309 | |
| 310 | Ref<TextureLayered> LightmapGIData::get_light_texture() const { |
| 311 | if (storage_light_textures.is_empty()) { |
| 312 | return Ref<TextureLayered>(); |
| 313 | } |
| 314 | return storage_light_textures.get(0); |
| 315 | } |
| 316 | |
| 317 | void LightmapGIData::_set_light_textures_data(const Array &p_data) { |
| 318 | set_lightmap_textures(p_data); |
| 319 | } |
| 320 | |
| 321 | Array LightmapGIData::_get_light_textures_data() const { |
| 322 | return Array(storage_light_textures); |
| 323 | } |
| 324 | #endif |
| 325 | |
| 326 | void LightmapGIData::_bind_methods() { |
| 327 | ClassDB::bind_method(D_METHOD("_set_user_data", "data"), &LightmapGIData::_set_user_data); |
| 328 | ClassDB::bind_method(D_METHOD("_get_user_data"), &LightmapGIData::_get_user_data); |
| 329 | |
| 330 | ClassDB::bind_method(D_METHOD("set_lightmap_textures", "light_textures"), &LightmapGIData::set_lightmap_textures); |
| 331 | ClassDB::bind_method(D_METHOD("get_lightmap_textures"), &LightmapGIData::get_lightmap_textures); |
| 332 | |
| 333 | ClassDB::bind_method(D_METHOD("set_shadowmask_textures", "shadowmask_textures"), &LightmapGIData::set_shadowmask_textures); |
| 334 | ClassDB::bind_method(D_METHOD("get_shadowmask_textures"), &LightmapGIData::get_shadowmask_textures); |
| 335 | |
| 336 | ClassDB::bind_method(D_METHOD("set_uses_spherical_harmonics", "uses_spherical_harmonics"), &LightmapGIData::set_uses_spherical_harmonics); |
| 337 | ClassDB::bind_method(D_METHOD("is_using_spherical_harmonics"), &LightmapGIData::is_using_spherical_harmonics); |
| 338 | |
| 339 | ClassDB::bind_method(D_METHOD("_set_uses_packed_directional", "_uses_packed_directional"), &LightmapGIData::_set_uses_packed_directional); |
| 340 | ClassDB::bind_method(D_METHOD("_is_using_packed_directional"), &LightmapGIData::_is_using_packed_directional); |
| 341 | |
| 342 | ClassDB::bind_method(D_METHOD("add_user", "path", "uv_scale", "slice_index", "sub_instance"), &LightmapGIData::add_user); |
| 343 | ClassDB::bind_method(D_METHOD("get_user_count"), &LightmapGIData::get_user_count); |
| 344 | ClassDB::bind_method(D_METHOD("get_user_path", "user_idx"), &LightmapGIData::get_user_path); |
| 345 | ClassDB::bind_method(D_METHOD("clear_users"), &LightmapGIData::clear_users); |
| 346 | |
| 347 | ClassDB::bind_method(D_METHOD("_set_probe_data", "data"), &LightmapGIData::_set_probe_data); |
| 348 | ClassDB::bind_method(D_METHOD("_get_probe_data"), &LightmapGIData::_get_probe_data); |
| 349 | |
| 350 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "lightmap_textures", PROPERTY_HINT_ARRAY_TYPE, "TextureLayered", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY), "set_lightmap_textures", "get_lightmap_textures"); |
| 351 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "shadowmask_textures", PROPERTY_HINT_ARRAY_TYPE, "TextureLayered", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY), "set_shadowmask_textures", "get_shadowmask_textures"); |
| 352 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uses_spherical_harmonics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_uses_spherical_harmonics", "is_using_spherical_harmonics"); |
| 353 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "user_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_user_data", "_get_user_data"); |
| 354 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "probe_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_probe_data", "_get_probe_data"); |
| 355 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "_uses_packed_directional", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_uses_packed_directional", "_is_using_packed_directional"); |
| 356 | |
| 357 | #ifndef DISABLE_DEPRECATED |
| 358 | ClassDB::bind_method(D_METHOD("set_light_texture", "light_texture"), &LightmapGIData::set_light_texture); |
| 359 | ClassDB::bind_method(D_METHOD("get_light_texture"), &LightmapGIData::get_light_texture); |
| 360 | |
| 361 | ClassDB::bind_method(D_METHOD("_set_light_textures_data", "data"), &LightmapGIData::_set_light_textures_data); |
| 362 | ClassDB::bind_method(D_METHOD("_get_light_textures_data"), &LightmapGIData::_get_light_textures_data); |
| 363 | |
| 364 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_texture", PROPERTY_HINT_RESOURCE_TYPE, "TextureLayered", PROPERTY_USAGE_NONE), "set_light_texture", "get_light_texture"); |
| 365 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "light_textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_INTERNAL), "_set_light_textures_data", "_get_light_textures_data"); |
| 366 | #endif |
| 367 | |
| 368 | BIND_ENUM_CONSTANT(SHADOWMASK_MODE_NONE); |
| 369 | BIND_ENUM_CONSTANT(SHADOWMASK_MODE_REPLACE); |
| 370 | BIND_ENUM_CONSTANT(SHADOWMASK_MODE_OVERLAY); |
| 371 | } |
| 372 | |
| 373 | LightmapGIData::LightmapGIData() { |
| 374 | lightmap = RS::get_singleton()->lightmap_create(); |
| 375 | } |
| 376 | |
| 377 | LightmapGIData::~LightmapGIData() { |
| 378 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 379 | RS::get_singleton()->free_rid(lightmap); |
| 380 | } |
| 381 | |
| 382 | /////////////////////////// |
| 383 | |
| 384 | void LightmapGI::_find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes) { |
| 385 | MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_at_node); |
| 386 | if (mi && mi->get_gi_mode() == GeometryInstance3D::GI_MODE_STATIC && mi->is_visible_in_tree()) { |
| 387 | Ref<Mesh> mesh = mi->get_mesh(); |
| 388 | if (mesh.is_valid()) { |
| 389 | bool all_have_uv2_and_normal = true; |
| 390 | bool surfaces_found = false; |
| 391 | for (int i = 0; i < mesh->get_surface_count(); i++) { |
| 392 | if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) { |
| 393 | continue; |
| 394 | } |
| 395 | if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_TEX_UV2)) { |
| 396 | all_have_uv2_and_normal = false; |
| 397 | break; |
| 398 | } |
| 399 | if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_NORMAL)) { |
| 400 | all_have_uv2_and_normal = false; |
| 401 | break; |
| 402 | } |
| 403 | surfaces_found = true; |
| 404 | } |
| 405 | |
| 406 | if (surfaces_found && all_have_uv2_and_normal) { |
| 407 | //READY TO BAKE! size hint could be computed if not found, actually.. |
| 408 | |
| 409 | MeshesFound mf; |
| 410 | mf.xform = get_global_transform().affine_inverse() * mi->get_global_transform(); |
| 411 | mf.node_path = get_path_to(mi); |
| 412 | mf.subindex = -1; |
| 413 | mf.mesh = mesh; |
| 414 | mf.lightmap_scale = mi->get_lightmap_texel_scale(); |
| 415 | |
| 416 | Ref<Material> all_override = mi->get_material_override(); |
| 417 | for (int i = 0; i < mesh->get_surface_count(); i++) { |
| 418 | if (all_override.is_valid()) { |
| 419 | mf.overrides.push_back(all_override); |
| 420 | } else { |
| 421 | mf.overrides.push_back(mi->get_surface_override_material(i)); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | meshes.push_back(mf); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | Node3D *s = Object::cast_to<Node3D>(p_at_node); |
| 431 | |
| 432 | if (!mi && s) { |
| 433 | Array bmeshes = p_at_node->call("get_bake_meshes"); |
| 434 | if (bmeshes.size() && (bmeshes.size() & 1) == 0) { |
| 435 | Transform3D xf = get_global_transform().affine_inverse() * s->get_global_transform(); |
| 436 | for (int i = 0; i < bmeshes.size(); i += 2) { |
| 437 | Ref<Mesh> mesh = bmeshes[i]; |
| 438 | if (mesh.is_null()) { |
| 439 | continue; |
| 440 | } |
| 441 | |
| 442 | MeshesFound mf; |
| 443 | |
| 444 | Transform3D mesh_xf = bmeshes[i + 1]; |
| 445 | mf.xform = xf * mesh_xf; |
| 446 | mf.node_path = get_path_to(s); |
| 447 | mf.subindex = i / 2; |
| 448 | mf.lightmap_scale = 1.0; |
| 449 | mf.mesh = mesh; |
| 450 | |
| 451 | meshes.push_back(mf); |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | Light3D *light = Object::cast_to<Light3D>(p_at_node); |
| 457 | |
| 458 | if (light && light->get_bake_mode() != Light3D::BAKE_DISABLED) { |
| 459 | LightsFound lf; |
| 460 | lf.xform = get_global_transform().affine_inverse() * light->get_global_transform(); |
| 461 | lf.light = light; |
| 462 | lights.push_back(lf); |
| 463 | } |
| 464 | |
| 465 | LightmapProbe *probe = Object::cast_to<LightmapProbe>(p_at_node); |
| 466 | |
| 467 | if (probe) { |
| 468 | Transform3D xf = get_global_transform().affine_inverse() * probe->get_global_transform(); |
| 469 | probes.push_back(xf.origin); |
| 470 | } |
| 471 | |
| 472 | for (int i = 0; i < p_at_node->get_child_count(); i++) { |
| 473 | Node *child = p_at_node->get_child(i); |
| 474 | if (!child->get_owner()) { |
| 475 | continue; //maybe a helper |
| 476 | } |
| 477 | |
| 478 | _find_meshes_and_lights(child, meshes, lights, probes); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | int LightmapGI::_bsp_get_simplex_side(const LocalVector<Vector3> &p_points, const LocalVector<BSPSimplex> &p_simplices, const Plane &p_plane, uint32_t p_simplex) const { |
| 483 | int over = 0; |
| 484 | int under = 0; |
| 485 | const BSPSimplex &s = p_simplices[p_simplex]; |
| 486 | for (int i = 0; i < 4; i++) { |
| 487 | const Vector3 v = p_points[s.vertices[i]]; |
| 488 | // The tolerance used here comes from experiments on scenes up to |
| 489 | // 1000x1000x100 meters. If it's any smaller, some simplices will |
| 490 | // appear to self-intersect due to a lack of precision in Plane. |
| 491 | if (p_plane.has_point(v, 1.0 / (1 << 13))) { |
| 492 | // Coplanar. |
| 493 | } else if (p_plane.is_point_over(v)) { |
| 494 | over++; |
| 495 | } else { |
| 496 | under++; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | ERR_FAIL_COND_V(under == 0 && over == 0, -2); //should never happen, we discarded flat simplices before, but in any case drop it from the bsp tree and throw an error |
| 501 | if (under == 0) { |
| 502 | return 1; // all over |
| 503 | } else if (over == 0) { |
| 504 | return -1; // all under |
| 505 | } else { |
| 506 | return 0; // crossing |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | //#define DEBUG_BSP |
| 511 | |
| 512 | int32_t LightmapGI::_compute_bsp_tree(const LocalVector<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) { |
| 513 | ERR_FAIL_COND_V(p_simplex_indices.size() < 2, -1); |
| 514 | |
| 515 | int32_t node_index = (int32_t)bsp_nodes.size(); |
| 516 | bsp_nodes.push_back(BSPNode()); |
| 517 | |
| 518 | //test with all the simplex planes |
| 519 | Plane best_plane; |
| 520 | float best_plane_score = -1.0; |
| 521 | |
| 522 | for (const int idx : p_simplex_indices) { |
| 523 | const BSPSimplex &s = p_simplices[idx]; |
| 524 | for (int j = 0; j < 4; j++) { |
| 525 | uint32_t plane_index = s.planes[j]; |
| 526 | if (planes_tested[plane_index] == node_index) { |
| 527 | continue; //tested this plane already |
| 528 | } |
| 529 | |
| 530 | planes_tested[plane_index] = node_index; |
| 531 | |
| 532 | static const int face_order[4][3] = { |
| 533 | { 0, 1, 2 }, |
| 534 | { 0, 2, 3 }, |
| 535 | { 0, 1, 3 }, |
| 536 | { 1, 2, 3 } |
| 537 | }; |
| 538 | |
| 539 | // despite getting rid of plane duplicates, we should still use here the actual plane to avoid numerical error |
| 540 | // from thinking this same simplex is intersecting rather than on a side |
| 541 | Vector3 v0 = p_points[s.vertices[face_order[j][0]]]; |
| 542 | Vector3 v1 = p_points[s.vertices[face_order[j][1]]]; |
| 543 | Vector3 v2 = p_points[s.vertices[face_order[j][2]]]; |
| 544 | |
| 545 | Plane plane(v0, v1, v2); |
| 546 | |
| 547 | //test with all the simplices |
| 548 | int over_count = 0; |
| 549 | int under_count = 0; |
| 550 | |
| 551 | for (const int &index : p_simplex_indices) { |
| 552 | int side = _bsp_get_simplex_side(p_points, p_simplices, plane, index); |
| 553 | if (side == -2) { |
| 554 | continue; //this simplex is invalid, skip for now |
| 555 | } else if (side < 0) { |
| 556 | under_count++; |
| 557 | } else if (side > 0) { |
| 558 | over_count++; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if (under_count == 0 && over_count == 0) { |
| 563 | continue; //most likely precision issue with a flat simplex, do not try this plane |
| 564 | } |
| 565 | |
| 566 | if (under_count > over_count) { //make sure under is always less than over, so we can compute the same ratio |
| 567 | SWAP(under_count, over_count); |
| 568 | } |
| 569 | |
| 570 | float score = 0; //by default, score is 0 (worst) |
| 571 | if (over_count > 0) { |
| 572 | // Simplices that are intersected by the plane are moved into both the over |
| 573 | // and under subtrees which makes the entire tree deeper, so the best plane |
| 574 | // will have the least intersections while separating the simplices evenly. |
| 575 | float balance = float(under_count) / over_count; |
| 576 | float separation = float(over_count + under_count) / p_simplex_indices.size(); |
| 577 | score = balance * separation * separation; |
| 578 | } |
| 579 | |
| 580 | if (score > best_plane_score) { |
| 581 | best_plane = plane; |
| 582 | best_plane_score = score; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | // We often end up with two (or on rare occasions, three) simplices that are |
| 588 | // either disjoint or share one vertex and don't have a separating plane |
| 589 | // among their faces. The fallback is to loop through new planes created |
| 590 | // with one vertex of the first simplex and two vertices of the second until |
| 591 | // we find a winner. |
| 592 | if (best_plane_score == 0) { |
| 593 | const BSPSimplex &simplex0 = p_simplices[p_simplex_indices[0]]; |
| 594 | const BSPSimplex &simplex1 = p_simplices[p_simplex_indices[1]]; |
| 595 | |
| 596 | for (uint32_t i = 0; i < 4 && !best_plane_score; i++) { |
| 597 | Vector3 v0 = p_points[simplex0.vertices[i]]; |
| 598 | for (uint32_t j = 0; j < 3 && !best_plane_score; j++) { |
| 599 | if (simplex0.vertices[i] == simplex1.vertices[j]) { |
| 600 | break; |
| 601 | } |
| 602 | Vector3 v1 = p_points[simplex1.vertices[j]]; |
| 603 | for (uint32_t k = j + 1; k < 4; k++) { |
| 604 | if (simplex0.vertices[i] == simplex1.vertices[k]) { |
| 605 | break; |
| 606 | } |
| 607 | Vector3 v2 = p_points[simplex1.vertices[k]]; |
| 608 | |
| 609 | Plane plane = Plane(v0, v1, v2); |
| 610 | if (plane == Plane()) { // When v0, v1, and v2 are collinear, they can't form a plane. |
| 611 | continue; |
| 612 | } |
| 613 | int32_t side0 = _bsp_get_simplex_side(p_points, p_simplices, plane, p_simplex_indices[0]); |
| 614 | int32_t side1 = _bsp_get_simplex_side(p_points, p_simplices, plane, p_simplex_indices[1]); |
| 615 | if ((side0 == 1 && side1 == -1) || (side0 == -1 && side1 == 1)) { |
| 616 | best_plane = plane; |
| 617 | best_plane_score = 1.0; |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | LocalVector<int32_t> indices_over; |
| 626 | LocalVector<int32_t> indices_under; |
| 627 | |
| 628 | //split again, but add to list |
| 629 | for (const uint32_t index : p_simplex_indices) { |
| 630 | int side = _bsp_get_simplex_side(p_points, p_simplices, best_plane, index); |
| 631 | |
| 632 | if (side == -2) { |
| 633 | continue; //simplex sits on the plane, does not make sense to use it |
| 634 | } |
| 635 | if (side <= 0) { |
| 636 | indices_under.push_back(index); |
| 637 | } |
| 638 | |
| 639 | if (side >= 0) { |
| 640 | indices_over.push_back(index); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | #ifdef DEBUG_BSP |
| 645 | print_line("node " + itos(node_index) + " found plane: " + best_plane + " score:" + rtos(best_plane_score) + " - over " + itos(indices_over.size()) + " under " + itos(indices_under.size()) + " intersecting " + itos(intersecting)); |
| 646 | #endif |
| 647 | |
| 648 | if (best_plane_score < 0.0 || indices_over.size() == p_simplex_indices.size() || indices_under.size() == p_simplex_indices.size()) { |
| 649 | // Failed to separate the tetrahedrons using planes |
| 650 | // this means Delaunay broke at some point. |
| 651 | // Luckily, because we are using tetrahedrons, we can resort to |
| 652 | // less precise but still working ways to generate the separating plane |
| 653 | // this will most likely look bad when interpolating, but at least it will not crash. |
| 654 | // and the artifact will most likely also be very small, so too difficult to notice. |
| 655 | |
| 656 | //find the longest axis |
| 657 | |
| 658 | WARN_PRINT("Inconsistency found in triangulation while building BSP, probe interpolation quality may degrade a bit."); |
| 659 | |
| 660 | LocalVector<Vector3> centers; |
| 661 | AABB bounds_all; |
| 662 | for (uint32_t i = 0; i < p_simplex_indices.size(); i++) { |
| 663 | AABB bounds; |
| 664 | for (uint32_t j = 0; j < 4; j++) { |
| 665 | Vector3 p = p_points[p_simplices[p_simplex_indices[i]].vertices[j]]; |
| 666 | if (j == 0) { |
| 667 | bounds.position = p; |
| 668 | } else { |
| 669 | bounds.expand_to(p); |
| 670 | } |
| 671 | } |
| 672 | if (i == 0) { |
| 673 | centers.push_back(bounds.get_center()); |
| 674 | } else { |
| 675 | bounds_all.merge_with(bounds); |
| 676 | } |
| 677 | } |
| 678 | Vector3::Axis longest_axis = Vector3::Axis(bounds_all.get_longest_axis_index()); |
| 679 | |
| 680 | //find the simplex that will go under |
| 681 | uint32_t min_d_idx = 0xFFFFFFFF; |
| 682 | float min_d_dist = 1e20; |
| 683 | |
| 684 | for (uint32_t i = 0; i < centers.size(); i++) { |
| 685 | if (centers[i][longest_axis] < min_d_dist) { |
| 686 | min_d_idx = i; |
| 687 | min_d_dist = centers[i][longest_axis]; |
| 688 | } |
| 689 | } |
| 690 | //rebuild best_plane and over/under arrays |
| 691 | best_plane = Plane(); |
| 692 | best_plane.normal[longest_axis] = 1.0; |
| 693 | best_plane.d = min_d_dist; |
| 694 | |
| 695 | indices_under.clear(); |
| 696 | indices_under.push_back(min_d_idx); |
| 697 | |
| 698 | indices_over.clear(); |
| 699 | |
| 700 | for (uint32_t i = 0; i < p_simplex_indices.size(); i++) { |
| 701 | if (i == min_d_idx) { |
| 702 | continue; |
| 703 | } |
| 704 | indices_over.push_back(p_simplex_indices[i]); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | BSPNode node; |
| 709 | node.plane = best_plane; |
| 710 | |
| 711 | if (indices_under.is_empty()) { |
| 712 | //nothing to do here |
| 713 | node.under = BSPNode::EMPTY_LEAF; |
| 714 | } else if (indices_under.size() == 1) { |
| 715 | node.under = -(indices_under[0] + 1); |
| 716 | } else { |
| 717 | node.under = _compute_bsp_tree(p_points, p_planes, planes_tested, p_simplices, indices_under, bsp_nodes); |
| 718 | } |
| 719 | |
| 720 | if (indices_over.is_empty()) { |
| 721 | //nothing to do here |
| 722 | node.over = BSPNode::EMPTY_LEAF; |
| 723 | } else if (indices_over.size() == 1) { |
| 724 | node.over = -(indices_over[0] + 1); |
| 725 | } else { |
| 726 | node.over = _compute_bsp_tree(p_points, p_planes, planes_tested, p_simplices, indices_over, bsp_nodes); |
| 727 | } |
| 728 | |
| 729 | bsp_nodes[node_index] = node; |
| 730 | |
| 731 | return node_index; |
| 732 | } |
| 733 | |
| 734 | bool LightmapGI::_lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh) { |
| 735 | BakeStepUD *bsud = (BakeStepUD *)ud; |
| 736 | bool ret = false; |
| 737 | if (bsud->func) { |
| 738 | ret = bsud->func(bsud->from_percent + p_completion * (bsud->to_percent - bsud->from_percent), p_text, bsud->ud, p_refresh); |
| 739 | } |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | void LightmapGI::_plot_triangle_into_octree(GenProbesOctree *p_cell, float p_cell_size, const Vector3 *p_triangle) { |
| 744 | for (int i = 0; i < 8; i++) { |
| 745 | Vector3i pos = p_cell->offset; |
| 746 | uint32_t half_size = p_cell->size / 2; |
| 747 | if (i & 1) { |
| 748 | pos.x += half_size; |
| 749 | } |
| 750 | if (i & 2) { |
| 751 | pos.y += half_size; |
| 752 | } |
| 753 | if (i & 4) { |
| 754 | pos.z += half_size; |
| 755 | } |
| 756 | |
| 757 | AABB subcell; |
| 758 | subcell.position = Vector3(pos) * p_cell_size; |
| 759 | subcell.size = Vector3(half_size, half_size, half_size) * p_cell_size; |
| 760 | |
| 761 | if (!Geometry3D::triangle_box_overlap(subcell.get_center(), subcell.size * 0.5, p_triangle)) { |
| 762 | continue; |
| 763 | } |
| 764 | |
| 765 | if (p_cell->children[i] == nullptr) { |
| 766 | GenProbesOctree *child = memnew(GenProbesOctree); |
| 767 | child->offset = pos; |
| 768 | child->size = half_size; |
| 769 | p_cell->children[i] = child; |
| 770 | } |
| 771 | |
| 772 | if (half_size > 1) { |
| 773 | //still levels missing |
| 774 | _plot_triangle_into_octree(p_cell->children[i], p_cell_size, p_triangle); |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | void LightmapGI::_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) { |
| 780 | for (int i = 0; i < 8; i++) { |
| 781 | Vector3i pos = p_cell->offset; |
| 782 | if (i & 1) { |
| 783 | pos.x += p_cell->size; |
| 784 | } |
| 785 | if (i & 2) { |
| 786 | pos.y += p_cell->size; |
| 787 | } |
| 788 | if (i & 4) { |
| 789 | pos.z += p_cell->size; |
| 790 | } |
| 791 | |
| 792 | if (p_cell->size == 1 && !positions_used.has(pos)) { |
| 793 | //new position to insert! |
| 794 | Vector3 real_pos = p_bounds.position + Vector3(pos) * p_cell_size; |
| 795 | //see if a user submitted probe is too close |
| 796 | int ppcount = probe_positions.size(); |
| 797 | const Vector3 *pp = probe_positions.ptr(); |
| 798 | bool exists = false; |
| 799 | for (int j = 0; j < ppcount; j++) { |
| 800 | if (pp[j].distance_to(real_pos) < (p_cell_size * 0.5f)) { |
| 801 | exists = true; |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if (!exists) { |
| 807 | new_probe_positions.push_back(real_pos); |
| 808 | } |
| 809 | |
| 810 | positions_used[pos] = true; |
| 811 | } |
| 812 | |
| 813 | if (p_cell->children[i] != nullptr) { |
| 814 | _gen_new_positions_from_octree(p_cell->children[i], p_cell_size, probe_positions, new_probe_positions, positions_used, p_bounds); |
| 815 | } |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | LightmapGI::BakeError LightmapGI::_save_and_reimport_atlas_textures(const Ref<Lightmapper> p_lightmapper, const String &p_base_name, TypedArray<TextureLayered> &r_textures, bool p_is_shadowmask) const { |
| 820 | Vector<Ref<Image>> images; |
| 821 | images.resize(p_is_shadowmask ? p_lightmapper->get_shadowmask_texture_count() : p_lightmapper->get_bake_texture_count()); |
| 822 | |
| 823 | for (int i = 0; i < images.size(); i++) { |
| 824 | images.set(i, p_is_shadowmask ? p_lightmapper->get_shadowmask_texture(i) : p_lightmapper->get_bake_texture(i)); |
| 825 | } |
| 826 | |
| 827 | const int slice_count = images.size(); |
| 828 | const int slice_width = images[0]->get_width(); |
| 829 | const int slice_height = images[0]->get_height(); |
| 830 | |
| 831 | const int slices_per_texture = Image::MAX_HEIGHT / slice_height; |
| 832 | const int texture_count = Math::ceil(slice_count / (float)slices_per_texture); |
| 833 | const int last_count = slice_count % slices_per_texture; |
| 834 | |
| 835 | r_textures.resize(texture_count); |
| 836 | |
| 837 | for (int i = 0; i < texture_count; i++) { |
| 838 | const int texture_slice_count = (i == texture_count - 1 && last_count != 0) ? last_count : slices_per_texture; |
| 839 | |
| 840 | Ref<Image> texture_image = Image::create_empty(slice_width, slice_height * texture_slice_count, false, images[0]->get_format()); |
| 841 | |
| 842 | for (int j = 0; j < texture_slice_count; j++) { |
| 843 | texture_image->blit_rect(images[i * slices_per_texture + j], Rect2i(0, 0, slice_width, slice_height), Point2i(0, slice_height * j)); |
| 844 | } |
| 845 | |
| 846 | const String atlas_path = (texture_count > 1 ? p_base_name + "_" + itos(i) : p_base_name) + (p_is_shadowmask ? ".png" : ".exr"); |
| 847 | const String config_path = atlas_path + ".import"; |
| 848 | |
| 849 | Ref<ConfigFile> config; |
| 850 | config.instantiate(); |
| 851 | |
| 852 | // Load an import configuration if present. |
| 853 | if (FileAccess::exists(config_path)) { |
| 854 | config->load(config_path); |
| 855 | } |
| 856 | |
| 857 | config->set_value("remap", "importer", "2d_array_texture"); |
| 858 | config->set_value("remap", "type", "CompressedTexture2DArray"); |
| 859 | if (!config->has_section_key("params", "compress/mode")) { |
| 860 | // Do not override an existing compression mode. |
| 861 | config->set_value("params", "compress/mode", 2); |
| 862 | } |
| 863 | config->set_value("params", "compress/channel_pack", 1); |
| 864 | config->set_value("params", "mipmaps/generate", false); |
| 865 | config->set_value("params", "slices/horizontal", 1); |
| 866 | config->set_value("params", "slices/vertical", texture_slice_count); |
| 867 | |
| 868 | config->save(config_path); |
| 869 | |
| 870 | if (supersampling_enabled) { |
| 871 | texture_image->resize(texture_image->get_width() / supersampling_factor, texture_image->get_height() / supersampling_factor, Image::INTERPOLATE_TRILINEAR); |
| 872 | } |
| 873 | |
| 874 | // Save the file. |
| 875 | Error save_err; |
| 876 | if (p_is_shadowmask) { |
| 877 | save_err = texture_image->save_png(atlas_path); |
| 878 | } else { |
| 879 | save_err = texture_image->save_exr(atlas_path, false); |
| 880 | } |
| 881 | |
| 882 | ERR_FAIL_COND_V(save_err, LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE); |
| 883 | |
| 884 | // Reimport the file. |
| 885 | ResourceLoader::import(atlas_path); |
| 886 | Ref<TextureLayered> t = ResourceLoader::load(atlas_path); // If already loaded, it will be updated on refocus? |
| 887 | ERR_FAIL_COND_V(t.is_null(), LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE); |
| 888 | |
| 889 | // Store the atlas in the array. |
| 890 | r_textures[i] = t; |
| 891 | } |
| 892 | |
| 893 | return LightmapGI::BAKE_ERROR_OK; |
| 894 | } |
| 895 | |
| 896 | LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_path, Lightmapper::BakeStepFunc p_bake_step, void *p_bake_userdata) { |
| 897 | if (p_image_data_path.is_empty()) { |
| 898 | if (get_light_data().is_null()) { |
| 899 | return BAKE_ERROR_NO_SAVE_PATH; |
| 900 | } |
| 901 | |
| 902 | p_image_data_path = get_light_data()->get_path(); |
| 903 | if (!p_image_data_path.is_resource_file()) { |
| 904 | return BAKE_ERROR_NO_SAVE_PATH; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | Ref<Lightmapper> lightmapper = Lightmapper::create(); |
| 909 | ERR_FAIL_COND_V(lightmapper.is_null(), BAKE_ERROR_NO_LIGHTMAPPER); |
| 910 | |
| 911 | BakeStepUD bsud; |
| 912 | bsud.func = p_bake_step; |
| 913 | bsud.ud = p_bake_userdata; |
| 914 | bsud.from_percent = 0.2; |
| 915 | bsud.to_percent = 0.8; |
| 916 | |
| 917 | if (p_bake_step) { |
| 918 | p_bake_step(0.0, RTR("Finding meshes, lights and probes"), p_bake_userdata, true); |
| 919 | } |
| 920 | /* STEP 1, FIND MESHES, LIGHTS AND PROBES */ |
| 921 | Vector<Lightmapper::MeshData> mesh_data; |
| 922 | Vector<LightsFound> lights_found; |
| 923 | Vector<Vector3> probes_found; |
| 924 | AABB bounds; |
| 925 | { |
| 926 | Vector<MeshesFound> meshes_found; |
| 927 | _find_meshes_and_lights(p_from_node ? p_from_node : get_parent(), meshes_found, lights_found, probes_found); |
| 928 | |
| 929 | if (meshes_found.is_empty()) { |
| 930 | return BAKE_ERROR_NO_MESHES; |
| 931 | } |
| 932 | // create mesh data for insert |
| 933 | |
| 934 | //get the base material textures, help compute atlas size and bounds |
| 935 | for (int m_i = 0; m_i < meshes_found.size(); m_i++) { |
| 936 | if (p_bake_step) { |
| 937 | float p = (float)(m_i) / meshes_found.size(); |
| 938 | p_bake_step(p * 0.1, vformat(RTR("Preparing geometry %d/%d"), m_i, meshes_found.size()), p_bake_userdata, false); |
| 939 | } |
| 940 | |
| 941 | MeshesFound &mf = meshes_found.write[m_i]; |
| 942 | |
| 943 | Size2i mesh_lightmap_size = mf.mesh->get_lightmap_size_hint(); |
| 944 | if (mesh_lightmap_size == Size2i(0, 0)) { |
| 945 | // TODO we should compute a size if no lightmap hint is set, as we did in 3.x. |
| 946 | // For now set to basic size to avoid crash. |
| 947 | mesh_lightmap_size = Size2i(64, 64); |
| 948 | } |
| 949 | // Double lightmap texel density if downsampling is enabled, as the final texture size will be halved before saving lightmaps. |
| 950 | Size2i lightmap_size = Size2i(Size2(mesh_lightmap_size) * mf.lightmap_scale * texel_scale) * (supersampling_enabled ? supersampling_factor : 1.0); |
| 951 | ERR_FAIL_COND_V(lightmap_size.x == 0 || lightmap_size.y == 0, BAKE_ERROR_LIGHTMAP_TOO_SMALL); |
| 952 | |
| 953 | TypedArray<RID> overrides; |
| 954 | overrides.resize(mf.overrides.size()); |
| 955 | for (int i = 0; i < mf.overrides.size(); i++) { |
| 956 | if (mf.overrides[i].is_valid()) { |
| 957 | overrides[i] = mf.overrides[i]->get_rid(); |
| 958 | } |
| 959 | } |
| 960 | TypedArray<Image> images = RS::get_singleton()->bake_render_uv2(mf.mesh->get_rid(), overrides, lightmap_size); |
| 961 | |
| 962 | ERR_FAIL_COND_V(images.is_empty(), BAKE_ERROR_CANT_CREATE_IMAGE); |
| 963 | |
| 964 | Ref<Image> albedo = images[RS::BAKE_CHANNEL_ALBEDO_ALPHA]; |
| 965 | Ref<Image> orm = images[RS::BAKE_CHANNEL_ORM]; |
| 966 | |
| 967 | //multiply albedo by metal |
| 968 | |
| 969 | Lightmapper::MeshData md; |
| 970 | |
| 971 | { |
| 972 | Dictionary d; |
| 973 | d["path"] = mf.node_path; |
| 974 | if (mf.subindex >= 0) { |
| 975 | d["subindex"] = mf.subindex; |
| 976 | } |
| 977 | md.userdata = d; |
| 978 | } |
| 979 | |
| 980 | { |
| 981 | if (albedo->get_format() != Image::FORMAT_RGBA8) { |
| 982 | albedo->convert(Image::FORMAT_RGBA8); |
| 983 | } |
| 984 | if (orm->get_format() != Image::FORMAT_RGBA8) { |
| 985 | orm->convert(Image::FORMAT_RGBA8); |
| 986 | } |
| 987 | Vector<uint8_t> albedo_alpha = albedo->get_data(); |
| 988 | Vector<uint8_t> orm_data = orm->get_data(); |
| 989 | |
| 990 | Vector<uint8_t> albedom; |
| 991 | uint32_t len = albedo_alpha.size(); |
| 992 | albedom.resize(len); |
| 993 | const uint8_t *r_aa = albedo_alpha.ptr(); |
| 994 | const uint8_t *r_orm = orm_data.ptr(); |
| 995 | uint8_t *w_albedo = albedom.ptrw(); |
| 996 | |
| 997 | for (uint32_t i = 0; i < len; i += 4) { |
| 998 | w_albedo[i + 0] = uint8_t(CLAMP(float(r_aa[i + 0]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255)); |
| 999 | w_albedo[i + 1] = uint8_t(CLAMP(float(r_aa[i + 1]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255)); |
| 1000 | w_albedo[i + 2] = uint8_t(CLAMP(float(r_aa[i + 2]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255)); |
| 1001 | w_albedo[i + 3] = r_aa[i + 3]; |
| 1002 | } |
| 1003 | |
| 1004 | md.albedo_on_uv2.instantiate(); |
| 1005 | md.albedo_on_uv2->set_data(lightmap_size.width, lightmap_size.height, false, Image::FORMAT_RGBA8, albedom); |
| 1006 | } |
| 1007 | |
| 1008 | md.emission_on_uv2 = images[RS::BAKE_CHANNEL_EMISSION]; |
| 1009 | if (md.emission_on_uv2->get_format() != Image::FORMAT_RGBAH) { |
| 1010 | md.emission_on_uv2->convert(Image::FORMAT_RGBAH); |
| 1011 | } |
| 1012 | |
| 1013 | //get geometry |
| 1014 | |
| 1015 | Basis normal_xform = mf.xform.basis.inverse().transposed(); |
| 1016 | |
| 1017 | for (int i = 0; i < mf.mesh->get_surface_count(); i++) { |
| 1018 | if (mf.mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) { |
| 1019 | continue; |
| 1020 | } |
| 1021 | Array a = mf.mesh->surface_get_arrays(i); |
| 1022 | Ref<Material> mat = mf.mesh->surface_get_material(i); |
| 1023 | RID mat_rid; |
| 1024 | if (mat.is_valid()) { |
| 1025 | mat_rid = mat->get_rid(); |
| 1026 | } |
| 1027 | |
| 1028 | Vector<Vector3> vertices = a[Mesh::ARRAY_VERTEX]; |
| 1029 | const Vector3 *vr = vertices.ptr(); |
| 1030 | Vector<Vector2> uv = a[Mesh::ARRAY_TEX_UV2]; |
| 1031 | const Vector2 *uvr = nullptr; |
| 1032 | Vector<Vector3> normals = a[Mesh::ARRAY_NORMAL]; |
| 1033 | const Vector3 *nr = nullptr; |
| 1034 | Vector<int> index = a[Mesh::ARRAY_INDEX]; |
| 1035 | |
| 1036 | ERR_CONTINUE(uv.is_empty()); |
| 1037 | ERR_CONTINUE(normals.is_empty()); |
| 1038 | |
| 1039 | uvr = uv.ptr(); |
| 1040 | nr = normals.ptr(); |
| 1041 | |
| 1042 | int facecount; |
| 1043 | const int *ir = nullptr; |
| 1044 | |
| 1045 | if (index.size()) { |
| 1046 | facecount = index.size() / 3; |
| 1047 | ir = index.ptr(); |
| 1048 | } else { |
| 1049 | facecount = vertices.size() / 3; |
| 1050 | } |
| 1051 | |
| 1052 | for (int j = 0; j < facecount; j++) { |
| 1053 | uint32_t vidx[3]; |
| 1054 | |
| 1055 | if (ir) { |
| 1056 | for (int k = 0; k < 3; k++) { |
| 1057 | vidx[k] = ir[j * 3 + k]; |
| 1058 | } |
| 1059 | } else { |
| 1060 | for (int k = 0; k < 3; k++) { |
| 1061 | vidx[k] = j * 3 + k; |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | for (int k = 0; k < 3; k++) { |
| 1066 | Vector3 v = mf.xform.xform(vr[vidx[k]]); |
| 1067 | if (bounds == AABB()) { |
| 1068 | bounds.position = v; |
| 1069 | } else { |
| 1070 | bounds.expand_to(v); |
| 1071 | } |
| 1072 | md.points.push_back(v); |
| 1073 | |
| 1074 | md.uv2.push_back(uvr[vidx[k]]); |
| 1075 | md.normal.push_back(normal_xform.xform(nr[vidx[k]]).normalized()); |
| 1076 | md.material.push_back(mat_rid); |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | mesh_data.push_back(md); |
| 1082 | } |
| 1083 | } |
| 1084 | |
| 1085 | /* STEP 2, CREATE PROBES */ |
| 1086 | |
| 1087 | if (p_bake_step) { |
| 1088 | p_bake_step(0.3, RTR("Creating probes"), p_bake_userdata, true); |
| 1089 | } |
| 1090 | |
| 1091 | //bounds need to include the user probes |
| 1092 | for (int i = 0; i < probes_found.size(); i++) { |
| 1093 | bounds.expand_to(probes_found[i]); |
| 1094 | } |
| 1095 | |
| 1096 | bounds.grow_by(bounds.size.length() * 0.001); |
| 1097 | |
| 1098 | if (gen_probes == GENERATE_PROBES_DISABLED) { |
| 1099 | // generate 8 probes on bound endpoints |
| 1100 | for (int i = 0; i < 8; i++) { |
| 1101 | probes_found.push_back(bounds.get_endpoint(i)); |
| 1102 | } |
| 1103 | } else { |
| 1104 | // detect probes from geometry |
| 1105 | static const int subdiv_values[6] = { 0, 4, 8, 16, 32 }; |
| 1106 | int subdiv = subdiv_values[gen_probes]; |
| 1107 | |
| 1108 | float subdiv_cell_size; |
| 1109 | Vector3i bound_limit; |
| 1110 | { |
| 1111 | int longest_axis = bounds.get_longest_axis_index(); |
| 1112 | subdiv_cell_size = bounds.size[longest_axis] / subdiv; |
| 1113 | int axis_n1 = (longest_axis + 1) % 3; |
| 1114 | int axis_n2 = (longest_axis + 2) % 3; |
| 1115 | |
| 1116 | bound_limit[longest_axis] = subdiv; |
| 1117 | bound_limit[axis_n1] = int(Math::ceil(bounds.size[axis_n1] / subdiv_cell_size)); |
| 1118 | bound_limit[axis_n2] = int(Math::ceil(bounds.size[axis_n2] / subdiv_cell_size)); |
| 1119 | //compensate bounds |
| 1120 | bounds.size[axis_n1] = bound_limit[axis_n1] * subdiv_cell_size; |
| 1121 | bounds.size[axis_n2] = bound_limit[axis_n2] * subdiv_cell_size; |
| 1122 | } |
| 1123 | |
| 1124 | GenProbesOctree octree; |
| 1125 | octree.size = subdiv; |
| 1126 | |
| 1127 | for (int i = 0; i < mesh_data.size(); i++) { |
| 1128 | if (p_bake_step) { |
| 1129 | float p = (float)(i) / mesh_data.size(); |
| 1130 | p_bake_step(0.3 + p * 0.1, vformat(RTR("Creating probes from mesh %d/%d"), i, mesh_data.size()), p_bake_userdata, false); |
| 1131 | } |
| 1132 | |
| 1133 | for (int j = 0; j < mesh_data[i].points.size(); j += 3) { |
| 1134 | Vector3 points[3] = { mesh_data[i].points[j + 0] - bounds.position, mesh_data[i].points[j + 1] - bounds.position, mesh_data[i].points[j + 2] - bounds.position }; |
| 1135 | _plot_triangle_into_octree(&octree, subdiv_cell_size, points); |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | LocalVector<Vector3> new_probe_positions; |
| 1140 | HashMap<Vector3i, bool> positions_used; |
| 1141 | for (uint32_t i = 0; i < 8; i++) { //insert bounding endpoints |
| 1142 | Vector3i pos; |
| 1143 | if (i & 1) { |
| 1144 | pos.x += bound_limit.x; |
| 1145 | } |
| 1146 | if (i & 2) { |
| 1147 | pos.y += bound_limit.y; |
| 1148 | } |
| 1149 | if (i & 4) { |
| 1150 | pos.z += bound_limit.z; |
| 1151 | } |
| 1152 | |
| 1153 | positions_used[pos] = true; |
| 1154 | Vector3 real_pos = bounds.position + Vector3(pos) * subdiv_cell_size; //use same formula for numerical stability |
| 1155 | new_probe_positions.push_back(real_pos); |
| 1156 | } |
| 1157 | //skip first level, since probes are always added at bounds endpoints anyway (code above this) |
| 1158 | for (int i = 0; i < 8; i++) { |
| 1159 | if (octree.children[i]) { |
| 1160 | _gen_new_positions_from_octree(octree.children[i], subdiv_cell_size, probes_found, new_probe_positions, positions_used, bounds); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | for (const Vector3 &position : new_probe_positions) { |
| 1165 | probes_found.push_back(position); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | // Add everything to lightmapper |
| 1170 | const bool use_physical_light_units = GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units"); |
| 1171 | if (p_bake_step) { |
| 1172 | p_bake_step(0.4, RTR("Preparing Lightmapper"), p_bake_userdata, true); |
| 1173 | } |
| 1174 | |
| 1175 | { |
| 1176 | for (int i = 0; i < mesh_data.size(); i++) { |
| 1177 | lightmapper->add_mesh(mesh_data[i]); |
| 1178 | } |
| 1179 | for (int i = 0; i < lights_found.size(); i++) { |
| 1180 | Light3D *light = lights_found[i].light; |
| 1181 | if (light->is_editor_only()) { |
| 1182 | // Don't include editor-only lights in the lightmap bake, |
| 1183 | // as this results in inconsistent visuals when running the project. |
| 1184 | continue; |
| 1185 | } |
| 1186 | |
| 1187 | Transform3D xf = lights_found[i].xform; |
| 1188 | |
| 1189 | // For the lightmapper, the indirect energy represents the multiplier for the indirect bounces caused by the light, so the value is not converted when using physical units. |
| 1190 | float indirect_energy = light->get_param(Light3D::PARAM_INDIRECT_ENERGY); |
| 1191 | Color linear_color = light->get_color().srgb_to_linear(); |
| 1192 | float energy = light->get_param(Light3D::PARAM_ENERGY); |
| 1193 | if (use_physical_light_units) { |
| 1194 | energy *= light->get_param(Light3D::PARAM_INTENSITY); |
| 1195 | linear_color *= light->get_correlated_color().srgb_to_linear(); |
| 1196 | } |
| 1197 | |
| 1198 | if (Object::cast_to<DirectionalLight3D>(light)) { |
| 1199 | DirectionalLight3D *l = Object::cast_to<DirectionalLight3D>(light); |
| 1200 | if (l->get_sky_mode() != DirectionalLight3D::SKY_MODE_SKY_ONLY) { |
| 1201 | lightmapper->add_directional_light(light->get_name(), light->get_bake_mode() == Light3D::BAKE_STATIC, -xf.basis.get_column(Vector3::AXIS_Z).normalized(), linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR)); |
| 1202 | } |
| 1203 | } else if (Object::cast_to<OmniLight3D>(light)) { |
| 1204 | OmniLight3D *l = Object::cast_to<OmniLight3D>(light); |
| 1205 | if (use_physical_light_units) { |
| 1206 | energy *= (1.0 / (Math::PI * 4.0)); |
| 1207 | } |
| 1208 | lightmapper->add_omni_light(light->get_name(), light->get_bake_mode() == Light3D::BAKE_STATIC, xf.origin, linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_RANGE), l->get_param(Light3D::PARAM_ATTENUATION), l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR)); |
| 1209 | } else if (Object::cast_to<SpotLight3D>(light)) { |
| 1210 | SpotLight3D *l = Object::cast_to<SpotLight3D>(light); |
| 1211 | if (use_physical_light_units) { |
| 1212 | energy *= (1.0 / Math::PI); |
| 1213 | } |
| 1214 | lightmapper->add_spot_light(light->get_name(), light->get_bake_mode() == Light3D::BAKE_STATIC, xf.origin, -xf.basis.get_column(Vector3::AXIS_Z).normalized(), linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_RANGE), l->get_param(Light3D::PARAM_ATTENUATION), l->get_param(Light3D::PARAM_SPOT_ANGLE), l->get_param(Light3D::PARAM_SPOT_ATTENUATION), l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR)); |
| 1215 | } |
| 1216 | } |
| 1217 | for (int i = 0; i < probes_found.size(); i++) { |
| 1218 | lightmapper->add_probe(probes_found[i]); |
| 1219 | } |
| 1220 | } |
| 1221 | |
| 1222 | Ref<Image> environment_image; |
| 1223 | Basis environment_transform; |
| 1224 | |
| 1225 | // Add everything to lightmapper |
| 1226 | if (environment_mode != ENVIRONMENT_MODE_DISABLED) { |
| 1227 | if (p_bake_step) { |
| 1228 | p_bake_step(4.1, RTR("Preparing Environment"), p_bake_userdata, true); |
| 1229 | } |
| 1230 | |
| 1231 | environment_transform = get_global_transform().basis; |
| 1232 | |
| 1233 | switch (environment_mode) { |
| 1234 | case ENVIRONMENT_MODE_DISABLED: { |
| 1235 | //nothing |
| 1236 | } break; |
| 1237 | case ENVIRONMENT_MODE_SCENE: { |
| 1238 | Ref<World3D> world = get_world_3d(); |
| 1239 | if (world.is_valid()) { |
| 1240 | Ref<Environment> env = world->get_environment(); |
| 1241 | if (env.is_null()) { |
| 1242 | env = world->get_fallback_environment(); |
| 1243 | } |
| 1244 | |
| 1245 | if (env.is_valid()) { |
| 1246 | environment_image = RS::get_singleton()->environment_bake_panorama(env->get_rid(), true, Size2i(128, 64)); |
| 1247 | environment_transform = Basis::from_euler(env->get_sky_rotation()).inverse(); |
| 1248 | } |
| 1249 | } |
| 1250 | } break; |
| 1251 | case ENVIRONMENT_MODE_CUSTOM_SKY: { |
| 1252 | if (environment_custom_sky.is_valid()) { |
| 1253 | environment_image = RS::get_singleton()->sky_bake_panorama(environment_custom_sky->get_rid(), environment_custom_energy, true, Size2i(128, 64)); |
| 1254 | } |
| 1255 | |
| 1256 | } break; |
| 1257 | case ENVIRONMENT_MODE_CUSTOM_COLOR: { |
| 1258 | environment_image.instantiate(); |
| 1259 | environment_image->initialize_data(128, 64, false, Image::FORMAT_RGBAF); |
| 1260 | Color c = environment_custom_color; |
| 1261 | c.r *= environment_custom_energy; |
| 1262 | c.g *= environment_custom_energy; |
| 1263 | c.b *= environment_custom_energy; |
| 1264 | environment_image->fill(c); |
| 1265 | |
| 1266 | } break; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | float exposure_normalization = 1.0; |
| 1271 | if (camera_attributes.is_valid()) { |
| 1272 | exposure_normalization = camera_attributes->get_exposure_multiplier(); |
| 1273 | if (use_physical_light_units) { |
| 1274 | exposure_normalization = camera_attributes->calculate_exposure_normalization(); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | Lightmapper::BakeError bake_err = lightmapper->bake(Lightmapper::BakeQuality(bake_quality), use_denoiser, denoiser_strength, denoiser_range, bounces, |
| 1279 | bounce_indirect_energy, bias, max_texture_size, directional, shadowmask_mode != LightmapGIData::SHADOWMASK_MODE_NONE, use_texture_for_bounces, |
| 1280 | Lightmapper::GenerateProbes(gen_probes), environment_image, environment_transform, _lightmap_bake_step_function, &bsud, exposure_normalization, (supersampling_enabled ? supersampling_factor : 1)); |
| 1281 | |
| 1282 | if (bake_err == Lightmapper::BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE) { |
| 1283 | return BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL; |
| 1284 | } else if (bake_err == Lightmapper::BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES) { |
| 1285 | return BAKE_ERROR_MESHES_INVALID; |
| 1286 | } else if (bake_err == Lightmapper::BAKE_ERROR_ATLAS_TOO_SMALL) { |
| 1287 | return BAKE_ERROR_ATLAS_TOO_SMALL; |
| 1288 | } else if (bake_err == Lightmapper::BAKE_ERROR_USER_ABORTED) { |
| 1289 | return BAKE_ERROR_USER_ABORTED; |
| 1290 | } |
| 1291 | |
| 1292 | // POSTBAKE: Save Textures. |
| 1293 | TypedArray<TextureLayered> lightmap_textures; |
| 1294 | TypedArray<TextureLayered> shadowmask_textures; |
| 1295 | |
| 1296 | const String texture_filename = p_image_data_path.get_basename(); |
| 1297 | const int shadowmask_texture_count = lightmapper->get_shadowmask_texture_count(); |
| 1298 | const bool save_shadowmask = shadowmask_mode != LightmapGIData::SHADOWMASK_MODE_NONE && shadowmask_texture_count > 0; |
| 1299 | |
| 1300 | // Save the lightmap atlases. |
| 1301 | BakeError save_err = _save_and_reimport_atlas_textures(lightmapper, texture_filename, lightmap_textures, false); |
| 1302 | ERR_FAIL_COND_V(save_err != BAKE_ERROR_OK, save_err); |
| 1303 | |
| 1304 | if (save_shadowmask) { |
| 1305 | // Save the shadowmask atlases. |
| 1306 | save_err = _save_and_reimport_atlas_textures(lightmapper, texture_filename + "_shadow", shadowmask_textures, true); |
| 1307 | ERR_FAIL_COND_V(save_err != BAKE_ERROR_OK, save_err); |
| 1308 | } |
| 1309 | |
| 1310 | // POSTBAKE: Save Light Data. |
| 1311 | Ref<LightmapGIData> gi_data; |
| 1312 | |
| 1313 | if (get_light_data().is_valid()) { |
| 1314 | gi_data = get_light_data(); |
| 1315 | set_light_data(Ref<LightmapGIData>()); // Clear. |
| 1316 | gi_data->clear(); |
| 1317 | |
| 1318 | } else { |
| 1319 | gi_data.instantiate(); |
| 1320 | } |
| 1321 | |
| 1322 | gi_data->set_lightmap_textures(lightmap_textures); |
| 1323 | |
| 1324 | if (save_shadowmask) { |
| 1325 | gi_data->set_shadowmask_textures(shadowmask_textures); |
| 1326 | } else { |
| 1327 | gi_data->clear_shadowmask_textures(); |
| 1328 | } |
| 1329 | |
| 1330 | gi_data->set_uses_spherical_harmonics(directional); |
| 1331 | gi_data->_set_uses_packed_directional(directional); // New SH lightmaps are packed automatically. |
| 1332 | |
| 1333 | for (int i = 0; i < lightmapper->get_bake_mesh_count(); i++) { |
| 1334 | Dictionary d = lightmapper->get_bake_mesh_userdata(i); |
| 1335 | NodePath np = d["path"]; |
| 1336 | int32_t subindex = -1; |
| 1337 | if (d.has("subindex")) { |
| 1338 | subindex = d["subindex"]; |
| 1339 | } |
| 1340 | |
| 1341 | Rect2 uv_scale = lightmapper->get_bake_mesh_uv_scale(i); |
| 1342 | int slice_index = lightmapper->get_bake_mesh_texture_slice(i); |
| 1343 | gi_data->add_user(np, uv_scale, slice_index, subindex); |
| 1344 | } |
| 1345 | |
| 1346 | int probe_count = lightmapper->get_bake_probe_count(); |
| 1347 | |
| 1348 | // Probe SH may change between bakes. |
| 1349 | LocalVector<Color> probe_sh; |
| 1350 | LocalVector<Vector3> probe_points; |
| 1351 | probe_sh.resize(probe_count * 9); |
| 1352 | probe_points.resize(probe_count); |
| 1353 | |
| 1354 | uint32_t bake_probe_hash = HASH_MURMUR3_SEED; |
| 1355 | for (int i = 0; i < probe_count; i++) { |
| 1356 | // Calculate the hash from probe positions. |
| 1357 | Vector3 point = lightmapper->get_bake_probe_point(i); |
| 1358 | bake_probe_hash = hash_murmur3_one_double(point.x, bake_probe_hash); |
| 1359 | bake_probe_hash = hash_murmur3_one_double(point.y, bake_probe_hash); |
| 1360 | bake_probe_hash = hash_murmur3_one_double(point.z, bake_probe_hash); |
| 1361 | |
| 1362 | probe_points[i] = point; |
| 1363 | Vector<Color> colors = lightmapper->get_bake_probe_sh(i); |
| 1364 | ERR_CONTINUE(colors.size() != 9); |
| 1365 | for (int j = 0; j < 9; j++) { |
| 1366 | probe_sh[i * 9 + j] = colors[j]; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | // If the probe hash doesn't match, build the BSP tree from scratch. |
| 1371 | if (bake_probe_hash != gi_data->get_lightprobe_hash()) { |
| 1372 | // Obtain solved simplices. |
| 1373 | if (p_bake_step) { |
| 1374 | p_bake_step(0.8, RTR("Generating Probe Volumes"), p_bake_userdata, true); |
| 1375 | } |
| 1376 | |
| 1377 | Vector<Delaunay3D::OutputSimplex> solved_simplices = Delaunay3D::tetrahedralize(Vector<Vector3>(probe_points)); |
| 1378 | int64_t simplex_count = solved_simplices.size(); |
| 1379 | |
| 1380 | LocalVector<BSPSimplex> bsp_simplices; |
| 1381 | LocalVector<Plane> bsp_planes; |
| 1382 | LocalVector<int32_t> bsp_simplex_indices; |
| 1383 | PackedInt32Array tetrahedrons; |
| 1384 | |
| 1385 | for (int i = 0; i < simplex_count; i++) { |
| 1386 | //Prepare a special representation of the simplex, which uses a BSP Tree |
| 1387 | BSPSimplex bsp_simplex; |
| 1388 | for (int j = 0; j < 4; j++) { |
| 1389 | bsp_simplex.vertices[j] = solved_simplices[i].points[j]; |
| 1390 | } |
| 1391 | for (int j = 0; j < 4; j++) { |
| 1392 | static const int face_order[4][3] = { |
| 1393 | { 0, 1, 2 }, |
| 1394 | { 0, 2, 3 }, |
| 1395 | { 0, 1, 3 }, |
| 1396 | { 1, 2, 3 } |
| 1397 | }; |
| 1398 | Vector3 a = probe_points[solved_simplices[i].points[face_order[j][0]]]; |
| 1399 | Vector3 b = probe_points[solved_simplices[i].points[face_order[j][1]]]; |
| 1400 | Vector3 c = probe_points[solved_simplices[i].points[face_order[j][2]]]; |
| 1401 | |
| 1402 | //store planes in an array, but ensure they are reused, to speed up processing |
| 1403 | |
| 1404 | Plane p(a, b, c); |
| 1405 | int plane_index = -1; |
| 1406 | for (uint32_t k = 0; k < bsp_planes.size(); k++) { |
| 1407 | if (bsp_planes[k].is_equal_approx_any_side(p)) { |
| 1408 | plane_index = k; |
| 1409 | break; |
| 1410 | } |
| 1411 | } |
| 1412 | |
| 1413 | if (plane_index == -1) { |
| 1414 | plane_index = bsp_planes.size(); |
| 1415 | bsp_planes.push_back(p); |
| 1416 | } |
| 1417 | |
| 1418 | bsp_simplex.planes[j] = plane_index; |
| 1419 | |
| 1420 | //also fill simplex array |
| 1421 | tetrahedrons.push_back(solved_simplices[i].points[j]); |
| 1422 | } |
| 1423 | |
| 1424 | bsp_simplex_indices.push_back(bsp_simplices.size()); |
| 1425 | bsp_simplices.push_back(bsp_simplex); |
| 1426 | } |
| 1427 | |
| 1428 | //#define DEBUG_SIMPLICES_AS_OBJ_FILE |
| 1429 | #ifdef DEBUG_SIMPLICES_AS_OBJ_FILE |
| 1430 | { |
| 1431 | Ref<FileAccess> f = FileAccess::open("res://bsp.obj", FileAccess::WRITE); |
| 1432 | for (uint32_t i = 0; i < bsp_simplices.size(); i++) { |
| 1433 | f->store_line("o Simplex" + itos(i)); |
| 1434 | for (int j = 0; j < 4; j++) { |
| 1435 | f->store_line(vformat("v %f %f %f", probe_points[bsp_simplices[i].vertices[j]].x, probe_points[bsp_simplices[i].vertices[j]].y, probe_points[bsp_simplices[i].vertices[j]].z)); |
| 1436 | } |
| 1437 | static const int face_order[4][3] = { |
| 1438 | { 1, 2, 3 }, |
| 1439 | { 1, 3, 4 }, |
| 1440 | { 1, 2, 4 }, |
| 1441 | { 2, 3, 4 } |
| 1442 | }; |
| 1443 | |
| 1444 | for (int j = 0; j < 4; j++) { |
| 1445 | f->store_line(vformat("f %d %d %d", 4 * i + face_order[j][0], 4 * i + face_order[j][1], 4 * i + face_order[j][2])); |
| 1446 | } |
| 1447 | } |
| 1448 | } |
| 1449 | #endif |
| 1450 | |
| 1451 | LocalVector<BSPNode> bsp_nodes; |
| 1452 | LocalVector<int32_t> planes_tested; |
| 1453 | planes_tested.resize(bsp_planes.size()); |
| 1454 | for (int &index : planes_tested) { |
| 1455 | index = 0x7FFFFFFF; |
| 1456 | } |
| 1457 | |
| 1458 | if (p_bake_step) { |
| 1459 | p_bake_step(0.9, RTR("Generating Probe Acceleration Structures"), p_bake_userdata, true); |
| 1460 | } |
| 1461 | |
| 1462 | // Compute a BSP tree of the simplices, so it's easy to find the exact one. |
| 1463 | _compute_bsp_tree(probe_points, bsp_planes, planes_tested, bsp_simplices, bsp_simplex_indices, bsp_nodes); |
| 1464 | |
| 1465 | PackedInt32Array bsp_array; |
| 1466 | bsp_array.resize(bsp_nodes.size() * 6); // six 32 bits values used for each BSP node |
| 1467 | { |
| 1468 | float *fptr = (float *)bsp_array.ptrw(); |
| 1469 | int32_t *iptr = (int32_t *)bsp_array.ptrw(); |
| 1470 | for (uint32_t i = 0; i < bsp_nodes.size(); i++) { |
| 1471 | fptr[i * 6 + 0] = bsp_nodes[i].plane.normal.x; |
| 1472 | fptr[i * 6 + 1] = bsp_nodes[i].plane.normal.y; |
| 1473 | fptr[i * 6 + 2] = bsp_nodes[i].plane.normal.z; |
| 1474 | fptr[i * 6 + 3] = bsp_nodes[i].plane.d; |
| 1475 | iptr[i * 6 + 4] = bsp_nodes[i].over; |
| 1476 | iptr[i * 6 + 5] = bsp_nodes[i].under; |
| 1477 | } |
| 1478 | //#define DEBUG_BSP_TREE |
| 1479 | #ifdef DEBUG_BSP_TREE |
| 1480 | Ref<FileAccess> f = FileAccess::open("res://bsp.txt", FileAccess::WRITE); |
| 1481 | for (uint32_t i = 0; i < bsp_nodes.size(); i++) { |
| 1482 | f->store_line(itos(i) + " - plane: " + bsp_nodes[i].plane + " over: " + itos(bsp_nodes[i].over) + " under: " + itos(bsp_nodes[i].under)); |
| 1483 | } |
| 1484 | #endif |
| 1485 | } |
| 1486 | |
| 1487 | gi_data->set_capture_data(bounds, interior, Vector<Vector3>(probe_points), Vector<Color>(probe_sh), tetrahedrons, bsp_array, exposure_normalization, bake_probe_hash); |
| 1488 | } else { |
| 1489 | gi_data->set_capture_data(bounds, interior, Vector<Vector3>(probe_points), Vector<Color>(probe_sh), gi_data->get_capture_tetrahedra(), gi_data->get_capture_bsp_tree(), exposure_normalization, bake_probe_hash); |
| 1490 | } |
| 1491 | |
| 1492 | gi_data->set_path(p_image_data_path, true); |
| 1493 | Error err = ResourceSaver::save(gi_data); |
| 1494 | |
| 1495 | if (err != OK) { |
| 1496 | return BAKE_ERROR_CANT_CREATE_IMAGE; |
| 1497 | } |
| 1498 | |
| 1499 | set_light_data(gi_data); |
| 1500 | update_configuration_warnings(); |
| 1501 | |
| 1502 | return BAKE_ERROR_OK; |
| 1503 | } |
| 1504 | |
| 1505 | void LightmapGI::_notification(int p_what) { |
| 1506 | switch (p_what) { |
| 1507 | case NOTIFICATION_POST_ENTER_TREE: { |
| 1508 | if (light_data.is_valid()) { |
| 1509 | ERR_FAIL_COND_MSG( |
| 1510 | light_data->is_using_spherical_harmonics() && !light_data->_is_using_packed_directional(), |
| 1511 | vformat( |
| 1512 | "%s (%s): The directional lightmap textures are stored in a format that isn't supported anymore. Please bake lightmaps again to make lightmaps display from this node again.", |
| 1513 | get_light_data()->get_path(), get_name())); |
| 1514 | |
| 1515 | if (last_owner && last_owner != get_owner()) { |
| 1516 | light_data->clear_users(); |
| 1517 | } |
| 1518 | |
| 1519 | _assign_lightmaps(); |
| 1520 | } |
| 1521 | } break; |
| 1522 | |
| 1523 | case NOTIFICATION_EXIT_TREE: { |
| 1524 | last_owner = get_owner(); |
| 1525 | |
| 1526 | if (light_data.is_valid()) { |
| 1527 | _clear_lightmaps(); |
| 1528 | } |
| 1529 | } break; |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | void LightmapGI::_assign_lightmaps() { |
| 1534 | ERR_FAIL_COND(light_data.is_null()); |
| 1535 | |
| 1536 | Vector<String> missing_node_paths; |
| 1537 | |
| 1538 | for (int i = 0; i < light_data->get_user_count(); i++) { |
| 1539 | NodePath user_path = light_data->get_user_path(i); |
| 1540 | Node *node = get_node_or_null(user_path); |
| 1541 | if (!node) { |
| 1542 | missing_node_paths.push_back(String(user_path)); |
| 1543 | continue; |
| 1544 | } |
| 1545 | int instance_idx = light_data->get_user_sub_instance(i); |
| 1546 | if (instance_idx >= 0) { |
| 1547 | RID instance_id = node->call("get_bake_mesh_instance", instance_idx); |
| 1548 | if (instance_id.is_valid()) { |
| 1549 | RS::get_singleton()->instance_geometry_set_lightmap(instance_id, get_instance(), light_data->get_user_lightmap_uv_scale(i), light_data->get_user_lightmap_slice_index(i)); |
| 1550 | } |
| 1551 | } else { |
| 1552 | VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node); |
| 1553 | ERR_CONTINUE(!vi); |
| 1554 | RS::get_singleton()->instance_geometry_set_lightmap(vi->get_instance(), get_instance(), light_data->get_user_lightmap_uv_scale(i), light_data->get_user_lightmap_slice_index(i)); |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | if (!missing_node_paths.is_empty()) { |
| 1559 | String missing_paths_text; |
| 1560 | if (missing_node_paths.size() <= 3) { |
| 1561 | missing_paths_text = String(", ").join(missing_node_paths); |
| 1562 | } else { |
| 1563 | missing_paths_text = vformat("%s and %d more", String(", ").join(missing_node_paths.slice(0, 3)), missing_node_paths.size() - 3); |
| 1564 | } |
| 1565 | WARN_PRINT(vformat("%s couldn't find previously baked nodes and needs a rebake (missing nodes: %s).", get_name(), missing_paths_text)); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | void LightmapGI::_clear_lightmaps() { |
| 1570 | ERR_FAIL_COND(light_data.is_null()); |
| 1571 | for (int i = 0; i < light_data->get_user_count(); i++) { |
| 1572 | Node *node = get_node_or_null(light_data->get_user_path(i)); |
| 1573 | if (!node) { |
| 1574 | continue; |
| 1575 | } |
| 1576 | int instance_idx = light_data->get_user_sub_instance(i); |
| 1577 | if (instance_idx >= 0) { |
| 1578 | RID instance_id = node->call("get_bake_mesh_instance", instance_idx); |
| 1579 | if (instance_id.is_valid()) { |
| 1580 | RS::get_singleton()->instance_geometry_set_lightmap(instance_id, RID(), Rect2(), 0); |
| 1581 | } |
| 1582 | } else { |
| 1583 | VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node); |
| 1584 | ERR_CONTINUE(!vi); |
| 1585 | RS::get_singleton()->instance_geometry_set_lightmap(vi->get_instance(), RID(), Rect2(), 0); |
| 1586 | } |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | void LightmapGI::set_light_data(const Ref<LightmapGIData> &p_data) { |
| 1591 | if (light_data.is_valid()) { |
| 1592 | if (is_inside_tree()) { |
| 1593 | _clear_lightmaps(); |
| 1594 | } |
| 1595 | set_base(RID()); |
| 1596 | } |
| 1597 | light_data = p_data; |
| 1598 | |
| 1599 | if (light_data.is_valid()) { |
| 1600 | set_base(light_data->get_rid()); |
| 1601 | if (is_inside_tree()) { |
| 1602 | _assign_lightmaps(); |
| 1603 | } |
| 1604 | light_data->update_shadowmask_mode(shadowmask_mode); |
| 1605 | } |
| 1606 | |
| 1607 | update_gizmos(); |
| 1608 | } |
| 1609 | |
| 1610 | Ref<LightmapGIData> LightmapGI::get_light_data() const { |
| 1611 | return light_data; |
| 1612 | } |
| 1613 | |
| 1614 | void LightmapGI::set_bake_quality(BakeQuality p_quality) { |
| 1615 | bake_quality = p_quality; |
| 1616 | } |
| 1617 | |
| 1618 | LightmapGI::BakeQuality LightmapGI::get_bake_quality() const { |
| 1619 | return bake_quality; |
| 1620 | } |
| 1621 | |
| 1622 | AABB LightmapGI::get_aabb() const { |
| 1623 | return AABB(); |
| 1624 | } |
| 1625 | |
| 1626 | void LightmapGI::set_use_denoiser(bool p_enable) { |
| 1627 | use_denoiser = p_enable; |
| 1628 | notify_property_list_changed(); |
| 1629 | } |
| 1630 | |
| 1631 | bool LightmapGI::is_using_denoiser() const { |
| 1632 | return use_denoiser; |
| 1633 | } |
| 1634 | |
| 1635 | void LightmapGI::set_denoiser_strength(float p_denoiser_strength) { |
| 1636 | denoiser_strength = p_denoiser_strength; |
| 1637 | } |
| 1638 | |
| 1639 | float LightmapGI::get_denoiser_strength() const { |
| 1640 | return denoiser_strength; |
| 1641 | } |
| 1642 | |
| 1643 | void LightmapGI::set_denoiser_range(int p_denoiser_range) { |
| 1644 | denoiser_range = p_denoiser_range; |
| 1645 | } |
| 1646 | |
| 1647 | int LightmapGI::get_denoiser_range() const { |
| 1648 | return denoiser_range; |
| 1649 | } |
| 1650 | |
| 1651 | void LightmapGI::set_directional(bool p_enable) { |
| 1652 | directional = p_enable; |
| 1653 | } |
| 1654 | |
| 1655 | bool LightmapGI::is_directional() const { |
| 1656 | return directional; |
| 1657 | } |
| 1658 | |
| 1659 | void LightmapGI::set_shadowmask_mode(LightmapGIData::ShadowmaskMode p_mode) { |
| 1660 | shadowmask_mode = p_mode; |
| 1661 | if (light_data.is_valid()) { |
| 1662 | light_data->update_shadowmask_mode(p_mode); |
| 1663 | } |
| 1664 | |
| 1665 | update_configuration_warnings(); |
| 1666 | } |
| 1667 | |
| 1668 | LightmapGIData::ShadowmaskMode LightmapGI::get_shadowmask_mode() const { |
| 1669 | return shadowmask_mode; |
| 1670 | } |
| 1671 | |
| 1672 | void LightmapGI::set_use_texture_for_bounces(bool p_enable) { |
| 1673 | use_texture_for_bounces = p_enable; |
| 1674 | } |
| 1675 | |
| 1676 | bool LightmapGI::is_using_texture_for_bounces() const { |
| 1677 | return use_texture_for_bounces; |
| 1678 | } |
| 1679 | |
| 1680 | void LightmapGI::set_interior(bool p_enable) { |
| 1681 | interior = p_enable; |
| 1682 | } |
| 1683 | |
| 1684 | bool LightmapGI::is_interior() const { |
| 1685 | return interior; |
| 1686 | } |
| 1687 | |
| 1688 | void LightmapGI::set_environment_mode(EnvironmentMode p_mode) { |
| 1689 | environment_mode = p_mode; |
| 1690 | notify_property_list_changed(); |
| 1691 | } |
| 1692 | |
| 1693 | LightmapGI::EnvironmentMode LightmapGI::get_environment_mode() const { |
| 1694 | return environment_mode; |
| 1695 | } |
| 1696 | |
| 1697 | void LightmapGI::set_environment_custom_sky(const Ref<Sky> &p_sky) { |
| 1698 | environment_custom_sky = p_sky; |
| 1699 | } |
| 1700 | |
| 1701 | Ref<Sky> LightmapGI::get_environment_custom_sky() const { |
| 1702 | return environment_custom_sky; |
| 1703 | } |
| 1704 | |
| 1705 | void LightmapGI::set_environment_custom_color(const Color &p_color) { |
| 1706 | environment_custom_color = p_color; |
| 1707 | } |
| 1708 | |
| 1709 | Color LightmapGI::get_environment_custom_color() const { |
| 1710 | return environment_custom_color; |
| 1711 | } |
| 1712 | |
| 1713 | void LightmapGI::set_environment_custom_energy(float p_energy) { |
| 1714 | environment_custom_energy = p_energy; |
| 1715 | } |
| 1716 | |
| 1717 | float LightmapGI::get_environment_custom_energy() const { |
| 1718 | return environment_custom_energy; |
| 1719 | } |
| 1720 | |
| 1721 | void LightmapGI::set_bounces(int p_bounces) { |
| 1722 | ERR_FAIL_COND(p_bounces < 0 || p_bounces > 16); |
| 1723 | bounces = p_bounces; |
| 1724 | } |
| 1725 | |
| 1726 | int LightmapGI::get_bounces() const { |
| 1727 | return bounces; |
| 1728 | } |
| 1729 | |
| 1730 | void LightmapGI::set_bounce_indirect_energy(float p_indirect_energy) { |
| 1731 | ERR_FAIL_COND(p_indirect_energy < 0.0); |
| 1732 | bounce_indirect_energy = p_indirect_energy; |
| 1733 | } |
| 1734 | |
| 1735 | float LightmapGI::get_bounce_indirect_energy() const { |
| 1736 | return bounce_indirect_energy; |
| 1737 | } |
| 1738 | |
| 1739 | void LightmapGI::set_bias(float p_bias) { |
| 1740 | ERR_FAIL_COND(p_bias < 0.00001); |
| 1741 | bias = p_bias; |
| 1742 | } |
| 1743 | |
| 1744 | float LightmapGI::get_bias() const { |
| 1745 | return bias; |
| 1746 | } |
| 1747 | |
| 1748 | void LightmapGI::set_texel_scale(float p_multiplier) { |
| 1749 | ERR_FAIL_COND(p_multiplier < (0.01 - CMP_EPSILON)); |
| 1750 | texel_scale = p_multiplier; |
| 1751 | } |
| 1752 | |
| 1753 | float LightmapGI::get_texel_scale() const { |
| 1754 | return texel_scale; |
| 1755 | } |
| 1756 | |
| 1757 | void LightmapGI::set_max_texture_size(int p_size) { |
| 1758 | ERR_FAIL_COND_MSG(p_size < 2048, vformat("The LightmapGI maximum texture size supplied (%d) is too small. The minimum allowed value is 2048.", p_size)); |
| 1759 | ERR_FAIL_COND_MSG(p_size > 16384, vformat("The LightmapGI maximum texture size supplied (%d) is too large. The maximum allowed value is 16384.", p_size)); |
| 1760 | max_texture_size = p_size; |
| 1761 | } |
| 1762 | |
| 1763 | int LightmapGI::get_max_texture_size() const { |
| 1764 | return max_texture_size; |
| 1765 | } |
| 1766 | |
| 1767 | void LightmapGI::set_supersampling_enabled(bool p_enable) { |
| 1768 | supersampling_enabled = p_enable; |
| 1769 | |
| 1770 | notify_property_list_changed(); |
| 1771 | } |
| 1772 | |
| 1773 | bool LightmapGI::is_supersampling_enabled() const { |
| 1774 | return supersampling_enabled; |
| 1775 | } |
| 1776 | |
| 1777 | void LightmapGI::set_supersampling_factor(float p_factor) { |
| 1778 | ERR_FAIL_COND(p_factor < 1); |
| 1779 | |
| 1780 | supersampling_factor = p_factor; |
| 1781 | } |
| 1782 | |
| 1783 | float LightmapGI::get_supersampling_factor() const { |
| 1784 | return supersampling_factor; |
| 1785 | } |
| 1786 | |
| 1787 | void LightmapGI::set_generate_probes(GenerateProbes p_generate_probes) { |
| 1788 | gen_probes = p_generate_probes; |
| 1789 | } |
| 1790 | |
| 1791 | LightmapGI::GenerateProbes LightmapGI::get_generate_probes() const { |
| 1792 | return gen_probes; |
| 1793 | } |
| 1794 | |
| 1795 | void LightmapGI::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) { |
| 1796 | camera_attributes = p_camera_attributes; |
| 1797 | } |
| 1798 | |
| 1799 | Ref<CameraAttributes> LightmapGI::get_camera_attributes() const { |
| 1800 | return camera_attributes; |
| 1801 | } |
| 1802 | |
| 1803 | PackedStringArray LightmapGI::get_configuration_warnings() const { |
| 1804 | PackedStringArray warnings = VisualInstance3D::get_configuration_warnings(); |
| 1805 | |
| 1806 | #ifdef MODULE_LIGHTMAPPER_RD_ENABLED |
| 1807 | if (!DisplayServer::get_singleton()->can_create_rendering_device()) { |
| 1808 | warnings.push_back(vformat(RTR("Lightmaps can only be baked from a GPU that supports the RenderingDevice backends.\nYour GPU (%s) does not support RenderingDevice, as it does not support Vulkan, Direct3D 12, or Metal.\nLightmap baking will not be available on this device, although rendering existing baked lightmaps will work."), RenderingServer::get_singleton()->get_video_adapter_name())); |
| 1809 | return warnings; |
| 1810 | } |
| 1811 | |
| 1812 | if (shadowmask_mode != LightmapGIData::SHADOWMASK_MODE_NONE && light_data.is_valid() && !light_data->has_shadowmask_textures()) { |
| 1813 | warnings.push_back(RTR("The lightmap has no baked shadowmask textures. Please rebake with the Shadowmask Mode set to anything other than None.")); |
| 1814 | } |
| 1815 | |
| 1816 | #elif defined(ANDROID_ENABLED) || defined(APPLE_EMBEDDED_ENABLED) |
| 1817 | warnings.push_back(vformat(RTR("Lightmaps cannot be baked on %s. Rendering existing baked lightmaps will still work."), OS::get_singleton()->get_name())); |
| 1818 | #else |
| 1819 | warnings.push_back(RTR("Lightmaps cannot be baked, as the `lightmapper_rd` module was disabled at compile-time. Rendering existing baked lightmaps will still work.")); |
| 1820 | #endif |
| 1821 | |
| 1822 | return warnings; |
| 1823 | } |
| 1824 | |
| 1825 | void LightmapGI::_validate_property(PropertyInfo &p_property) const { |
| 1826 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 1827 | return; |
| 1828 | } |
| 1829 | if (p_property.name == "supersampling_factor" && !supersampling_enabled) { |
| 1830 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 1831 | } |
| 1832 | if (p_property.name == "environment_custom_sky" && environment_mode != ENVIRONMENT_MODE_CUSTOM_SKY) { |
| 1833 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 1834 | } |
| 1835 | if (p_property.name == "environment_custom_color" && environment_mode != ENVIRONMENT_MODE_CUSTOM_COLOR) { |
| 1836 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 1837 | } |
| 1838 | if (p_property.name == "environment_custom_energy" && environment_mode != ENVIRONMENT_MODE_CUSTOM_COLOR && environment_mode != ENVIRONMENT_MODE_CUSTOM_SKY) { |
| 1839 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 1840 | } |
| 1841 | if (p_property.name == "denoiser_strength" && !use_denoiser) { |
| 1842 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 1843 | } |
| 1844 | if (p_property.name == "denoiser_range" && !use_denoiser) { |
| 1845 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 1846 | } |
| 1847 | } |
| 1848 | |
| 1849 | void LightmapGI::_bind_methods() { |
| 1850 | ClassDB::bind_method(D_METHOD("set_light_data", "data"), &LightmapGI::set_light_data); |
| 1851 | ClassDB::bind_method(D_METHOD("get_light_data"), &LightmapGI::get_light_data); |
| 1852 | |
| 1853 | ClassDB::bind_method(D_METHOD("set_bake_quality", "bake_quality"), &LightmapGI::set_bake_quality); |
| 1854 | ClassDB::bind_method(D_METHOD("get_bake_quality"), &LightmapGI::get_bake_quality); |
| 1855 | |
| 1856 | ClassDB::bind_method(D_METHOD("set_bounces", "bounces"), &LightmapGI::set_bounces); |
| 1857 | ClassDB::bind_method(D_METHOD("get_bounces"), &LightmapGI::get_bounces); |
| 1858 | |
| 1859 | ClassDB::bind_method(D_METHOD("set_bounce_indirect_energy", "bounce_indirect_energy"), &LightmapGI::set_bounce_indirect_energy); |
| 1860 | ClassDB::bind_method(D_METHOD("get_bounce_indirect_energy"), &LightmapGI::get_bounce_indirect_energy); |
| 1861 | |
| 1862 | ClassDB::bind_method(D_METHOD("set_generate_probes", "subdivision"), &LightmapGI::set_generate_probes); |
| 1863 | ClassDB::bind_method(D_METHOD("get_generate_probes"), &LightmapGI::get_generate_probes); |
| 1864 | |
| 1865 | ClassDB::bind_method(D_METHOD("set_bias", "bias"), &LightmapGI::set_bias); |
| 1866 | ClassDB::bind_method(D_METHOD("get_bias"), &LightmapGI::get_bias); |
| 1867 | |
| 1868 | ClassDB::bind_method(D_METHOD("set_environment_mode", "mode"), &LightmapGI::set_environment_mode); |
| 1869 | ClassDB::bind_method(D_METHOD("get_environment_mode"), &LightmapGI::get_environment_mode); |
| 1870 | |
| 1871 | ClassDB::bind_method(D_METHOD("set_environment_custom_sky", "sky"), &LightmapGI::set_environment_custom_sky); |
| 1872 | ClassDB::bind_method(D_METHOD("get_environment_custom_sky"), &LightmapGI::get_environment_custom_sky); |
| 1873 | |
| 1874 | ClassDB::bind_method(D_METHOD("set_environment_custom_color", "color"), &LightmapGI::set_environment_custom_color); |
| 1875 | ClassDB::bind_method(D_METHOD("get_environment_custom_color"), &LightmapGI::get_environment_custom_color); |
| 1876 | |
| 1877 | ClassDB::bind_method(D_METHOD("set_environment_custom_energy", "energy"), &LightmapGI::set_environment_custom_energy); |
| 1878 | ClassDB::bind_method(D_METHOD("get_environment_custom_energy"), &LightmapGI::get_environment_custom_energy); |
| 1879 | |
| 1880 | ClassDB::bind_method(D_METHOD("set_texel_scale", "texel_scale"), &LightmapGI::set_texel_scale); |
| 1881 | ClassDB::bind_method(D_METHOD("get_texel_scale"), &LightmapGI::get_texel_scale); |
| 1882 | |
| 1883 | ClassDB::bind_method(D_METHOD("set_max_texture_size", "max_texture_size"), &LightmapGI::set_max_texture_size); |
| 1884 | ClassDB::bind_method(D_METHOD("get_max_texture_size"), &LightmapGI::get_max_texture_size); |
| 1885 | |
| 1886 | ClassDB::bind_method(D_METHOD("set_supersampling_enabled", "enable"), &LightmapGI::set_supersampling_enabled); |
| 1887 | ClassDB::bind_method(D_METHOD("is_supersampling_enabled"), &LightmapGI::is_supersampling_enabled); |
| 1888 | |
| 1889 | ClassDB::bind_method(D_METHOD("set_supersampling_factor", "factor"), &LightmapGI::set_supersampling_factor); |
| 1890 | ClassDB::bind_method(D_METHOD("get_supersampling_factor"), &LightmapGI::get_supersampling_factor); |
| 1891 | |
| 1892 | ClassDB::bind_method(D_METHOD("set_use_denoiser", "use_denoiser"), &LightmapGI::set_use_denoiser); |
| 1893 | ClassDB::bind_method(D_METHOD("is_using_denoiser"), &LightmapGI::is_using_denoiser); |
| 1894 | |
| 1895 | ClassDB::bind_method(D_METHOD("set_denoiser_strength", "denoiser_strength"), &LightmapGI::set_denoiser_strength); |
| 1896 | ClassDB::bind_method(D_METHOD("get_denoiser_strength"), &LightmapGI::get_denoiser_strength); |
| 1897 | |
| 1898 | ClassDB::bind_method(D_METHOD("set_denoiser_range", "denoiser_range"), &LightmapGI::set_denoiser_range); |
| 1899 | ClassDB::bind_method(D_METHOD("get_denoiser_range"), &LightmapGI::get_denoiser_range); |
| 1900 | |
| 1901 | ClassDB::bind_method(D_METHOD("set_interior", "enable"), &LightmapGI::set_interior); |
| 1902 | ClassDB::bind_method(D_METHOD("is_interior"), &LightmapGI::is_interior); |
| 1903 | |
| 1904 | ClassDB::bind_method(D_METHOD("set_directional", "directional"), &LightmapGI::set_directional); |
| 1905 | ClassDB::bind_method(D_METHOD("is_directional"), &LightmapGI::is_directional); |
| 1906 | |
| 1907 | ClassDB::bind_method(D_METHOD("set_shadowmask_mode", "mode"), &LightmapGI::set_shadowmask_mode); |
| 1908 | ClassDB::bind_method(D_METHOD("get_shadowmask_mode"), &LightmapGI::get_shadowmask_mode); |
| 1909 | |
| 1910 | ClassDB::bind_method(D_METHOD("set_use_texture_for_bounces", "use_texture_for_bounces"), &LightmapGI::set_use_texture_for_bounces); |
| 1911 | ClassDB::bind_method(D_METHOD("is_using_texture_for_bounces"), &LightmapGI::is_using_texture_for_bounces); |
| 1912 | |
| 1913 | ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &LightmapGI::set_camera_attributes); |
| 1914 | ClassDB::bind_method(D_METHOD("get_camera_attributes"), &LightmapGI::get_camera_attributes); |
| 1915 | |
| 1916 | // ClassDB::bind_method(D_METHOD("bake", "from_node"), &LightmapGI::bake, DEFVAL(Variant())); |
| 1917 | |
| 1918 | ADD_GROUP("Tweaks", ""); |
| 1919 | ADD_PROPERTY(PropertyInfo(Variant::INT, "quality", PROPERTY_HINT_ENUM, "Low,Medium,High,Ultra"), "set_bake_quality", "get_bake_quality"); |
| 1920 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "supersampling"), "set_supersampling_enabled", "is_supersampling_enabled"); |
| 1921 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "supersampling_factor", PROPERTY_HINT_RANGE, "1,8,1"), "set_supersampling_factor", "get_supersampling_factor"); |
| 1922 | ADD_PROPERTY(PropertyInfo(Variant::INT, "bounces", PROPERTY_HINT_RANGE, "0,6,1,or_greater"), "set_bounces", "get_bounces"); |
| 1923 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bounce_indirect_energy", PROPERTY_HINT_RANGE, "0,2,0.01"), "set_bounce_indirect_energy", "get_bounce_indirect_energy"); |
| 1924 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "directional"), "set_directional", "is_directional"); |
| 1925 | ADD_PROPERTY(PropertyInfo(Variant::INT, "shadowmask_mode", PROPERTY_HINT_ENUM, "None,Replace,Overlay"), "set_shadowmask_mode", "get_shadowmask_mode"); |
| 1926 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_texture_for_bounces"), "set_use_texture_for_bounces", "is_using_texture_for_bounces"); |
| 1927 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior"); |
| 1928 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_denoiser"), "set_use_denoiser", "is_using_denoiser"); |
| 1929 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "denoiser_strength", PROPERTY_HINT_RANGE, "0.001,0.2,0.001,or_greater"), "set_denoiser_strength", "get_denoiser_strength"); |
| 1930 | ADD_PROPERTY(PropertyInfo(Variant::INT, "denoiser_range", PROPERTY_HINT_RANGE, "1,20"), "set_denoiser_range", "get_denoiser_range"); |
| 1931 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0.00001,0.1,0.00001,or_greater"), "set_bias", "get_bias"); |
| 1932 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texel_scale", PROPERTY_HINT_RANGE, "0.01,100.0,0.01"), "set_texel_scale", "get_texel_scale"); |
| 1933 | ADD_PROPERTY(PropertyInfo(Variant::INT, "max_texture_size", PROPERTY_HINT_RANGE, "2048,16384,1"), "set_max_texture_size", "get_max_texture_size"); |
| 1934 | ADD_GROUP("Environment", "environment_"); |
| 1935 | ADD_PROPERTY(PropertyInfo(Variant::INT, "environment_mode", PROPERTY_HINT_ENUM, "Disabled,Scene,Custom Sky,Custom Color"), "set_environment_mode", "get_environment_mode"); |
| 1936 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment_custom_sky", PROPERTY_HINT_RESOURCE_TYPE, "Sky"), "set_environment_custom_sky", "get_environment_custom_sky"); |
| 1937 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "environment_custom_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_environment_custom_color", "get_environment_custom_color"); |
| 1938 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "environment_custom_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_environment_custom_energy", "get_environment_custom_energy"); |
| 1939 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes"); |
| 1940 | ADD_GROUP("Gen Probes", "generate_probes_"); |
| 1941 | ADD_PROPERTY(PropertyInfo(Variant::INT, "generate_probes_subdiv", PROPERTY_HINT_ENUM, "Disabled,4,8,16,32"), "set_generate_probes", "get_generate_probes"); |
| 1942 | ADD_GROUP("Data", ""); |
| 1943 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_data", PROPERTY_HINT_RESOURCE_TYPE, "LightmapGIData"), "set_light_data", "get_light_data"); |
| 1944 | |
| 1945 | BIND_ENUM_CONSTANT(BAKE_QUALITY_LOW); |
| 1946 | BIND_ENUM_CONSTANT(BAKE_QUALITY_MEDIUM); |
| 1947 | BIND_ENUM_CONSTANT(BAKE_QUALITY_HIGH); |
| 1948 | BIND_ENUM_CONSTANT(BAKE_QUALITY_ULTRA); |
| 1949 | |
| 1950 | BIND_ENUM_CONSTANT(GENERATE_PROBES_DISABLED); |
| 1951 | BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_4); |
| 1952 | BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_8); |
| 1953 | BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_16); |
| 1954 | BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_32); |
| 1955 | |
| 1956 | BIND_ENUM_CONSTANT(BAKE_ERROR_OK); |
| 1957 | BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SCENE_ROOT); |
| 1958 | BIND_ENUM_CONSTANT(BAKE_ERROR_FOREIGN_DATA); |
| 1959 | BIND_ENUM_CONSTANT(BAKE_ERROR_NO_LIGHTMAPPER); |
| 1960 | BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SAVE_PATH); |
| 1961 | BIND_ENUM_CONSTANT(BAKE_ERROR_NO_MESHES); |
| 1962 | BIND_ENUM_CONSTANT(BAKE_ERROR_MESHES_INVALID); |
| 1963 | BIND_ENUM_CONSTANT(BAKE_ERROR_CANT_CREATE_IMAGE); |
| 1964 | BIND_ENUM_CONSTANT(BAKE_ERROR_USER_ABORTED); |
| 1965 | BIND_ENUM_CONSTANT(BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL); |
| 1966 | BIND_ENUM_CONSTANT(BAKE_ERROR_LIGHTMAP_TOO_SMALL); |
| 1967 | BIND_ENUM_CONSTANT(BAKE_ERROR_ATLAS_TOO_SMALL); |
| 1968 | |
| 1969 | BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_DISABLED); |
| 1970 | BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_SCENE); |
| 1971 | BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_CUSTOM_SKY); |
| 1972 | BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_CUSTOM_COLOR); |
| 1973 | } |
| 1974 | |
| 1975 | LightmapGI::LightmapGI() { |
| 1976 | } |