lightmap_gi.cpp 53 KB

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