voxel_gi.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 "core/core_string_names.h"
  33. #include "mesh_instance_3d.h"
  34. #include "multimesh_instance_3d.h"
  35. #include "scene/resources/camera_attributes.h"
  36. #include "voxelizer.h"
  37. void VoxelGIData::_set_data(const Dictionary &p_data) {
  38. ERR_FAIL_COND(!p_data.has("bounds"));
  39. ERR_FAIL_COND(!p_data.has("octree_size"));
  40. ERR_FAIL_COND(!p_data.has("octree_cells"));
  41. ERR_FAIL_COND(!p_data.has("octree_data"));
  42. ERR_FAIL_COND(!p_data.has("octree_df") && !p_data.has("octree_df_png"));
  43. ERR_FAIL_COND(!p_data.has("level_counts"));
  44. ERR_FAIL_COND(!p_data.has("to_cell_xform"));
  45. AABB bounds_new = p_data["bounds"];
  46. Vector3 octree_size_new = p_data["octree_size"];
  47. Vector<uint8_t> octree_cells = p_data["octree_cells"];
  48. Vector<uint8_t> octree_data = p_data["octree_data"];
  49. Vector<uint8_t> octree_df;
  50. if (p_data.has("octree_df")) {
  51. octree_df = p_data["octree_df"];
  52. } else if (p_data.has("octree_df_png")) {
  53. Vector<uint8_t> octree_df_png = p_data["octree_df_png"];
  54. Ref<Image> img;
  55. img.instantiate();
  56. Error err = img->load_png_from_buffer(octree_df_png);
  57. ERR_FAIL_COND(err != OK);
  58. ERR_FAIL_COND(img->get_format() != Image::FORMAT_L8);
  59. octree_df = img->get_data();
  60. }
  61. Vector<int> octree_levels = p_data["level_counts"];
  62. Transform3D to_cell_xform_new = p_data["to_cell_xform"];
  63. allocate(to_cell_xform_new, bounds_new, octree_size_new, octree_cells, octree_data, octree_df, octree_levels);
  64. }
  65. Dictionary VoxelGIData::_get_data() const {
  66. Dictionary d;
  67. d["bounds"] = get_bounds();
  68. Vector3i otsize = get_octree_size();
  69. d["octree_size"] = Vector3(otsize);
  70. d["octree_cells"] = get_octree_cells();
  71. d["octree_data"] = get_data_cells();
  72. if (otsize != Vector3i()) {
  73. Ref<Image> img = Image::create_from_data(otsize.x * otsize.y, otsize.z, false, Image::FORMAT_L8, get_distance_field());
  74. Vector<uint8_t> df_png = img->save_png_to_buffer();
  75. ERR_FAIL_COND_V(df_png.size() == 0, Dictionary());
  76. d["octree_df_png"] = df_png;
  77. } else {
  78. d["octree_df"] = Vector<uint8_t>();
  79. }
  80. d["level_counts"] = get_level_counts();
  81. d["to_cell_xform"] = get_to_cell_xform();
  82. return d;
  83. }
  84. 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) {
  85. 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);
  86. bounds = p_aabb;
  87. to_cell_xform = p_to_cell_xform;
  88. octree_size = p_octree_size;
  89. }
  90. AABB VoxelGIData::get_bounds() const {
  91. return bounds;
  92. }
  93. Vector3 VoxelGIData::get_octree_size() const {
  94. return octree_size;
  95. }
  96. Vector<uint8_t> VoxelGIData::get_octree_cells() const {
  97. return RS::get_singleton()->voxel_gi_get_octree_cells(probe);
  98. }
  99. Vector<uint8_t> VoxelGIData::get_data_cells() const {
  100. return RS::get_singleton()->voxel_gi_get_data_cells(probe);
  101. }
  102. Vector<uint8_t> VoxelGIData::get_distance_field() const {
  103. return RS::get_singleton()->voxel_gi_get_distance_field(probe);
  104. }
  105. Vector<int> VoxelGIData::get_level_counts() const {
  106. return RS::get_singleton()->voxel_gi_get_level_counts(probe);
  107. }
  108. Transform3D VoxelGIData::get_to_cell_xform() const {
  109. return to_cell_xform;
  110. }
  111. void VoxelGIData::set_dynamic_range(float p_range) {
  112. RS::get_singleton()->voxel_gi_set_dynamic_range(probe, p_range);
  113. dynamic_range = p_range;
  114. }
  115. float VoxelGIData::get_dynamic_range() const {
  116. return dynamic_range;
  117. }
  118. void VoxelGIData::set_propagation(float p_propagation) {
  119. RS::get_singleton()->voxel_gi_set_propagation(probe, p_propagation);
  120. propagation = p_propagation;
  121. }
  122. float VoxelGIData::get_propagation() const {
  123. return propagation;
  124. }
  125. void VoxelGIData::set_energy(float p_energy) {
  126. RS::get_singleton()->voxel_gi_set_energy(probe, p_energy);
  127. energy = p_energy;
  128. }
  129. float VoxelGIData::get_energy() const {
  130. return energy;
  131. }
  132. void VoxelGIData::set_bias(float p_bias) {
  133. RS::get_singleton()->voxel_gi_set_bias(probe, p_bias);
  134. bias = p_bias;
  135. }
  136. float VoxelGIData::get_bias() const {
  137. return bias;
  138. }
  139. void VoxelGIData::set_normal_bias(float p_normal_bias) {
  140. RS::get_singleton()->voxel_gi_set_normal_bias(probe, p_normal_bias);
  141. normal_bias = p_normal_bias;
  142. }
  143. float VoxelGIData::get_normal_bias() const {
  144. return normal_bias;
  145. }
  146. void VoxelGIData::set_interior(bool p_enable) {
  147. RS::get_singleton()->voxel_gi_set_interior(probe, p_enable);
  148. interior = p_enable;
  149. }
  150. bool VoxelGIData::is_interior() const {
  151. return interior;
  152. }
  153. void VoxelGIData::set_use_two_bounces(bool p_enable) {
  154. RS::get_singleton()->voxel_gi_set_use_two_bounces(probe, p_enable);
  155. use_two_bounces = p_enable;
  156. }
  157. bool VoxelGIData::is_using_two_bounces() const {
  158. return use_two_bounces;
  159. }
  160. RID VoxelGIData::get_rid() const {
  161. return probe;
  162. }
  163. void VoxelGIData::_bind_methods() {
  164. ClassDB::bind_method(D_METHOD("allocate", "to_cell_xform", "aabb", "octree_size", "octree_cells", "data_cells", "distance_field", "level_counts"), &VoxelGIData::allocate);
  165. ClassDB::bind_method(D_METHOD("get_bounds"), &VoxelGIData::get_bounds);
  166. ClassDB::bind_method(D_METHOD("get_octree_size"), &VoxelGIData::get_octree_size);
  167. ClassDB::bind_method(D_METHOD("get_to_cell_xform"), &VoxelGIData::get_to_cell_xform);
  168. ClassDB::bind_method(D_METHOD("get_octree_cells"), &VoxelGIData::get_octree_cells);
  169. ClassDB::bind_method(D_METHOD("get_data_cells"), &VoxelGIData::get_data_cells);
  170. ClassDB::bind_method(D_METHOD("get_level_counts"), &VoxelGIData::get_level_counts);
  171. ClassDB::bind_method(D_METHOD("set_dynamic_range", "dynamic_range"), &VoxelGIData::set_dynamic_range);
  172. ClassDB::bind_method(D_METHOD("get_dynamic_range"), &VoxelGIData::get_dynamic_range);
  173. ClassDB::bind_method(D_METHOD("set_energy", "energy"), &VoxelGIData::set_energy);
  174. ClassDB::bind_method(D_METHOD("get_energy"), &VoxelGIData::get_energy);
  175. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &VoxelGIData::set_bias);
  176. ClassDB::bind_method(D_METHOD("get_bias"), &VoxelGIData::get_bias);
  177. ClassDB::bind_method(D_METHOD("set_normal_bias", "bias"), &VoxelGIData::set_normal_bias);
  178. ClassDB::bind_method(D_METHOD("get_normal_bias"), &VoxelGIData::get_normal_bias);
  179. ClassDB::bind_method(D_METHOD("set_propagation", "propagation"), &VoxelGIData::set_propagation);
  180. ClassDB::bind_method(D_METHOD("get_propagation"), &VoxelGIData::get_propagation);
  181. ClassDB::bind_method(D_METHOD("set_interior", "interior"), &VoxelGIData::set_interior);
  182. ClassDB::bind_method(D_METHOD("is_interior"), &VoxelGIData::is_interior);
  183. ClassDB::bind_method(D_METHOD("set_use_two_bounces", "enable"), &VoxelGIData::set_use_two_bounces);
  184. ClassDB::bind_method(D_METHOD("is_using_two_bounces"), &VoxelGIData::is_using_two_bounces);
  185. ClassDB::bind_method(D_METHOD("_set_data", "data"), &VoxelGIData::_set_data);
  186. ClassDB::bind_method(D_METHOD("_get_data"), &VoxelGIData::_get_data);
  187. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
  188. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "dynamic_range", PROPERTY_HINT_RANGE, "1,8,0.01"), "set_dynamic_range", "get_dynamic_range");
  189. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_energy", "get_energy");
  190. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,8,0.01"), "set_bias", "get_bias");
  191. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "normal_bias", PROPERTY_HINT_RANGE, "0,8,0.01"), "set_normal_bias", "get_normal_bias");
  192. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "propagation", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_propagation", "get_propagation");
  193. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_two_bounces"), "set_use_two_bounces", "is_using_two_bounces");
  194. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior");
  195. }
  196. #ifndef DISABLE_DEPRECATED
  197. bool VoxelGI::_set(const StringName &p_name, const Variant &p_value) {
  198. if (p_name == "extents") { // Compatibility with Godot 3.x.
  199. set_size((Vector3)p_value * 2);
  200. return true;
  201. }
  202. return false;
  203. }
  204. bool VoxelGI::_get(const StringName &p_name, Variant &r_property) const {
  205. if (p_name == "extents") { // Compatibility with Godot 3.x.
  206. r_property = size / 2;
  207. return true;
  208. }
  209. return false;
  210. }
  211. #endif // DISABLE_DEPRECATED
  212. VoxelGIData::VoxelGIData() {
  213. probe = RS::get_singleton()->voxel_gi_create();
  214. }
  215. VoxelGIData::~VoxelGIData() {
  216. ERR_FAIL_NULL(RenderingServer::get_singleton());
  217. RS::get_singleton()->free(probe);
  218. }
  219. //////////////////////
  220. //////////////////////
  221. void VoxelGI::set_probe_data(const Ref<VoxelGIData> &p_data) {
  222. if (p_data.is_valid()) {
  223. RS::get_singleton()->instance_set_base(get_instance(), p_data->get_rid());
  224. RS::get_singleton()->voxel_gi_set_baked_exposure_normalization(p_data->get_rid(), _get_camera_exposure_normalization());
  225. } else {
  226. RS::get_singleton()->instance_set_base(get_instance(), RID());
  227. }
  228. probe_data = p_data;
  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 = Vector3(MAX(1.0, p_size.x), MAX(1.0, p_size.y), MAX(1.0, p_size.z));
  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_valid()) {
  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. Vector3i VoxelGI::get_estimated_cell_size() const {
  323. static const int subdiv_value[SUBDIV_MAX] = { 6, 7, 8, 9 };
  324. int cell_subdiv = subdiv_value[subdiv];
  325. int axis_cell_size[3];
  326. AABB bounds = AABB(-size / 2, size);
  327. int longest_axis = bounds.get_longest_axis_index();
  328. axis_cell_size[longest_axis] = 1 << cell_subdiv;
  329. for (int i = 0; i < 3; i++) {
  330. if (i == longest_axis) {
  331. continue;
  332. }
  333. axis_cell_size[i] = axis_cell_size[longest_axis];
  334. float axis_size = bounds.size[longest_axis];
  335. //shrink until fit subdiv
  336. while (axis_size / 2.0 >= bounds.size[i]) {
  337. axis_size /= 2.0;
  338. axis_cell_size[i] >>= 1;
  339. }
  340. }
  341. return Vector3i(axis_cell_size[0], axis_cell_size[1], axis_cell_size[2]);
  342. }
  343. void VoxelGI::bake(Node *p_from_node, bool p_create_visual_debug) {
  344. static const int subdiv_value[SUBDIV_MAX] = { 6, 7, 8, 9 };
  345. p_from_node = p_from_node ? p_from_node : get_parent();
  346. ERR_FAIL_NULL(p_from_node);
  347. float exposure_normalization = _get_camera_exposure_normalization();
  348. Voxelizer baker;
  349. baker.begin_bake(subdiv_value[subdiv], AABB(-size / 2, size), exposure_normalization);
  350. List<PlotMesh> mesh_list;
  351. _find_meshes(p_from_node, mesh_list);
  352. if (bake_begin_function) {
  353. bake_begin_function(mesh_list.size() + 1);
  354. }
  355. int pmc = 0;
  356. for (PlotMesh &E : mesh_list) {
  357. if (bake_step_function) {
  358. bake_step_function(pmc, RTR("Plotting Meshes") + " " + itos(pmc) + "/" + itos(mesh_list.size()));
  359. }
  360. pmc++;
  361. baker.plot_mesh(E.local_xform, E.mesh, E.instance_materials, E.override_material);
  362. }
  363. if (bake_step_function) {
  364. bake_step_function(pmc++, RTR("Finishing Plot"));
  365. }
  366. baker.end_bake();
  367. //create the data for rendering server
  368. if (p_create_visual_debug) {
  369. MultiMeshInstance3D *mmi = memnew(MultiMeshInstance3D);
  370. mmi->set_multimesh(baker.create_debug_multimesh());
  371. add_child(mmi, true);
  372. #ifdef TOOLS_ENABLED
  373. if (is_inside_tree() && get_tree()->get_edited_scene_root() == this) {
  374. mmi->set_owner(this);
  375. } else {
  376. mmi->set_owner(get_owner());
  377. }
  378. #else
  379. mmi->set_owner(get_owner());
  380. #endif
  381. } else {
  382. Ref<VoxelGIData> probe_data_new = get_probe_data();
  383. if (probe_data_new.is_null()) {
  384. probe_data_new.instantiate();
  385. }
  386. if (bake_step_function) {
  387. bake_step_function(pmc++, RTR("Generating Distance Field"));
  388. }
  389. Vector<uint8_t> df = baker.get_sdf_3d_image();
  390. RS::get_singleton()->voxel_gi_set_baked_exposure_normalization(probe_data_new->get_rid(), exposure_normalization);
  391. 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());
  392. set_probe_data(probe_data_new);
  393. #ifdef TOOLS_ENABLED
  394. probe_data_new->set_edited(true); //so it gets saved
  395. #endif
  396. }
  397. if (bake_end_function) {
  398. bake_end_function();
  399. }
  400. notify_property_list_changed(); //bake property may have changed
  401. }
  402. void VoxelGI::_debug_bake() {
  403. bake(nullptr, true);
  404. }
  405. float VoxelGI::_get_camera_exposure_normalization() {
  406. float exposure_normalization = 1.0;
  407. if (camera_attributes.is_valid()) {
  408. exposure_normalization = camera_attributes->get_exposure_multiplier();
  409. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  410. exposure_normalization = camera_attributes->calculate_exposure_normalization();
  411. }
  412. }
  413. return exposure_normalization;
  414. }
  415. AABB VoxelGI::get_aabb() const {
  416. return AABB(-size / 2, size);
  417. }
  418. PackedStringArray VoxelGI::get_configuration_warnings() const {
  419. PackedStringArray warnings = Node::get_configuration_warnings();
  420. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  421. warnings.push_back(RTR("VoxelGI nodes are not supported when using the GL Compatibility backend yet. Support will be added in a future release."));
  422. } else if (probe_data.is_null()) {
  423. warnings.push_back(RTR("No VoxelGI data set, so this node is disabled. Bake static objects to enable GI."));
  424. }
  425. return warnings;
  426. }
  427. void VoxelGI::_bind_methods() {
  428. ClassDB::bind_method(D_METHOD("set_probe_data", "data"), &VoxelGI::set_probe_data);
  429. ClassDB::bind_method(D_METHOD("get_probe_data"), &VoxelGI::get_probe_data);
  430. ClassDB::bind_method(D_METHOD("set_subdiv", "subdiv"), &VoxelGI::set_subdiv);
  431. ClassDB::bind_method(D_METHOD("get_subdiv"), &VoxelGI::get_subdiv);
  432. ClassDB::bind_method(D_METHOD("set_size", "size"), &VoxelGI::set_size);
  433. ClassDB::bind_method(D_METHOD("get_size"), &VoxelGI::get_size);
  434. ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &VoxelGI::set_camera_attributes);
  435. ClassDB::bind_method(D_METHOD("get_camera_attributes"), &VoxelGI::get_camera_attributes);
  436. ClassDB::bind_method(D_METHOD("bake", "from_node", "create_visual_debug"), &VoxelGI::bake, DEFVAL(Variant()), DEFVAL(false));
  437. ClassDB::bind_method(D_METHOD("debug_bake"), &VoxelGI::_debug_bake);
  438. ClassDB::set_method_flags(get_class_static(), _scs_create("debug_bake"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  439. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdiv", PROPERTY_HINT_ENUM, "64,128,256,512"), "set_subdiv", "get_subdiv");
  440. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size", PROPERTY_HINT_NONE, "suffix:m"), "set_size", "get_size");
  441. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");
  442. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "VoxelGIData", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE), "set_probe_data", "get_probe_data");
  443. BIND_ENUM_CONSTANT(SUBDIV_64);
  444. BIND_ENUM_CONSTANT(SUBDIV_128);
  445. BIND_ENUM_CONSTANT(SUBDIV_256);
  446. BIND_ENUM_CONSTANT(SUBDIV_512);
  447. BIND_ENUM_CONSTANT(SUBDIV_MAX);
  448. }
  449. VoxelGI::VoxelGI() {
  450. voxel_gi = RS::get_singleton()->voxel_gi_create();
  451. set_disable_scale(true);
  452. }
  453. VoxelGI::~VoxelGI() {
  454. ERR_FAIL_NULL(RenderingServer::get_singleton());
  455. RS::get_singleton()->free(voxel_gi);
  456. }