lightmap_gi.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576
  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/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. ERR_FAIL_COND_MSG(texture.is_null(), vformat("Invalid TextureLayered at index %d.", i));
  95. for (int j = 0; j < texture->get_layers(); j++) {
  96. images.push_back(texture->get_layer_data(j));
  97. }
  98. }
  99. Ref<Texture2DArray> combined_texture;
  100. combined_texture.instantiate();
  101. combined_texture->create_from_images(images);
  102. set_light_texture(combined_texture);
  103. }
  104. }
  105. Array LightmapGIData::_get_light_textures_data() const {
  106. Array ret;
  107. if (light_texture.is_null() || light_texture->get_layers() == 0) {
  108. return ret;
  109. }
  110. Vector<Ref<Image>> images;
  111. for (int i = 0; i < light_texture->get_layers(); i++) {
  112. images.push_back(light_texture->get_layer_data(i));
  113. }
  114. int slice_count = images.size();
  115. int slice_width = images[0]->get_width();
  116. int slice_height = images[0]->get_height();
  117. int slices_per_texture = Image::MAX_HEIGHT / slice_height;
  118. int texture_count = Math::ceil(slice_count / (float)slices_per_texture);
  119. ret.resize(texture_count);
  120. String base_name = get_path().get_basename();
  121. int last_count = slice_count % slices_per_texture;
  122. for (int i = 0; i < texture_count; i++) {
  123. int texture_slice_count = (i == texture_count - 1 && last_count != 0) ? last_count : slices_per_texture;
  124. Ref<Image> texture_image = Image::create_empty(slice_width, slice_height * texture_slice_count, false, images[0]->get_format());
  125. for (int j = 0; j < texture_slice_count; j++) {
  126. texture_image->blit_rect(images[i * slices_per_texture + j], Rect2i(0, 0, slice_width, slice_height), Point2i(0, slice_height * j));
  127. }
  128. String texture_path = texture_count > 1 ? base_name + "_" + itos(i) + ".exr" : base_name + ".exr";
  129. Ref<ConfigFile> config;
  130. config.instantiate();
  131. if (FileAccess::exists(texture_path + ".import")) {
  132. config->load(texture_path + ".import");
  133. }
  134. config->set_value("remap", "importer", "2d_array_texture");
  135. config->set_value("remap", "type", "CompressedTexture2DArray");
  136. if (!config->has_section_key("params", "compress/mode")) {
  137. // User may want another compression, so leave it be, but default to VRAM uncompressed.
  138. config->set_value("params", "compress/mode", 3);
  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. ERR_FAIL_NULL(RenderingServer::get_singleton());
  261. RS::get_singleton()->free(lightmap);
  262. }
  263. ///////////////////////////
  264. void LightmapGI::_find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes) {
  265. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_at_node);
  266. if (mi && mi->get_gi_mode() == GeometryInstance3D::GI_MODE_STATIC && mi->is_visible_in_tree()) {
  267. Ref<Mesh> mesh = mi->get_mesh();
  268. if (mesh.is_valid()) {
  269. bool all_have_uv2_and_normal = true;
  270. bool surfaces_found = false;
  271. for (int i = 0; i < mesh->get_surface_count(); i++) {
  272. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  273. continue;
  274. }
  275. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_TEX_UV2)) {
  276. all_have_uv2_and_normal = false;
  277. break;
  278. }
  279. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_NORMAL)) {
  280. all_have_uv2_and_normal = false;
  281. break;
  282. }
  283. surfaces_found = true;
  284. }
  285. if (surfaces_found && all_have_uv2_and_normal) {
  286. //READY TO BAKE! size hint could be computed if not found, actually..
  287. MeshesFound mf;
  288. mf.xform = get_global_transform().affine_inverse() * mi->get_global_transform();
  289. mf.node_path = get_path_to(mi);
  290. mf.subindex = -1;
  291. mf.mesh = mesh;
  292. static const int lightmap_scale[GeometryInstance3D::LIGHTMAP_SCALE_MAX] = { 1, 2, 4, 8 };
  293. mf.lightmap_scale = lightmap_scale[mi->get_lightmap_scale()];
  294. Ref<Material> all_override = mi->get_material_override();
  295. for (int i = 0; i < mesh->get_surface_count(); i++) {
  296. if (all_override.is_valid()) {
  297. mf.overrides.push_back(all_override);
  298. } else {
  299. mf.overrides.push_back(mi->get_surface_override_material(i));
  300. }
  301. }
  302. meshes.push_back(mf);
  303. }
  304. }
  305. }
  306. Node3D *s = Object::cast_to<Node3D>(p_at_node);
  307. if (!mi && s) {
  308. Array bmeshes = p_at_node->call("get_bake_bmeshes");
  309. if (bmeshes.size() && (bmeshes.size() & 1) == 0) {
  310. Transform3D xf = get_global_transform().affine_inverse() * s->get_global_transform();
  311. for (int i = 0; i < bmeshes.size(); i += 2) {
  312. Ref<Mesh> mesh = bmeshes[i];
  313. if (!mesh.is_valid()) {
  314. continue;
  315. }
  316. MeshesFound mf;
  317. Transform3D mesh_xf = bmeshes[i + 1];
  318. mf.xform = xf * mesh_xf;
  319. mf.node_path = get_path_to(s);
  320. mf.subindex = i / 2;
  321. mf.lightmap_scale = 1;
  322. mf.mesh = mesh;
  323. meshes.push_back(mf);
  324. }
  325. }
  326. }
  327. Light3D *light = Object::cast_to<Light3D>(p_at_node);
  328. if (light && light->get_bake_mode() != Light3D::BAKE_DISABLED) {
  329. LightsFound lf;
  330. lf.xform = get_global_transform().affine_inverse() * light->get_global_transform();
  331. lf.light = light;
  332. lights.push_back(lf);
  333. }
  334. LightmapProbe *probe = Object::cast_to<LightmapProbe>(p_at_node);
  335. if (probe) {
  336. Transform3D xf = get_global_transform().affine_inverse() * probe->get_global_transform();
  337. probes.push_back(xf.origin);
  338. }
  339. for (int i = 0; i < p_at_node->get_child_count(); i++) {
  340. Node *child = p_at_node->get_child(i);
  341. if (!child->get_owner()) {
  342. continue; //maybe a helper
  343. }
  344. _find_meshes_and_lights(child, meshes, lights, probes);
  345. }
  346. }
  347. 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 {
  348. int over = 0;
  349. int under = 0;
  350. const BSPSimplex &s = p_simplices[p_simplex];
  351. for (int i = 0; i < 4; i++) {
  352. const Vector3 v = p_points[s.vertices[i]];
  353. if (p_plane.has_point(v)) {
  354. // Coplanar.
  355. } else if (p_plane.is_point_over(v)) {
  356. over++;
  357. } else {
  358. under++;
  359. }
  360. }
  361. 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
  362. if (under == 0) {
  363. return 1; // all over
  364. } else if (over == 0) {
  365. return -1; // all under
  366. } else {
  367. return 0; // crossing
  368. }
  369. }
  370. //#define DEBUG_BSP
  371. 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) {
  372. //if we reach here, it means there is more than one simplex
  373. int32_t node_index = (int32_t)bsp_nodes.size();
  374. bsp_nodes.push_back(BSPNode());
  375. //test with all the simplex planes
  376. Plane best_plane;
  377. float best_plane_score = -1.0;
  378. for (const int idx : p_simplex_indices) {
  379. const BSPSimplex &s = p_simplices[idx];
  380. for (int j = 0; j < 4; j++) {
  381. uint32_t plane_index = s.planes[j];
  382. if (planes_tested[plane_index] == node_index) {
  383. continue; //tested this plane already
  384. }
  385. planes_tested[plane_index] = node_index;
  386. static const int face_order[4][3] = {
  387. { 0, 1, 2 },
  388. { 0, 2, 3 },
  389. { 0, 1, 3 },
  390. { 1, 2, 3 }
  391. };
  392. // despite getting rid of plane duplicates, we should still use here the actual plane to avoid numerical error
  393. // from thinking this same simplex is intersecting rather than on a side
  394. Vector3 v0 = p_points[s.vertices[face_order[j][0]]];
  395. Vector3 v1 = p_points[s.vertices[face_order[j][1]]];
  396. Vector3 v2 = p_points[s.vertices[face_order[j][2]]];
  397. Plane plane(v0, v1, v2);
  398. //test with all the simplices
  399. int over_count = 0;
  400. int under_count = 0;
  401. for (const int &index : p_simplex_indices) {
  402. int side = _bsp_get_simplex_side(p_points, p_simplices, plane, index);
  403. if (side == -2) {
  404. continue; //this simplex is invalid, skip for now
  405. } else if (side < 0) {
  406. under_count++;
  407. } else if (side > 0) {
  408. over_count++;
  409. }
  410. }
  411. if (under_count == 0 && over_count == 0) {
  412. continue; //most likely precision issue with a flat simplex, do not try this plane
  413. }
  414. if (under_count > over_count) { //make sure under is always less than over, so we can compute the same ratio
  415. SWAP(under_count, over_count);
  416. }
  417. float score = 0; //by default, score is 0 (worst)
  418. if (over_count > 0) {
  419. //give score mainly based on ratio (under / over), this means that this plane is splitting simplices a lot, but its balanced
  420. score = float(under_count) / over_count;
  421. }
  422. //adjusting priority over least splits, probably not a great idea
  423. //score *= Math::sqrt(float(over_count + under_count) / p_simplex_indices.size()); //also multiply score
  424. if (score > best_plane_score) {
  425. best_plane = plane;
  426. best_plane_score = score;
  427. }
  428. }
  429. }
  430. LocalVector<int32_t> indices_over;
  431. LocalVector<int32_t> indices_under;
  432. //split again, but add to list
  433. for (const uint32_t index : p_simplex_indices) {
  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();
  635. if (lightmap_size == Size2i(0, 0)) {
  636. // TODO we should compute a size if no lightmap hint is set, as we did in 3.x.
  637. // For now set to basic size to avoid crash.
  638. lightmap_size = Size2i(64, 64);
  639. }
  640. lightmap_size *= mf.lightmap_scale;
  641. TypedArray<RID> overrides;
  642. overrides.resize(mf.overrides.size());
  643. for (int i = 0; i < mf.overrides.size(); i++) {
  644. if (mf.overrides[i].is_valid()) {
  645. overrides[i] = mf.overrides[i]->get_rid();
  646. }
  647. }
  648. TypedArray<Image> images = RS::get_singleton()->bake_render_uv2(mf.mesh->get_rid(), overrides, lightmap_size);
  649. ERR_FAIL_COND_V(images.is_empty(), BAKE_ERROR_CANT_CREATE_IMAGE);
  650. Ref<Image> albedo = images[RS::BAKE_CHANNEL_ALBEDO_ALPHA];
  651. Ref<Image> orm = images[RS::BAKE_CHANNEL_ORM];
  652. //multiply albedo by metal
  653. Lightmapper::MeshData md;
  654. {
  655. Dictionary d;
  656. d["path"] = mf.node_path;
  657. if (mf.subindex >= 0) {
  658. d["subindex"] = mf.subindex;
  659. }
  660. md.userdata = d;
  661. }
  662. {
  663. if (albedo->get_format() != Image::FORMAT_RGBA8) {
  664. albedo->convert(Image::FORMAT_RGBA8);
  665. }
  666. if (orm->get_format() != Image::FORMAT_RGBA8) {
  667. orm->convert(Image::FORMAT_RGBA8);
  668. }
  669. Vector<uint8_t> albedo_alpha = albedo->get_data();
  670. Vector<uint8_t> orm_data = orm->get_data();
  671. Vector<uint8_t> albedom;
  672. uint32_t len = albedo_alpha.size();
  673. albedom.resize(len);
  674. const uint8_t *r_aa = albedo_alpha.ptr();
  675. const uint8_t *r_orm = orm_data.ptr();
  676. uint8_t *w_albedo = albedom.ptrw();
  677. for (uint32_t i = 0; i < len; i += 4) {
  678. w_albedo[i + 0] = uint8_t(CLAMP(float(r_aa[i + 0]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  679. w_albedo[i + 1] = uint8_t(CLAMP(float(r_aa[i + 1]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  680. w_albedo[i + 2] = uint8_t(CLAMP(float(r_aa[i + 2]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  681. w_albedo[i + 3] = 255;
  682. }
  683. md.albedo_on_uv2.instantiate();
  684. md.albedo_on_uv2->set_data(lightmap_size.width, lightmap_size.height, false, Image::FORMAT_RGBA8, albedom);
  685. }
  686. md.emission_on_uv2 = images[RS::BAKE_CHANNEL_EMISSION];
  687. if (md.emission_on_uv2->get_format() != Image::FORMAT_RGBAH) {
  688. md.emission_on_uv2->convert(Image::FORMAT_RGBAH);
  689. }
  690. //get geometry
  691. Basis normal_xform = mf.xform.basis.inverse().transposed();
  692. for (int i = 0; i < mf.mesh->get_surface_count(); i++) {
  693. if (mf.mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  694. continue;
  695. }
  696. Array a = mf.mesh->surface_get_arrays(i);
  697. Vector<Vector3> vertices = a[Mesh::ARRAY_VERTEX];
  698. const Vector3 *vr = vertices.ptr();
  699. Vector<Vector2> uv = a[Mesh::ARRAY_TEX_UV2];
  700. const Vector2 *uvr = nullptr;
  701. Vector<Vector3> normals = a[Mesh::ARRAY_NORMAL];
  702. const Vector3 *nr = nullptr;
  703. Vector<int> index = a[Mesh::ARRAY_INDEX];
  704. ERR_CONTINUE(uv.size() == 0);
  705. ERR_CONTINUE(normals.size() == 0);
  706. uvr = uv.ptr();
  707. nr = normals.ptr();
  708. int facecount;
  709. const int *ir = nullptr;
  710. if (index.size()) {
  711. facecount = index.size() / 3;
  712. ir = index.ptr();
  713. } else {
  714. facecount = vertices.size() / 3;
  715. }
  716. for (int j = 0; j < facecount; j++) {
  717. uint32_t vidx[3];
  718. if (ir) {
  719. for (int k = 0; k < 3; k++) {
  720. vidx[k] = ir[j * 3 + k];
  721. }
  722. } else {
  723. for (int k = 0; k < 3; k++) {
  724. vidx[k] = j * 3 + k;
  725. }
  726. }
  727. for (int k = 0; k < 3; k++) {
  728. Vector3 v = mf.xform.xform(vr[vidx[k]]);
  729. if (bounds == AABB()) {
  730. bounds.position = v;
  731. } else {
  732. bounds.expand_to(v);
  733. }
  734. md.points.push_back(v);
  735. md.uv2.push_back(uvr[vidx[k]]);
  736. md.normal.push_back(normal_xform.xform(nr[vidx[k]]).normalized());
  737. }
  738. }
  739. }
  740. mesh_data.push_back(md);
  741. }
  742. }
  743. /* STEP 2, CREATE PROBES */
  744. if (p_bake_step) {
  745. p_bake_step(0.3, RTR("Creating probes"), p_bake_userdata, true);
  746. }
  747. //bounds need to include the user probes
  748. for (int i = 0; i < probes_found.size(); i++) {
  749. bounds.expand_to(probes_found[i]);
  750. }
  751. bounds.grow_by(bounds.size.length() * 0.001);
  752. if (gen_probes == GENERATE_PROBES_DISABLED) {
  753. // generate 8 probes on bound endpoints
  754. for (int i = 0; i < 8; i++) {
  755. probes_found.push_back(bounds.get_endpoint(i));
  756. }
  757. } else {
  758. // detect probes from geometry
  759. static const int subdiv_values[6] = { 0, 4, 8, 16, 32 };
  760. int subdiv = subdiv_values[gen_probes];
  761. float subdiv_cell_size;
  762. Vector3i bound_limit;
  763. {
  764. int longest_axis = bounds.get_longest_axis_index();
  765. subdiv_cell_size = bounds.size[longest_axis] / subdiv;
  766. int axis_n1 = (longest_axis + 1) % 3;
  767. int axis_n2 = (longest_axis + 2) % 3;
  768. bound_limit[longest_axis] = subdiv;
  769. bound_limit[axis_n1] = int(Math::ceil(bounds.size[axis_n1] / subdiv_cell_size));
  770. bound_limit[axis_n2] = int(Math::ceil(bounds.size[axis_n2] / subdiv_cell_size));
  771. //compensate bounds
  772. bounds.size[axis_n1] = bound_limit[axis_n1] * subdiv_cell_size;
  773. bounds.size[axis_n2] = bound_limit[axis_n2] * subdiv_cell_size;
  774. }
  775. GenProbesOctree octree;
  776. octree.size = subdiv;
  777. for (int i = 0; i < mesh_data.size(); i++) {
  778. if (p_bake_step) {
  779. float p = (float)(i) / mesh_data.size();
  780. p_bake_step(0.3 + p * 0.1, vformat(RTR("Creating probes from mesh %d/%d"), i, mesh_data.size()), p_bake_userdata, false);
  781. }
  782. for (int j = 0; j < mesh_data[i].points.size(); j += 3) {
  783. 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 };
  784. _plot_triangle_into_octree(&octree, subdiv_cell_size, points);
  785. }
  786. }
  787. LocalVector<Vector3> new_probe_positions;
  788. HashMap<Vector3i, bool> positions_used;
  789. for (uint32_t i = 0; i < 8; i++) { //insert bounding endpoints
  790. Vector3i pos;
  791. if (i & 1) {
  792. pos.x += bound_limit.x;
  793. }
  794. if (i & 2) {
  795. pos.y += bound_limit.y;
  796. }
  797. if (i & 4) {
  798. pos.z += bound_limit.z;
  799. }
  800. positions_used[pos] = true;
  801. Vector3 real_pos = bounds.position + Vector3(pos) * subdiv_cell_size; //use same formula for numerical stability
  802. new_probe_positions.push_back(real_pos);
  803. }
  804. //skip first level, since probes are always added at bounds endpoints anyway (code above this)
  805. for (int i = 0; i < 8; i++) {
  806. if (octree.children[i]) {
  807. _gen_new_positions_from_octree(octree.children[i], subdiv_cell_size, probes_found, new_probe_positions, positions_used, bounds);
  808. }
  809. }
  810. for (const Vector3 &position : new_probe_positions) {
  811. probes_found.push_back(position);
  812. }
  813. }
  814. // Add everything to lightmapper
  815. if (p_bake_step) {
  816. p_bake_step(0.4, RTR("Preparing Lightmapper"), p_bake_userdata, true);
  817. }
  818. {
  819. for (int i = 0; i < mesh_data.size(); i++) {
  820. lightmapper->add_mesh(mesh_data[i]);
  821. }
  822. for (int i = 0; i < lights_found.size(); i++) {
  823. Light3D *light = lights_found[i].light;
  824. Transform3D xf = lights_found[i].xform;
  825. Color linear_color = light->get_color().srgb_to_linear();
  826. float energy = light->get_param(Light3D::PARAM_ENERGY);
  827. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  828. energy *= light->get_param(Light3D::PARAM_INTENSITY);
  829. linear_color *= light->get_correlated_color().srgb_to_linear();
  830. }
  831. if (Object::cast_to<DirectionalLight3D>(light)) {
  832. DirectionalLight3D *l = Object::cast_to<DirectionalLight3D>(light);
  833. 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));
  834. } else if (Object::cast_to<OmniLight3D>(light)) {
  835. OmniLight3D *l = Object::cast_to<OmniLight3D>(light);
  836. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  837. energy *= (1.0 / (Math_PI * 4.0));
  838. }
  839. lightmapper->add_omni_light(light->get_bake_mode() == Light3D::BAKE_STATIC, xf.origin, linear_color, 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));
  840. } else if (Object::cast_to<SpotLight3D>(light)) {
  841. SpotLight3D *l = Object::cast_to<SpotLight3D>(light);
  842. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  843. energy *= (1.0 / Math_PI);
  844. }
  845. lightmapper->add_spot_light(light->get_bake_mode() == Light3D::BAKE_STATIC, xf.origin, -xf.basis.get_column(Vector3::AXIS_Z).normalized(), linear_color, 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));
  846. }
  847. }
  848. for (int i = 0; i < probes_found.size(); i++) {
  849. lightmapper->add_probe(probes_found[i]);
  850. }
  851. }
  852. Ref<Image> environment_image;
  853. Basis environment_transform;
  854. // Add everything to lightmapper
  855. if (environment_mode != ENVIRONMENT_MODE_DISABLED) {
  856. if (p_bake_step) {
  857. p_bake_step(4.1, RTR("Preparing Environment"), p_bake_userdata, true);
  858. }
  859. environment_transform = get_global_transform().basis;
  860. switch (environment_mode) {
  861. case ENVIRONMENT_MODE_DISABLED: {
  862. //nothing
  863. } break;
  864. case ENVIRONMENT_MODE_SCENE: {
  865. Ref<World3D> world = get_world_3d();
  866. if (world.is_valid()) {
  867. Ref<Environment> env = world->get_environment();
  868. if (env.is_null()) {
  869. env = world->get_fallback_environment();
  870. }
  871. if (env.is_valid()) {
  872. environment_image = RS::get_singleton()->environment_bake_panorama(env->get_rid(), true, Size2i(128, 64));
  873. }
  874. }
  875. } break;
  876. case ENVIRONMENT_MODE_CUSTOM_SKY: {
  877. if (environment_custom_sky.is_valid()) {
  878. environment_image = RS::get_singleton()->sky_bake_panorama(environment_custom_sky->get_rid(), environment_custom_energy, true, Size2i(128, 64));
  879. }
  880. } break;
  881. case ENVIRONMENT_MODE_CUSTOM_COLOR: {
  882. environment_image.instantiate();
  883. environment_image->initialize_data(128, 64, false, Image::FORMAT_RGBAF);
  884. Color c = environment_custom_color;
  885. c.r *= environment_custom_energy;
  886. c.g *= environment_custom_energy;
  887. c.b *= environment_custom_energy;
  888. environment_image->fill(c);
  889. } break;
  890. }
  891. }
  892. float exposure_normalization = 1.0;
  893. if (camera_attributes.is_valid()) {
  894. exposure_normalization = camera_attributes->get_exposure_multiplier();
  895. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  896. exposure_normalization = camera_attributes->calculate_exposure_normalization();
  897. }
  898. }
  899. 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);
  900. if (bake_err == Lightmapper::BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES) {
  901. return BAKE_ERROR_MESHES_INVALID;
  902. }
  903. /* POSTBAKE: Save Light Data */
  904. Ref<LightmapGIData> gi_data;
  905. if (get_light_data().is_valid()) {
  906. gi_data = get_light_data();
  907. set_light_data(Ref<LightmapGIData>()); //clear
  908. gi_data->clear();
  909. } else {
  910. gi_data.instantiate();
  911. }
  912. Ref<Texture2DArray> texture;
  913. {
  914. Vector<Ref<Image>> images;
  915. for (int i = 0; i < lightmapper->get_bake_texture_count(); i++) {
  916. images.push_back(lightmapper->get_bake_texture(i));
  917. }
  918. texture.instantiate();
  919. texture->create_from_images(images);
  920. }
  921. gi_data->set_light_texture(texture);
  922. gi_data->set_uses_spherical_harmonics(directional);
  923. for (int i = 0; i < lightmapper->get_bake_mesh_count(); i++) {
  924. Dictionary d = lightmapper->get_bake_mesh_userdata(i);
  925. NodePath np = d["path"];
  926. int32_t subindex = -1;
  927. if (d.has("subindex")) {
  928. subindex = d["subindex"];
  929. }
  930. Rect2 uv_scale = lightmapper->get_bake_mesh_uv_scale(i);
  931. int slice_index = lightmapper->get_bake_mesh_texture_slice(i);
  932. gi_data->add_user(np, uv_scale, slice_index, subindex);
  933. }
  934. {
  935. // create tetrahedrons
  936. Vector<Vector3> points;
  937. Vector<Color> sh;
  938. points.resize(lightmapper->get_bake_probe_count());
  939. sh.resize(lightmapper->get_bake_probe_count() * 9);
  940. for (int i = 0; i < lightmapper->get_bake_probe_count(); i++) {
  941. points.write[i] = lightmapper->get_bake_probe_point(i);
  942. Vector<Color> colors = lightmapper->get_bake_probe_sh(i);
  943. ERR_CONTINUE(colors.size() != 9);
  944. for (int j = 0; j < 9; j++) {
  945. sh.write[i * 9 + j] = colors[j];
  946. }
  947. }
  948. //Obtain solved simplices
  949. if (p_bake_step) {
  950. p_bake_step(0.8, RTR("Generating Probe Volumes"), p_bake_userdata, true);
  951. }
  952. Vector<Delaunay3D::OutputSimplex> solved_simplices = Delaunay3D::tetrahedralize(points);
  953. LocalVector<BSPSimplex> bsp_simplices;
  954. LocalVector<Plane> bsp_planes;
  955. LocalVector<int32_t> bsp_simplex_indices;
  956. PackedInt32Array tetrahedrons;
  957. for (int i = 0; i < solved_simplices.size(); i++) {
  958. //Prepare a special representation of the simplex, which uses a BSP Tree
  959. BSPSimplex bsp_simplex;
  960. for (int j = 0; j < 4; j++) {
  961. bsp_simplex.vertices[j] = solved_simplices[i].points[j];
  962. }
  963. for (int j = 0; j < 4; j++) {
  964. static const int face_order[4][3] = {
  965. { 0, 1, 2 },
  966. { 0, 2, 3 },
  967. { 0, 1, 3 },
  968. { 1, 2, 3 }
  969. };
  970. Vector3 a = points[solved_simplices[i].points[face_order[j][0]]];
  971. Vector3 b = points[solved_simplices[i].points[face_order[j][1]]];
  972. Vector3 c = points[solved_simplices[i].points[face_order[j][2]]];
  973. //store planes in an array, but ensure they are reused, to speed up processing
  974. Plane p(a, b, c);
  975. int plane_index = -1;
  976. for (uint32_t k = 0; k < bsp_planes.size(); k++) {
  977. if (bsp_planes[k].is_equal_approx_any_side(p)) {
  978. plane_index = k;
  979. break;
  980. }
  981. }
  982. if (plane_index == -1) {
  983. plane_index = bsp_planes.size();
  984. bsp_planes.push_back(p);
  985. }
  986. bsp_simplex.planes[j] = plane_index;
  987. //also fill simplex array
  988. tetrahedrons.push_back(solved_simplices[i].points[j]);
  989. }
  990. bsp_simplex_indices.push_back(bsp_simplices.size());
  991. bsp_simplices.push_back(bsp_simplex);
  992. }
  993. //#define DEBUG_SIMPLICES_AS_OBJ_FILE
  994. #ifdef DEBUG_SIMPLICES_AS_OBJ_FILE
  995. {
  996. Ref<FileAccess> f = FileAccess::open("res://bsp.obj", FileAccess::WRITE);
  997. for (uint32_t i = 0; i < bsp_simplices.size(); i++) {
  998. f->store_line("o Simplex" + itos(i));
  999. for (int j = 0; j < 4; j++) {
  1000. 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));
  1001. }
  1002. static const int face_order[4][3] = {
  1003. { 1, 2, 3 },
  1004. { 1, 3, 4 },
  1005. { 1, 2, 4 },
  1006. { 2, 3, 4 }
  1007. };
  1008. for (int j = 0; j < 4; j++) {
  1009. 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]));
  1010. }
  1011. }
  1012. }
  1013. #endif
  1014. LocalVector<BSPNode> bsp_nodes;
  1015. LocalVector<int32_t> planes_tested;
  1016. planes_tested.resize(bsp_planes.size());
  1017. for (int &index : planes_tested) {
  1018. index = 0x7FFFFFFF;
  1019. }
  1020. if (p_bake_step) {
  1021. p_bake_step(0.9, RTR("Generating Probe Acceleration Structures"), p_bake_userdata, true);
  1022. }
  1023. _compute_bsp_tree(points, bsp_planes, planes_tested, bsp_simplices, bsp_simplex_indices, bsp_nodes);
  1024. PackedInt32Array bsp_array;
  1025. bsp_array.resize(bsp_nodes.size() * 6); // six 32 bits values used for each BSP node
  1026. {
  1027. float *fptr = (float *)bsp_array.ptrw();
  1028. int32_t *iptr = (int32_t *)bsp_array.ptrw();
  1029. for (uint32_t i = 0; i < bsp_nodes.size(); i++) {
  1030. fptr[i * 6 + 0] = bsp_nodes[i].plane.normal.x;
  1031. fptr[i * 6 + 1] = bsp_nodes[i].plane.normal.y;
  1032. fptr[i * 6 + 2] = bsp_nodes[i].plane.normal.z;
  1033. fptr[i * 6 + 3] = bsp_nodes[i].plane.d;
  1034. iptr[i * 6 + 4] = bsp_nodes[i].over;
  1035. iptr[i * 6 + 5] = bsp_nodes[i].under;
  1036. }
  1037. //#define DEBUG_BSP_TREE
  1038. #ifdef DEBUG_BSP_TREE
  1039. Ref<FileAccess> f = FileAccess::open("res://bsp.txt", FileAccess::WRITE);
  1040. for (uint32_t i = 0; i < bsp_nodes.size(); i++) {
  1041. f->store_line(itos(i) + " - plane: " + bsp_nodes[i].plane + " over: " + itos(bsp_nodes[i].over) + " under: " + itos(bsp_nodes[i].under));
  1042. }
  1043. #endif
  1044. }
  1045. /* Obtain the colors from the images, they will be re-created as cubemaps on the server, depending on the driver */
  1046. gi_data->set_capture_data(bounds, interior, points, sh, tetrahedrons, bsp_array, exposure_normalization);
  1047. /* Compute a BSP tree of the simplices, so it's easy to find the exact one */
  1048. }
  1049. gi_data->set_path(p_image_data_path);
  1050. Error err = ResourceSaver::save(gi_data);
  1051. if (err != OK) {
  1052. return BAKE_ERROR_CANT_CREATE_IMAGE;
  1053. }
  1054. set_light_data(gi_data);
  1055. return BAKE_ERROR_OK;
  1056. }
  1057. void LightmapGI::_notification(int p_what) {
  1058. switch (p_what) {
  1059. case NOTIFICATION_POST_ENTER_TREE: {
  1060. if (light_data.is_valid()) {
  1061. _assign_lightmaps();
  1062. }
  1063. } break;
  1064. case NOTIFICATION_EXIT_TREE: {
  1065. if (light_data.is_valid()) {
  1066. _clear_lightmaps();
  1067. }
  1068. } break;
  1069. }
  1070. }
  1071. void LightmapGI::_assign_lightmaps() {
  1072. ERR_FAIL_COND(!light_data.is_valid());
  1073. for (int i = 0; i < light_data->get_user_count(); i++) {
  1074. Node *node = get_node(light_data->get_user_path(i));
  1075. int instance_idx = light_data->get_user_sub_instance(i);
  1076. if (instance_idx >= 0) {
  1077. RID instance_id = node->call("get_bake_mesh_instance", instance_idx);
  1078. if (instance_id.is_valid()) {
  1079. 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));
  1080. }
  1081. } else {
  1082. VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node);
  1083. ERR_CONTINUE(!vi);
  1084. 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));
  1085. }
  1086. }
  1087. }
  1088. void LightmapGI::_clear_lightmaps() {
  1089. ERR_FAIL_COND(!light_data.is_valid());
  1090. for (int i = 0; i < light_data->get_user_count(); i++) {
  1091. Node *node = get_node(light_data->get_user_path(i));
  1092. int instance_idx = light_data->get_user_sub_instance(i);
  1093. if (instance_idx >= 0) {
  1094. RID instance_id = node->call("get_bake_mesh_instance", instance_idx);
  1095. if (instance_id.is_valid()) {
  1096. RS::get_singleton()->instance_geometry_set_lightmap(instance_id, RID(), Rect2(), 0);
  1097. }
  1098. } else {
  1099. VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node);
  1100. ERR_CONTINUE(!vi);
  1101. RS::get_singleton()->instance_geometry_set_lightmap(vi->get_instance(), RID(), Rect2(), 0);
  1102. }
  1103. }
  1104. }
  1105. void LightmapGI::set_light_data(const Ref<LightmapGIData> &p_data) {
  1106. if (light_data.is_valid()) {
  1107. if (is_inside_tree()) {
  1108. _clear_lightmaps();
  1109. }
  1110. set_base(RID());
  1111. }
  1112. light_data = p_data;
  1113. if (light_data.is_valid()) {
  1114. set_base(light_data->get_rid());
  1115. if (is_inside_tree()) {
  1116. _assign_lightmaps();
  1117. }
  1118. }
  1119. update_gizmos();
  1120. }
  1121. Ref<LightmapGIData> LightmapGI::get_light_data() const {
  1122. return light_data;
  1123. }
  1124. void LightmapGI::set_bake_quality(BakeQuality p_quality) {
  1125. bake_quality = p_quality;
  1126. }
  1127. LightmapGI::BakeQuality LightmapGI::get_bake_quality() const {
  1128. return bake_quality;
  1129. }
  1130. AABB LightmapGI::get_aabb() const {
  1131. return AABB();
  1132. }
  1133. void LightmapGI::set_use_denoiser(bool p_enable) {
  1134. use_denoiser = p_enable;
  1135. }
  1136. bool LightmapGI::is_using_denoiser() const {
  1137. return use_denoiser;
  1138. }
  1139. void LightmapGI::set_directional(bool p_enable) {
  1140. directional = p_enable;
  1141. }
  1142. bool LightmapGI::is_directional() const {
  1143. return directional;
  1144. }
  1145. void LightmapGI::set_interior(bool p_enable) {
  1146. interior = p_enable;
  1147. }
  1148. bool LightmapGI::is_interior() const {
  1149. return interior;
  1150. }
  1151. void LightmapGI::set_environment_mode(EnvironmentMode p_mode) {
  1152. environment_mode = p_mode;
  1153. notify_property_list_changed();
  1154. }
  1155. LightmapGI::EnvironmentMode LightmapGI::get_environment_mode() const {
  1156. return environment_mode;
  1157. }
  1158. void LightmapGI::set_environment_custom_sky(const Ref<Sky> &p_sky) {
  1159. environment_custom_sky = p_sky;
  1160. }
  1161. Ref<Sky> LightmapGI::get_environment_custom_sky() const {
  1162. return environment_custom_sky;
  1163. }
  1164. void LightmapGI::set_environment_custom_color(const Color &p_color) {
  1165. environment_custom_color = p_color;
  1166. }
  1167. Color LightmapGI::get_environment_custom_color() const {
  1168. return environment_custom_color;
  1169. }
  1170. void LightmapGI::set_environment_custom_energy(float p_energy) {
  1171. environment_custom_energy = p_energy;
  1172. }
  1173. float LightmapGI::get_environment_custom_energy() const {
  1174. return environment_custom_energy;
  1175. }
  1176. void LightmapGI::set_bounces(int p_bounces) {
  1177. ERR_FAIL_COND(p_bounces < 0 || p_bounces > 16);
  1178. bounces = p_bounces;
  1179. }
  1180. int LightmapGI::get_bounces() const {
  1181. return bounces;
  1182. }
  1183. void LightmapGI::set_bias(float p_bias) {
  1184. ERR_FAIL_COND(p_bias < 0.00001);
  1185. bias = p_bias;
  1186. }
  1187. float LightmapGI::get_bias() const {
  1188. return bias;
  1189. }
  1190. void LightmapGI::set_max_texture_size(int p_size) {
  1191. 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));
  1192. 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));
  1193. max_texture_size = p_size;
  1194. }
  1195. int LightmapGI::get_max_texture_size() const {
  1196. return max_texture_size;
  1197. }
  1198. void LightmapGI::set_generate_probes(GenerateProbes p_generate_probes) {
  1199. gen_probes = p_generate_probes;
  1200. }
  1201. LightmapGI::GenerateProbes LightmapGI::get_generate_probes() const {
  1202. return gen_probes;
  1203. }
  1204. void LightmapGI::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) {
  1205. camera_attributes = p_camera_attributes;
  1206. }
  1207. Ref<CameraAttributes> LightmapGI::get_camera_attributes() const {
  1208. return camera_attributes;
  1209. }
  1210. PackedStringArray LightmapGI::get_configuration_warnings() const {
  1211. PackedStringArray warnings = Node::get_configuration_warnings();
  1212. if (OS::get_singleton()->get_current_rendering_method() == "gl_compatibility") {
  1213. warnings.push_back(RTR("LightmapGI nodes are not supported when using the GL Compatibility backend yet. Support will be added in a future release."));
  1214. return warnings;
  1215. }
  1216. return warnings;
  1217. }
  1218. void LightmapGI::_validate_property(PropertyInfo &p_property) const {
  1219. if (p_property.name == "environment_custom_sky" && environment_mode != ENVIRONMENT_MODE_CUSTOM_SKY) {
  1220. p_property.usage = PROPERTY_USAGE_NONE;
  1221. }
  1222. if (p_property.name == "environment_custom_color" && environment_mode != ENVIRONMENT_MODE_CUSTOM_COLOR) {
  1223. p_property.usage = PROPERTY_USAGE_NONE;
  1224. }
  1225. if (p_property.name == "environment_custom_energy" && environment_mode != ENVIRONMENT_MODE_CUSTOM_COLOR && environment_mode != ENVIRONMENT_MODE_CUSTOM_SKY) {
  1226. p_property.usage = PROPERTY_USAGE_NONE;
  1227. }
  1228. }
  1229. void LightmapGI::_bind_methods() {
  1230. ClassDB::bind_method(D_METHOD("set_light_data", "data"), &LightmapGI::set_light_data);
  1231. ClassDB::bind_method(D_METHOD("get_light_data"), &LightmapGI::get_light_data);
  1232. ClassDB::bind_method(D_METHOD("set_bake_quality", "bake_quality"), &LightmapGI::set_bake_quality);
  1233. ClassDB::bind_method(D_METHOD("get_bake_quality"), &LightmapGI::get_bake_quality);
  1234. ClassDB::bind_method(D_METHOD("set_bounces", "bounces"), &LightmapGI::set_bounces);
  1235. ClassDB::bind_method(D_METHOD("get_bounces"), &LightmapGI::get_bounces);
  1236. ClassDB::bind_method(D_METHOD("set_generate_probes", "subdivision"), &LightmapGI::set_generate_probes);
  1237. ClassDB::bind_method(D_METHOD("get_generate_probes"), &LightmapGI::get_generate_probes);
  1238. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &LightmapGI::set_bias);
  1239. ClassDB::bind_method(D_METHOD("get_bias"), &LightmapGI::get_bias);
  1240. ClassDB::bind_method(D_METHOD("set_environment_mode", "mode"), &LightmapGI::set_environment_mode);
  1241. ClassDB::bind_method(D_METHOD("get_environment_mode"), &LightmapGI::get_environment_mode);
  1242. ClassDB::bind_method(D_METHOD("set_environment_custom_sky", "sky"), &LightmapGI::set_environment_custom_sky);
  1243. ClassDB::bind_method(D_METHOD("get_environment_custom_sky"), &LightmapGI::get_environment_custom_sky);
  1244. ClassDB::bind_method(D_METHOD("set_environment_custom_color", "color"), &LightmapGI::set_environment_custom_color);
  1245. ClassDB::bind_method(D_METHOD("get_environment_custom_color"), &LightmapGI::get_environment_custom_color);
  1246. ClassDB::bind_method(D_METHOD("set_environment_custom_energy", "energy"), &LightmapGI::set_environment_custom_energy);
  1247. ClassDB::bind_method(D_METHOD("get_environment_custom_energy"), &LightmapGI::get_environment_custom_energy);
  1248. ClassDB::bind_method(D_METHOD("set_max_texture_size", "max_texture_size"), &LightmapGI::set_max_texture_size);
  1249. ClassDB::bind_method(D_METHOD("get_max_texture_size"), &LightmapGI::get_max_texture_size);
  1250. ClassDB::bind_method(D_METHOD("set_use_denoiser", "use_denoiser"), &LightmapGI::set_use_denoiser);
  1251. ClassDB::bind_method(D_METHOD("is_using_denoiser"), &LightmapGI::is_using_denoiser);
  1252. ClassDB::bind_method(D_METHOD("set_interior", "enable"), &LightmapGI::set_interior);
  1253. ClassDB::bind_method(D_METHOD("is_interior"), &LightmapGI::is_interior);
  1254. ClassDB::bind_method(D_METHOD("set_directional", "directional"), &LightmapGI::set_directional);
  1255. ClassDB::bind_method(D_METHOD("is_directional"), &LightmapGI::is_directional);
  1256. ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &LightmapGI::set_camera_attributes);
  1257. ClassDB::bind_method(D_METHOD("get_camera_attributes"), &LightmapGI::get_camera_attributes);
  1258. // ClassDB::bind_method(D_METHOD("bake", "from_node"), &LightmapGI::bake, DEFVAL(Variant()));
  1259. ADD_GROUP("Tweaks", "");
  1260. ADD_PROPERTY(PropertyInfo(Variant::INT, "quality", PROPERTY_HINT_ENUM, "Low,Medium,High,Ultra"), "set_bake_quality", "get_bake_quality");
  1261. ADD_PROPERTY(PropertyInfo(Variant::INT, "bounces", PROPERTY_HINT_RANGE, "0,16,1"), "set_bounces", "get_bounces");
  1262. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "directional"), "set_directional", "is_directional");
  1263. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior");
  1264. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_denoiser"), "set_use_denoiser", "is_using_denoiser");
  1265. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0.00001,0.1,0.00001,or_greater"), "set_bias", "get_bias");
  1266. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_texture_size", PROPERTY_HINT_RANGE, "2048,16384,1"), "set_max_texture_size", "get_max_texture_size");
  1267. ADD_GROUP("Environment", "environment_");
  1268. ADD_PROPERTY(PropertyInfo(Variant::INT, "environment_mode", PROPERTY_HINT_ENUM, "Disabled,Scene,Custom Sky,Custom Color"), "set_environment_mode", "get_environment_mode");
  1269. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment_custom_sky", PROPERTY_HINT_RESOURCE_TYPE, "Sky"), "set_environment_custom_sky", "get_environment_custom_sky");
  1270. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "environment_custom_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_environment_custom_color", "get_environment_custom_color");
  1271. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "environment_custom_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_environment_custom_energy", "get_environment_custom_energy");
  1272. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");
  1273. ADD_GROUP("Gen Probes", "generate_probes_");
  1274. ADD_PROPERTY(PropertyInfo(Variant::INT, "generate_probes_subdiv", PROPERTY_HINT_ENUM, "Disabled,4,8,16,32"), "set_generate_probes", "get_generate_probes");
  1275. ADD_GROUP("Data", "");
  1276. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_data", PROPERTY_HINT_RESOURCE_TYPE, "LightmapGIData"), "set_light_data", "get_light_data");
  1277. BIND_ENUM_CONSTANT(BAKE_QUALITY_LOW);
  1278. BIND_ENUM_CONSTANT(BAKE_QUALITY_MEDIUM);
  1279. BIND_ENUM_CONSTANT(BAKE_QUALITY_HIGH);
  1280. BIND_ENUM_CONSTANT(BAKE_QUALITY_ULTRA);
  1281. BIND_ENUM_CONSTANT(GENERATE_PROBES_DISABLED);
  1282. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_4);
  1283. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_8);
  1284. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_16);
  1285. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_32);
  1286. BIND_ENUM_CONSTANT(BAKE_ERROR_OK);
  1287. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SCENE_ROOT);
  1288. BIND_ENUM_CONSTANT(BAKE_ERROR_FOREIGN_DATA);
  1289. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_LIGHTMAPPER);
  1290. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SAVE_PATH);
  1291. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_MESHES);
  1292. BIND_ENUM_CONSTANT(BAKE_ERROR_MESHES_INVALID);
  1293. BIND_ENUM_CONSTANT(BAKE_ERROR_CANT_CREATE_IMAGE);
  1294. BIND_ENUM_CONSTANT(BAKE_ERROR_USER_ABORTED);
  1295. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_DISABLED);
  1296. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_SCENE);
  1297. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_CUSTOM_SKY);
  1298. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_CUSTOM_COLOR);
  1299. }
  1300. LightmapGI::LightmapGI() {
  1301. }