lightmap_gi.cpp 72 KB

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