lightmap_gi.cpp 71 KB

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