| 1 | /**************************************************************************/ |
| 2 | /* voxel_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 "voxel_gi.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "mesh_instance_3d.h" |
| 35 | #include "multimesh_instance_3d.h" |
| 36 | #include "scene/resources/camera_attributes.h" |
| 37 | #include "voxelizer.h" |
| 38 | |
| 39 | void VoxelGIData::_set_data(const Dictionary &p_data) { |
| 40 | ERR_FAIL_COND(!p_data.has("bounds")); |
| 41 | ERR_FAIL_COND(!p_data.has("octree_size")); |
| 42 | ERR_FAIL_COND(!p_data.has("octree_cells")); |
| 43 | ERR_FAIL_COND(!p_data.has("octree_data")); |
| 44 | ERR_FAIL_COND(!p_data.has("octree_df") && !p_data.has("octree_df_png")); |
| 45 | ERR_FAIL_COND(!p_data.has("level_counts")); |
| 46 | ERR_FAIL_COND(!p_data.has("to_cell_xform")); |
| 47 | |
| 48 | AABB bounds_new = p_data["bounds"]; |
| 49 | Vector3 octree_size_new = p_data["octree_size"]; |
| 50 | Vector<uint8_t> octree_cells = p_data["octree_cells"]; |
| 51 | Vector<uint8_t> octree_data = p_data["octree_data"]; |
| 52 | |
| 53 | Vector<uint8_t> octree_df; |
| 54 | if (p_data.has("octree_df")) { |
| 55 | octree_df = p_data["octree_df"]; |
| 56 | } else if (p_data.has("octree_df_png")) { |
| 57 | Vector<uint8_t> octree_df_png = p_data["octree_df_png"]; |
| 58 | Ref<Image> img; |
| 59 | img.instantiate(); |
| 60 | Error err = img->load_png_from_buffer(octree_df_png); |
| 61 | ERR_FAIL_COND(err != OK); |
| 62 | ERR_FAIL_COND(img->get_format() != Image::FORMAT_L8); |
| 63 | octree_df = img->get_data(); |
| 64 | } |
| 65 | Vector<int> octree_levels = p_data["level_counts"]; |
| 66 | Transform3D to_cell_xform_new = p_data["to_cell_xform"]; |
| 67 | |
| 68 | allocate(to_cell_xform_new, bounds_new, octree_size_new, octree_cells, octree_data, octree_df, octree_levels); |
| 69 | } |
| 70 | |
| 71 | Dictionary VoxelGIData::_get_data() const { |
| 72 | Dictionary d; |
| 73 | d["bounds"] = get_bounds(); |
| 74 | Vector3i otsize = get_octree_size(); |
| 75 | d["octree_size"] = Vector3(otsize); |
| 76 | d["octree_cells"] = get_octree_cells(); |
| 77 | d["octree_data"] = get_data_cells(); |
| 78 | if (otsize != Vector3i()) { |
| 79 | Ref<Image> img = Image::create_from_data(otsize.x * otsize.y, otsize.z, false, Image::FORMAT_L8, get_distance_field()); |
| 80 | Vector<uint8_t> df_png = img->save_png_to_buffer(); |
| 81 | ERR_FAIL_COND_V(df_png.is_empty(), Dictionary()); |
| 82 | d["octree_df_png"] = df_png; |
| 83 | } else { |
| 84 | d["octree_df"] = Vector<uint8_t>(); |
| 85 | } |
| 86 | |
| 87 | d["level_counts"] = get_level_counts(); |
| 88 | d["to_cell_xform"] = get_to_cell_xform(); |
| 89 | return d; |
| 90 | } |
| 91 | |
| 92 | void VoxelGIData::allocate(const Transform3D &p_to_cell_xform, const AABB &p_aabb, const Vector3 &p_octree_size, const Vector<uint8_t> &p_octree_cells, const Vector<uint8_t> &p_data_cells, const Vector<uint8_t> &p_distance_field, const Vector<int> &p_level_counts) { |
| 93 | RS::get_singleton()->voxel_gi_allocate_data(probe, p_to_cell_xform, p_aabb, p_octree_size, p_octree_cells, p_data_cells, p_distance_field, p_level_counts); |
| 94 | bounds = p_aabb; |
| 95 | to_cell_xform = p_to_cell_xform; |
| 96 | octree_size = p_octree_size; |
| 97 | } |
| 98 | |
| 99 | AABB VoxelGIData::get_bounds() const { |
| 100 | return bounds; |
| 101 | } |
| 102 | |
| 103 | Vector3 VoxelGIData::get_octree_size() const { |
| 104 | return octree_size; |
| 105 | } |
| 106 | |
| 107 | Vector<uint8_t> VoxelGIData::get_octree_cells() const { |
| 108 | return RS::get_singleton()->voxel_gi_get_octree_cells(probe); |
| 109 | } |
| 110 | |
| 111 | Vector<uint8_t> VoxelGIData::get_data_cells() const { |
| 112 | return RS::get_singleton()->voxel_gi_get_data_cells(probe); |
| 113 | } |
| 114 | |
| 115 | Vector<uint8_t> VoxelGIData::get_distance_field() const { |
| 116 | return RS::get_singleton()->voxel_gi_get_distance_field(probe); |
| 117 | } |
| 118 | |
| 119 | Vector<int> VoxelGIData::get_level_counts() const { |
| 120 | return RS::get_singleton()->voxel_gi_get_level_counts(probe); |
| 121 | } |
| 122 | |
| 123 | Transform3D VoxelGIData::get_to_cell_xform() const { |
| 124 | return to_cell_xform; |
| 125 | } |
| 126 | |
| 127 | void VoxelGIData::set_dynamic_range(float p_range) { |
| 128 | RS::get_singleton()->voxel_gi_set_dynamic_range(probe, p_range); |
| 129 | dynamic_range = p_range; |
| 130 | } |
| 131 | |
| 132 | float VoxelGIData::get_dynamic_range() const { |
| 133 | return dynamic_range; |
| 134 | } |
| 135 | |
| 136 | void VoxelGIData::set_propagation(float p_propagation) { |
| 137 | RS::get_singleton()->voxel_gi_set_propagation(probe, p_propagation); |
| 138 | propagation = p_propagation; |
| 139 | } |
| 140 | |
| 141 | float VoxelGIData::get_propagation() const { |
| 142 | return propagation; |
| 143 | } |
| 144 | |
| 145 | void VoxelGIData::set_energy(float p_energy) { |
| 146 | RS::get_singleton()->voxel_gi_set_energy(probe, p_energy); |
| 147 | energy = p_energy; |
| 148 | } |
| 149 | |
| 150 | float VoxelGIData::get_energy() const { |
| 151 | return energy; |
| 152 | } |
| 153 | |
| 154 | void VoxelGIData::set_bias(float p_bias) { |
| 155 | RS::get_singleton()->voxel_gi_set_bias(probe, p_bias); |
| 156 | bias = p_bias; |
| 157 | } |
| 158 | |
| 159 | float VoxelGIData::get_bias() const { |
| 160 | return bias; |
| 161 | } |
| 162 | |
| 163 | void VoxelGIData::set_normal_bias(float p_normal_bias) { |
| 164 | RS::get_singleton()->voxel_gi_set_normal_bias(probe, p_normal_bias); |
| 165 | normal_bias = p_normal_bias; |
| 166 | } |
| 167 | |
| 168 | float VoxelGIData::get_normal_bias() const { |
| 169 | return normal_bias; |
| 170 | } |
| 171 | |
| 172 | void VoxelGIData::set_interior(bool p_enable) { |
| 173 | RS::get_singleton()->voxel_gi_set_interior(probe, p_enable); |
| 174 | interior = p_enable; |
| 175 | } |
| 176 | |
| 177 | bool VoxelGIData::is_interior() const { |
| 178 | return interior; |
| 179 | } |
| 180 | |
| 181 | void VoxelGIData::set_use_two_bounces(bool p_enable) { |
| 182 | RS::get_singleton()->voxel_gi_set_use_two_bounces(probe, p_enable); |
| 183 | use_two_bounces = p_enable; |
| 184 | } |
| 185 | |
| 186 | bool VoxelGIData::is_using_two_bounces() const { |
| 187 | return use_two_bounces; |
| 188 | } |
| 189 | |
| 190 | RID VoxelGIData::get_rid() const { |
| 191 | return probe; |
| 192 | } |
| 193 | |
| 194 | void VoxelGIData::_bind_methods() { |
| 195 | ClassDB::bind_method(D_METHOD("allocate", "to_cell_xform", "aabb", "octree_size", "octree_cells", "data_cells", "distance_field", "level_counts"), &VoxelGIData::allocate); |
| 196 | |
| 197 | ClassDB::bind_method(D_METHOD("get_bounds"), &VoxelGIData::get_bounds); |
| 198 | ClassDB::bind_method(D_METHOD("get_octree_size"), &VoxelGIData::get_octree_size); |
| 199 | ClassDB::bind_method(D_METHOD("get_to_cell_xform"), &VoxelGIData::get_to_cell_xform); |
| 200 | ClassDB::bind_method(D_METHOD("get_octree_cells"), &VoxelGIData::get_octree_cells); |
| 201 | ClassDB::bind_method(D_METHOD("get_data_cells"), &VoxelGIData::get_data_cells); |
| 202 | ClassDB::bind_method(D_METHOD("get_level_counts"), &VoxelGIData::get_level_counts); |
| 203 | |
| 204 | ClassDB::bind_method(D_METHOD("set_dynamic_range", "dynamic_range"), &VoxelGIData::set_dynamic_range); |
| 205 | ClassDB::bind_method(D_METHOD("get_dynamic_range"), &VoxelGIData::get_dynamic_range); |
| 206 | |
| 207 | ClassDB::bind_method(D_METHOD("set_energy", "energy"), &VoxelGIData::set_energy); |
| 208 | ClassDB::bind_method(D_METHOD("get_energy"), &VoxelGIData::get_energy); |
| 209 | |
| 210 | ClassDB::bind_method(D_METHOD("set_bias", "bias"), &VoxelGIData::set_bias); |
| 211 | ClassDB::bind_method(D_METHOD("get_bias"), &VoxelGIData::get_bias); |
| 212 | |
| 213 | ClassDB::bind_method(D_METHOD("set_normal_bias", "bias"), &VoxelGIData::set_normal_bias); |
| 214 | ClassDB::bind_method(D_METHOD("get_normal_bias"), &VoxelGIData::get_normal_bias); |
| 215 | |
| 216 | ClassDB::bind_method(D_METHOD("set_propagation", "propagation"), &VoxelGIData::set_propagation); |
| 217 | ClassDB::bind_method(D_METHOD("get_propagation"), &VoxelGIData::get_propagation); |
| 218 | |
| 219 | ClassDB::bind_method(D_METHOD("set_interior", "interior"), &VoxelGIData::set_interior); |
| 220 | ClassDB::bind_method(D_METHOD("is_interior"), &VoxelGIData::is_interior); |
| 221 | |
| 222 | ClassDB::bind_method(D_METHOD("set_use_two_bounces", "enable"), &VoxelGIData::set_use_two_bounces); |
| 223 | ClassDB::bind_method(D_METHOD("is_using_two_bounces"), &VoxelGIData::is_using_two_bounces); |
| 224 | |
| 225 | ClassDB::bind_method(D_METHOD("_set_data", "data"), &VoxelGIData::_set_data); |
| 226 | ClassDB::bind_method(D_METHOD("_get_data"), &VoxelGIData::_get_data); |
| 227 | |
| 228 | ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data"); |
| 229 | |
| 230 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dynamic_range", PROPERTY_HINT_RANGE, "1,8,0.01"), "set_dynamic_range", "get_dynamic_range"); |
| 231 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_energy", "get_energy"); |
| 232 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,8,0.01"), "set_bias", "get_bias"); |
| 233 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "normal_bias", PROPERTY_HINT_RANGE, "0,8,0.01"), "set_normal_bias", "get_normal_bias"); |
| 234 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "propagation", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_propagation", "get_propagation"); |
| 235 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_two_bounces"), "set_use_two_bounces", "is_using_two_bounces"); |
| 236 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior"); |
| 237 | } |
| 238 | |
| 239 | #ifndef DISABLE_DEPRECATED |
| 240 | bool VoxelGI::_set(const StringName &p_name, const Variant &p_value) { |
| 241 | if (p_name == "extents") { // Compatibility with Godot 3.x. |
| 242 | set_size((Vector3)p_value * 2); |
| 243 | return true; |
| 244 | } |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | bool VoxelGI::_get(const StringName &p_name, Variant &r_property) const { |
| 249 | if (p_name == "extents") { // Compatibility with Godot 3.x. |
| 250 | r_property = size / 2; |
| 251 | return true; |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | #endif // DISABLE_DEPRECATED |
| 256 | |
| 257 | VoxelGIData::VoxelGIData() { |
| 258 | probe = RS::get_singleton()->voxel_gi_create(); |
| 259 | } |
| 260 | |
| 261 | VoxelGIData::~VoxelGIData() { |
| 262 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 263 | RS::get_singleton()->free_rid(probe); |
| 264 | } |
| 265 | |
| 266 | ////////////////////// |
| 267 | ////////////////////// |
| 268 | |
| 269 | void VoxelGI::set_probe_data(const Ref<VoxelGIData> &p_data) { |
| 270 | if (p_data.is_valid()) { |
| 271 | RS::get_singleton()->instance_set_base(get_instance(), p_data->get_rid()); |
| 272 | RS::get_singleton()->voxel_gi_set_baked_exposure_normalization(p_data->get_rid(), _get_camera_exposure_normalization()); |
| 273 | } else { |
| 274 | RS::get_singleton()->instance_set_base(get_instance(), RID()); |
| 275 | } |
| 276 | |
| 277 | probe_data = p_data; |
| 278 | update_configuration_warnings(); |
| 279 | } |
| 280 | |
| 281 | Ref<VoxelGIData> VoxelGI::get_probe_data() const { |
| 282 | return probe_data; |
| 283 | } |
| 284 | |
| 285 | void VoxelGI::set_subdiv(Subdiv p_subdiv) { |
| 286 | ERR_FAIL_INDEX(p_subdiv, SUBDIV_MAX); |
| 287 | subdiv = p_subdiv; |
| 288 | update_gizmos(); |
| 289 | } |
| 290 | |
| 291 | VoxelGI::Subdiv VoxelGI::get_subdiv() const { |
| 292 | return subdiv; |
| 293 | } |
| 294 | |
| 295 | void VoxelGI::set_size(const Vector3 &p_size) { |
| 296 | // Prevent very small size dimensions as these breaks baking if other size dimensions are set very high. |
| 297 | size = p_size.maxf(1.0); |
| 298 | update_gizmos(); |
| 299 | } |
| 300 | |
| 301 | Vector3 VoxelGI::get_size() const { |
| 302 | return size; |
| 303 | } |
| 304 | |
| 305 | void VoxelGI::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) { |
| 306 | camera_attributes = p_camera_attributes; |
| 307 | |
| 308 | if (probe_data.is_valid()) { |
| 309 | RS::get_singleton()->voxel_gi_set_baked_exposure_normalization(probe_data->get_rid(), _get_camera_exposure_normalization()); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | Ref<CameraAttributes> VoxelGI::get_camera_attributes() const { |
| 314 | return camera_attributes; |
| 315 | } |
| 316 | |
| 317 | static bool is_node_voxel_bakeable(Node3D *p_node) { |
| 318 | if (!p_node->is_visible_in_tree()) { |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | GeometryInstance3D *geometry = Object::cast_to<GeometryInstance3D>(p_node); |
| 323 | if (geometry != nullptr && geometry->get_gi_mode() != GeometryInstance3D::GI_MODE_STATIC) { |
| 324 | return false; |
| 325 | } |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | void VoxelGI::_find_meshes(Node *p_at_node, List<PlotMesh> &plot_meshes) { |
| 330 | MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_at_node); |
| 331 | if (mi && is_node_voxel_bakeable(mi)) { |
| 332 | Ref<Mesh> mesh = mi->get_mesh(); |
| 333 | if (mesh.is_valid()) { |
| 334 | AABB aabb = mesh->get_aabb(); |
| 335 | |
| 336 | Transform3D xf = get_global_transform().affine_inverse() * mi->get_global_transform(); |
| 337 | |
| 338 | if (AABB(-size / 2, size).intersects(xf.xform(aabb))) { |
| 339 | PlotMesh pm; |
| 340 | pm.local_xform = xf; |
| 341 | pm.mesh = mesh; |
| 342 | for (int i = 0; i < mesh->get_surface_count(); i++) { |
| 343 | pm.instance_materials.push_back(mi->get_surface_override_material(i)); |
| 344 | } |
| 345 | pm.override_material = mi->get_material_override(); |
| 346 | plot_meshes.push_back(pm); |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | Node3D *s = Object::cast_to<Node3D>(p_at_node); |
| 352 | if (s) { |
| 353 | if (is_node_voxel_bakeable(s)) { |
| 354 | Array meshes; |
| 355 | MultiMeshInstance3D *multi_mesh = Object::cast_to<MultiMeshInstance3D>(p_at_node); |
| 356 | if (multi_mesh) { |
| 357 | meshes = multi_mesh->get_meshes(); |
| 358 | } else { |
| 359 | meshes = p_at_node->call("get_meshes"); |
| 360 | } |
| 361 | |
| 362 | for (int i = 0; i < meshes.size(); i += 2) { |
| 363 | Transform3D mxf = meshes[i]; |
| 364 | Ref<Mesh> mesh = meshes[i + 1]; |
| 365 | if (mesh.is_null()) { |
| 366 | continue; |
| 367 | } |
| 368 | |
| 369 | AABB aabb = mesh->get_aabb(); |
| 370 | |
| 371 | Transform3D xf = get_global_transform().affine_inverse() * (s->get_global_transform() * mxf); |
| 372 | |
| 373 | if (AABB(-size / 2, size).intersects(xf.xform(aabb))) { |
| 374 | PlotMesh pm; |
| 375 | pm.local_xform = xf; |
| 376 | pm.mesh = mesh; |
| 377 | plot_meshes.push_back(pm); |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | for (int i = 0; i < p_at_node->get_child_count(); i++) { |
| 384 | Node *child = p_at_node->get_child(i); |
| 385 | _find_meshes(child, plot_meshes); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | VoxelGI::BakeBeginFunc VoxelGI::bake_begin_function = nullptr; |
| 390 | VoxelGI::BakeStepFunc VoxelGI::bake_step_function = nullptr; |
| 391 | VoxelGI::BakeEndFunc VoxelGI::bake_end_function = nullptr; |
| 392 | |
| 393 | static int voxelizer_plot_bake_base = 0; |
| 394 | static int voxelizer_plot_bake_total = 0; |
| 395 | |
| 396 | static bool voxelizer_plot_bake_step_function(int current, int) { |
| 397 | return VoxelGI::bake_step_function((voxelizer_plot_bake_base + current) * 500 / voxelizer_plot_bake_total, RTR("Plotting Meshes")); |
| 398 | } |
| 399 | |
| 400 | static bool voxelizer_sdf_bake_step_function(int current, int total) { |
| 401 | return VoxelGI::bake_step_function(500 + current * 500 / total, RTR("Generating Distance Field")); |
| 402 | } |
| 403 | |
| 404 | Vector3i VoxelGI::get_estimated_cell_size() const { |
| 405 | static const int subdiv_value[SUBDIV_MAX] = { 6, 7, 8, 9 }; |
| 406 | int cell_subdiv = subdiv_value[subdiv]; |
| 407 | int axis_cell_size[3]; |
| 408 | AABB bounds = AABB(-size / 2, size); |
| 409 | int longest_axis = bounds.get_longest_axis_index(); |
| 410 | axis_cell_size[longest_axis] = 1 << cell_subdiv; |
| 411 | |
| 412 | for (int i = 0; i < 3; i++) { |
| 413 | if (i == longest_axis) { |
| 414 | continue; |
| 415 | } |
| 416 | |
| 417 | axis_cell_size[i] = axis_cell_size[longest_axis]; |
| 418 | float axis_size = bounds.size[longest_axis]; |
| 419 | |
| 420 | //shrink until fit subdiv |
| 421 | while (axis_size / 2.0 >= bounds.size[i]) { |
| 422 | axis_size /= 2.0; |
| 423 | axis_cell_size[i] >>= 1; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return Vector3i(axis_cell_size[0], axis_cell_size[1], axis_cell_size[2]); |
| 428 | } |
| 429 | |
| 430 | void VoxelGI::bake(Node *p_from_node, bool p_create_visual_debug) { |
| 431 | static const int subdiv_value[SUBDIV_MAX] = { 6, 7, 8, 9 }; |
| 432 | |
| 433 | p_from_node = p_from_node ? p_from_node : get_parent(); |
| 434 | ERR_FAIL_NULL(p_from_node); |
| 435 | |
| 436 | float exposure_normalization = _get_camera_exposure_normalization(); |
| 437 | |
| 438 | Voxelizer baker; |
| 439 | |
| 440 | baker.begin_bake(subdiv_value[subdiv], AABB(-size / 2, size), exposure_normalization); |
| 441 | |
| 442 | List<PlotMesh> mesh_list; |
| 443 | |
| 444 | _find_meshes(p_from_node, mesh_list); |
| 445 | |
| 446 | if (bake_begin_function) { |
| 447 | bake_begin_function(); |
| 448 | } |
| 449 | |
| 450 | Voxelizer::BakeStepFunc voxelizer_step_func = bake_step_function != nullptr ? voxelizer_plot_bake_step_function : nullptr; |
| 451 | |
| 452 | voxelizer_plot_bake_total = voxelizer_plot_bake_base = 0; |
| 453 | for (PlotMesh &E : mesh_list) { |
| 454 | voxelizer_plot_bake_total += baker.get_bake_steps(E.mesh); |
| 455 | } |
| 456 | for (PlotMesh &E : mesh_list) { |
| 457 | if (baker.plot_mesh(E.local_xform, E.mesh, E.instance_materials, E.override_material, voxelizer_step_func) != Voxelizer::BAKE_RESULT_OK) { |
| 458 | baker.end_bake(); |
| 459 | if (bake_end_function) { |
| 460 | bake_end_function(); |
| 461 | } |
| 462 | return; |
| 463 | } |
| 464 | voxelizer_plot_bake_base += baker.get_bake_steps(E.mesh); |
| 465 | } |
| 466 | if (bake_step_function) { |
| 467 | bake_step_function(500, RTR("Finishing Plot")); |
| 468 | } |
| 469 | |
| 470 | baker.end_bake(); |
| 471 | |
| 472 | //create the data for rendering server |
| 473 | |
| 474 | if (p_create_visual_debug) { |
| 475 | MultiMeshInstance3D *mmi = memnew(MultiMeshInstance3D); |
| 476 | mmi->set_multimesh(baker.create_debug_multimesh()); |
| 477 | add_child(mmi, true); |
| 478 | #ifdef TOOLS_ENABLED |
| 479 | if (is_inside_tree() && get_tree()->get_edited_scene_root() == this) { |
| 480 | mmi->set_owner(this); |
| 481 | } else { |
| 482 | mmi->set_owner(get_owner()); |
| 483 | } |
| 484 | #else |
| 485 | mmi->set_owner(get_owner()); |
| 486 | #endif |
| 487 | |
| 488 | } else { |
| 489 | Ref<VoxelGIData> probe_data_new = get_probe_data(); |
| 490 | |
| 491 | if (probe_data_new.is_null()) { |
| 492 | probe_data_new.instantiate(); |
| 493 | } |
| 494 | |
| 495 | if (bake_step_function) { |
| 496 | bake_step_function(500, RTR("Generating Distance Field")); |
| 497 | } |
| 498 | |
| 499 | voxelizer_step_func = bake_step_function != nullptr ? voxelizer_sdf_bake_step_function : nullptr; |
| 500 | |
| 501 | Vector<uint8_t> df; |
| 502 | if (baker.get_sdf_3d_image(df, voxelizer_step_func) == Voxelizer::BAKE_RESULT_OK) { |
| 503 | RS::get_singleton()->voxel_gi_set_baked_exposure_normalization(probe_data_new->get_rid(), exposure_normalization); |
| 504 | |
| 505 | probe_data_new->allocate(baker.get_to_cell_space_xform(), AABB(-size / 2, size), baker.get_voxel_gi_octree_size(), baker.get_voxel_gi_octree_cells(), baker.get_voxel_gi_data_cells(), df, baker.get_voxel_gi_level_cell_count()); |
| 506 | |
| 507 | set_probe_data(probe_data_new); |
| 508 | #ifdef TOOLS_ENABLED |
| 509 | probe_data_new->set_edited(true); //so it gets saved |
| 510 | #endif |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (bake_end_function) { |
| 515 | bake_end_function(); |
| 516 | } |
| 517 | |
| 518 | notify_property_list_changed(); //bake property may have changed |
| 519 | } |
| 520 | |
| 521 | void VoxelGI::_debug_bake() { |
| 522 | bake(nullptr, true); |
| 523 | } |
| 524 | |
| 525 | float VoxelGI::_get_camera_exposure_normalization() { |
| 526 | float exposure_normalization = 1.0; |
| 527 | if (camera_attributes.is_valid()) { |
| 528 | exposure_normalization = camera_attributes->get_exposure_multiplier(); |
| 529 | if (GLOBAL_GET_CACHED(bool, "rendering/lights_and_shadows/use_physical_light_units")) { |
| 530 | exposure_normalization = camera_attributes->calculate_exposure_normalization(); |
| 531 | } |
| 532 | } |
| 533 | return exposure_normalization; |
| 534 | } |
| 535 | |
| 536 | AABB VoxelGI::get_aabb() const { |
| 537 | return AABB(-size / 2, size); |
| 538 | } |
| 539 | |
| 540 | PackedStringArray VoxelGI::get_configuration_warnings() const { |
| 541 | PackedStringArray warnings = VisualInstance3D::get_configuration_warnings(); |
| 542 | |
| 543 | if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") { |
| 544 | warnings.push_back(RTR("VoxelGI nodes are not supported when using the Compatibility renderer yet. Support will be added in a future release.")); |
| 545 | } else if (OS::get_singleton()->get_current_rendering_method() == "dummy") { |
| 546 | warnings.push_back(RTR("VoxelGI nodes are not supported when using the Dummy renderer.")); |
| 547 | } else if (probe_data.is_null()) { |
| 548 | warnings.push_back(RTR("No VoxelGI data set, so this node is disabled. Bake static objects to enable GI.")); |
| 549 | } |
| 550 | return warnings; |
| 551 | } |
| 552 | |
| 553 | void VoxelGI::_bind_methods() { |
| 554 | ClassDB::bind_method(D_METHOD("set_probe_data", "data"), &VoxelGI::set_probe_data); |
| 555 | ClassDB::bind_method(D_METHOD("get_probe_data"), &VoxelGI::get_probe_data); |
| 556 | |
| 557 | ClassDB::bind_method(D_METHOD("set_subdiv", "subdiv"), &VoxelGI::set_subdiv); |
| 558 | ClassDB::bind_method(D_METHOD("get_subdiv"), &VoxelGI::get_subdiv); |
| 559 | |
| 560 | ClassDB::bind_method(D_METHOD("set_size", "size"), &VoxelGI::set_size); |
| 561 | ClassDB::bind_method(D_METHOD("get_size"), &VoxelGI::get_size); |
| 562 | |
| 563 | ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &VoxelGI::set_camera_attributes); |
| 564 | ClassDB::bind_method(D_METHOD("get_camera_attributes"), &VoxelGI::get_camera_attributes); |
| 565 | |
| 566 | ClassDB::bind_method(D_METHOD("bake", "from_node", "create_visual_debug"), &VoxelGI::bake, DEFVAL(Variant()), DEFVAL(false)); |
| 567 | ClassDB::bind_method(D_METHOD("debug_bake"), &VoxelGI::_debug_bake); |
| 568 | ClassDB::set_method_flags(get_class_static(), StringName("debug_bake"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR); |
| 569 | |
| 570 | ADD_PROPERTY(PropertyInfo(Variant::INT, "subdiv", PROPERTY_HINT_ENUM, "64,128,256,512"), "set_subdiv", "get_subdiv"); |
| 571 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size"); |
| 572 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes"); |
| 573 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "VoxelGIData", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE), "set_probe_data", "get_probe_data"); |
| 574 | |
| 575 | BIND_ENUM_CONSTANT(SUBDIV_64); |
| 576 | BIND_ENUM_CONSTANT(SUBDIV_128); |
| 577 | BIND_ENUM_CONSTANT(SUBDIV_256); |
| 578 | BIND_ENUM_CONSTANT(SUBDIV_512); |
| 579 | BIND_ENUM_CONSTANT(SUBDIV_MAX); |
| 580 | } |
| 581 | |
| 582 | VoxelGI::VoxelGI() { |
| 583 | voxel_gi = RS::get_singleton()->voxel_gi_create(); |
| 584 | set_disable_scale(true); |
| 585 | } |
| 586 | |
| 587 | VoxelGI::~VoxelGI() { |
| 588 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 589 | RS::get_singleton()->free_rid(voxel_gi); |
| 590 | } |