voxel_gi.cpp 21 KB

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