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