lightmap_gi.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933
  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 "core/object/object.h"
  35. #include "scene/3d/lightmap_probe.h"
  36. #include "scene/3d/mesh_instance_3d.h"
  37. #include "scene/resources/camera_attributes.h"
  38. #include "scene/resources/environment.h"
  39. #include "scene/resources/image_texture.h"
  40. #include "scene/resources/sky.h"
  41. #include "modules/modules_enabled.gen.h" // For lightmapper_rd.
  42. void LightmapGIData::add_user(const NodePath &p_path, const Rect2 &p_uv_scale, int p_slice_index, int32_t p_sub_instance) {
  43. User user;
  44. user.path = p_path;
  45. user.uv_scale = p_uv_scale;
  46. user.slice_index = p_slice_index;
  47. user.sub_instance = p_sub_instance;
  48. users.push_back(user);
  49. }
  50. int LightmapGIData::get_user_count() const {
  51. return users.size();
  52. }
  53. NodePath LightmapGIData::get_user_path(int p_user) const {
  54. ERR_FAIL_INDEX_V(p_user, users.size(), NodePath());
  55. return users[p_user].path;
  56. }
  57. int32_t LightmapGIData::get_user_sub_instance(int p_user) const {
  58. ERR_FAIL_INDEX_V(p_user, users.size(), -1);
  59. return users[p_user].sub_instance;
  60. }
  61. Rect2 LightmapGIData::get_user_lightmap_uv_scale(int p_user) const {
  62. ERR_FAIL_INDEX_V(p_user, users.size(), Rect2());
  63. return users[p_user].uv_scale;
  64. }
  65. int LightmapGIData::get_user_lightmap_slice_index(int p_user) const {
  66. ERR_FAIL_INDEX_V(p_user, users.size(), -1);
  67. return users[p_user].slice_index;
  68. }
  69. void LightmapGIData::clear_users() {
  70. users.clear();
  71. }
  72. void LightmapGIData::_set_user_data(const Array &p_data) {
  73. ERR_FAIL_COND(p_data.is_empty());
  74. ERR_FAIL_COND((p_data.size() % 4) != 0);
  75. for (int i = 0; i < p_data.size(); i += 4) {
  76. add_user(p_data[i + 0], p_data[i + 1], p_data[i + 2], p_data[i + 3]);
  77. }
  78. }
  79. Array LightmapGIData::_get_user_data() const {
  80. Array ret;
  81. for (int i = 0; i < users.size(); i++) {
  82. ret.push_back(users[i].path);
  83. ret.push_back(users[i].uv_scale);
  84. ret.push_back(users[i].slice_index);
  85. ret.push_back(users[i].sub_instance);
  86. }
  87. return ret;
  88. }
  89. void LightmapGIData::set_lightmap_textures(const TypedArray<TextureLayered> &p_data) {
  90. storage_light_textures = p_data;
  91. if (p_data.is_empty()) {
  92. combined_light_texture = Ref<TextureLayered>();
  93. _reset_lightmap_textures();
  94. return;
  95. }
  96. if (p_data.size() == 1) {
  97. combined_light_texture = p_data[0];
  98. } else {
  99. Vector<Ref<Image>> images;
  100. for (int i = 0; i < p_data.size(); i++) {
  101. Ref<TextureLayered> texture = p_data[i];
  102. ERR_FAIL_COND_MSG(texture.is_null(), vformat("Invalid TextureLayered at index %d.", i));
  103. for (int j = 0; j < texture->get_layers(); j++) {
  104. images.push_back(texture->get_layer_data(j));
  105. }
  106. }
  107. Ref<Texture2DArray> combined_texture;
  108. combined_texture.instantiate();
  109. combined_texture->create_from_images(images);
  110. combined_light_texture = combined_texture;
  111. }
  112. _reset_lightmap_textures();
  113. }
  114. TypedArray<TextureLayered> LightmapGIData::get_lightmap_textures() const {
  115. return storage_light_textures;
  116. }
  117. void LightmapGIData::set_shadowmask_textures(const TypedArray<TextureLayered> &p_data) {
  118. storage_shadowmask_textures = p_data;
  119. if (p_data.is_empty()) {
  120. combined_shadowmask_texture = Ref<TextureLayered>();
  121. _reset_shadowmask_textures();
  122. return;
  123. }
  124. if (p_data.size() == 1) {
  125. combined_shadowmask_texture = p_data[0];
  126. } else {
  127. Vector<Ref<Image>> images;
  128. for (int i = 0; i < p_data.size(); i++) {
  129. Ref<TextureLayered> texture = p_data[i];
  130. ERR_FAIL_COND_MSG(texture.is_null(), vformat("Invalid TextureLayered at index %d.", i));
  131. for (int j = 0; j < texture->get_layers(); j++) {
  132. images.push_back(texture->get_layer_data(j));
  133. }
  134. }
  135. Ref<Texture2DArray> combined_texture;
  136. combined_texture.instantiate();
  137. combined_texture->create_from_images(images);
  138. combined_shadowmask_texture = combined_texture;
  139. }
  140. _reset_shadowmask_textures();
  141. }
  142. TypedArray<TextureLayered> LightmapGIData::get_shadowmask_textures() const {
  143. return storage_shadowmask_textures;
  144. }
  145. void LightmapGIData::clear_shadowmask_textures() {
  146. RS::get_singleton()->lightmap_set_shadowmask_textures(lightmap, RID());
  147. storage_shadowmask_textures.clear();
  148. combined_shadowmask_texture.unref();
  149. }
  150. bool LightmapGIData::has_shadowmask_textures() {
  151. return !storage_shadowmask_textures.is_empty() && combined_shadowmask_texture.is_valid();
  152. }
  153. RID LightmapGIData::get_rid() const {
  154. return lightmap;
  155. }
  156. void LightmapGIData::clear() {
  157. users.clear();
  158. }
  159. void LightmapGIData::_reset_lightmap_textures() {
  160. RS::get_singleton()->lightmap_set_textures(lightmap, combined_light_texture.is_valid() ? combined_light_texture->get_rid() : RID(), uses_spherical_harmonics);
  161. }
  162. void LightmapGIData::_reset_shadowmask_textures() {
  163. RS::get_singleton()->lightmap_set_shadowmask_textures(lightmap, combined_shadowmask_texture.is_valid() ? combined_shadowmask_texture->get_rid() : RID());
  164. }
  165. void LightmapGIData::set_uses_spherical_harmonics(bool p_enable) {
  166. uses_spherical_harmonics = p_enable;
  167. _reset_lightmap_textures();
  168. }
  169. bool LightmapGIData::is_using_spherical_harmonics() const {
  170. return uses_spherical_harmonics;
  171. }
  172. void LightmapGIData::_set_uses_packed_directional(bool p_enable) {
  173. _uses_packed_directional = p_enable;
  174. }
  175. bool LightmapGIData::_is_using_packed_directional() const {
  176. return _uses_packed_directional;
  177. }
  178. void LightmapGIData::update_shadowmask_mode(ShadowmaskMode p_mode) {
  179. RS::get_singleton()->lightmap_set_shadowmask_mode(lightmap, (RS::ShadowmaskMode)p_mode);
  180. }
  181. LightmapGIData::ShadowmaskMode LightmapGIData::get_shadowmask_mode() const {
  182. return (ShadowmaskMode)RS::get_singleton()->lightmap_get_shadowmask_mode(lightmap);
  183. }
  184. 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) {
  185. if (p_points.size()) {
  186. int pc = p_points.size();
  187. ERR_FAIL_COND(pc * 9 != p_point_sh.size());
  188. ERR_FAIL_COND((p_tetrahedra.size() % 4) != 0);
  189. ERR_FAIL_COND((p_bsp_tree.size() % 6) != 0);
  190. RS::get_singleton()->lightmap_set_probe_capture_data(lightmap, p_points, p_point_sh, p_tetrahedra, p_bsp_tree);
  191. RS::get_singleton()->lightmap_set_probe_bounds(lightmap, p_bounds);
  192. RS::get_singleton()->lightmap_set_probe_interior(lightmap, p_interior);
  193. } else {
  194. RS::get_singleton()->lightmap_set_probe_capture_data(lightmap, PackedVector3Array(), PackedColorArray(), PackedInt32Array(), PackedInt32Array());
  195. RS::get_singleton()->lightmap_set_probe_bounds(lightmap, AABB());
  196. RS::get_singleton()->lightmap_set_probe_interior(lightmap, false);
  197. }
  198. RS::get_singleton()->lightmap_set_baked_exposure_normalization(lightmap, p_baked_exposure);
  199. baked_exposure = p_baked_exposure;
  200. interior = p_interior;
  201. bounds = p_bounds;
  202. }
  203. PackedVector3Array LightmapGIData::get_capture_points() const {
  204. return RS::get_singleton()->lightmap_get_probe_capture_points(lightmap);
  205. }
  206. PackedColorArray LightmapGIData::get_capture_sh() const {
  207. return RS::get_singleton()->lightmap_get_probe_capture_sh(lightmap);
  208. }
  209. PackedInt32Array LightmapGIData::get_capture_tetrahedra() const {
  210. return RS::get_singleton()->lightmap_get_probe_capture_tetrahedra(lightmap);
  211. }
  212. PackedInt32Array LightmapGIData::get_capture_bsp_tree() const {
  213. return RS::get_singleton()->lightmap_get_probe_capture_bsp_tree(lightmap);
  214. }
  215. AABB LightmapGIData::get_capture_bounds() const {
  216. return bounds;
  217. }
  218. bool LightmapGIData::is_interior() const {
  219. return interior;
  220. }
  221. float LightmapGIData::get_baked_exposure() const {
  222. return baked_exposure;
  223. }
  224. void LightmapGIData::_set_probe_data(const Dictionary &p_data) {
  225. ERR_FAIL_COND(!p_data.has("bounds"));
  226. ERR_FAIL_COND(!p_data.has("points"));
  227. ERR_FAIL_COND(!p_data.has("tetrahedra"));
  228. ERR_FAIL_COND(!p_data.has("bsp"));
  229. ERR_FAIL_COND(!p_data.has("sh"));
  230. ERR_FAIL_COND(!p_data.has("interior"));
  231. ERR_FAIL_COND(!p_data.has("baked_exposure"));
  232. 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"]);
  233. }
  234. Dictionary LightmapGIData::_get_probe_data() const {
  235. Dictionary d;
  236. d["bounds"] = get_capture_bounds();
  237. d["points"] = get_capture_points();
  238. d["tetrahedra"] = get_capture_tetrahedra();
  239. d["bsp"] = get_capture_bsp_tree();
  240. d["sh"] = get_capture_sh();
  241. d["interior"] = is_interior();
  242. d["baked_exposure"] = get_baked_exposure();
  243. return d;
  244. }
  245. #ifndef DISABLE_DEPRECATED
  246. void LightmapGIData::set_light_texture(const Ref<TextureLayered> &p_light_texture) {
  247. TypedArray<TextureLayered> arr;
  248. arr.append(p_light_texture);
  249. set_lightmap_textures(arr);
  250. }
  251. Ref<TextureLayered> LightmapGIData::get_light_texture() const {
  252. if (storage_light_textures.is_empty()) {
  253. return Ref<TextureLayered>();
  254. }
  255. return storage_light_textures.get(0);
  256. }
  257. void LightmapGIData::_set_light_textures_data(const Array &p_data) {
  258. set_lightmap_textures(p_data);
  259. }
  260. Array LightmapGIData::_get_light_textures_data() const {
  261. return Array(storage_light_textures);
  262. }
  263. #endif
  264. void LightmapGIData::_bind_methods() {
  265. ClassDB::bind_method(D_METHOD("_set_user_data", "data"), &LightmapGIData::_set_user_data);
  266. ClassDB::bind_method(D_METHOD("_get_user_data"), &LightmapGIData::_get_user_data);
  267. ClassDB::bind_method(D_METHOD("set_lightmap_textures", "light_textures"), &LightmapGIData::set_lightmap_textures);
  268. ClassDB::bind_method(D_METHOD("get_lightmap_textures"), &LightmapGIData::get_lightmap_textures);
  269. ClassDB::bind_method(D_METHOD("set_shadowmask_textures", "shadowmask_textures"), &LightmapGIData::set_shadowmask_textures);
  270. ClassDB::bind_method(D_METHOD("get_shadowmask_textures"), &LightmapGIData::get_shadowmask_textures);
  271. ClassDB::bind_method(D_METHOD("set_uses_spherical_harmonics", "uses_spherical_harmonics"), &LightmapGIData::set_uses_spherical_harmonics);
  272. ClassDB::bind_method(D_METHOD("is_using_spherical_harmonics"), &LightmapGIData::is_using_spherical_harmonics);
  273. ClassDB::bind_method(D_METHOD("_set_uses_packed_directional", "_uses_packed_directional"), &LightmapGIData::_set_uses_packed_directional);
  274. ClassDB::bind_method(D_METHOD("_is_using_packed_directional"), &LightmapGIData::_is_using_packed_directional);
  275. ClassDB::bind_method(D_METHOD("add_user", "path", "uv_scale", "slice_index", "sub_instance"), &LightmapGIData::add_user);
  276. ClassDB::bind_method(D_METHOD("get_user_count"), &LightmapGIData::get_user_count);
  277. ClassDB::bind_method(D_METHOD("get_user_path", "user_idx"), &LightmapGIData::get_user_path);
  278. ClassDB::bind_method(D_METHOD("clear_users"), &LightmapGIData::clear_users);
  279. ClassDB::bind_method(D_METHOD("_set_probe_data", "data"), &LightmapGIData::_set_probe_data);
  280. ClassDB::bind_method(D_METHOD("_get_probe_data"), &LightmapGIData::_get_probe_data);
  281. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "lightmap_textures", PROPERTY_HINT_ARRAY_TYPE, "TextureLayered", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY), "set_lightmap_textures", "get_lightmap_textures");
  282. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "shadowmask_textures", PROPERTY_HINT_ARRAY_TYPE, "TextureLayered", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY), "set_shadowmask_textures", "get_shadowmask_textures");
  283. 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");
  284. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "user_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_user_data", "_get_user_data");
  285. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "probe_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_probe_data", "_get_probe_data");
  286. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "_uses_packed_directional", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_uses_packed_directional", "_is_using_packed_directional");
  287. #ifndef DISABLE_DEPRECATED
  288. ClassDB::bind_method(D_METHOD("set_light_texture", "light_texture"), &LightmapGIData::set_light_texture);
  289. ClassDB::bind_method(D_METHOD("get_light_texture"), &LightmapGIData::get_light_texture);
  290. ClassDB::bind_method(D_METHOD("_set_light_textures_data", "data"), &LightmapGIData::_set_light_textures_data);
  291. ClassDB::bind_method(D_METHOD("_get_light_textures_data"), &LightmapGIData::_get_light_textures_data);
  292. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_texture", PROPERTY_HINT_RESOURCE_TYPE, "TextureLayered", PROPERTY_USAGE_NONE), "set_light_texture", "get_light_texture");
  293. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "light_textures", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_INTERNAL), "_set_light_textures_data", "_get_light_textures_data");
  294. #endif
  295. BIND_ENUM_CONSTANT(SHADOWMASK_MODE_NONE);
  296. BIND_ENUM_CONSTANT(SHADOWMASK_MODE_REPLACE);
  297. BIND_ENUM_CONSTANT(SHADOWMASK_MODE_OVERLAY);
  298. }
  299. LightmapGIData::LightmapGIData() {
  300. lightmap = RS::get_singleton()->lightmap_create();
  301. }
  302. LightmapGIData::~LightmapGIData() {
  303. ERR_FAIL_NULL(RenderingServer::get_singleton());
  304. RS::get_singleton()->free(lightmap);
  305. }
  306. ///////////////////////////
  307. void LightmapGI::_find_meshes_and_lights(Node *p_at_node, Vector<MeshesFound> &meshes, Vector<LightsFound> &lights, Vector<Vector3> &probes) {
  308. MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(p_at_node);
  309. if (mi && mi->get_gi_mode() == GeometryInstance3D::GI_MODE_STATIC && mi->is_visible_in_tree()) {
  310. Ref<Mesh> mesh = mi->get_mesh();
  311. if (mesh.is_valid()) {
  312. bool all_have_uv2_and_normal = true;
  313. bool surfaces_found = false;
  314. for (int i = 0; i < mesh->get_surface_count(); i++) {
  315. if (mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  316. continue;
  317. }
  318. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_TEX_UV2)) {
  319. all_have_uv2_and_normal = false;
  320. break;
  321. }
  322. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_NORMAL)) {
  323. all_have_uv2_and_normal = false;
  324. break;
  325. }
  326. surfaces_found = true;
  327. }
  328. if (surfaces_found && all_have_uv2_and_normal) {
  329. //READY TO BAKE! size hint could be computed if not found, actually..
  330. MeshesFound mf;
  331. mf.xform = get_global_transform().affine_inverse() * mi->get_global_transform();
  332. mf.node_path = get_path_to(mi);
  333. mf.subindex = -1;
  334. mf.mesh = mesh;
  335. mf.lightmap_scale = mi->get_lightmap_texel_scale();
  336. Ref<Material> all_override = mi->get_material_override();
  337. for (int i = 0; i < mesh->get_surface_count(); i++) {
  338. if (all_override.is_valid()) {
  339. mf.overrides.push_back(all_override);
  340. } else {
  341. mf.overrides.push_back(mi->get_surface_override_material(i));
  342. }
  343. }
  344. meshes.push_back(mf);
  345. }
  346. }
  347. }
  348. Node3D *s = Object::cast_to<Node3D>(p_at_node);
  349. if (!mi && s) {
  350. Array bmeshes = p_at_node->call("get_bake_meshes");
  351. if (bmeshes.size() && (bmeshes.size() & 1) == 0) {
  352. Transform3D xf = get_global_transform().affine_inverse() * s->get_global_transform();
  353. for (int i = 0; i < bmeshes.size(); i += 2) {
  354. Ref<Mesh> mesh = bmeshes[i];
  355. if (mesh.is_null()) {
  356. continue;
  357. }
  358. MeshesFound mf;
  359. Transform3D mesh_xf = bmeshes[i + 1];
  360. mf.xform = xf * mesh_xf;
  361. mf.node_path = get_path_to(s);
  362. mf.subindex = i / 2;
  363. mf.lightmap_scale = 1.0;
  364. mf.mesh = mesh;
  365. meshes.push_back(mf);
  366. }
  367. }
  368. }
  369. Light3D *light = Object::cast_to<Light3D>(p_at_node);
  370. if (light && light->get_bake_mode() != Light3D::BAKE_DISABLED) {
  371. LightsFound lf;
  372. lf.xform = get_global_transform().affine_inverse() * light->get_global_transform();
  373. lf.light = light;
  374. lights.push_back(lf);
  375. }
  376. LightmapProbe *probe = Object::cast_to<LightmapProbe>(p_at_node);
  377. if (probe) {
  378. Transform3D xf = get_global_transform().affine_inverse() * probe->get_global_transform();
  379. probes.push_back(xf.origin);
  380. }
  381. for (int i = 0; i < p_at_node->get_child_count(); i++) {
  382. Node *child = p_at_node->get_child(i);
  383. if (!child->get_owner()) {
  384. continue; //maybe a helper
  385. }
  386. _find_meshes_and_lights(child, meshes, lights, probes);
  387. }
  388. }
  389. 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 {
  390. int over = 0;
  391. int under = 0;
  392. const BSPSimplex &s = p_simplices[p_simplex];
  393. for (int i = 0; i < 4; i++) {
  394. const Vector3 v = p_points[s.vertices[i]];
  395. // The tolerance used here comes from experiments on scenes up to
  396. // 1000x1000x100 meters. If it's any smaller, some simplices will
  397. // appear to self-intersect due to a lack of precision in Plane.
  398. if (p_plane.has_point(v, 1.0 / (1 << 13))) {
  399. // Coplanar.
  400. } else if (p_plane.is_point_over(v)) {
  401. over++;
  402. } else {
  403. under++;
  404. }
  405. }
  406. 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
  407. if (under == 0) {
  408. return 1; // all over
  409. } else if (over == 0) {
  410. return -1; // all under
  411. } else {
  412. return 0; // crossing
  413. }
  414. }
  415. //#define DEBUG_BSP
  416. 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) {
  417. ERR_FAIL_COND_V(p_simplex_indices.size() < 2, -1);
  418. int32_t node_index = (int32_t)bsp_nodes.size();
  419. bsp_nodes.push_back(BSPNode());
  420. //test with all the simplex planes
  421. Plane best_plane;
  422. float best_plane_score = -1.0;
  423. for (const int idx : p_simplex_indices) {
  424. const BSPSimplex &s = p_simplices[idx];
  425. for (int j = 0; j < 4; j++) {
  426. uint32_t plane_index = s.planes[j];
  427. if (planes_tested[plane_index] == node_index) {
  428. continue; //tested this plane already
  429. }
  430. planes_tested[plane_index] = node_index;
  431. static const int face_order[4][3] = {
  432. { 0, 1, 2 },
  433. { 0, 2, 3 },
  434. { 0, 1, 3 },
  435. { 1, 2, 3 }
  436. };
  437. // despite getting rid of plane duplicates, we should still use here the actual plane to avoid numerical error
  438. // from thinking this same simplex is intersecting rather than on a side
  439. Vector3 v0 = p_points[s.vertices[face_order[j][0]]];
  440. Vector3 v1 = p_points[s.vertices[face_order[j][1]]];
  441. Vector3 v2 = p_points[s.vertices[face_order[j][2]]];
  442. Plane plane(v0, v1, v2);
  443. //test with all the simplices
  444. int over_count = 0;
  445. int under_count = 0;
  446. for (const int &index : p_simplex_indices) {
  447. int side = _bsp_get_simplex_side(p_points, p_simplices, plane, index);
  448. if (side == -2) {
  449. continue; //this simplex is invalid, skip for now
  450. } else if (side < 0) {
  451. under_count++;
  452. } else if (side > 0) {
  453. over_count++;
  454. }
  455. }
  456. if (under_count == 0 && over_count == 0) {
  457. continue; //most likely precision issue with a flat simplex, do not try this plane
  458. }
  459. if (under_count > over_count) { //make sure under is always less than over, so we can compute the same ratio
  460. SWAP(under_count, over_count);
  461. }
  462. float score = 0; //by default, score is 0 (worst)
  463. if (over_count > 0) {
  464. // Simplices that are intersected by the plane are moved into both the over
  465. // and under subtrees which makes the entire tree deeper, so the best plane
  466. // will have the least intersections while separating the simplices evenly.
  467. float balance = float(under_count) / over_count;
  468. float separation = float(over_count + under_count) / p_simplex_indices.size();
  469. score = balance * separation * separation;
  470. }
  471. if (score > best_plane_score) {
  472. best_plane = plane;
  473. best_plane_score = score;
  474. }
  475. }
  476. }
  477. // We often end up with two (or on rare occasions, three) simplices that are
  478. // either disjoint or share one vertex and don't have a separating plane
  479. // among their faces. The fallback is to loop through new planes created
  480. // with one vertex of the first simplex and two vertices of the second until
  481. // we find a winner.
  482. if (best_plane_score == 0) {
  483. const BSPSimplex &simplex0 = p_simplices[p_simplex_indices[0]];
  484. const BSPSimplex &simplex1 = p_simplices[p_simplex_indices[1]];
  485. for (uint32_t i = 0; i < 4 && !best_plane_score; i++) {
  486. Vector3 v0 = p_points[simplex0.vertices[i]];
  487. for (uint32_t j = 0; j < 3 && !best_plane_score; j++) {
  488. if (simplex0.vertices[i] == simplex1.vertices[j]) {
  489. break;
  490. }
  491. Vector3 v1 = p_points[simplex1.vertices[j]];
  492. for (uint32_t k = j + 1; k < 4; k++) {
  493. if (simplex0.vertices[i] == simplex1.vertices[k]) {
  494. break;
  495. }
  496. Vector3 v2 = p_points[simplex1.vertices[k]];
  497. Plane plane = Plane(v0, v1, v2);
  498. if (plane == Plane()) { // When v0, v1, and v2 are collinear, they can't form a plane.
  499. continue;
  500. }
  501. int32_t side0 = _bsp_get_simplex_side(p_points, p_simplices, plane, p_simplex_indices[0]);
  502. int32_t side1 = _bsp_get_simplex_side(p_points, p_simplices, plane, p_simplex_indices[1]);
  503. if ((side0 == 1 && side1 == -1) || (side0 == -1 && side1 == 1)) {
  504. best_plane = plane;
  505. best_plane_score = 1.0;
  506. break;
  507. }
  508. }
  509. }
  510. }
  511. }
  512. LocalVector<int32_t> indices_over;
  513. LocalVector<int32_t> indices_under;
  514. //split again, but add to list
  515. for (const uint32_t index : p_simplex_indices) {
  516. int side = _bsp_get_simplex_side(p_points, p_simplices, best_plane, index);
  517. if (side == -2) {
  518. continue; //simplex sits on the plane, does not make sense to use it
  519. }
  520. if (side <= 0) {
  521. indices_under.push_back(index);
  522. }
  523. if (side >= 0) {
  524. indices_over.push_back(index);
  525. }
  526. }
  527. #ifdef DEBUG_BSP
  528. 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));
  529. #endif
  530. if (best_plane_score < 0.0 || indices_over.size() == p_simplex_indices.size() || indices_under.size() == p_simplex_indices.size()) {
  531. // Failed to separate the tetrahedrons using planes
  532. // this means Delaunay broke at some point.
  533. // Luckily, because we are using tetrahedrons, we can resort to
  534. // less precise but still working ways to generate the separating plane
  535. // this will most likely look bad when interpolating, but at least it will not crash.
  536. // and the artifact will most likely also be very small, so too difficult to notice.
  537. //find the longest axis
  538. WARN_PRINT("Inconsistency found in triangulation while building BSP, probe interpolation quality may degrade a bit.");
  539. LocalVector<Vector3> centers;
  540. AABB bounds_all;
  541. for (uint32_t i = 0; i < p_simplex_indices.size(); i++) {
  542. AABB bounds;
  543. for (uint32_t j = 0; j < 4; j++) {
  544. Vector3 p = p_points[p_simplices[p_simplex_indices[i]].vertices[j]];
  545. if (j == 0) {
  546. bounds.position = p;
  547. } else {
  548. bounds.expand_to(p);
  549. }
  550. }
  551. if (i == 0) {
  552. centers.push_back(bounds.get_center());
  553. } else {
  554. bounds_all.merge_with(bounds);
  555. }
  556. }
  557. Vector3::Axis longest_axis = Vector3::Axis(bounds_all.get_longest_axis_index());
  558. //find the simplex that will go under
  559. uint32_t min_d_idx = 0xFFFFFFFF;
  560. float min_d_dist = 1e20;
  561. for (uint32_t i = 0; i < centers.size(); i++) {
  562. if (centers[i][longest_axis] < min_d_dist) {
  563. min_d_idx = i;
  564. min_d_dist = centers[i][longest_axis];
  565. }
  566. }
  567. //rebuild best_plane and over/under arrays
  568. best_plane = Plane();
  569. best_plane.normal[longest_axis] = 1.0;
  570. best_plane.d = min_d_dist;
  571. indices_under.clear();
  572. indices_under.push_back(min_d_idx);
  573. indices_over.clear();
  574. for (uint32_t i = 0; i < p_simplex_indices.size(); i++) {
  575. if (i == min_d_idx) {
  576. continue;
  577. }
  578. indices_over.push_back(p_simplex_indices[i]);
  579. }
  580. }
  581. BSPNode node;
  582. node.plane = best_plane;
  583. if (indices_under.size() == 0) {
  584. //nothing to do here
  585. node.under = BSPNode::EMPTY_LEAF;
  586. } else if (indices_under.size() == 1) {
  587. node.under = -(indices_under[0] + 1);
  588. } else {
  589. node.under = _compute_bsp_tree(p_points, p_planes, planes_tested, p_simplices, indices_under, bsp_nodes);
  590. }
  591. if (indices_over.size() == 0) {
  592. //nothing to do here
  593. node.over = BSPNode::EMPTY_LEAF;
  594. } else if (indices_over.size() == 1) {
  595. node.over = -(indices_over[0] + 1);
  596. } else {
  597. node.over = _compute_bsp_tree(p_points, p_planes, planes_tested, p_simplices, indices_over, bsp_nodes);
  598. }
  599. bsp_nodes[node_index] = node;
  600. return node_index;
  601. }
  602. bool LightmapGI::_lightmap_bake_step_function(float p_completion, const String &p_text, void *ud, bool p_refresh) {
  603. BakeStepUD *bsud = (BakeStepUD *)ud;
  604. bool ret = false;
  605. if (bsud->func) {
  606. ret = bsud->func(bsud->from_percent + p_completion * (bsud->to_percent - bsud->from_percent), p_text, bsud->ud, p_refresh);
  607. }
  608. return ret;
  609. }
  610. void LightmapGI::_plot_triangle_into_octree(GenProbesOctree *p_cell, float p_cell_size, const Vector3 *p_triangle) {
  611. for (int i = 0; i < 8; i++) {
  612. Vector3i pos = p_cell->offset;
  613. uint32_t half_size = p_cell->size / 2;
  614. if (i & 1) {
  615. pos.x += half_size;
  616. }
  617. if (i & 2) {
  618. pos.y += half_size;
  619. }
  620. if (i & 4) {
  621. pos.z += half_size;
  622. }
  623. AABB subcell;
  624. subcell.position = Vector3(pos) * p_cell_size;
  625. subcell.size = Vector3(half_size, half_size, half_size) * p_cell_size;
  626. if (!Geometry3D::triangle_box_overlap(subcell.get_center(), subcell.size * 0.5, p_triangle)) {
  627. continue;
  628. }
  629. if (p_cell->children[i] == nullptr) {
  630. GenProbesOctree *child = memnew(GenProbesOctree);
  631. child->offset = pos;
  632. child->size = half_size;
  633. p_cell->children[i] = child;
  634. }
  635. if (half_size > 1) {
  636. //still levels missing
  637. _plot_triangle_into_octree(p_cell->children[i], p_cell_size, p_triangle);
  638. }
  639. }
  640. }
  641. 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) {
  642. for (int i = 0; i < 8; i++) {
  643. Vector3i pos = p_cell->offset;
  644. if (i & 1) {
  645. pos.x += p_cell->size;
  646. }
  647. if (i & 2) {
  648. pos.y += p_cell->size;
  649. }
  650. if (i & 4) {
  651. pos.z += p_cell->size;
  652. }
  653. if (p_cell->size == 1 && !positions_used.has(pos)) {
  654. //new position to insert!
  655. Vector3 real_pos = p_bounds.position + Vector3(pos) * p_cell_size;
  656. //see if a user submitted probe is too close
  657. int ppcount = probe_positions.size();
  658. const Vector3 *pp = probe_positions.ptr();
  659. bool exists = false;
  660. for (int j = 0; j < ppcount; j++) {
  661. if (pp[j].distance_to(real_pos) < (p_cell_size * 0.5f)) {
  662. exists = true;
  663. break;
  664. }
  665. }
  666. if (!exists) {
  667. new_probe_positions.push_back(real_pos);
  668. }
  669. positions_used[pos] = true;
  670. }
  671. if (p_cell->children[i] != nullptr) {
  672. _gen_new_positions_from_octree(p_cell->children[i], p_cell_size, probe_positions, new_probe_positions, positions_used, p_bounds);
  673. }
  674. }
  675. }
  676. LightmapGI::BakeError LightmapGI::_save_and_reimport_atlas_textures(const Ref<Lightmapper> p_lightmapper, const String &p_base_name, TypedArray<TextureLayered> &r_textures, bool p_is_shadowmask) const {
  677. Vector<Ref<Image>> images;
  678. images.resize(p_is_shadowmask ? p_lightmapper->get_shadowmask_texture_count() : p_lightmapper->get_bake_texture_count());
  679. for (int i = 0; i < images.size(); i++) {
  680. images.set(i, p_is_shadowmask ? p_lightmapper->get_shadowmask_texture(i) : p_lightmapper->get_bake_texture(i));
  681. }
  682. const int slice_count = images.size();
  683. const int slice_width = images[0]->get_width();
  684. const int slice_height = images[0]->get_height();
  685. const int slices_per_texture = Image::MAX_HEIGHT / slice_height;
  686. const int texture_count = Math::ceil(slice_count / (float)slices_per_texture);
  687. const int last_count = slice_count % slices_per_texture;
  688. r_textures.resize(texture_count);
  689. for (int i = 0; i < texture_count; i++) {
  690. const int texture_slice_count = (i == texture_count - 1 && last_count != 0) ? last_count : slices_per_texture;
  691. Ref<Image> texture_image = Image::create_empty(slice_width, slice_height * texture_slice_count, false, images[0]->get_format());
  692. for (int j = 0; j < texture_slice_count; j++) {
  693. texture_image->blit_rect(images[i * slices_per_texture + j], Rect2i(0, 0, slice_width, slice_height), Point2i(0, slice_height * j));
  694. }
  695. const String atlas_path = (texture_count > 1 ? p_base_name + "_" + itos(i) : p_base_name) + (p_is_shadowmask ? ".png" : ".exr");
  696. const String config_path = atlas_path + ".import";
  697. Ref<ConfigFile> config;
  698. config.instantiate();
  699. // Load an import configuration if present.
  700. if (FileAccess::exists(config_path)) {
  701. config->load(config_path);
  702. }
  703. config->set_value("remap", "importer", "2d_array_texture");
  704. config->set_value("remap", "type", "CompressedTexture2DArray");
  705. if (!config->has_section_key("params", "compress/mode")) {
  706. // Do not override an existing compression mode.
  707. config->set_value("params", "compress/mode", 2);
  708. }
  709. config->set_value("params", "compress/channel_pack", 1);
  710. config->set_value("params", "mipmaps/generate", false);
  711. config->set_value("params", "slices/horizontal", 1);
  712. config->set_value("params", "slices/vertical", texture_slice_count);
  713. config->save(config_path);
  714. if (supersampling_enabled) {
  715. texture_image->resize(texture_image->get_width() / supersampling_factor, texture_image->get_height() / supersampling_factor, Image::INTERPOLATE_TRILINEAR);
  716. }
  717. // Save the file.
  718. Error save_err;
  719. if (p_is_shadowmask) {
  720. save_err = texture_image->save_png(atlas_path);
  721. } else {
  722. save_err = texture_image->save_exr(atlas_path, false);
  723. }
  724. ERR_FAIL_COND_V(save_err, LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE);
  725. // Reimport the file.
  726. ResourceLoader::import(atlas_path);
  727. Ref<TextureLayered> t = ResourceLoader::load(atlas_path); // If already loaded, it will be updated on refocus?
  728. ERR_FAIL_COND_V(t.is_null(), LightmapGI::BAKE_ERROR_CANT_CREATE_IMAGE);
  729. // Store the atlas in the array.
  730. r_textures[i] = t;
  731. }
  732. return LightmapGI::BAKE_ERROR_OK;
  733. }
  734. LightmapGI::BakeError LightmapGI::bake(Node *p_from_node, String p_image_data_path, Lightmapper::BakeStepFunc p_bake_step, void *p_bake_userdata) {
  735. if (p_image_data_path.is_empty()) {
  736. if (get_light_data().is_null()) {
  737. return BAKE_ERROR_NO_SAVE_PATH;
  738. }
  739. p_image_data_path = get_light_data()->get_path();
  740. if (!p_image_data_path.is_resource_file()) {
  741. return BAKE_ERROR_NO_SAVE_PATH;
  742. }
  743. }
  744. Ref<Lightmapper> lightmapper = Lightmapper::create();
  745. ERR_FAIL_COND_V(lightmapper.is_null(), BAKE_ERROR_NO_LIGHTMAPPER);
  746. BakeStepUD bsud;
  747. bsud.func = p_bake_step;
  748. bsud.ud = p_bake_userdata;
  749. bsud.from_percent = 0.2;
  750. bsud.to_percent = 0.8;
  751. if (p_bake_step) {
  752. p_bake_step(0.0, RTR("Finding meshes, lights and probes"), p_bake_userdata, true);
  753. }
  754. /* STEP 1, FIND MESHES, LIGHTS AND PROBES */
  755. Vector<Lightmapper::MeshData> mesh_data;
  756. Vector<LightsFound> lights_found;
  757. Vector<Vector3> probes_found;
  758. AABB bounds;
  759. {
  760. Vector<MeshesFound> meshes_found;
  761. _find_meshes_and_lights(p_from_node ? p_from_node : get_parent(), meshes_found, lights_found, probes_found);
  762. if (meshes_found.size() == 0) {
  763. return BAKE_ERROR_NO_MESHES;
  764. }
  765. // create mesh data for insert
  766. //get the base material textures, help compute atlas size and bounds
  767. for (int m_i = 0; m_i < meshes_found.size(); m_i++) {
  768. if (p_bake_step) {
  769. float p = (float)(m_i) / meshes_found.size();
  770. p_bake_step(p * 0.1, vformat(RTR("Preparing geometry %d/%d"), m_i, meshes_found.size()), p_bake_userdata, false);
  771. }
  772. MeshesFound &mf = meshes_found.write[m_i];
  773. Size2i mesh_lightmap_size = mf.mesh->get_lightmap_size_hint();
  774. if (mesh_lightmap_size == Size2i(0, 0)) {
  775. // TODO we should compute a size if no lightmap hint is set, as we did in 3.x.
  776. // For now set to basic size to avoid crash.
  777. mesh_lightmap_size = Size2i(64, 64);
  778. }
  779. // Double lightmap texel density if downsampling is enabled, as the final texture size will be halved before saving lightmaps.
  780. Size2i lightmap_size = Size2i(Size2(mesh_lightmap_size) * mf.lightmap_scale * texel_scale) * (supersampling_enabled ? supersampling_factor : 1.0);
  781. ERR_FAIL_COND_V(lightmap_size.x == 0 || lightmap_size.y == 0, BAKE_ERROR_LIGHTMAP_TOO_SMALL);
  782. TypedArray<RID> overrides;
  783. overrides.resize(mf.overrides.size());
  784. for (int i = 0; i < mf.overrides.size(); i++) {
  785. if (mf.overrides[i].is_valid()) {
  786. overrides[i] = mf.overrides[i]->get_rid();
  787. }
  788. }
  789. TypedArray<Image> images = RS::get_singleton()->bake_render_uv2(mf.mesh->get_rid(), overrides, lightmap_size);
  790. ERR_FAIL_COND_V(images.is_empty(), BAKE_ERROR_CANT_CREATE_IMAGE);
  791. Ref<Image> albedo = images[RS::BAKE_CHANNEL_ALBEDO_ALPHA];
  792. Ref<Image> orm = images[RS::BAKE_CHANNEL_ORM];
  793. //multiply albedo by metal
  794. Lightmapper::MeshData md;
  795. {
  796. Dictionary d;
  797. d["path"] = mf.node_path;
  798. if (mf.subindex >= 0) {
  799. d["subindex"] = mf.subindex;
  800. }
  801. md.userdata = d;
  802. }
  803. {
  804. if (albedo->get_format() != Image::FORMAT_RGBA8) {
  805. albedo->convert(Image::FORMAT_RGBA8);
  806. }
  807. if (orm->get_format() != Image::FORMAT_RGBA8) {
  808. orm->convert(Image::FORMAT_RGBA8);
  809. }
  810. Vector<uint8_t> albedo_alpha = albedo->get_data();
  811. Vector<uint8_t> orm_data = orm->get_data();
  812. Vector<uint8_t> albedom;
  813. uint32_t len = albedo_alpha.size();
  814. albedom.resize(len);
  815. const uint8_t *r_aa = albedo_alpha.ptr();
  816. const uint8_t *r_orm = orm_data.ptr();
  817. uint8_t *w_albedo = albedom.ptrw();
  818. for (uint32_t i = 0; i < len; i += 4) {
  819. w_albedo[i + 0] = uint8_t(CLAMP(float(r_aa[i + 0]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  820. w_albedo[i + 1] = uint8_t(CLAMP(float(r_aa[i + 1]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  821. w_albedo[i + 2] = uint8_t(CLAMP(float(r_aa[i + 2]) * (1.0 - float(r_orm[i + 2] / 255.0)), 0, 255));
  822. w_albedo[i + 3] = r_aa[i + 3];
  823. }
  824. md.albedo_on_uv2.instantiate();
  825. md.albedo_on_uv2->set_data(lightmap_size.width, lightmap_size.height, false, Image::FORMAT_RGBA8, albedom);
  826. }
  827. md.emission_on_uv2 = images[RS::BAKE_CHANNEL_EMISSION];
  828. if (md.emission_on_uv2->get_format() != Image::FORMAT_RGBAH) {
  829. md.emission_on_uv2->convert(Image::FORMAT_RGBAH);
  830. }
  831. //get geometry
  832. Basis normal_xform = mf.xform.basis.inverse().transposed();
  833. for (int i = 0; i < mf.mesh->get_surface_count(); i++) {
  834. if (mf.mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES) {
  835. continue;
  836. }
  837. Array a = mf.mesh->surface_get_arrays(i);
  838. Ref<Material> mat = mf.mesh->surface_get_material(i);
  839. RID mat_rid;
  840. if (mat.is_valid()) {
  841. mat_rid = mat->get_rid();
  842. }
  843. Vector<Vector3> vertices = a[Mesh::ARRAY_VERTEX];
  844. const Vector3 *vr = vertices.ptr();
  845. Vector<Vector2> uv = a[Mesh::ARRAY_TEX_UV2];
  846. const Vector2 *uvr = nullptr;
  847. Vector<Vector3> normals = a[Mesh::ARRAY_NORMAL];
  848. const Vector3 *nr = nullptr;
  849. Vector<int> index = a[Mesh::ARRAY_INDEX];
  850. ERR_CONTINUE(uv.size() == 0);
  851. ERR_CONTINUE(normals.size() == 0);
  852. uvr = uv.ptr();
  853. nr = normals.ptr();
  854. int facecount;
  855. const int *ir = nullptr;
  856. if (index.size()) {
  857. facecount = index.size() / 3;
  858. ir = index.ptr();
  859. } else {
  860. facecount = vertices.size() / 3;
  861. }
  862. for (int j = 0; j < facecount; j++) {
  863. uint32_t vidx[3];
  864. if (ir) {
  865. for (int k = 0; k < 3; k++) {
  866. vidx[k] = ir[j * 3 + k];
  867. }
  868. } else {
  869. for (int k = 0; k < 3; k++) {
  870. vidx[k] = j * 3 + k;
  871. }
  872. }
  873. for (int k = 0; k < 3; k++) {
  874. Vector3 v = mf.xform.xform(vr[vidx[k]]);
  875. if (bounds == AABB()) {
  876. bounds.position = v;
  877. } else {
  878. bounds.expand_to(v);
  879. }
  880. md.points.push_back(v);
  881. md.uv2.push_back(uvr[vidx[k]]);
  882. md.normal.push_back(normal_xform.xform(nr[vidx[k]]).normalized());
  883. md.material.push_back(mat_rid);
  884. }
  885. }
  886. }
  887. mesh_data.push_back(md);
  888. }
  889. }
  890. /* STEP 2, CREATE PROBES */
  891. if (p_bake_step) {
  892. p_bake_step(0.3, RTR("Creating probes"), p_bake_userdata, true);
  893. }
  894. //bounds need to include the user probes
  895. for (int i = 0; i < probes_found.size(); i++) {
  896. bounds.expand_to(probes_found[i]);
  897. }
  898. bounds.grow_by(bounds.size.length() * 0.001);
  899. if (gen_probes == GENERATE_PROBES_DISABLED) {
  900. // generate 8 probes on bound endpoints
  901. for (int i = 0; i < 8; i++) {
  902. probes_found.push_back(bounds.get_endpoint(i));
  903. }
  904. } else {
  905. // detect probes from geometry
  906. static const int subdiv_values[6] = { 0, 4, 8, 16, 32 };
  907. int subdiv = subdiv_values[gen_probes];
  908. float subdiv_cell_size;
  909. Vector3i bound_limit;
  910. {
  911. int longest_axis = bounds.get_longest_axis_index();
  912. subdiv_cell_size = bounds.size[longest_axis] / subdiv;
  913. int axis_n1 = (longest_axis + 1) % 3;
  914. int axis_n2 = (longest_axis + 2) % 3;
  915. bound_limit[longest_axis] = subdiv;
  916. bound_limit[axis_n1] = int(Math::ceil(bounds.size[axis_n1] / subdiv_cell_size));
  917. bound_limit[axis_n2] = int(Math::ceil(bounds.size[axis_n2] / subdiv_cell_size));
  918. //compensate bounds
  919. bounds.size[axis_n1] = bound_limit[axis_n1] * subdiv_cell_size;
  920. bounds.size[axis_n2] = bound_limit[axis_n2] * subdiv_cell_size;
  921. }
  922. GenProbesOctree octree;
  923. octree.size = subdiv;
  924. for (int i = 0; i < mesh_data.size(); i++) {
  925. if (p_bake_step) {
  926. float p = (float)(i) / mesh_data.size();
  927. p_bake_step(0.3 + p * 0.1, vformat(RTR("Creating probes from mesh %d/%d"), i, mesh_data.size()), p_bake_userdata, false);
  928. }
  929. for (int j = 0; j < mesh_data[i].points.size(); j += 3) {
  930. 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 };
  931. _plot_triangle_into_octree(&octree, subdiv_cell_size, points);
  932. }
  933. }
  934. LocalVector<Vector3> new_probe_positions;
  935. HashMap<Vector3i, bool> positions_used;
  936. for (uint32_t i = 0; i < 8; i++) { //insert bounding endpoints
  937. Vector3i pos;
  938. if (i & 1) {
  939. pos.x += bound_limit.x;
  940. }
  941. if (i & 2) {
  942. pos.y += bound_limit.y;
  943. }
  944. if (i & 4) {
  945. pos.z += bound_limit.z;
  946. }
  947. positions_used[pos] = true;
  948. Vector3 real_pos = bounds.position + Vector3(pos) * subdiv_cell_size; //use same formula for numerical stability
  949. new_probe_positions.push_back(real_pos);
  950. }
  951. //skip first level, since probes are always added at bounds endpoints anyway (code above this)
  952. for (int i = 0; i < 8; i++) {
  953. if (octree.children[i]) {
  954. _gen_new_positions_from_octree(octree.children[i], subdiv_cell_size, probes_found, new_probe_positions, positions_used, bounds);
  955. }
  956. }
  957. for (const Vector3 &position : new_probe_positions) {
  958. probes_found.push_back(position);
  959. }
  960. }
  961. // Add everything to lightmapper
  962. const bool use_physical_light_units = GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units");
  963. if (p_bake_step) {
  964. p_bake_step(0.4, RTR("Preparing Lightmapper"), p_bake_userdata, true);
  965. }
  966. {
  967. for (int i = 0; i < mesh_data.size(); i++) {
  968. lightmapper->add_mesh(mesh_data[i]);
  969. }
  970. for (int i = 0; i < lights_found.size(); i++) {
  971. Light3D *light = lights_found[i].light;
  972. if (light->is_editor_only()) {
  973. // Don't include editor-only lights in the lightmap bake,
  974. // as this results in inconsistent visuals when running the project.
  975. continue;
  976. }
  977. Transform3D xf = lights_found[i].xform;
  978. // For the lightmapper, the indirect energy represents the multiplier for the indirect bounces caused by the light, so the value is not converted when using physical units.
  979. float indirect_energy = light->get_param(Light3D::PARAM_INDIRECT_ENERGY);
  980. Color linear_color = light->get_color().srgb_to_linear();
  981. float energy = light->get_param(Light3D::PARAM_ENERGY);
  982. if (use_physical_light_units) {
  983. energy *= light->get_param(Light3D::PARAM_INTENSITY);
  984. linear_color *= light->get_correlated_color().srgb_to_linear();
  985. }
  986. if (Object::cast_to<DirectionalLight3D>(light)) {
  987. DirectionalLight3D *l = Object::cast_to<DirectionalLight3D>(light);
  988. if (l->get_sky_mode() != DirectionalLight3D::SKY_MODE_SKY_ONLY) {
  989. lightmapper->add_directional_light(light->get_name(), light->get_bake_mode() == Light3D::BAKE_STATIC, -xf.basis.get_column(Vector3::AXIS_Z).normalized(), linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR));
  990. }
  991. } else if (Object::cast_to<OmniLight3D>(light)) {
  992. OmniLight3D *l = Object::cast_to<OmniLight3D>(light);
  993. if (use_physical_light_units) {
  994. energy *= (1.0 / (Math_PI * 4.0));
  995. }
  996. lightmapper->add_omni_light(light->get_name(), light->get_bake_mode() == Light3D::BAKE_STATIC, xf.origin, linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_RANGE), l->get_param(Light3D::PARAM_ATTENUATION), l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR));
  997. } else if (Object::cast_to<SpotLight3D>(light)) {
  998. SpotLight3D *l = Object::cast_to<SpotLight3D>(light);
  999. if (use_physical_light_units) {
  1000. energy *= (1.0 / Math_PI);
  1001. }
  1002. lightmapper->add_spot_light(light->get_name(), light->get_bake_mode() == Light3D::BAKE_STATIC, xf.origin, -xf.basis.get_column(Vector3::AXIS_Z).normalized(), linear_color, energy, indirect_energy, l->get_param(Light3D::PARAM_RANGE), l->get_param(Light3D::PARAM_ATTENUATION), l->get_param(Light3D::PARAM_SPOT_ANGLE), l->get_param(Light3D::PARAM_SPOT_ATTENUATION), l->get_param(Light3D::PARAM_SIZE), l->get_param(Light3D::PARAM_SHADOW_BLUR));
  1003. }
  1004. }
  1005. for (int i = 0; i < probes_found.size(); i++) {
  1006. lightmapper->add_probe(probes_found[i]);
  1007. }
  1008. }
  1009. Ref<Image> environment_image;
  1010. Basis environment_transform;
  1011. // Add everything to lightmapper
  1012. if (environment_mode != ENVIRONMENT_MODE_DISABLED) {
  1013. if (p_bake_step) {
  1014. p_bake_step(4.1, RTR("Preparing Environment"), p_bake_userdata, true);
  1015. }
  1016. environment_transform = get_global_transform().basis;
  1017. switch (environment_mode) {
  1018. case ENVIRONMENT_MODE_DISABLED: {
  1019. //nothing
  1020. } break;
  1021. case ENVIRONMENT_MODE_SCENE: {
  1022. Ref<World3D> world = get_world_3d();
  1023. if (world.is_valid()) {
  1024. Ref<Environment> env = world->get_environment();
  1025. if (env.is_null()) {
  1026. env = world->get_fallback_environment();
  1027. }
  1028. if (env.is_valid()) {
  1029. environment_image = RS::get_singleton()->environment_bake_panorama(env->get_rid(), true, Size2i(128, 64));
  1030. environment_transform = Basis::from_euler(env->get_sky_rotation()).inverse();
  1031. }
  1032. }
  1033. } break;
  1034. case ENVIRONMENT_MODE_CUSTOM_SKY: {
  1035. if (environment_custom_sky.is_valid()) {
  1036. environment_image = RS::get_singleton()->sky_bake_panorama(environment_custom_sky->get_rid(), environment_custom_energy, true, Size2i(128, 64));
  1037. }
  1038. } break;
  1039. case ENVIRONMENT_MODE_CUSTOM_COLOR: {
  1040. environment_image.instantiate();
  1041. environment_image->initialize_data(128, 64, false, Image::FORMAT_RGBAF);
  1042. Color c = environment_custom_color;
  1043. c.r *= environment_custom_energy;
  1044. c.g *= environment_custom_energy;
  1045. c.b *= environment_custom_energy;
  1046. environment_image->fill(c);
  1047. } break;
  1048. }
  1049. }
  1050. float exposure_normalization = 1.0;
  1051. if (camera_attributes.is_valid()) {
  1052. exposure_normalization = camera_attributes->get_exposure_multiplier();
  1053. if (use_physical_light_units) {
  1054. exposure_normalization = camera_attributes->calculate_exposure_normalization();
  1055. }
  1056. }
  1057. Lightmapper::BakeError bake_err = lightmapper->bake(Lightmapper::BakeQuality(bake_quality), use_denoiser, denoiser_strength, denoiser_range, bounces,
  1058. bounce_indirect_energy, bias, max_texture_size, directional, shadowmask_mode != LightmapGIData::SHADOWMASK_MODE_NONE, use_texture_for_bounces,
  1059. Lightmapper::GenerateProbes(gen_probes), environment_image, environment_transform, _lightmap_bake_step_function, &bsud, exposure_normalization, (supersampling_enabled ? supersampling_factor : 1));
  1060. if (bake_err == Lightmapper::BAKE_ERROR_TEXTURE_EXCEEDS_MAX_SIZE) {
  1061. return BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL;
  1062. } else if (bake_err == Lightmapper::BAKE_ERROR_LIGHTMAP_CANT_PRE_BAKE_MESHES) {
  1063. return BAKE_ERROR_MESHES_INVALID;
  1064. } else if (bake_err == Lightmapper::BAKE_ERROR_ATLAS_TOO_SMALL) {
  1065. return BAKE_ERROR_ATLAS_TOO_SMALL;
  1066. } else if (bake_err == Lightmapper::BAKE_ERROR_USER_ABORTED) {
  1067. return BAKE_ERROR_USER_ABORTED;
  1068. }
  1069. // POSTBAKE: Save Textures.
  1070. TypedArray<TextureLayered> lightmap_textures;
  1071. TypedArray<TextureLayered> shadowmask_textures;
  1072. const String texture_filename = p_image_data_path.get_basename();
  1073. const int shadowmask_texture_count = lightmapper->get_shadowmask_texture_count();
  1074. const bool save_shadowmask = shadowmask_mode != LightmapGIData::SHADOWMASK_MODE_NONE && shadowmask_texture_count > 0;
  1075. // Save the lightmap atlases.
  1076. BakeError save_err = _save_and_reimport_atlas_textures(lightmapper, texture_filename, lightmap_textures, false);
  1077. ERR_FAIL_COND_V(save_err != BAKE_ERROR_OK, save_err);
  1078. if (save_shadowmask) {
  1079. // Save the shadowmask atlases.
  1080. save_err = _save_and_reimport_atlas_textures(lightmapper, texture_filename + "_shadow", shadowmask_textures, true);
  1081. ERR_FAIL_COND_V(save_err != BAKE_ERROR_OK, save_err);
  1082. }
  1083. // POSTBAKE: Save Light Data.
  1084. Ref<LightmapGIData> gi_data;
  1085. if (get_light_data().is_valid()) {
  1086. gi_data = get_light_data();
  1087. set_light_data(Ref<LightmapGIData>()); // Clear.
  1088. gi_data->clear();
  1089. } else {
  1090. gi_data.instantiate();
  1091. }
  1092. gi_data->set_lightmap_textures(lightmap_textures);
  1093. if (save_shadowmask) {
  1094. gi_data->set_shadowmask_textures(shadowmask_textures);
  1095. } else {
  1096. gi_data->clear_shadowmask_textures();
  1097. }
  1098. gi_data->set_uses_spherical_harmonics(directional);
  1099. gi_data->_set_uses_packed_directional(directional); // New SH lightmaps are packed automatically.
  1100. for (int i = 0; i < lightmapper->get_bake_mesh_count(); i++) {
  1101. Dictionary d = lightmapper->get_bake_mesh_userdata(i);
  1102. NodePath np = d["path"];
  1103. int32_t subindex = -1;
  1104. if (d.has("subindex")) {
  1105. subindex = d["subindex"];
  1106. }
  1107. Rect2 uv_scale = lightmapper->get_bake_mesh_uv_scale(i);
  1108. int slice_index = lightmapper->get_bake_mesh_texture_slice(i);
  1109. gi_data->add_user(np, uv_scale, slice_index, subindex);
  1110. }
  1111. {
  1112. // Create tetrahedrons.
  1113. Vector<Vector3> points;
  1114. Vector<Color> sh;
  1115. points.resize(lightmapper->get_bake_probe_count());
  1116. sh.resize(lightmapper->get_bake_probe_count() * 9);
  1117. for (int i = 0; i < lightmapper->get_bake_probe_count(); i++) {
  1118. points.write[i] = lightmapper->get_bake_probe_point(i);
  1119. Vector<Color> colors = lightmapper->get_bake_probe_sh(i);
  1120. ERR_CONTINUE(colors.size() != 9);
  1121. for (int j = 0; j < 9; j++) {
  1122. sh.write[i * 9 + j] = colors[j];
  1123. }
  1124. }
  1125. // Obtain solved simplices.
  1126. if (p_bake_step) {
  1127. p_bake_step(0.8, RTR("Generating Probe Volumes"), p_bake_userdata, true);
  1128. }
  1129. Vector<Delaunay3D::OutputSimplex> solved_simplices = Delaunay3D::tetrahedralize(points);
  1130. LocalVector<BSPSimplex> bsp_simplices;
  1131. LocalVector<Plane> bsp_planes;
  1132. LocalVector<int32_t> bsp_simplex_indices;
  1133. PackedInt32Array tetrahedrons;
  1134. for (int i = 0; i < solved_simplices.size(); i++) {
  1135. //Prepare a special representation of the simplex, which uses a BSP Tree
  1136. BSPSimplex bsp_simplex;
  1137. for (int j = 0; j < 4; j++) {
  1138. bsp_simplex.vertices[j] = solved_simplices[i].points[j];
  1139. }
  1140. for (int j = 0; j < 4; j++) {
  1141. static const int face_order[4][3] = {
  1142. { 0, 1, 2 },
  1143. { 0, 2, 3 },
  1144. { 0, 1, 3 },
  1145. { 1, 2, 3 }
  1146. };
  1147. Vector3 a = points[solved_simplices[i].points[face_order[j][0]]];
  1148. Vector3 b = points[solved_simplices[i].points[face_order[j][1]]];
  1149. Vector3 c = points[solved_simplices[i].points[face_order[j][2]]];
  1150. //store planes in an array, but ensure they are reused, to speed up processing
  1151. Plane p(a, b, c);
  1152. int plane_index = -1;
  1153. for (uint32_t k = 0; k < bsp_planes.size(); k++) {
  1154. if (bsp_planes[k].is_equal_approx_any_side(p)) {
  1155. plane_index = k;
  1156. break;
  1157. }
  1158. }
  1159. if (plane_index == -1) {
  1160. plane_index = bsp_planes.size();
  1161. bsp_planes.push_back(p);
  1162. }
  1163. bsp_simplex.planes[j] = plane_index;
  1164. //also fill simplex array
  1165. tetrahedrons.push_back(solved_simplices[i].points[j]);
  1166. }
  1167. bsp_simplex_indices.push_back(bsp_simplices.size());
  1168. bsp_simplices.push_back(bsp_simplex);
  1169. }
  1170. //#define DEBUG_SIMPLICES_AS_OBJ_FILE
  1171. #ifdef DEBUG_SIMPLICES_AS_OBJ_FILE
  1172. {
  1173. Ref<FileAccess> f = FileAccess::open("res://bsp.obj", FileAccess::WRITE);
  1174. for (uint32_t i = 0; i < bsp_simplices.size(); i++) {
  1175. f->store_line("o Simplex" + itos(i));
  1176. for (int j = 0; j < 4; j++) {
  1177. 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));
  1178. }
  1179. static const int face_order[4][3] = {
  1180. { 1, 2, 3 },
  1181. { 1, 3, 4 },
  1182. { 1, 2, 4 },
  1183. { 2, 3, 4 }
  1184. };
  1185. for (int j = 0; j < 4; j++) {
  1186. 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]));
  1187. }
  1188. }
  1189. }
  1190. #endif
  1191. LocalVector<BSPNode> bsp_nodes;
  1192. LocalVector<int32_t> planes_tested;
  1193. planes_tested.resize(bsp_planes.size());
  1194. for (int &index : planes_tested) {
  1195. index = 0x7FFFFFFF;
  1196. }
  1197. if (p_bake_step) {
  1198. p_bake_step(0.9, RTR("Generating Probe Acceleration Structures"), p_bake_userdata, true);
  1199. }
  1200. _compute_bsp_tree(points, bsp_planes, planes_tested, bsp_simplices, bsp_simplex_indices, bsp_nodes);
  1201. PackedInt32Array bsp_array;
  1202. bsp_array.resize(bsp_nodes.size() * 6); // six 32 bits values used for each BSP node
  1203. {
  1204. float *fptr = (float *)bsp_array.ptrw();
  1205. int32_t *iptr = (int32_t *)bsp_array.ptrw();
  1206. for (uint32_t i = 0; i < bsp_nodes.size(); i++) {
  1207. fptr[i * 6 + 0] = bsp_nodes[i].plane.normal.x;
  1208. fptr[i * 6 + 1] = bsp_nodes[i].plane.normal.y;
  1209. fptr[i * 6 + 2] = bsp_nodes[i].plane.normal.z;
  1210. fptr[i * 6 + 3] = bsp_nodes[i].plane.d;
  1211. iptr[i * 6 + 4] = bsp_nodes[i].over;
  1212. iptr[i * 6 + 5] = bsp_nodes[i].under;
  1213. }
  1214. //#define DEBUG_BSP_TREE
  1215. #ifdef DEBUG_BSP_TREE
  1216. Ref<FileAccess> f = FileAccess::open("res://bsp.txt", FileAccess::WRITE);
  1217. for (uint32_t i = 0; i < bsp_nodes.size(); i++) {
  1218. f->store_line(itos(i) + " - plane: " + bsp_nodes[i].plane + " over: " + itos(bsp_nodes[i].over) + " under: " + itos(bsp_nodes[i].under));
  1219. }
  1220. #endif
  1221. }
  1222. /* Obtain the colors from the images, they will be re-created as cubemaps on the server, depending on the driver */
  1223. gi_data->set_capture_data(bounds, interior, points, sh, tetrahedrons, bsp_array, exposure_normalization);
  1224. /* Compute a BSP tree of the simplices, so it's easy to find the exact one */
  1225. }
  1226. gi_data->set_path(p_image_data_path, true);
  1227. Error err = ResourceSaver::save(gi_data);
  1228. if (err != OK) {
  1229. return BAKE_ERROR_CANT_CREATE_IMAGE;
  1230. }
  1231. set_light_data(gi_data);
  1232. update_configuration_warnings();
  1233. return BAKE_ERROR_OK;
  1234. }
  1235. void LightmapGI::_notification(int p_what) {
  1236. switch (p_what) {
  1237. case NOTIFICATION_POST_ENTER_TREE: {
  1238. if (light_data.is_valid()) {
  1239. ERR_FAIL_COND_MSG(
  1240. light_data->is_using_spherical_harmonics() && !light_data->_is_using_packed_directional(),
  1241. vformat(
  1242. "%s (%s): The directional lightmap textures are stored in a format that isn't supported anymore. Please bake lightmaps again to make lightmaps display from this node again.",
  1243. get_light_data()->get_path(), get_name()));
  1244. if (last_owner && last_owner != get_owner()) {
  1245. light_data->clear_users();
  1246. }
  1247. _assign_lightmaps();
  1248. }
  1249. } break;
  1250. case NOTIFICATION_EXIT_TREE: {
  1251. last_owner = get_owner();
  1252. if (light_data.is_valid()) {
  1253. _clear_lightmaps();
  1254. }
  1255. } break;
  1256. }
  1257. }
  1258. void LightmapGI::_assign_lightmaps() {
  1259. ERR_FAIL_COND(light_data.is_null());
  1260. for (int i = 0; i < light_data->get_user_count(); i++) {
  1261. Node *node = get_node(light_data->get_user_path(i));
  1262. int instance_idx = light_data->get_user_sub_instance(i);
  1263. if (instance_idx >= 0) {
  1264. RID instance_id = node->call("get_bake_mesh_instance", instance_idx);
  1265. if (instance_id.is_valid()) {
  1266. 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));
  1267. }
  1268. } else {
  1269. VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node);
  1270. ERR_CONTINUE(!vi);
  1271. 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));
  1272. }
  1273. }
  1274. }
  1275. void LightmapGI::_clear_lightmaps() {
  1276. ERR_FAIL_COND(light_data.is_null());
  1277. for (int i = 0; i < light_data->get_user_count(); i++) {
  1278. Node *node = get_node(light_data->get_user_path(i));
  1279. int instance_idx = light_data->get_user_sub_instance(i);
  1280. if (instance_idx >= 0) {
  1281. RID instance_id = node->call("get_bake_mesh_instance", instance_idx);
  1282. if (instance_id.is_valid()) {
  1283. RS::get_singleton()->instance_geometry_set_lightmap(instance_id, RID(), Rect2(), 0);
  1284. }
  1285. } else {
  1286. VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(node);
  1287. ERR_CONTINUE(!vi);
  1288. RS::get_singleton()->instance_geometry_set_lightmap(vi->get_instance(), RID(), Rect2(), 0);
  1289. }
  1290. }
  1291. }
  1292. void LightmapGI::set_light_data(const Ref<LightmapGIData> &p_data) {
  1293. if (light_data.is_valid()) {
  1294. if (is_inside_tree()) {
  1295. _clear_lightmaps();
  1296. }
  1297. set_base(RID());
  1298. }
  1299. light_data = p_data;
  1300. if (light_data.is_valid()) {
  1301. set_base(light_data->get_rid());
  1302. if (is_inside_tree()) {
  1303. _assign_lightmaps();
  1304. }
  1305. light_data->update_shadowmask_mode(shadowmask_mode);
  1306. }
  1307. update_gizmos();
  1308. }
  1309. Ref<LightmapGIData> LightmapGI::get_light_data() const {
  1310. return light_data;
  1311. }
  1312. void LightmapGI::set_bake_quality(BakeQuality p_quality) {
  1313. bake_quality = p_quality;
  1314. }
  1315. LightmapGI::BakeQuality LightmapGI::get_bake_quality() const {
  1316. return bake_quality;
  1317. }
  1318. AABB LightmapGI::get_aabb() const {
  1319. return AABB();
  1320. }
  1321. void LightmapGI::set_use_denoiser(bool p_enable) {
  1322. use_denoiser = p_enable;
  1323. notify_property_list_changed();
  1324. }
  1325. bool LightmapGI::is_using_denoiser() const {
  1326. return use_denoiser;
  1327. }
  1328. void LightmapGI::set_denoiser_strength(float p_denoiser_strength) {
  1329. denoiser_strength = p_denoiser_strength;
  1330. }
  1331. float LightmapGI::get_denoiser_strength() const {
  1332. return denoiser_strength;
  1333. }
  1334. void LightmapGI::set_denoiser_range(int p_denoiser_range) {
  1335. denoiser_range = p_denoiser_range;
  1336. }
  1337. int LightmapGI::get_denoiser_range() const {
  1338. return denoiser_range;
  1339. }
  1340. void LightmapGI::set_directional(bool p_enable) {
  1341. directional = p_enable;
  1342. }
  1343. bool LightmapGI::is_directional() const {
  1344. return directional;
  1345. }
  1346. void LightmapGI::set_shadowmask_mode(LightmapGIData::ShadowmaskMode p_mode) {
  1347. shadowmask_mode = p_mode;
  1348. if (light_data.is_valid()) {
  1349. light_data->update_shadowmask_mode(p_mode);
  1350. }
  1351. update_configuration_warnings();
  1352. }
  1353. LightmapGIData::ShadowmaskMode LightmapGI::get_shadowmask_mode() const {
  1354. return shadowmask_mode;
  1355. }
  1356. void LightmapGI::set_use_texture_for_bounces(bool p_enable) {
  1357. use_texture_for_bounces = p_enable;
  1358. }
  1359. bool LightmapGI::is_using_texture_for_bounces() const {
  1360. return use_texture_for_bounces;
  1361. }
  1362. void LightmapGI::set_interior(bool p_enable) {
  1363. interior = p_enable;
  1364. }
  1365. bool LightmapGI::is_interior() const {
  1366. return interior;
  1367. }
  1368. void LightmapGI::set_environment_mode(EnvironmentMode p_mode) {
  1369. environment_mode = p_mode;
  1370. notify_property_list_changed();
  1371. }
  1372. LightmapGI::EnvironmentMode LightmapGI::get_environment_mode() const {
  1373. return environment_mode;
  1374. }
  1375. void LightmapGI::set_environment_custom_sky(const Ref<Sky> &p_sky) {
  1376. environment_custom_sky = p_sky;
  1377. }
  1378. Ref<Sky> LightmapGI::get_environment_custom_sky() const {
  1379. return environment_custom_sky;
  1380. }
  1381. void LightmapGI::set_environment_custom_color(const Color &p_color) {
  1382. environment_custom_color = p_color;
  1383. }
  1384. Color LightmapGI::get_environment_custom_color() const {
  1385. return environment_custom_color;
  1386. }
  1387. void LightmapGI::set_environment_custom_energy(float p_energy) {
  1388. environment_custom_energy = p_energy;
  1389. }
  1390. float LightmapGI::get_environment_custom_energy() const {
  1391. return environment_custom_energy;
  1392. }
  1393. void LightmapGI::set_bounces(int p_bounces) {
  1394. ERR_FAIL_COND(p_bounces < 0 || p_bounces > 16);
  1395. bounces = p_bounces;
  1396. }
  1397. int LightmapGI::get_bounces() const {
  1398. return bounces;
  1399. }
  1400. void LightmapGI::set_bounce_indirect_energy(float p_indirect_energy) {
  1401. ERR_FAIL_COND(p_indirect_energy < 0.0);
  1402. bounce_indirect_energy = p_indirect_energy;
  1403. }
  1404. float LightmapGI::get_bounce_indirect_energy() const {
  1405. return bounce_indirect_energy;
  1406. }
  1407. void LightmapGI::set_bias(float p_bias) {
  1408. ERR_FAIL_COND(p_bias < 0.00001);
  1409. bias = p_bias;
  1410. }
  1411. float LightmapGI::get_bias() const {
  1412. return bias;
  1413. }
  1414. void LightmapGI::set_texel_scale(float p_multiplier) {
  1415. ERR_FAIL_COND(p_multiplier < (0.01 - CMP_EPSILON));
  1416. texel_scale = p_multiplier;
  1417. }
  1418. float LightmapGI::get_texel_scale() const {
  1419. return texel_scale;
  1420. }
  1421. void LightmapGI::set_max_texture_size(int p_size) {
  1422. 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));
  1423. 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));
  1424. max_texture_size = p_size;
  1425. }
  1426. int LightmapGI::get_max_texture_size() const {
  1427. return max_texture_size;
  1428. }
  1429. void LightmapGI::set_supersampling_enabled(bool p_enable) {
  1430. supersampling_enabled = p_enable;
  1431. notify_property_list_changed();
  1432. }
  1433. bool LightmapGI::is_supersampling_enabled() const {
  1434. return supersampling_enabled;
  1435. }
  1436. void LightmapGI::set_supersampling_factor(float p_factor) {
  1437. ERR_FAIL_COND(p_factor < 1);
  1438. supersampling_factor = p_factor;
  1439. }
  1440. float LightmapGI::get_supersampling_factor() const {
  1441. return supersampling_factor;
  1442. }
  1443. void LightmapGI::set_generate_probes(GenerateProbes p_generate_probes) {
  1444. gen_probes = p_generate_probes;
  1445. }
  1446. LightmapGI::GenerateProbes LightmapGI::get_generate_probes() const {
  1447. return gen_probes;
  1448. }
  1449. void LightmapGI::set_camera_attributes(const Ref<CameraAttributes> &p_camera_attributes) {
  1450. camera_attributes = p_camera_attributes;
  1451. }
  1452. Ref<CameraAttributes> LightmapGI::get_camera_attributes() const {
  1453. return camera_attributes;
  1454. }
  1455. PackedStringArray LightmapGI::get_configuration_warnings() const {
  1456. PackedStringArray warnings = VisualInstance3D::get_configuration_warnings();
  1457. #ifdef MODULE_LIGHTMAPPER_RD_ENABLED
  1458. if (!DisplayServer::get_singleton()->can_create_rendering_device()) {
  1459. warnings.push_back(vformat(RTR("Lightmaps can only be baked from a GPU that supports the RenderingDevice backends.\nYour GPU (%s) does not support RenderingDevice, as it does not support Vulkan, Direct3D 12, or Metal.\nLightmap baking will not be available on this device, although rendering existing baked lightmaps will work."), RenderingServer::get_singleton()->get_video_adapter_name()));
  1460. return warnings;
  1461. }
  1462. if (shadowmask_mode != LightmapGIData::SHADOWMASK_MODE_NONE && light_data.is_valid() && !light_data->has_shadowmask_textures()) {
  1463. warnings.push_back(RTR("The lightmap has no baked shadowmask textures. Please rebake with the Shadowmask Mode set to anything other than None."));
  1464. }
  1465. #elif defined(ANDROID_ENABLED) || defined(IOS_ENABLED)
  1466. warnings.push_back(vformat(RTR("Lightmaps cannot be baked on %s. Rendering existing baked lightmaps will still work."), OS::get_singleton()->get_name()));
  1467. #else
  1468. warnings.push_back(RTR("Lightmaps cannot be baked, as the `lightmapper_rd` module was disabled at compile-time. Rendering existing baked lightmaps will still work."));
  1469. #endif
  1470. return warnings;
  1471. }
  1472. void LightmapGI::_validate_property(PropertyInfo &p_property) const {
  1473. if (p_property.name == "supersampling_factor" && !supersampling_enabled) {
  1474. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1475. }
  1476. if (p_property.name == "environment_custom_sky" && environment_mode != ENVIRONMENT_MODE_CUSTOM_SKY) {
  1477. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1478. }
  1479. if (p_property.name == "environment_custom_color" && environment_mode != ENVIRONMENT_MODE_CUSTOM_COLOR) {
  1480. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1481. }
  1482. if (p_property.name == "environment_custom_energy" && environment_mode != ENVIRONMENT_MODE_CUSTOM_COLOR && environment_mode != ENVIRONMENT_MODE_CUSTOM_SKY) {
  1483. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1484. }
  1485. if (p_property.name == "denoiser_strength" && !use_denoiser) {
  1486. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1487. }
  1488. if (p_property.name == "denoiser_range" && !use_denoiser) {
  1489. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  1490. }
  1491. }
  1492. void LightmapGI::_bind_methods() {
  1493. ClassDB::bind_method(D_METHOD("set_light_data", "data"), &LightmapGI::set_light_data);
  1494. ClassDB::bind_method(D_METHOD("get_light_data"), &LightmapGI::get_light_data);
  1495. ClassDB::bind_method(D_METHOD("set_bake_quality", "bake_quality"), &LightmapGI::set_bake_quality);
  1496. ClassDB::bind_method(D_METHOD("get_bake_quality"), &LightmapGI::get_bake_quality);
  1497. ClassDB::bind_method(D_METHOD("set_bounces", "bounces"), &LightmapGI::set_bounces);
  1498. ClassDB::bind_method(D_METHOD("get_bounces"), &LightmapGI::get_bounces);
  1499. ClassDB::bind_method(D_METHOD("set_bounce_indirect_energy", "bounce_indirect_energy"), &LightmapGI::set_bounce_indirect_energy);
  1500. ClassDB::bind_method(D_METHOD("get_bounce_indirect_energy"), &LightmapGI::get_bounce_indirect_energy);
  1501. ClassDB::bind_method(D_METHOD("set_generate_probes", "subdivision"), &LightmapGI::set_generate_probes);
  1502. ClassDB::bind_method(D_METHOD("get_generate_probes"), &LightmapGI::get_generate_probes);
  1503. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &LightmapGI::set_bias);
  1504. ClassDB::bind_method(D_METHOD("get_bias"), &LightmapGI::get_bias);
  1505. ClassDB::bind_method(D_METHOD("set_environment_mode", "mode"), &LightmapGI::set_environment_mode);
  1506. ClassDB::bind_method(D_METHOD("get_environment_mode"), &LightmapGI::get_environment_mode);
  1507. ClassDB::bind_method(D_METHOD("set_environment_custom_sky", "sky"), &LightmapGI::set_environment_custom_sky);
  1508. ClassDB::bind_method(D_METHOD("get_environment_custom_sky"), &LightmapGI::get_environment_custom_sky);
  1509. ClassDB::bind_method(D_METHOD("set_environment_custom_color", "color"), &LightmapGI::set_environment_custom_color);
  1510. ClassDB::bind_method(D_METHOD("get_environment_custom_color"), &LightmapGI::get_environment_custom_color);
  1511. ClassDB::bind_method(D_METHOD("set_environment_custom_energy", "energy"), &LightmapGI::set_environment_custom_energy);
  1512. ClassDB::bind_method(D_METHOD("get_environment_custom_energy"), &LightmapGI::get_environment_custom_energy);
  1513. ClassDB::bind_method(D_METHOD("set_texel_scale", "texel_scale"), &LightmapGI::set_texel_scale);
  1514. ClassDB::bind_method(D_METHOD("get_texel_scale"), &LightmapGI::get_texel_scale);
  1515. ClassDB::bind_method(D_METHOD("set_max_texture_size", "max_texture_size"), &LightmapGI::set_max_texture_size);
  1516. ClassDB::bind_method(D_METHOD("get_max_texture_size"), &LightmapGI::get_max_texture_size);
  1517. ClassDB::bind_method(D_METHOD("set_supersampling_enabled", "enable"), &LightmapGI::set_supersampling_enabled);
  1518. ClassDB::bind_method(D_METHOD("is_supersampling_enabled"), &LightmapGI::is_supersampling_enabled);
  1519. ClassDB::bind_method(D_METHOD("set_supersampling_factor", "factor"), &LightmapGI::set_supersampling_factor);
  1520. ClassDB::bind_method(D_METHOD("get_supersampling_factor"), &LightmapGI::get_supersampling_factor);
  1521. ClassDB::bind_method(D_METHOD("set_use_denoiser", "use_denoiser"), &LightmapGI::set_use_denoiser);
  1522. ClassDB::bind_method(D_METHOD("is_using_denoiser"), &LightmapGI::is_using_denoiser);
  1523. ClassDB::bind_method(D_METHOD("set_denoiser_strength", "denoiser_strength"), &LightmapGI::set_denoiser_strength);
  1524. ClassDB::bind_method(D_METHOD("get_denoiser_strength"), &LightmapGI::get_denoiser_strength);
  1525. ClassDB::bind_method(D_METHOD("set_denoiser_range", "denoiser_range"), &LightmapGI::set_denoiser_range);
  1526. ClassDB::bind_method(D_METHOD("get_denoiser_range"), &LightmapGI::get_denoiser_range);
  1527. ClassDB::bind_method(D_METHOD("set_interior", "enable"), &LightmapGI::set_interior);
  1528. ClassDB::bind_method(D_METHOD("is_interior"), &LightmapGI::is_interior);
  1529. ClassDB::bind_method(D_METHOD("set_directional", "directional"), &LightmapGI::set_directional);
  1530. ClassDB::bind_method(D_METHOD("is_directional"), &LightmapGI::is_directional);
  1531. ClassDB::bind_method(D_METHOD("set_shadowmask_mode", "mode"), &LightmapGI::set_shadowmask_mode);
  1532. ClassDB::bind_method(D_METHOD("get_shadowmask_mode"), &LightmapGI::get_shadowmask_mode);
  1533. ClassDB::bind_method(D_METHOD("set_use_texture_for_bounces", "use_texture_for_bounces"), &LightmapGI::set_use_texture_for_bounces);
  1534. ClassDB::bind_method(D_METHOD("is_using_texture_for_bounces"), &LightmapGI::is_using_texture_for_bounces);
  1535. ClassDB::bind_method(D_METHOD("set_camera_attributes", "camera_attributes"), &LightmapGI::set_camera_attributes);
  1536. ClassDB::bind_method(D_METHOD("get_camera_attributes"), &LightmapGI::get_camera_attributes);
  1537. // ClassDB::bind_method(D_METHOD("bake", "from_node"), &LightmapGI::bake, DEFVAL(Variant()));
  1538. ADD_GROUP("Tweaks", "");
  1539. ADD_PROPERTY(PropertyInfo(Variant::INT, "quality", PROPERTY_HINT_ENUM, "Low,Medium,High,Ultra"), "set_bake_quality", "get_bake_quality");
  1540. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "supersampling"), "set_supersampling_enabled", "is_supersampling_enabled");
  1541. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "supersampling_factor", PROPERTY_HINT_RANGE, "1,8,1"), "set_supersampling_factor", "get_supersampling_factor");
  1542. ADD_PROPERTY(PropertyInfo(Variant::INT, "bounces", PROPERTY_HINT_RANGE, "0,6,1,or_greater"), "set_bounces", "get_bounces");
  1543. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bounce_indirect_energy", PROPERTY_HINT_RANGE, "0,2,0.01"), "set_bounce_indirect_energy", "get_bounce_indirect_energy");
  1544. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "directional"), "set_directional", "is_directional");
  1545. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadowmask_mode", PROPERTY_HINT_ENUM, "None,Replace,Overlay"), "set_shadowmask_mode", "get_shadowmask_mode");
  1546. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_texture_for_bounces"), "set_use_texture_for_bounces", "is_using_texture_for_bounces");
  1547. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior");
  1548. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_denoiser"), "set_use_denoiser", "is_using_denoiser");
  1549. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "denoiser_strength", PROPERTY_HINT_RANGE, "0.001,0.2,0.001,or_greater"), "set_denoiser_strength", "get_denoiser_strength");
  1550. ADD_PROPERTY(PropertyInfo(Variant::INT, "denoiser_range", PROPERTY_HINT_RANGE, "1,20"), "set_denoiser_range", "get_denoiser_range");
  1551. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0.00001,0.1,0.00001,or_greater"), "set_bias", "get_bias");
  1552. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "texel_scale", PROPERTY_HINT_RANGE, "0.01,100.0,0.01"), "set_texel_scale", "get_texel_scale");
  1553. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_texture_size", PROPERTY_HINT_RANGE, "2048,16384,1"), "set_max_texture_size", "get_max_texture_size");
  1554. ADD_GROUP("Environment", "environment_");
  1555. ADD_PROPERTY(PropertyInfo(Variant::INT, "environment_mode", PROPERTY_HINT_ENUM, "Disabled,Scene,Custom Sky,Custom Color"), "set_environment_mode", "get_environment_mode");
  1556. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment_custom_sky", PROPERTY_HINT_RESOURCE_TYPE, "Sky"), "set_environment_custom_sky", "get_environment_custom_sky");
  1557. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "environment_custom_color", PROPERTY_HINT_COLOR_NO_ALPHA), "set_environment_custom_color", "get_environment_custom_color");
  1558. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "environment_custom_energy", PROPERTY_HINT_RANGE, "0,64,0.01"), "set_environment_custom_energy", "get_environment_custom_energy");
  1559. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "camera_attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_camera_attributes", "get_camera_attributes");
  1560. ADD_GROUP("Gen Probes", "generate_probes_");
  1561. ADD_PROPERTY(PropertyInfo(Variant::INT, "generate_probes_subdiv", PROPERTY_HINT_ENUM, "Disabled,4,8,16,32"), "set_generate_probes", "get_generate_probes");
  1562. ADD_GROUP("Data", "");
  1563. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_data", PROPERTY_HINT_RESOURCE_TYPE, "LightmapGIData"), "set_light_data", "get_light_data");
  1564. BIND_ENUM_CONSTANT(BAKE_QUALITY_LOW);
  1565. BIND_ENUM_CONSTANT(BAKE_QUALITY_MEDIUM);
  1566. BIND_ENUM_CONSTANT(BAKE_QUALITY_HIGH);
  1567. BIND_ENUM_CONSTANT(BAKE_QUALITY_ULTRA);
  1568. BIND_ENUM_CONSTANT(GENERATE_PROBES_DISABLED);
  1569. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_4);
  1570. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_8);
  1571. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_16);
  1572. BIND_ENUM_CONSTANT(GENERATE_PROBES_SUBDIV_32);
  1573. BIND_ENUM_CONSTANT(BAKE_ERROR_OK);
  1574. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SCENE_ROOT);
  1575. BIND_ENUM_CONSTANT(BAKE_ERROR_FOREIGN_DATA);
  1576. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_LIGHTMAPPER);
  1577. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SAVE_PATH);
  1578. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_MESHES);
  1579. BIND_ENUM_CONSTANT(BAKE_ERROR_MESHES_INVALID);
  1580. BIND_ENUM_CONSTANT(BAKE_ERROR_CANT_CREATE_IMAGE);
  1581. BIND_ENUM_CONSTANT(BAKE_ERROR_USER_ABORTED);
  1582. BIND_ENUM_CONSTANT(BAKE_ERROR_TEXTURE_SIZE_TOO_SMALL);
  1583. BIND_ENUM_CONSTANT(BAKE_ERROR_LIGHTMAP_TOO_SMALL);
  1584. BIND_ENUM_CONSTANT(BAKE_ERROR_ATLAS_TOO_SMALL);
  1585. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_DISABLED);
  1586. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_SCENE);
  1587. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_CUSTOM_SKY);
  1588. BIND_ENUM_CONSTANT(ENVIRONMENT_MODE_CUSTOM_COLOR);
  1589. }
  1590. LightmapGI::LightmapGI() {
  1591. }