lightmap_gi.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. /**************************************************************************/
  2. /* lightmap_gi.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "lightmap_gi.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/config_file.h"
  33. #include "core/math/delaunay_3d.h"
  34. #include "lightmap_probe.h"
  35. #include "scene/3d/mesh_instance_3d.h"
  36. #include "scene/resources/camera_attributes.h"
  37. #include "scene/resources/environment.h"
  38. #include "scene/resources/image_texture.h"
  39. #include "scene/resources/sky.h"
  40. void LightmapGIData::add_user(const NodePath &p_path, const Rect2 &p_uv_scale, int p_slice_index, int32_t p_sub_instance) {
  41. User user;
  42. user.path = p_path;
  43. user.uv_scale = p_uv_scale;
  44. user.slice_index = p_slice_index;
  45. user.sub_instance = p_sub_instance;
  46. users.push_back(user);
  47. }
  48. int LightmapGIData::get_user_count() const {
  49. return users.size();
  50. }
  51. NodePath LightmapGIData::get_user_path(int p_user) const {
  52. ERR_FAIL_INDEX_V(p_user, users.size(), NodePath());
  53. return users[p_user].path;
  54. }
  55. int32_t LightmapGIData::get_user_sub_instance(int p_user) const {
  56. ERR_FAIL_INDEX_V(p_user, users.size(), -1);
  57. return users[p_user].sub_instance;
  58. }
  59. Rect2 LightmapGIData::get_user_lightmap_uv_scale(int p_user) const {
  60. ERR_FAIL_INDEX_V(p_user, users.size(), Rect2());
  61. return users[p_user].uv_scale;
  62. }
  63. int LightmapGIData::get_user_lightmap_slice_index(int p_user) const {
  64. ERR_FAIL_INDEX_V(p_user, users.size(), -1);
  65. return users[p_user].slice_index;
  66. }
  67. void LightmapGIData::clear_users() {
  68. users.clear();
  69. }
  70. void LightmapGIData::_set_user_data(const Array &p_data) {
  71. ERR_FAIL_COND(p_data.is_empty());
  72. ERR_FAIL_COND((p_data.size() % 4) != 0);
  73. for (int i = 0; i < p_data.size(); i += 4) {
  74. add_user(p_data[i + 0], p_data[i + 1], p_data[i + 2], p_data[i + 3]);
  75. }
  76. }
  77. Array LightmapGIData::_get_user_data() const {
  78. Array ret;
  79. for (int i = 0; i < users.size(); i++) {
  80. ret.push_back(users[i].path);
  81. ret.push_back(users[i].uv_scale);
  82. ret.push_back(users[i].slice_index);
  83. ret.push_back(users[i].sub_instance);
  84. }
  85. return ret;
  86. }
  87. void LightmapGIData::set_lightmap_textures(const TypedArray<TextureLayered> &p_data) {
  88. light_textures = p_data;
  89. if (p_data.is_empty()) {
  90. light_texture = Ref<TextureLayered>();
  91. _reset_lightmap_textures();
  92. return;
  93. }
  94. if (p_data.size() == 1) {
  95. light_texture = p_data[0];
  96. } else {
  97. Vector<Ref<Image>> images;
  98. for (int i = 0; i < p_data.size(); i++) {
  99. Ref<TextureLayered> texture = p_data[i];
  100. ERR_FAIL_COND_MSG(texture.is_null(), vformat("Invalid TextureLayered at index %d.", i));
  101. for (int j = 0; j < texture->get_layers(); j++) {
  102. images.push_back(texture->get_layer_data(j));
  103. }
  104. }
  105. Ref<Texture2DArray> combined_texture;
  106. combined_texture.instantiate();
  107. combined_texture->create_from_images(images);
  108. light_texture = combined_texture;
  109. }
  110. _reset_lightmap_textures();
  111. }
  112. TypedArray<TextureLayered> LightmapGIData::get_lightmap_textures() const {
  113. return light_textures;
  114. }
  115. RID LightmapGIData::get_rid() const {
  116. return lightmap;
  117. }
  118. void LightmapGIData::clear() {
  119. users.clear();
  120. }
  121. void LightmapGIData::_reset_lightmap_textures() {
  122. RS::get_singleton()->lightmap_set_textures(lightmap, light_texture.is_valid() ? light_texture->get_rid() : RID(), uses_spherical_harmonics);
  123. }
  124. void LightmapGIData::set_uses_spherical_harmonics(bool p_enable) {
  125. uses_spherical_harmonics = p_enable;
  126. _reset_lightmap_textures();
  127. }
  128. bool LightmapGIData::is_using_spherical_harmonics() const {
  129. return uses_spherical_harmonics;
  130. }
  131. void LightmapGIData::set_capture_data(const AABB &p_bounds, bool p_interior, const PackedVector3Array &p_points, const PackedColorArray &p_point_sh, const PackedInt32Array &p_tetrahedra, const PackedInt32Array &p_bsp_tree, float p_baked_exposure) {
  132. if (p_points.size()) {
  133. int pc = p_points.size();
  134. ERR_FAIL_COND(pc * 9 != p_point_sh.size());
  135. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  136. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  137. RS::get_singleton()->lightmap_set_probe_capture_data(lightmap, p_points, p_point_sh, p_tetrahedra, p_bsp_tree);
  138. RS::get_singleton()->lightmap_set_probe_bounds(lightmap, p_bounds);
  139. RS::get_singleton()->lightmap_set_probe_interior(lightmap, p_interior);
  140. } else {
  141. RS::get_singleton()->lightmap_set_probe_capture_data(lightmap, PackedVector3Array(), PackedColorArray(), PackedInt32Array(), PackedInt32Array());
  142. RS::get_singleton()->lightmap_set_probe_bounds(lightmap, AABB());
  143. RS::get_singleton()->lightmap_set_probe_interior(lightmap, false);
  144. }
  145. RS::get_singleton()->lightmap_set_baked_exposure_normalization(lightmap, p_baked_exposure);
  146. baked_exposure = p_baked_exposure;
  147. interior = p_interior;
  148. bounds = p_bounds;
  149. }
  150. PackedVector3Array LightmapGIData::get_capture_points() const {
  151. return RS::get_singleton()->lightmap_get_probe_capture_points(lightmap);
  152. }
  153. PackedColorArray LightmapGIData::get_capture_sh() const {
  154. return RS::get_singleton()->lightmap_get_probe_capture_sh(lightmap);
  155. }
  156. PackedInt32Array LightmapGIData::get_capture_tetrahedra() const {
  157. return RS::get_singleton()->lightmap_get_probe_capture_tetrahedra(lightmap);
  158. }
  159. PackedInt32Array LightmapGIData::get_capture_bsp_tree() const {
  160. return RS::get_singleton()->lightmap_get_probe_capture_bsp_tree(lightmap);
  161. }
  162. AABB LightmapGIData::get_capture_bounds() const {
  163. return bounds;
  164. }
  165. bool LightmapGIData::is_interior() const {
  166. return interior;
  167. }
  168. float LightmapGIData::get_baked_exposure() const {
  169. return baked_exposure;
  170. }
  171. void LightmapGIData::_set_probe_data(const Dictionary &p_data) {
  172. ERR_FAIL_COND(!p_data.has("bounds"));
  173. ERR_FAIL_COND(!p_data.has("points"));
  174. ERR_FAIL_COND(!p_data.has("tetrahedra"));
  175. ERR_FAIL_COND(!p_data.has("bsp"));
  176. ERR_FAIL_COND(!p_data.has("sh"));
  177. ERR_FAIL_COND(!p_data.has("interior"));
  178. ERR_FAIL_COND(!p_data.has("baked_exposure"));
  179. set_capture_data(p_data["bounds"], p_data["interior"], p_data["points"], p_data["sh"], p_data["tetrahedra"], p_data["bsp"], p_data["baked_exposure"]);
  180. }
  181. Dictionary LightmapGIData::_get_probe_data() const {
  182. Dictionary d;
  183. d["bounds"] = get_capture_bounds();
  184. d["points"] = get_capture_points();
  185. d["tetrahedra"] = get_capture_tetrahedra();
  186. d["bsp"] = get_capture_bsp_tree();
  187. d["sh"] = get_capture_sh();
  188. d["interior"] = is_interior();
  189. d["baked_exposure"] = get_baked_exposure();
  190. return d;
  191. }
  192. #ifndef DISABLE_DEPRECATED
  193. void LightmapGIData::set_light_texture(const Ref<TextureLayered> &p_light_texture) {
  194. TypedArray<TextureLayered> arr;
  195. arr.append(p_light_texture);
  196. set_lightmap_textures(arr);
  197. }
  198. Ref<TextureLayered> LightmapGIData::get_light_texture() const {
  199. if (light_textures.is_empty()) {
  200. return Ref<TextureLayered>();
  201. }
  202. return light_textures.get(0);
  203. }
  204. void LightmapGIData::_set_light_textures_data(const Array &p_data) {
  205. set_lightmap_textures(p_data);
  206. }
  207. Array LightmapGIData::_get_light_textures_data() const {
  208. return Array(light_textures);
  209. }
  210. #endif
  211. void LightmapGIData::_bind_methods() {
  212. ClassDB::bind_method(D_METHOD("_set_user_data", "data"), &LightmapGIData::_set_user_data);
  213. ClassDB::bind_method(D_METHOD("_get_user_data"), &LightmapGIData::_get_user_data);
  214. ClassDB::bind_method(D_METHOD("set_lightmap_textures", "light_textures"), &LightmapGIData::set_lightmap_textures);
  215. ClassDB::bind_method(D_METHOD("get_lightmap_textures"), &LightmapGIData::get_lightmap_textures);
  216. ClassDB::bind_method(D_METHOD("set_uses_spherical_harmonics", "uses_spherical_harmonics"), &LightmapGIData::set_uses_spherical_harmonics);
  217. ClassDB::bind_method(D_METHOD("is_using_spherical_harmonics"), &LightmapGIData::is_using_spherical_harmonics);
  218. ClassDB::bind_method(D_METHOD("add_user", "path", "uv_scale", "slice_index", "sub_instance"), &LightmapGIData::add_user);
  219. ClassDB::bind_method(D_METHOD("get_user_count"), &LightmapGIData::get_user_count);
  220. ClassDB::bind_method(D_METHOD("get_user_path", "user_idx"), &LightmapGIData::get_user_path);
  221. ClassDB::bind_method(D_METHOD("clear_users"), &LightmapGIData::clear_users);
  222. ClassDB::bind_method(D_METHOD("_set_probe_data", "data"), &LightmapGIData::_set_probe_data);
  223. ClassDB::bind_method(D_METHOD("_get_probe_data"), &LightmapGIData::_get_probe_data);
  224. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "lightmap_textures", PROPERTY_HINT_ARRAY_TYPE, "TextureLayered", PROPERTY_USAGE_NO_EDITOR), "set_lightmap_textures", "get_lightmap_textures");
  225. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "uses_spherical_harmonics", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_uses_spherical_harmonics", "is_using_spherical_harmonics");
  226. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "user_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_user_data", "_get_user_data");
  227. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "probe_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_probe_data", "_get_probe_data");
  228. #ifndef DISABLE_DEPRECATED
  229. ClassDB::bind_method(D_METHOD("set_light_texture", "light_texture"), &LightmapGIData::set_light_texture);
  230. ClassDB::bind_method(D_METHOD("get_light_texture"), &LightmapGIData::get_light_texture);
  231. ClassDB::bind_method(D_METHOD("_set_light_textures_data", "data"), &LightmapGIData::_set_light_textures_data);
  232. ClassDB::bind_method(D_METHOD("_get_light_textures_data"), &LightmapGIData::_get_light_textures_data);
  233. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_texture", PROPERTY_HINT_RESOURCE_TYPE, "TextureLayered", PROPERTY_USAGE_EDITOR), "set_light_texture", "get_light_texture");
  234. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "light_textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_light_textures_data", "_get_light_textures_data");
  235. #endif
  236. }
  237. LightmapGIData::LightmapGIData() {
  238. lightmap = RS::get_singleton()->lightmap_create();
  239. }
  240. LightmapGIData::~LightmapGIData() {
  241. ERR_FAIL_NULL(RenderingServer::get_singleton());
  242. RS::get_singleton()->free(lightmap);
  243. }
  244. ///////////////////////////
  245. void LightmapGI::_find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes) {
  246. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_at_node);
  247. if (mi && mi->get_gi_mode() == GeometryInstance3D::GI_MODE_STATIC && mi->is_visible_in_tree()) {
  248. Ref<Mesh> mesh = mi->get_mesh();
  249. if (mesh.is_valid()) {
  250. bool all_have_uv2_and_normal = true;
  251. bool surfaces_found = false;
  252. for (int i = 0; i < mesh->get_surface_count(); i++) {
  253. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  254. continue;
  255. }
  256. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_TEX_UV2)) {
  257. all_have_uv2_and_normal = false;
  258. break;
  259. }
  260. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_NORMAL)) {
  261. all_have_uv2_and_normal = false;
  262. break;
  263. }
  264. surfaces_found = true;
  265. }
  266. if (surfaces_found && all_have_uv2_and_normal) {
  267. //READY TO BAKE! size hint could be computed if not found, actually..
  268. MeshesFound mf;
  269. mf.xform = get_global_transform().affine_inverse() * mi->get_global_transform();
  270. mf.node_path = get_path_to(mi);
  271. mf.subindex = -1;
  272. mf.mesh = mesh;
  273. static const int lightmap_scale[GeometryInstance3D::LIGHTMAP_SCALE_MAX] = { 1, 2, 4, 8 };
  274. mf.lightmap_scale = lightmap_scale[mi->get_lightmap_scale()];
  275. Ref<Material> all_override = mi->get_material_override();
  276. for (int i = 0; i < mesh->get_surface_count(); i++) {
  277. if (all_override.is_valid()) {
  278. mf.overrides.push_back(all_override);
  279. } else {
  280. mf.overrides.push_back(mi->get_surface_override_material(i));
  281. }
  282. }
  283. meshes.push_back(mf);
  284. }
  285. }
  286. }
  287. Node3D *s = Object::cast_to<Node3D>(p_at_node);
  288. if (!mi && s) {
  289. Array bmeshes = p_at_node->call("get_bake_meshes");
  290. if (bmeshes.size() && (bmeshes.size() & 1) == 0) {
  291. Transform3D xf = get_global_transform().affine_inverse() * s->get_global_transform();
  292. for (int i = 0; i < bmeshes.size(); i += 2) {
  293. Ref<Mesh> mesh = bmeshes[i];
  294. if (!mesh.is_valid()) {
  295. continue;
  296. }
  297. MeshesFound mf;
  298. Transform3D mesh_xf = bmeshes[i + 1];
  299. mf.xform = xf * mesh_xf;
  300. mf.node_path = get_path_to(s);
  301. mf.subindex = i / 2;
  302. mf.lightmap_scale = 1;
  303. mf.mesh = mesh;
  304. meshes.push_back(mf);
  305. }
  306. }
  307. }
  308. Light3D *light = Object::cast_to<Light3D>(p_at_node);
  309. if (light && light->get_bake_mode() != Light3D::BAKE_DISABLED) {
  310. LightsFound lf;
  311. lf.xform = get_global_transform().affine_inverse() * light->get_global_transform();
  312. lf.light = light;
  313. lights.push_back(lf);
  314. }
  315. LightmapProbe *probe = Object::cast_to<LightmapProbe>(p_at_node);
  316. if (probe) {
  317. Transform3D xf = get_global_transform().affine_inverse() * probe->get_global_transform();
  318. probes.push_back(xf.origin);
  319. }
  320. for (int i = 0; i < p_at_node->get_child_count(); i++) {
  321. Node *child = p_at_node->get_child(i);
  322. if (!child->get_owner()) {
  323. continue; //maybe a helper
  324. }
  325. _find_meshes_and_lights(child, meshes, lights, probes);
  326. }
  327. }
  328. int LightmapGI::_bsp_get_simplex_side(const Vector<Vector3> &p_points, const LocalVector<BSPSimplex> &p_simplices, const Plane &p_plane, uint32_t p_simplex) const {
  329. int over = 0;
  330. int under = 0;
  331. const BSPSimplex &s = p_simplices[p_simplex];
  332. for (int i = 0; i < 4; i++) {
  333. const Vector3 v = p_points[s.vertices[i]];
  334. if (p_plane.has_point(v)) {
  335. // Coplanar.
  336. } else if (p_plane.is_point_over(v)) {
  337. over++;
  338. } else {
  339. under++;
  340. }
  341. }
  342. ERR_FAIL_COND_V(under == 0 && over == 0, -2); //should never happen, we discarded flat simplices before, but in any case drop it from the bsp tree and throw an error
  343. if (under == 0) {
  344. return 1; // all over
  345. } else if (over == 0) {
  346. return -1; // all under
  347. } else {
  348. return 0; // crossing
  349. }
  350. }
  351. //#define DEBUG_BSP
  352. int32_t LightmapGI::_compute_bsp_tree(const Vector<Vector3> &p_points, const LocalVector<Plane> &p_planes, LocalVector<int32_t> &planes_tested, const LocalVector<BSPSimplex> &p_simplices, const LocalVector<int32_t> &p_simplex_indices, LocalVector<BSPNode> &bsp_nodes) {
  353. //if we reach here, it means there is more than one simplex
  354. int32_t node_index = (int32_t)bsp_nodes.size();
  355. bsp_nodes.push_back(BSPNode());
  356. //test with all the simplex planes
  357. Plane best_plane;
  358. float best_plane_score = -1.0;
  359. for (const int idx : p_simplex_indices) {
  360. const BSPSimplex &s = p_simplices[idx];
  361. for (int j = 0; j < 4; j++) {
  362. uint32_t plane_index = s.planes[j];
  363. if (planes_tested[plane_index] == node_index) {
  364. continue; //tested this plane already
  365. }
  366. planes_tested[plane_index] = node_index;
  367. static const int face_order[4][3] = {
  368. { 0, 1, 2 },
  369. { 0, 2, 3 },
  370. { 0, 1, 3 },
  371. { 1, 2, 3 }
  372. };
  373. // despite getting rid of plane duplicates, we should still use here the actual plane to avoid numerical error
  374. // from thinking this same simplex is intersecting rather than on a side
  375. Vector3 v0 = p_points[s.vertices[face_order[j][0]]];
  376. Vector3 v1 = p_points[s.vertices[face_order[j][1]]];
  377. Vector3 v2 = p_points[s.vertices[face_order[j][2]]];
  378. Plane plane(v0, v1, v2);
  379. //test with all the simplices
  380. int over_count = 0;
  381. int under_count = 0;
  382. for (const int &index : p_simplex_indices) {
  383. int side = _bsp_get_simplex_side(p_points, p_simplices, plane, index);
  384. if (side == -2) {
  385. continue; //this simplex is invalid, skip for now
  386. } else if (side < 0) {
  387. under_count++;
  388. } else if (side > 0) {
  389. over_count++;
  390. }
  391. }
  392. if (under_count == 0 && over_count == 0) {
  393. continue; //most likely precision issue with a flat simplex, do not try this plane
  394. }
  395. if (under_count > over_count) { //make sure under is always less than over, so we can compute the same ratio
  396. SWAP(under_count, over_count);
  397. }
  398. float score = 0; //by default, score is 0 (worst)
  399. if (over_count > 0) {
  400. //give score mainly based on ratio (under / over), this means that this plane is splitting simplices a lot, but its balanced
  401. score = float(under_count) / over_count;
  402. }
  403. //adjusting priority over least splits, probably not a great idea
  404. //score *= Math::sqrt(float(over_count + under_count) / p_simplex_indices.size()); //also multiply score
  405. if (score > best_plane_score) {
  406. best_plane = plane;
  407. best_plane_score = score;
  408. }
  409. }
  410. }
  411. LocalVector<int32_t> indices_over;
  412. LocalVector<int32_t> indices_under;
  413. //split again, but add to list
  414. for (const uint32_t index : p_simplex_indices) {
  415. int side = _bsp_get_simplex_side(p_points, p_simplices, best_plane, index);
  416. if (side == -2) {
  417. continue; //simplex sits on the plane, does not make sense to use it
  418. }
  419. if (side <= 0) {
  420. indices_under.push_back(index);
  421. }
  422. if (side >= 0) {
  423. indices_over.push_back(index);
  424. }
  425. }
  426. #ifdef DEBUG_BSP
  427. print_line("node " + itos(node_index) + " found plane: " + best_plane + " score:" + rtos(best_plane_score) + " - over " + itos(indices_over.size()) + " under " + itos(indices_under.size()) + " intersecting " + itos(intersecting));
  428. #endif
  429. if (best_plane_score < 0.0 || indices_over.size() == p_simplex_indices.size() || indices_under.size() == p_simplex_indices.size()) {
  430. ERR_FAIL_COND_V(p_simplex_indices.size() <= 1, 0); //should not happen, this is a bug
  431. // Failed to separate the tetrahedrons using planes
  432. // this means Delaunay broke at some point.
  433. // Luckily, because we are using tetrahedrons, we can resort to
  434. // less precise but still working ways to generate the separating plane
  435. // this will most likely look bad when interpolating, but at least it will not crash.
  436. // and the artifact will most likely also be very small, so too difficult to notice.
  437. //find the longest axis
  438. WARN_PRINT("Inconsistency found in triangulation while building BSP, probe interpolation quality may degrade a bit.");
  439. LocalVector<Vector3> centers;
  440. AABB bounds_all;
  441. for (uint32_t i = 0; i < p_simplex_indices.size(); i++) {
  442. AABB bounds;
  443. for (uint32_t j = 0; j < 4; j++) {
  444. Vector3 p = p_points[p_simplices[p_simplex_indices[i]].vertices[j]];
  445. if (j == 0) {
  446. bounds.position = p;
  447. } else {
  448. bounds.expand_to(p);
  449. }
  450. }
  451. if (i == 0) {
  452. centers.push_back(bounds.get_center());
  453. } else {
  454. bounds_all.merge_with(bounds);
  455. }
  456. }
  457. Vector3::Axis longest_axis = Vector3::Axis(bounds_all.get_longest_axis_index());
  458. //find the simplex that will go under
  459. uint32_t min_d_idx = 0xFFFFFFFF;
  460. float min_d_dist = 1e20;
  461. for (uint32_t i = 0; i < centers.size(); i++) {
  462. if (centers[i][longest_axis] < min_d_dist) {
  463. min_d_idx = i;
  464. min_d_dist = centers[i][longest_axis];
  465. }
  466. }
  467. //rebuild best_plane and over/under arrays
  468. best_plane = Plane();
  469. best_plane.normal[longest_axis] = 1.0;
  470. best_plane.d = min_d_dist;
  471. indices_under.clear();
  472. indices_under.push_back(min_d_idx);
  473. indices_over.clear();
  474. for (uint32_t i = 0; i < p_simplex_indices.size(); i++) {
  475. if (i == min_d_idx) {
  476. continue;
  477. }
  478. indices_over.push_back(p_simplex_indices[i]);
  479. }
  480. }
  481. BSPNode node;
  482. node.plane = best_plane;
  483. if (indices_under.size() == 0) {
  484. //nothing to do here
  485. node.under = BSPNode::EMPTY_LEAF;
  486. } else if (indices_under.size() == 1) {
  487. node.under = -(indices_under[0] + 1);
  488. } else {
  489. node.under = _compute_bsp_tree(p_points, p_planes, planes_tested, p_simplices, indices_under, bsp_nodes);
  490. }
  491. if (indices_over.size() == 0) {
  492. //nothing to do here
  493. node.over = BSPNode::EMPTY_LEAF;
  494. } else if (indices_over.size() == 1) {
  495. node.over = -(indices_over[0] + 1);
  496. } else {
  497. node.over = _compute_bsp_tree(p_points, p_planes, planes_tested, p_simplices, indices_over, bsp_nodes);
  498. }
  499. bsp_nodes[node_index] = node;
  500. return node_index;
  501. }
  502. bool LightmapGI::_lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh) {
  503. BakeStepUD *bsud = (BakeStepUD *)ud;
  504. bool ret = false;
  505. if (bsud->func) {
  506. ret = bsud->func(bsud->from_percent + p_completion * (bsud->to_percent - bsud->from_percent), p_text, bsud->ud, p_refresh);
  507. }
  508. return ret;
  509. }
  510. void LightmapGI::_plot_triangle_into_octree(GenProbesOctree *p_cell, float p_cell_size, const Vector3 *p_triangle) {
  511. for (int i = 0; i < 8; i++) {
  512. Vector3i pos = p_cell->offset;
  513. uint32_t half_size = p_cell->size / 2;
  514. if (i & 1) {
  515. pos.x += half_size;
  516. }
  517. if (i & 2) {
  518. pos.y += half_size;
  519. }
  520. if (i & 4) {
  521. pos.z += half_size;
  522. }
  523. AABB subcell;
  524. subcell.position = Vector3(pos) * p_cell_size;
  525. subcell.size = Vector3(half_size, half_size, half_size) * p_cell_size;
  526. if (!Geometry3D::triangle_box_overlap(subcell.get_center(), subcell.size * 0.5, p_triangle)) {
  527. continue;
  528. }
  529. if (p_cell->children[i] == nullptr) {
  530. GenProbesOctree *child = memnew(GenProbesOctree);
  531. child->offset = pos;
  532. child->size = half_size;
  533. p_cell->children[i] = child;
  534. }
  535. if (half_size > 1) {
  536. //still levels missing
  537. _plot_triangle_into_octree(p_cell->children[i], p_cell_size, p_triangle);
  538. }
  539. }
  540. }
  541. void LightmapGI::_gen_new_positions_from_octree(const GenProbesOctree *p_cell, float p_cell_size, const Vector<Vector3> &probe_positions, LocalVector<Vector3> &new_probe_positions, HashMap<Vector3i, bool> &positions_used, const AABB &p_bounds) {
  542. for (int i = 0; i < 8; i++) {
  543. Vector3i pos = p_cell->offset;
  544. if (i & 1) {
  545. pos.x += p_cell->size;
  546. }
  547. if (i & 2) {
  548. pos.y += p_cell->size;
  549. }
  550. if (i & 4) {
  551. pos.z += p_cell->size;
  552. }
  553. if (p_cell->size == 1 && !positions_used.has(pos)) {
  554. //new position to insert!
  555. Vector3 real_pos = p_bounds.position + Vector3(pos) * p_cell_size;
  556. //see if a user submitted probe is too close
  557. int ppcount = probe_positions.size();
  558. const Vector3 *pp = probe_positions.ptr();
  559. bool exists = false;
  560. for (int j = 0; j < ppcount; j++) {
  561. if (pp[j].is_equal_approx(real_pos)) {
  562. exists = true;
  563. break;
  564. }
  565. }
  566. if (!exists) {
  567. new_probe_positions.push_back(real_pos);
  568. }
  569. positions_used[pos] = true;
  570. }
  571. if (p_cell->children[i] != nullptr) {
  572. _gen_new_positions_from_octree(p_cell->children[i], p_cell_size, probe_positions, new_probe_positions, positions_used, p_bounds);
  573. }
  574. }
  575. }
  576. LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_path, Lightmapper::BakeStepFunc p_bake_step, void *p_bake_userdata) {
  577. if (p_image_data_path.is_empty()) {
  578. if (get_light_data().is_null()) {
  579. return BAKE_ERROR_NO_SAVE_PATH;
  580. }
  581. p_image_data_path = get_light_data()->get_path();
  582. if (!p_image_data_path.is_resource_file()) {
  583. return BAKE_ERROR_NO_SAVE_PATH;
  584. }
  585. }
  586. Ref<Lightmapper> lightmapper = Lightmapper::create();
  587. ERR_FAIL_COND_V(lightmapper.is_null(), BAKE_ERROR_NO_LIGHTMAPPER);
  588. BakeStepUD bsud;
  589. bsud.func = p_bake_step;
  590. bsud.ud = p_bake_userdata;
  591. bsud.from_percent = 0.2;
  592. bsud.to_percent = 0.8;
  593. if (p_bake_step) {
  594. p_bake_step(0.0, RTR("Finding meshes, lights and probes"), p_bake_userdata, true);
  595. }
  596. /* STEP 1, FIND MESHES, LIGHTS AND PROBES */
  597. Vector<Lightmapper::MeshData> mesh_data;
  598. Vector<LightsFound> lights_found;
  599. Vector<Vector3> probes_found;
  600. AABB bounds;
  601. {
  602. Vector<MeshesFound> meshes_found;
  603. _find_meshes_and_lights(p_from_node ? p_from_node : get_parent(), meshes_found, lights_found, probes_found);
  604. if (meshes_found.size() == 0) {
  605. return BAKE_ERROR_NO_MESHES;
  606. }
  607. // create mesh data for insert
  608. //get the base material textures, help compute atlas size and bounds
  609. for (int m_i = 0; m_i < meshes_found.size(); m_i++) {
  610. if (p_bake_step) {
  611. float p = (float)(m_i) / meshes_found.size();
  612. p_bake_step(p * 0.1, vformat(RTR("Preparing geometry %d/%d"), m_i, meshes_found.size()), p_bake_userdata, false);
  613. }
  614. MeshesFound &mf = meshes_found.write[m_i];
  615. Size2i lightmap_size = mf.mesh->get_lightmap_size_hint();
  616. if (lightmap_size == Size2i(0, 0)) {
  617. // TODO we should compute a size if no lightmap hint is set, as we did in 3.x.
  618. // For now set to basic size to avoid crash.
  619. lightmap_size = Size2i(64, 64);
  620. }
  621. lightmap_size *= mf.lightmap_scale;
  622. TypedArray<RID> overrides;
  623. overrides.resize(mf.overrides.size());
  624. for (int i = 0; i < mf.overrides.size(); i++) {
  625. if (mf.overrides[i].is_valid()) {
  626. overrides[i] = mf.overrides[i]->get_rid();
  627. }
  628. }
  629. TypedArray<Image> images = RS::get_singleton()->bake_render_uv2(mf.mesh->get_rid(), overrides, lightmap_size);
  630. ERR_FAIL_COND_V(images.is_empty(), BAKE_ERROR_CANT_CREATE_IMAGE);
  631. Ref<Image> albedo = images[RS::BAKE_CHANNEL_ALBEDO_ALPHA];
  632. Ref<Image> orm = images[RS::BAKE_CHANNEL_ORM];
  633. //multiply albedo by metal
  634. Lightmapper::MeshData md;
  635. {
  636. Dictionary d;
  637. d["path"] = mf.node_path;
  638. if (mf.subindex >= 0) {
  639. d["subindex"] = mf.subindex;
  640. }
  641. md.userdata = d;
  642. }
  643. {
  644. if (albedo->get_format() != Image::FORMAT_RGBA8) {
  645. albedo->convert(Image::FORMAT_RGBA8);
  646. }
  647. if (orm->get_format() != Image::FORMAT_RGBA8) {
  648. orm->convert(Image::FORMAT_RGBA8);
  649. }
  650. Vector<uint8_t> albedo_alpha = albedo->get_data();
  651. Vector<uint8_t> orm_data = orm->get_data();
  652. Vector<uint8_t> albedom;
  653. uint32_t len = albedo_alpha.size();
  654. albedom.resize(len);
  655. const uint8_t *r_aa = albedo_alpha.ptr();
  656. const uint8_t *r_orm = orm_data.ptr();
  657. uint8_t *w_albedo = albedom.ptrw();
  658. for (uint32_t i = 0; i < len; i += 4) {
  659. w_albedo[i + 0] = uint8_t(CLAMP(float(r_aa[i + 0]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  660. w_albedo[i + 1] = uint8_t(CLAMP(float(r_aa[i + 1]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  661. w_albedo[i + 2] = uint8_t(CLAMP(float(r_aa[i + 2]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  662. w_albedo[i + 3] = 255;
  663. }
  664. md.albedo_on_uv2.instantiate();
  665. md.albedo_on_uv2->set_data(lightmap_size.width, lightmap_size.height, false, Image::FORMAT_RGBA8, albedom);
  666. }
  667. md.emission_on_uv2 = images[RS::BAKE_CHANNEL_EMISSION];
  668. if (md.emission_on_uv2->get_format() != Image::FORMAT_RGBAH) {
  669. md.emission_on_uv2->convert(Image::FORMAT_RGBAH);
  670. }
  671. //get geometry
  672. Basis normal_xform = mf.xform.basis.inverse().transposed();
  673. for (int i = 0; i < mf.mesh->get_surface_count(); i++) {
  674. if (mf.mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  675. continue;
  676. }
  677. Array a = mf.mesh->surface_get_arrays(i);
  678. Vector<Vector3> vertices = a[Mesh::ARRAY_VERTEX];
  679. const Vector3 *vr = vertices.ptr();
  680. Vector<Vector2> uv = a[Mesh::ARRAY_TEX_UV2];
  681. const Vector2 *uvr = nullptr;
  682. Vector<Vector3> normals = a[Mesh::ARRAY_NORMAL];
  683. const Vector3 *nr = nullptr;
  684. Vector<int> index = a[Mesh::ARRAY_INDEX];
  685. ERR_CONTINUE(uv.size() == 0);
  686. ERR_CONTINUE(normals.size() == 0);
  687. uvr = uv.ptr();
  688. nr = normals.ptr();
  689. int facecount;
  690. const int *ir = nullptr;
  691. if (index.size()) {
  692. facecount = index.size() / 3;
  693. ir = index.ptr();
  694. } else {
  695. facecount = vertices.size() / 3;
  696. }
  697. for (int j = 0; j < facecount; j++) {
  698. uint32_t vidx[3];
  699. if (ir) {
  700. for (int k = 0; k < 3; k++) {
  701. vidx[k] = ir[j * 3 + k];
  702. }
  703. } else {
  704. for (int k = 0; k < 3; k++) {
  705. vidx[k] = j * 3 + k;
  706. }
  707. }
  708. for (int k = 0; k < 3; k++) {
  709. Vector3 v = mf.xform.xform(vr[vidx[k]]);
  710. if (bounds == AABB()) {
  711. bounds.position = v;
  712. } else {
  713. bounds.expand_to(v);
  714. }
  715. md.points.push_back(v);
  716. md.uv2.push_back(uvr[vidx[k]]);
  717. md.normal.push_back(normal_xform.xform(nr[vidx[k]]).normalized());
  718. }
  719. }
  720. }
  721. mesh_data.push_back(md);
  722. }
  723. }
  724. /* STEP 2, CREATE PROBES */
  725. if (p_bake_step) {
  726. p_bake_step(0.3, RTR("Creating probes"), p_bake_userdata, true);
  727. }
  728. //bounds need to include the user probes
  729. for (int i = 0; i < probes_found.size(); i++) {
  730. bounds.expand_to(probes_found[i]);
  731. }
  732. bounds.grow_by(bounds.size.length() * 0.001);
  733. if (gen_probes == GENERATE_PROBES_DISABLED) {
  734. // generate 8 probes on bound endpoints
  735. for (int i = 0; i < 8; i++) {
  736. probes_found.push_back(bounds.get_endpoint(i));
  737. }
  738. } else {
  739. // detect probes from geometry
  740. static const int subdiv_values[6] = { 0, 4, 8, 16, 32 };
  741. int subdiv = subdiv_values[gen_probes];
  742. float subdiv_cell_size;
  743. Vector3i bound_limit;
  744. {
  745. int longest_axis = bounds.get_longest_axis_index();
  746. subdiv_cell_size = bounds.size[longest_axis] / subdiv;
  747. int axis_n1 = (longest_axis + 1) % 3;
  748. int axis_n2 = (longest_axis + 2) % 3;
  749. bound_limit[longest_axis] = subdiv;
  750. bound_limit[axis_n1] = int(Math::ceil(bounds.size[axis_n1] / subdiv_cell_size));
  751. bound_limit[axis_n2] = int(Math::ceil(bounds.size[axis_n2] / subdiv_cell_size));
  752. //compensate bounds
  753. bounds.size[axis_n1] = bound_limit[axis_n1] * subdiv_cell_size;
  754. bounds.size[axis_n2] = bound_limit[axis_n2] * subdiv_cell_size;
  755. }
  756. GenProbesOctree octree;
  757. octree.size = subdiv;
  758. for (int i = 0; i < mesh_data.size(); i++) {
  759. if (p_bake_step) {
  760. float p = (float)(i) / mesh_data.size();
  761. p_bake_step(0.3 + p * 0.1, vformat(RTR("Creating probes from mesh %d/%d"), i, mesh_data.size()), p_bake_userdata, false);
  762. }
  763. for (int j = 0; j < mesh_data[i].points.size(); j += 3) {
  764. Vector3 points[3] = { mesh_data[i].points[j + 0] - bounds.position, mesh_data[i].points[j + 1] - bounds.position, mesh_data[i].points[j + 2] - bounds.position };
  765. _plot_triangle_into_octree(&octree, subdiv_cell_size, points);
  766. }
  767. }
  768. LocalVector<Vector3> new_probe_positions;
  769. HashMap<Vector3i, bool> positions_used;
  770. for (uint32_t i = 0; i < 8; i++) { //insert bounding endpoints
  771. Vector3i pos;
  772. if (i & 1) {
  773. pos.x += bound_limit.x;
  774. }
  775. if (i & 2) {
  776. pos.y += bound_limit.y;
  777. }
  778. if (i & 4) {
  779. pos.z += bound_limit.z;
  780. }
  781. positions_used[pos] = true;
  782. Vector3 real_pos = bounds.position + Vector3(pos) * subdiv_cell_size; //use same formula for numerical stability
  783. new_probe_positions.push_back(real_pos);
  784. }
  785. //skip first level, since probes are always added at bounds endpoints anyway (code above this)
  786. for (int i = 0; i < 8; i++) {
  787. if (octree.children[i]) {
  788. _gen_new_positions_from_octree(octree.children[i], subdiv_cell_size, probes_found, new_probe_positions, positions_used, bounds);
  789. }
  790. }
  791. for (const Vector3 &position : new_probe_positions) {
  792. probes_found.push_back(position);
  793. }
  794. }
  795. // Add everything to lightmapper
  796. const bool use_physical_light_units = GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units");
  797. if (p_bake_step) {
  798. p_bake_step(0.4, RTR("Preparing Lightmapper"), p_bake_userdata, true);
  799. }
  800. {
  801. for (int i = 0; i < mesh_data.size(); i++) {
  802. lightmapper->add_mesh(mesh_data[i]);
  803. }
  804. for (int i = 0; i < lights_found.size(); i++) {
  805. Light3D *light = lights_found[i].light;
  806. if (light->is_editor_only()) {
  807. // Don't include editor-only lights in the lightmap bake,
  808. // as this results in inconsistent visuals when running the project.
  809. continue;
  810. }
  811. Transform3D xf = lights_found[i].xform;
  812. // For the lightmapper, the indirect energy represents the multiplier for the indirect bounces caused by the light, so the value is not converted when using physical units.
  813. float indirect_energy = light->get_param(Light3D::PARAM_INDIRECT_ENERGY);
  814. Color linear_color = light->get_color().srgb_to_linear();
  815. float energy = light->get_param(Light3D::PARAM_ENERGY);
  816. if (use_physical_light_units) {
  817. energy *= light->get_param(Light3D::PARAM_INTENSITY);
  818. linear_color *= light->get_correlated_color().srgb_to_linear();
  819. }
  820. if (Object::cast_to<DirectionalLight3D>(light)) {
  821. DirectionalLight3D *l = Object::cast_to<DirectionalLight3D>(light);
  822. if (l->get_sky_mode() != DirectionalLight3D::SKY_MODE_SKY_ONLY) {
  823. lightmapper->add_directional_light(light->get_bake_mode() == Light3D::BAKE_STATIC, -xf.basis.get_column(Vector3::AXIS_Z).normalized(), linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR));
  824. }
  825. } else if (Object::cast_to<OmniLight3D>(light)) {
  826. OmniLight3D *l = Object::cast_to<OmniLight3D>(light);
  827. if (use_physical_light_units) {
  828. energy *= (1.0 / (Math_PI * 4.0));
  829. }
  830. lightmapper->add_omni_light(light->get_bake_mode() == Light3D::BAKE_STATIC, xf.origin, linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_RANGE), l->get_param(Light3D::PARAM_ATTENUATION), l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR));
  831. } else if (Object::cast_to<SpotLight3D>(light)) {
  832. SpotLight3D *l = Object::cast_to<SpotLight3D>(light);
  833. if (use_physical_light_units) {
  834. energy *= (1.0 / Math_PI);
  835. }
  836. lightmapper->add_spot_light(light->get_bake_mode() == Light3D::BAKE_STATIC, xf.origin, -xf.basis.get_column(Vector3::AXIS_Z).normalized(), linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_RANGE), l->get_param(Light3D::PARAM_ATTENUATION), l->get_param(Light3D::PARAM_SPOT_ANGLE), l->get_param(Light3D::PARAM_SPOT_ATTENUATION), l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR));
  837. }
  838. }
  839. for (int i = 0; i < probes_found.size(); i++) {
  840. lightmapper->add_probe(probes_found[i]);
  841. }
  842. }
  843. Ref<Image> environment_image;
  844. Basis environment_transform;
  845. // Add everything to lightmapper
  846. if (environment_mode != ENVIRONMENT_MODE_DISABLED) {
  847. if (p_bake_step) {
  848. p_bake_step(4.1, RTR("Preparing Environment"), p_bake_userdata, true);
  849. }
  850. environment_transform = get_global_transform().basis;
  851. switch (environment_mode) {
  852. case ENVIRONMENT_MODE_DISABLED: {
  853. //nothing
  854. } break;
  855. case ENVIRONMENT_MODE_SCENE: {
  856. Ref<World3D> world = get_world_3d();
  857. if (world.is_valid()) {
  858. Ref<Environment> env = world->get_environment();
  859. if (env.is_null()) {
  860. env = world->get_fallback_environment();
  861. }
  862. if (env.is_valid()) {
  863. environment_image = RS::get_singleton()->environment_bake_panorama(env->get_rid(), true, Size2i(128, 64));
  864. }
  865. }
  866. } break;
  867. case ENVIRONMENT_MODE_CUSTOM_SKY: {
  868. if (environment_custom_sky.is_valid()) {
  869. environment_image = RS::get_singleton()->sky_bake_panorama(environment_custom_sky->get_rid(), environment_custom_energy, true, Size2i(128, 64));
  870. }
  871. } break;
  872. case ENVIRONMENT_MODE_CUSTOM_COLOR: {
  873. environment_image.instantiate();
  874. environment_image->initialize_data(128, 64, false, Image::FORMAT_RGBAF);
  875. Color c = environment_custom_color;
  876. c.r *= environment_custom_energy;
  877. c.g *= environment_custom_energy;
  878. c.b *= environment_custom_energy;
  879. environment_image->fill(c);
  880. } break;
  881. }
  882. }
  883. float exposure_normalization = 1.0;
  884. if (camera_attributes.is_valid()) {
  885. exposure_normalization = camera_attributes->get_exposure_multiplier();
  886. if (use_physical_light_units) {
  887. exposure_normalization = camera_attributes->calculate_exposure_normalization();
  888. }
  889. }
  890. Lightmapper::BakeError bake_err = lightmapper->bake(Lightmapper::BakeQuality(bake_quality), use_denoiser, denoiser_strength, bounces, bounce_indirect_energy, bias, max_texture_size, directional, use_texture_for_bounces, Lightmapper::GenerateProbes(gen_probes), environment_image, environment_transform, _lightmap_bake_step_function, &bsud, exposure_normalization);
  891. if (bake_err == Lightmapper::BAKE_ERROR_LIGHTMAP_TOO_SMALL) {
  892. return BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL;
  893. } else if (bake_err == Lightmapper::BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES) {
  894. return BAKE_ERROR_MESHES_INVALID;
  895. }
  896. // POSTBAKE: Save Textures.
  897. TypedArray<TextureLayered> textures;
  898. {
  899. Vector<Ref<Image>> images;
  900. images.resize(lightmapper->get_bake_texture_count());
  901. for (int i = 0; i < images.size(); i++) {
  902. images.set(i, lightmapper->get_bake_texture(i));
  903. }
  904. int slice_count = images.size();
  905. int slice_width = images[0]->get_width();
  906. int slice_height = images[0]->get_height();
  907. int slices_per_texture = Image::MAX_HEIGHT / slice_height;
  908. int texture_count = Math::ceil(slice_count / (float)slices_per_texture);
  909. textures.resize(texture_count);
  910. String base_path = p_image_data_path.get_basename();
  911. int last_count = slice_count % slices_per_texture;
  912. for (int i = 0; i < texture_count; i++) {
  913. int texture_slice_count = (i == texture_count - 1 && last_count != 0) ? last_count : slices_per_texture;
  914. Ref<Image> texture_image = Image::create_empty(slice_width, slice_height * texture_slice_count, false, images[0]->get_format());
  915. for (int j = 0; j < texture_slice_count; j++) {
  916. texture_image->blit_rect(images[i * slices_per_texture + j], Rect2i(0, 0, slice_width, slice_height), Point2i(0, slice_height * j));
  917. }
  918. String texture_path = texture_count > 1 ? base_path + "_" + itos(i) + ".exr" : base_path + ".exr";
  919. Ref<ConfigFile> config;
  920. config.instantiate();
  921. if (FileAccess::exists(texture_path + ".import")) {
  922. config->load(texture_path + ".import");
  923. }
  924. config->set_value("remap", "importer", "2d_array_texture");
  925. config->set_value("remap", "type", "CompressedTexture2DArray");
  926. if (!config->has_section_key("params", "compress/mode")) {
  927. // User may want another compression, so leave it be, but default to VRAM uncompressed.
  928. config->set_value("params", "compress/mode", 3);
  929. }
  930. config->set_value("params", "compress/channel_pack", 1);
  931. config->set_value("params", "mipmaps/generate", false);
  932. config->set_value("params", "slices/horizontal", 1);
  933. config->set_value("params", "slices/vertical", texture_slice_count);
  934. config->save(texture_path + ".import");
  935. Error err = texture_image->save_exr(texture_path, false);
  936. ERR_FAIL_COND_V(err, BAKE_ERROR_CANT_CREATE_IMAGE);
  937. ResourceLoader::import(texture_path);
  938. Ref<TextureLayered> t = ResourceLoader::load(texture_path); // If already loaded, it will be updated on refocus?
  939. ERR_FAIL_COND_V(t.is_null(), BAKE_ERROR_CANT_CREATE_IMAGE);
  940. textures[i] = t;
  941. }
  942. }
  943. /* POSTBAKE: Save Light Data */
  944. Ref<LightmapGIData> gi_data;
  945. if (get_light_data().is_valid()) {
  946. gi_data = get_light_data();
  947. set_light_data(Ref<LightmapGIData>()); //clear
  948. gi_data->clear();
  949. } else {
  950. gi_data.instantiate();
  951. }
  952. gi_data->set_lightmap_textures(textures);
  953. gi_data->set_uses_spherical_harmonics(directional);
  954. for (int i = 0; i < lightmapper->get_bake_mesh_count(); i++) {
  955. Dictionary d = lightmapper->get_bake_mesh_userdata(i);
  956. NodePath np = d["path"];
  957. int32_t subindex = -1;
  958. if (d.has("subindex")) {
  959. subindex = d["subindex"];
  960. }
  961. Rect2 uv_scale = lightmapper->get_bake_mesh_uv_scale(i);
  962. int slice_index = lightmapper->get_bake_mesh_texture_slice(i);
  963. gi_data->add_user(np, uv_scale, slice_index, subindex);
  964. }
  965. {
  966. // create tetrahedrons
  967. Vector<Vector3> points;
  968. Vector<Color> sh;
  969. points.resize(lightmapper->get_bake_probe_count());
  970. sh.resize(lightmapper->get_bake_probe_count() * 9);
  971. for (int i = 0; i < lightmapper->get_bake_probe_count(); i++) {
  972. points.write[i] = lightmapper->get_bake_probe_point(i);
  973. Vector<Color> colors = lightmapper->get_bake_probe_sh(i);
  974. ERR_CONTINUE(colors.size() != 9);
  975. for (int j = 0; j < 9; j++) {
  976. sh.write[i * 9 + j] = colors[j];
  977. }
  978. }
  979. //Obtain solved simplices
  980. if (p_bake_step) {
  981. p_bake_step(0.8, RTR("Generating Probe Volumes"), p_bake_userdata, true);
  982. }
  983. Vector<Delaunay3D::OutputSimplex> solved_simplices = Delaunay3D::tetrahedralize(points);
  984. LocalVector<BSPSimplex> bsp_simplices;
  985. LocalVector<Plane> bsp_planes;
  986. LocalVector<int32_t> bsp_simplex_indices;
  987. PackedInt32Array tetrahedrons;
  988. for (int i = 0; i < solved_simplices.size(); i++) {
  989. //Prepare a special representation of the simplex, which uses a BSP Tree
  990. BSPSimplex bsp_simplex;
  991. for (int j = 0; j < 4; j++) {
  992. bsp_simplex.vertices[j] = solved_simplices[i].points[j];
  993. }
  994. for (int j = 0; j < 4; j++) {
  995. static const int face_order[4][3] = {
  996. { 0, 1, 2 },
  997. { 0, 2, 3 },
  998. { 0, 1, 3 },
  999. { 1, 2, 3 }
  1000. };
  1001. Vector3 a = points[solved_simplices[i].points[face_order[j][0]]];
  1002. Vector3 b = points[solved_simplices[i].points[face_order[j][1]]];
  1003. Vector3 c = points[solved_simplices[i].points[face_order[j][2]]];
  1004. //store planes in an array, but ensure they are reused, to speed up processing
  1005. Plane p(a, b, c);
  1006. int plane_index = -1;
  1007. for (uint32_t k = 0; k < bsp_planes.size(); k++) {
  1008. if (bsp_planes[k].is_equal_approx_any_side(p)) {
  1009. plane_index = k;
  1010. break;
  1011. }
  1012. }
  1013. if (plane_index == -1) {
  1014. plane_index = bsp_planes.size();
  1015. bsp_planes.push_back(p);
  1016. }
  1017. bsp_simplex.planes[j] = plane_index;
  1018. //also fill simplex array
  1019. tetrahedrons.push_back(solved_simplices[i].points[j]);
  1020. }
  1021. bsp_simplex_indices.push_back(bsp_simplices.size());
  1022. bsp_simplices.push_back(bsp_simplex);
  1023. }
  1024. //#define DEBUG_SIMPLICES_AS_OBJ_FILE
  1025. #ifdef DEBUG_SIMPLICES_AS_OBJ_FILE
  1026. {
  1027. Ref<FileAccess> f = FileAccess::open("res://bsp.obj", FileAccess::WRITE);
  1028. for (uint32_t i = 0; i < bsp_simplices.size(); i++) {
  1029. f->store_line("o Simplex" + itos(i));
  1030. for (int j = 0; j < 4; j++) {
  1031. f->store_line(vformat("v %f %f %f", points[bsp_simplices[i].vertices[j]].x, points[bsp_simplices[i].vertices[j]].y, points[bsp_simplices[i].vertices[j]].z));
  1032. }
  1033. static const int face_order[4][3] = {
  1034. { 1, 2, 3 },
  1035. { 1, 3, 4 },
  1036. { 1, 2, 4 },
  1037. { 2, 3, 4 }
  1038. };
  1039. for (int j = 0; j < 4; j++) {
  1040. f->store_line(vformat("f %d %d %d", 4 * i + face_order[j][0], 4 * i + face_order[j][1], 4 * i + face_order[j][2]));
  1041. }
  1042. }
  1043. }
  1044. #endif
  1045. LocalVector<BSPNode> bsp_nodes;
  1046. LocalVector<int32_t> planes_tested;
  1047. planes_tested.resize(bsp_planes.size());
  1048. for (int &index : planes_tested) {
  1049. index = 0x7FFFFFFF;
  1050. }
  1051. if (p_bake_step) {
  1052. p_bake_step(0.9, RTR("Generating Probe Acceleration Structures"), p_bake_userdata, true);
  1053. }
  1054. _compute_bsp_tree(points, bsp_planes, planes_tested, bsp_simplices, bsp_simplex_indices, bsp_nodes);
  1055. PackedInt32Array bsp_array;
  1056. bsp_array.resize(bsp_nodes.size() * 6); // six 32 bits values used for each BSP node
  1057. {
  1058. float *fptr = (float *)bsp_array.ptrw();
  1059. int32_t *iptr = (int32_t *)bsp_array.ptrw();
  1060. for (uint32_t i = 0; i < bsp_nodes.size(); i++) {
  1061. fptr[i * 6 + 0] = bsp_nodes[i].plane.normal.x;
  1062. fptr[i * 6 + 1] = bsp_nodes[i].plane.normal.y;
  1063. fptr[i * 6 + 2] = bsp_nodes[i].plane.normal.z;
  1064. fptr[i * 6 + 3] = bsp_nodes[i].plane.d;
  1065. iptr[i * 6 + 4] = bsp_nodes[i].over;
  1066. iptr[i * 6 + 5] = bsp_nodes[i].under;
  1067. }
  1068. //#define DEBUG_BSP_TREE
  1069. #ifdef DEBUG_BSP_TREE
  1070. Ref<FileAccess> f = FileAccess::open("res://bsp.txt", FileAccess::WRITE);
  1071. for (uint32_t i = 0; i < bsp_nodes.size(); i++) {
  1072. f->store_line(itos(i) + " - plane: " + bsp_nodes[i].plane + " over: " + itos(bsp_nodes[i].over) + " under: " + itos(bsp_nodes[i].under));
  1073. }
  1074. #endif
  1075. }
  1076. /* Obtain the colors from the images, they will be re-created as cubemaps on the server, depending on the driver */
  1077. gi_data->set_capture_data(bounds, interior, points, sh, tetrahedrons, bsp_array, exposure_normalization);
  1078. /* Compute a BSP tree of the simplices, so it's easy to find the exact one */
  1079. }
  1080. gi_data->set_path(p_image_data_path);
  1081. Error err = ResourceSaver::save(gi_data);
  1082. if (err != OK) {
  1083. return BAKE_ERROR_CANT_CREATE_IMAGE;
  1084. }
  1085. set_light_data(gi_data);
  1086. return BAKE_ERROR_OK;
  1087. }
  1088. void LightmapGI::_notification(int p_what) {
  1089. switch (p_what) {
  1090. case NOTIFICATION_POST_ENTER_TREE: {
  1091. if (light_data.is_valid()) {
  1092. _assign_lightmaps();
  1093. }
  1094. } break;
  1095. case NOTIFICATION_EXIT_TREE: {
  1096. if (light_data.is_valid()) {
  1097. _clear_lightmaps();
  1098. }
  1099. } break;
  1100. }
  1101. }
  1102. void LightmapGI::_assign_lightmaps() {
  1103. ERR_FAIL_COND(!light_data.is_valid());
  1104. for (int i = 0; i < light_data->get_user_count(); i++) {
  1105. Node *node = get_node(light_data->get_user_path(i));
  1106. int instance_idx = light_data->get_user_sub_instance(i);
  1107. if (instance_idx >= 0) {
  1108. RID instance_id = node->call("get_bake_mesh_instance", instance_idx);
  1109. if (instance_id.is_valid()) {
  1110. RS::get_singleton()->instance_geometry_set_lightmap(instance_id, get_instance(), light_data->get_user_lightmap_uv_scale(i), light_data->get_user_lightmap_slice_index(i));
  1111. }
  1112. } else {
  1113. VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node);
  1114. ERR_CONTINUE(!vi);
  1115. RS::get_singleton()->instance_geometry_set_lightmap(vi->get_instance(), get_instance(), light_data->get_user_lightmap_uv_scale(i), light_data->get_user_lightmap_slice_index(i));
  1116. }
  1117. }
  1118. }
  1119. void LightmapGI::_clear_lightmaps() {
  1120. ERR_FAIL_COND(!light_data.is_valid());
  1121. for (int i = 0; i < light_data->get_user_count(); i++) {
  1122. Node *node = get_node(light_data->get_user_path(i));
  1123. int instance_idx = light_data->get_user_sub_instance(i);
  1124. if (instance_idx >= 0) {
  1125. RID instance_id = node->call("get_bake_mesh_instance", instance_idx);
  1126. if (instance_id.is_valid()) {
  1127. RS::get_singleton()->instance_geometry_set_lightmap(instance_id, RID(), Rect2(), 0);
  1128. }
  1129. } else {
  1130. VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node);
  1131. ERR_CONTINUE(!vi);
  1132. RS::get_singleton()->instance_geometry_set_lightmap(vi->get_instance(), RID(), Rect2(), 0);
  1133. }
  1134. }
  1135. }
  1136. void LightmapGI::set_light_data(const Ref<LightmapGIData> &p_data) {
  1137. if (light_data.is_valid()) {
  1138. if (is_inside_tree()) {
  1139. _clear_lightmaps();
  1140. }
  1141. set_base(RID());
  1142. }
  1143. light_data = p_data;
  1144. if (light_data.is_valid()) {
  1145. set_base(light_data->get_rid());
  1146. if (is_inside_tree()) {
  1147. _assign_lightmaps();
  1148. }
  1149. }
  1150. update_gizmos();
  1151. }
  1152. Ref<LightmapGIData> LightmapGI::get_light_data() const {
  1153. return light_data;
  1154. }
  1155. void LightmapGI::set_bake_quality(BakeQuality p_quality) {
  1156. bake_quality = p_quality;
  1157. }
  1158. LightmapGI::BakeQuality LightmapGI::get_bake_quality() const {
  1159. return bake_quality;
  1160. }
  1161. AABB LightmapGI::get_aabb() const {
  1162. return AABB();
  1163. }
  1164. void LightmapGI::set_use_denoiser(bool p_enable) {
  1165. use_denoiser = p_enable;
  1166. notify_property_list_changed();
  1167. }
  1168. bool LightmapGI::is_using_denoiser() const {
  1169. return use_denoiser;
  1170. }
  1171. void LightmapGI::set_denoiser_strength(float p_denoiser_strength) {
  1172. denoiser_strength = p_denoiser_strength;
  1173. }
  1174. float LightmapGI::get_denoiser_strength() const {
  1175. return denoiser_strength;
  1176. }
  1177. void LightmapGI::set_directional(bool p_enable) {
  1178. directional = p_enable;
  1179. }
  1180. bool LightmapGI::is_directional() const {
  1181. return directional;
  1182. }
  1183. void LightmapGI::set_use_texture_for_bounces(bool p_enable) {
  1184. use_texture_for_bounces = p_enable;
  1185. }
  1186. bool LightmapGI::is_using_texture_for_bounces() const {
  1187. return use_texture_for_bounces;
  1188. }
  1189. void LightmapGI::set_interior(bool p_enable) {
  1190. interior = p_enable;
  1191. }
  1192. bool LightmapGI::is_interior() const {
  1193. return interior;
  1194. }
  1195. void LightmapGI::set_environment_mode(EnvironmentMode p_mode) {
  1196. environment_mode = p_mode;
  1197. notify_property_list_changed();
  1198. }
  1199. LightmapGI::EnvironmentMode LightmapGI::get_environment_mode() const {
  1200. return environment_mode;
  1201. }
  1202. void LightmapGI::set_environment_custom_sky(const Ref<Sky> &p_sky) {
  1203. environment_custom_sky = p_sky;
  1204. }
  1205. Ref<Sky> LightmapGI::get_environment_custom_sky() const {
  1206. return environment_custom_sky;
  1207. }
  1208. void LightmapGI::set_environment_custom_color(const Color &p_color) {
  1209. environment_custom_color = p_color;
  1210. }
  1211. Color LightmapGI::get_environment_custom_color() const {
  1212. return environment_custom_color;
  1213. }
  1214. void LightmapGI::set_environment_custom_energy(float p_energy) {
  1215. environment_custom_energy = p_energy;
  1216. }
  1217. float LightmapGI::get_environment_custom_energy() const {
  1218. return environment_custom_energy;
  1219. }
  1220. void LightmapGI::set_bounces(int p_bounces) {
  1221. ERR_FAIL_COND(p_bounces < 0 || p_bounces > 16);
  1222. bounces = p_bounces;
  1223. }
  1224. int LightmapGI::get_bounces() const {
  1225. return bounces;
  1226. }
  1227. void LightmapGI::set_bounce_indirect_energy(float p_indirect_energy) {
  1228. ERR_FAIL_COND(p_indirect_energy < 0.0);
  1229. bounce_indirect_energy = p_indirect_energy;
  1230. }
  1231. float LightmapGI::get_bounce_indirect_energy() const {
  1232. return bounce_indirect_energy;
  1233. }
  1234. void LightmapGI::set_bias(float p_bias) {
  1235. ERR_FAIL_COND(p_bias < 0.00001);
  1236. bias = p_bias;
  1237. }
  1238. float LightmapGI::get_bias() const {
  1239. return bias;
  1240. }
  1241. void LightmapGI::set_max_texture_size(int p_size) {
  1242. ERR_FAIL_COND_MSG(p_size < 2048, vformat("The LightmapGI maximum texture size supplied (%d) is too small. The minimum allowed value is 2048.", p_size));
  1243. ERR_FAIL_COND_MSG(p_size > 16384, vformat("The LightmapGI maximum texture size supplied (%d) is too large. The maximum allowed value is 16384.", p_size));
  1244. max_texture_size = p_size;
  1245. }
  1246. int LightmapGI::get_max_texture_size() const {
  1247. return max_texture_size;
  1248. }
  1249. void LightmapGI::set_generate_probes(GenerateProbes p_generate_probes) {
  1250. gen_probes = p_generate_probes;
  1251. }
  1252. LightmapGI::GenerateProbes LightmapGI::get_generate_probes() const {
  1253. return gen_probes;
  1254. }
  1255. void LightmapGI::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) {
  1256. camera_attributes = p_camera_attributes;
  1257. }
  1258. Ref<CameraAttributes> LightmapGI::get_camera_attributes() const {
  1259. return camera_attributes;
  1260. }
  1261. PackedStringArray LightmapGI::get_configuration_warnings() const {
  1262. PackedStringArray warnings = Node::get_configuration_warnings();
  1263. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  1264. warnings.push_back(RTR("LightmapGI nodes are not supported when using the GL Compatibility backend yet. Support will be added in a future release."));
  1265. return warnings;
  1266. }
  1267. return warnings;
  1268. }
  1269. void LightmapGI::_validate_property(PropertyInfo &p_property) const {
  1270. if (p_property.name == "environment_custom_sky" && environment_mode != ENVIRONMENT_MODE_CUSTOM_SKY) {
  1271. p_property.usage = PROPERTY_USAGE_NONE;
  1272. }
  1273. if (p_property.name == "environment_custom_color" && environment_mode != ENVIRONMENT_MODE_CUSTOM_COLOR) {
  1274. p_property.usage = PROPERTY_USAGE_NONE;
  1275. }
  1276. if (p_property.name == "environment_custom_energy" && environment_mode != ENVIRONMENT_MODE_CUSTOM_COLOR && environment_mode != ENVIRONMENT_MODE_CUSTOM_SKY) {
  1277. p_property.usage = PROPERTY_USAGE_NONE;
  1278. }
  1279. if (p_property.name == "denoiser_strength" && !use_denoiser) {
  1280. p_property.usage = PROPERTY_USAGE_NONE;
  1281. }
  1282. }
  1283. void LightmapGI::_bind_methods() {
  1284. ClassDB::bind_method(D_METHOD("set_light_data", "data"), &LightmapGI::set_light_data);
  1285. ClassDB::bind_method(D_METHOD("get_light_data"), &LightmapGI::get_light_data);
  1286. ClassDB::bind_method(D_METHOD("set_bake_quality", "bake_quality"), &LightmapGI::set_bake_quality);
  1287. ClassDB::bind_method(D_METHOD("get_bake_quality"), &LightmapGI::get_bake_quality);
  1288. ClassDB::bind_method(D_METHOD("set_bounces", "bounces"), &LightmapGI::set_bounces);
  1289. ClassDB::bind_method(D_METHOD("get_bounces"), &LightmapGI::get_bounces);
  1290. ClassDB::bind_method(D_METHOD("set_bounce_indirect_energy", "bounce_indirect_energy"), &LightmapGI::set_bounce_indirect_energy);
  1291. ClassDB::bind_method(D_METHOD("get_bounce_indirect_energy"), &LightmapGI::get_bounce_indirect_energy);
  1292. ClassDB::bind_method(D_METHOD("set_generate_probes", "subdivision"), &LightmapGI::set_generate_probes);
  1293. ClassDB::bind_method(D_METHOD("get_generate_probes"), &LightmapGI::get_generate_probes);
  1294. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &LightmapGI::set_bias);
  1295. ClassDB::bind_method(D_METHOD("get_bias"), &LightmapGI::get_bias);
  1296. ClassDB::bind_method(D_METHOD("set_environment_mode", "mode"), &LightmapGI::set_environment_mode);
  1297. ClassDB::bind_method(D_METHOD("get_environment_mode"), &LightmapGI::get_environment_mode);
  1298. ClassDB::bind_method(D_METHOD("set_environment_custom_sky", "sky"), &LightmapGI::set_environment_custom_sky);
  1299. ClassDB::bind_method(D_METHOD("get_environment_custom_sky"), &LightmapGI::get_environment_custom_sky);
  1300. ClassDB::bind_method(D_METHOD("set_environment_custom_color", "color"), &LightmapGI::set_environment_custom_color);
  1301. ClassDB::bind_method(D_METHOD("get_environment_custom_color"), &LightmapGI::get_environment_custom_color);
  1302. ClassDB::bind_method(D_METHOD("set_environment_custom_energy", "energy"), &LightmapGI::set_environment_custom_energy);
  1303. ClassDB::bind_method(D_METHOD("get_environment_custom_energy"), &LightmapGI::get_environment_custom_energy);
  1304. ClassDB::bind_method(D_METHOD("set_max_texture_size", "max_texture_size"), &LightmapGI::set_max_texture_size);
  1305. ClassDB::bind_method(D_METHOD("get_max_texture_size"), &LightmapGI::get_max_texture_size);
  1306. ClassDB::bind_method(D_METHOD("set_use_denoiser", "use_denoiser"), &LightmapGI::set_use_denoiser);
  1307. ClassDB::bind_method(D_METHOD("is_using_denoiser"), &LightmapGI::is_using_denoiser);
  1308. ClassDB::bind_method(D_METHOD("set_denoiser_strength", "denoiser_strength"), &LightmapGI::set_denoiser_strength);
  1309. ClassDB::bind_method(D_METHOD("get_denoiser_strength"), &LightmapGI::get_denoiser_strength);
  1310. ClassDB::bind_method(D_METHOD("set_interior", "enable"), &LightmapGI::set_interior);
  1311. ClassDB::bind_method(D_METHOD("is_interior"), &LightmapGI::is_interior);
  1312. ClassDB::bind_method(D_METHOD("set_directional", "directional"), &LightmapGI::set_directional);
  1313. ClassDB::bind_method(D_METHOD("is_directional"), &LightmapGI::is_directional);
  1314. ClassDB::bind_method(D_METHOD("set_use_texture_for_bounces", "use_texture_for_bounces"), &LightmapGI::set_use_texture_for_bounces);
  1315. ClassDB::bind_method(D_METHOD("is_using_texture_for_bounces"), &LightmapGI::is_using_texture_for_bounces);
  1316. ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &LightmapGI::set_camera_attributes);
  1317. ClassDB::bind_method(D_METHOD("get_camera_attributes"), &LightmapGI::get_camera_attributes);
  1318. // ClassDB::bind_method(D_METHOD("bake", "from_node"), &LightmapGI::bake, DEFVAL(Variant()));
  1319. ADD_GROUP("Tweaks", "");
  1320. ADD_PROPERTY(PropertyInfo(Variant::INT, "quality", PROPERTY_HINT_ENUM, "Low,Medium,High,Ultra"), "set_bake_quality", "get_bake_quality");
  1321. ADD_PROPERTY(PropertyInfo(Variant::INT, "bounces", PROPERTY_HINT_RANGE, "0,6,1,or_greater"), "set_bounces", "get_bounces");
  1322. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bounce_indirect_energy", PROPERTY_HINT_RANGE, "0,2,0.01"), "set_bounce_indirect_energy", "get_bounce_indirect_energy");
  1323. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "directional"), "set_directional", "is_directional");
  1324. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_texture_for_bounces"), "set_use_texture_for_bounces", "is_using_texture_for_bounces");
  1325. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior");
  1326. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_denoiser"), "set_use_denoiser", "is_using_denoiser");
  1327. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "denoiser_strength", PROPERTY_HINT_RANGE, "0.001,0.2,0.001,or_greater"), "set_denoiser_strength", "get_denoiser_strength");
  1328. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0.00001,0.1,0.00001,or_greater"), "set_bias", "get_bias");
  1329. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_texture_size", PROPERTY_HINT_RANGE, "2048,16384,1"), "set_max_texture_size", "get_max_texture_size");
  1330. ADD_GROUP("Environment", "environment_");
  1331. ADD_PROPERTY(PropertyInfo(Variant::INT, "environment_mode", PROPERTY_HINT_ENUM, "Disabled,Scene,Custom Sky,Custom Color"), "set_environment_mode", "get_environment_mode");
  1332. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment_custom_sky", PROPERTY_HINT_RESOURCE_TYPE, "Sky"), "set_environment_custom_sky", "get_environment_custom_sky");
  1333. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "environment_custom_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_environment_custom_color", "get_environment_custom_color");
  1334. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "environment_custom_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_environment_custom_energy", "get_environment_custom_energy");
  1335. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");
  1336. ADD_GROUP("Gen Probes", "generate_probes_");
  1337. ADD_PROPERTY(PropertyInfo(Variant::INT, "generate_probes_subdiv", PROPERTY_HINT_ENUM, "Disabled,4,8,16,32"), "set_generate_probes", "get_generate_probes");
  1338. ADD_GROUP("Data", "");
  1339. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_data", PROPERTY_HINT_RESOURCE_TYPE, "LightmapGIData"), "set_light_data", "get_light_data");
  1340. BIND_ENUM_CONSTANT(BAKE_QUALITY_LOW);
  1341. BIND_ENUM_CONSTANT(BAKE_QUALITY_MEDIUM);
  1342. BIND_ENUM_CONSTANT(BAKE_QUALITY_HIGH);
  1343. BIND_ENUM_CONSTANT(BAKE_QUALITY_ULTRA);
  1344. BIND_ENUM_CONSTANT(GENERATE_PROBES_DISABLED);
  1345. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_4);
  1346. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_8);
  1347. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_16);
  1348. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_32);
  1349. BIND_ENUM_CONSTANT(BAKE_ERROR_OK);
  1350. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SCENE_ROOT);
  1351. BIND_ENUM_CONSTANT(BAKE_ERROR_FOREIGN_DATA);
  1352. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_LIGHTMAPPER);
  1353. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SAVE_PATH);
  1354. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_MESHES);
  1355. BIND_ENUM_CONSTANT(BAKE_ERROR_MESHES_INVALID);
  1356. BIND_ENUM_CONSTANT(BAKE_ERROR_CANT_CREATE_IMAGE);
  1357. BIND_ENUM_CONSTANT(BAKE_ERROR_USER_ABORTED);
  1358. BIND_ENUM_CONSTANT(BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL);
  1359. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_DISABLED);
  1360. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_SCENE);
  1361. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_CUSTOM_SKY);
  1362. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_CUSTOM_COLOR);
  1363. }
  1364. LightmapGI::LightmapGI() {
  1365. }