baked_lightmap.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*************************************************************************/
  2. /* baked_lightmap.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "baked_lightmap.h"
  31. #include "core/io/config_file.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/os/dir_access.h"
  34. #include "core/os/os.h"
  35. #include "voxel_light_baker.h"
  36. void BakedLightmapData::set_bounds(const AABB &p_bounds) {
  37. bounds = p_bounds;
  38. VS::get_singleton()->lightmap_capture_set_bounds(baked_light, p_bounds);
  39. }
  40. AABB BakedLightmapData::get_bounds() const {
  41. return bounds;
  42. }
  43. void BakedLightmapData::set_octree(const PoolVector<uint8_t> &p_octree) {
  44. VS::get_singleton()->lightmap_capture_set_octree(baked_light, p_octree);
  45. }
  46. PoolVector<uint8_t> BakedLightmapData::get_octree() const {
  47. return VS::get_singleton()->lightmap_capture_get_octree(baked_light);
  48. }
  49. void BakedLightmapData::set_cell_space_transform(const Transform &p_xform) {
  50. cell_space_xform = p_xform;
  51. VS::get_singleton()->lightmap_capture_set_octree_cell_transform(baked_light, p_xform);
  52. }
  53. Transform BakedLightmapData::get_cell_space_transform() const {
  54. return cell_space_xform;
  55. }
  56. void BakedLightmapData::set_cell_subdiv(int p_cell_subdiv) {
  57. cell_subdiv = p_cell_subdiv;
  58. VS::get_singleton()->lightmap_capture_set_octree_cell_subdiv(baked_light, p_cell_subdiv);
  59. }
  60. int BakedLightmapData::get_cell_subdiv() const {
  61. return cell_subdiv;
  62. }
  63. void BakedLightmapData::set_energy(float p_energy) {
  64. energy = p_energy;
  65. VS::get_singleton()->lightmap_capture_set_energy(baked_light, energy);
  66. }
  67. float BakedLightmapData::get_energy() const {
  68. return energy;
  69. }
  70. void BakedLightmapData::add_user(const NodePath &p_path, const Ref<Texture> &p_lightmap, int p_instance) {
  71. ERR_FAIL_COND(p_lightmap.is_null());
  72. User user;
  73. user.path = p_path;
  74. user.lightmap = p_lightmap;
  75. user.instance_index = p_instance;
  76. users.push_back(user);
  77. }
  78. int BakedLightmapData::get_user_count() const {
  79. return users.size();
  80. }
  81. NodePath BakedLightmapData::get_user_path(int p_user) const {
  82. ERR_FAIL_INDEX_V(p_user, users.size(), NodePath());
  83. return users[p_user].path;
  84. }
  85. Ref<Texture> BakedLightmapData::get_user_lightmap(int p_user) const {
  86. ERR_FAIL_INDEX_V(p_user, users.size(), Ref<Texture>());
  87. return users[p_user].lightmap;
  88. }
  89. int BakedLightmapData::get_user_instance(int p_user) const {
  90. ERR_FAIL_INDEX_V(p_user, users.size(), -1);
  91. return users[p_user].instance_index;
  92. }
  93. void BakedLightmapData::clear_users() {
  94. users.clear();
  95. }
  96. void BakedLightmapData::_set_user_data(const Array &p_data) {
  97. ERR_FAIL_COND((p_data.size() % 3) != 0);
  98. for (int i = 0; i < p_data.size(); i += 3) {
  99. add_user(p_data[i], p_data[i + 1], p_data[i + 2]);
  100. }
  101. }
  102. Array BakedLightmapData::_get_user_data() const {
  103. Array ret;
  104. for (int i = 0; i < users.size(); i++) {
  105. ret.push_back(users[i].path);
  106. ret.push_back(users[i].lightmap);
  107. ret.push_back(users[i].instance_index);
  108. }
  109. return ret;
  110. }
  111. RID BakedLightmapData::get_rid() const {
  112. return baked_light;
  113. }
  114. void BakedLightmapData::_bind_methods() {
  115. ClassDB::bind_method(D_METHOD("_set_user_data", "data"), &BakedLightmapData::_set_user_data);
  116. ClassDB::bind_method(D_METHOD("_get_user_data"), &BakedLightmapData::_get_user_data);
  117. ClassDB::bind_method(D_METHOD("set_bounds", "bounds"), &BakedLightmapData::set_bounds);
  118. ClassDB::bind_method(D_METHOD("get_bounds"), &BakedLightmapData::get_bounds);
  119. ClassDB::bind_method(D_METHOD("set_cell_space_transform", "xform"), &BakedLightmapData::set_cell_space_transform);
  120. ClassDB::bind_method(D_METHOD("get_cell_space_transform"), &BakedLightmapData::get_cell_space_transform);
  121. ClassDB::bind_method(D_METHOD("set_cell_subdiv", "cell_subdiv"), &BakedLightmapData::set_cell_subdiv);
  122. ClassDB::bind_method(D_METHOD("get_cell_subdiv"), &BakedLightmapData::get_cell_subdiv);
  123. ClassDB::bind_method(D_METHOD("set_octree", "octree"), &BakedLightmapData::set_octree);
  124. ClassDB::bind_method(D_METHOD("get_octree"), &BakedLightmapData::get_octree);
  125. ClassDB::bind_method(D_METHOD("set_energy", "energy"), &BakedLightmapData::set_energy);
  126. ClassDB::bind_method(D_METHOD("get_energy"), &BakedLightmapData::get_energy);
  127. ClassDB::bind_method(D_METHOD("add_user", "path", "lightmap", "instance"), &BakedLightmapData::add_user);
  128. ClassDB::bind_method(D_METHOD("get_user_count"), &BakedLightmapData::get_user_count);
  129. ClassDB::bind_method(D_METHOD("get_user_path", "user_idx"), &BakedLightmapData::get_user_path);
  130. ClassDB::bind_method(D_METHOD("get_user_lightmap", "user_idx"), &BakedLightmapData::get_user_lightmap);
  131. ClassDB::bind_method(D_METHOD("clear_users"), &BakedLightmapData::clear_users);
  132. ADD_PROPERTY(PropertyInfo(Variant::AABB, "bounds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_bounds", "get_bounds");
  133. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "cell_space_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_cell_space_transform", "get_cell_space_transform");
  134. ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_subdiv", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_cell_subdiv", "get_cell_subdiv");
  135. ADD_PROPERTY(PropertyInfo(Variant::REAL, "energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_energy", "get_energy");
  136. ADD_PROPERTY(PropertyInfo(Variant::POOL_BYTE_ARRAY, "octree", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_octree", "get_octree");
  137. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "user_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "_set_user_data", "_get_user_data");
  138. }
  139. BakedLightmapData::BakedLightmapData() {
  140. baked_light = VS::get_singleton()->lightmap_capture_create();
  141. energy = 1;
  142. cell_subdiv = 1;
  143. }
  144. BakedLightmapData::~BakedLightmapData() {
  145. VS::get_singleton()->free(baked_light);
  146. }
  147. ///////////////////////////
  148. BakedLightmap::BakeBeginFunc BakedLightmap::bake_begin_function = NULL;
  149. BakedLightmap::BakeStepFunc BakedLightmap::bake_step_function = NULL;
  150. BakedLightmap::BakeEndFunc BakedLightmap::bake_end_function = NULL;
  151. void BakedLightmap::set_bake_cell_size(float p_cell_size) {
  152. bake_cell_size = p_cell_size;
  153. }
  154. float BakedLightmap::get_bake_cell_size() const {
  155. return bake_cell_size;
  156. }
  157. void BakedLightmap::set_capture_cell_size(float p_cell_size) {
  158. capture_cell_size = p_cell_size;
  159. }
  160. float BakedLightmap::get_capture_cell_size() const {
  161. return capture_cell_size;
  162. }
  163. void BakedLightmap::set_extents(const Vector3 &p_extents) {
  164. extents = p_extents;
  165. update_gizmo();
  166. }
  167. Vector3 BakedLightmap::get_extents() const {
  168. return extents;
  169. }
  170. void BakedLightmap::_find_meshes_and_lights(Node *p_at_node, List<PlotMesh> &plot_meshes, List<PlotLight> &plot_lights) {
  171. MeshInstance *mi = Object::cast_to<MeshInstance>(p_at_node);
  172. if (mi && mi->get_flag(GeometryInstance::FLAG_USE_BAKED_LIGHT) && mi->is_visible_in_tree()) {
  173. Ref<Mesh> mesh = mi->get_mesh();
  174. if (mesh.is_valid()) {
  175. bool all_have_uv2 = true;
  176. for (int i = 0; i < mesh->get_surface_count(); i++) {
  177. if (!(mesh->surface_get_format(i) & Mesh::ARRAY_FORMAT_TEX_UV2)) {
  178. all_have_uv2 = false;
  179. break;
  180. }
  181. }
  182. if (all_have_uv2 && mesh->get_lightmap_size_hint() != Size2()) {
  183. //READY TO BAKE! size hint could be computed if not found, actually..
  184. AABB aabb = mesh->get_aabb();
  185. Transform xf = get_global_transform().affine_inverse() * mi->get_global_transform();
  186. if (AABB(-extents, extents * 2).intersects(xf.xform(aabb))) {
  187. PlotMesh pm;
  188. pm.local_xform = xf;
  189. pm.mesh = mesh;
  190. pm.path = get_path_to(mi);
  191. pm.instance_idx = -1;
  192. for (int i = 0; i < mesh->get_surface_count(); i++) {
  193. pm.instance_materials.push_back(mi->get_surface_material(i));
  194. }
  195. pm.override_material = mi->get_material_override();
  196. plot_meshes.push_back(pm);
  197. }
  198. }
  199. }
  200. }
  201. Spatial *s = Object::cast_to<Spatial>(p_at_node);
  202. if (!mi && s) {
  203. Array meshes = p_at_node->call("get_bake_meshes");
  204. if (meshes.size() && (meshes.size() & 1) == 0) {
  205. Transform xf = get_global_transform().affine_inverse() * s->get_global_transform();
  206. for (int i = 0; i < meshes.size(); i += 2) {
  207. PlotMesh pm;
  208. Transform mesh_xf = meshes[i + 1];
  209. pm.local_xform = xf * mesh_xf;
  210. pm.mesh = meshes[i];
  211. pm.instance_idx = i / 2;
  212. if (!pm.mesh.is_valid())
  213. continue;
  214. pm.path = get_path_to(s);
  215. plot_meshes.push_back(pm);
  216. }
  217. }
  218. }
  219. Light *light = Object::cast_to<Light>(p_at_node);
  220. if (light && light->get_bake_mode() != Light::BAKE_DISABLED) {
  221. PlotLight pl;
  222. Transform xf = get_global_transform().affine_inverse() * light->get_global_transform();
  223. pl.local_xform = xf;
  224. pl.light = light;
  225. plot_lights.push_back(pl);
  226. }
  227. for (int i = 0; i < p_at_node->get_child_count(); i++) {
  228. Node *child = p_at_node->get_child(i);
  229. if (!child->get_owner())
  230. continue; //maybe a helper
  231. _find_meshes_and_lights(child, plot_meshes, plot_lights);
  232. }
  233. }
  234. void BakedLightmap::set_hdr(bool p_enable) {
  235. hdr = p_enable;
  236. }
  237. bool BakedLightmap::is_hdr() const {
  238. return hdr;
  239. }
  240. bool BakedLightmap::_bake_time(void *ud, float p_secs, float p_progress) {
  241. uint64_t time = OS::get_singleton()->get_ticks_usec();
  242. BakeTimeData *btd = (BakeTimeData *)ud;
  243. if (time - btd->last_step > 1000000) {
  244. int mins_left = p_secs / 60;
  245. int secs_left = Math::fmod(p_secs, 60.0f);
  246. int percent = p_progress * 100;
  247. bool abort = bake_step_function(btd->pass + percent, btd->text + " " + vformat(RTR("%d%%"), percent) + " " + vformat(RTR("(Time Left: %d:%02d s)"), mins_left, secs_left));
  248. btd->last_step = time;
  249. if (abort)
  250. return true;
  251. }
  252. return false;
  253. }
  254. BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, bool p_create_visual_debug) {
  255. String save_path;
  256. if (image_path.begins_with("res://")) {
  257. save_path = image_path;
  258. } else {
  259. if (get_filename() != "") {
  260. save_path = get_filename().get_base_dir();
  261. } else if (get_owner() && get_owner()->get_filename() != "") {
  262. save_path = get_owner()->get_filename().get_base_dir();
  263. }
  264. if (save_path == "") {
  265. return BAKE_ERROR_NO_SAVE_PATH;
  266. }
  267. if (image_path != "") {
  268. save_path.plus_file(image_path);
  269. }
  270. }
  271. {
  272. //check for valid save path
  273. DirAccessRef d = DirAccess::open(save_path);
  274. if (!d) {
  275. ERR_PRINTS("Invalid Save Path: " + save_path);
  276. return BAKE_ERROR_NO_SAVE_PATH;
  277. }
  278. }
  279. Ref<BakedLightmapData> new_light_data;
  280. new_light_data.instance();
  281. VoxelLightBaker baker;
  282. int bake_subdiv;
  283. int capture_subdiv;
  284. AABB bake_bounds;
  285. {
  286. bake_bounds = AABB(-extents, extents * 2.0);
  287. int subdiv = nearest_power_of_2_templated(int(bake_bounds.get_longest_axis_size() / bake_cell_size));
  288. bake_bounds.size[bake_bounds.get_longest_axis_index()] = subdiv * bake_cell_size;
  289. bake_subdiv = nearest_shift(subdiv) + 1;
  290. capture_subdiv = bake_subdiv;
  291. float css = bake_cell_size;
  292. while (css < capture_cell_size && capture_subdiv > 2) {
  293. capture_subdiv--;
  294. css *= 2.0;
  295. }
  296. }
  297. baker.begin_bake(bake_subdiv, bake_bounds);
  298. List<PlotMesh> mesh_list;
  299. List<PlotLight> light_list;
  300. _find_meshes_and_lights(p_from_node ? p_from_node : get_parent(), mesh_list, light_list);
  301. if (bake_begin_function) {
  302. bake_begin_function(mesh_list.size() + light_list.size() + 1 + mesh_list.size() * 100);
  303. }
  304. int step = 0;
  305. int pmc = 0;
  306. for (List<PlotMesh>::Element *E = mesh_list.front(); E; E = E->next()) {
  307. if (bake_step_function) {
  308. bake_step_function(step++, RTR("Plotting Meshes: ") + " (" + itos(pmc + 1) + "/" + itos(mesh_list.size()) + ")");
  309. }
  310. pmc++;
  311. baker.plot_mesh(E->get().local_xform, E->get().mesh, E->get().instance_materials, E->get().override_material);
  312. }
  313. pmc = 0;
  314. baker.begin_bake_light(VoxelLightBaker::BakeQuality(bake_quality), VoxelLightBaker::BakeMode(bake_mode), propagation, energy);
  315. for (List<PlotLight>::Element *E = light_list.front(); E; E = E->next()) {
  316. if (bake_step_function) {
  317. bake_step_function(step++, RTR("Plotting Lights:") + " (" + itos(pmc + 1) + "/" + itos(light_list.size()) + ")");
  318. }
  319. pmc++;
  320. PlotLight pl = E->get();
  321. switch (pl.light->get_light_type()) {
  322. case VS::LIGHT_DIRECTIONAL: {
  323. baker.plot_light_directional(-pl.local_xform.basis.get_axis(2), pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_bake_mode() == Light::BAKE_ALL);
  324. } break;
  325. case VS::LIGHT_OMNI: {
  326. baker.plot_light_omni(pl.local_xform.origin, pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_param(Light::PARAM_RANGE), pl.light->get_param(Light::PARAM_ATTENUATION), pl.light->get_bake_mode() == Light::BAKE_ALL);
  327. } break;
  328. case VS::LIGHT_SPOT: {
  329. baker.plot_light_spot(pl.local_xform.origin, pl.local_xform.basis.get_axis(2), pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_param(Light::PARAM_RANGE), pl.light->get_param(Light::PARAM_ATTENUATION), pl.light->get_param(Light::PARAM_SPOT_ANGLE), pl.light->get_param(Light::PARAM_SPOT_ATTENUATION), pl.light->get_bake_mode() == Light::BAKE_ALL);
  330. } break;
  331. }
  332. }
  333. /*if (bake_step_function) {
  334. bake_step_function(pmc++, RTR("Finishing Plot"));
  335. }*/
  336. baker.end_bake();
  337. Set<String> used_mesh_names;
  338. pmc = 0;
  339. for (List<PlotMesh>::Element *E = mesh_list.front(); E; E = E->next()) {
  340. String mesh_name = E->get().mesh->get_name();
  341. if (mesh_name == "" || mesh_name.find(":") != -1 || mesh_name.find("/") != -1) {
  342. mesh_name = "LightMap";
  343. }
  344. if (used_mesh_names.has(mesh_name)) {
  345. int idx = 2;
  346. String base = mesh_name;
  347. while (true) {
  348. mesh_name = base + itos(idx);
  349. if (!used_mesh_names.has(mesh_name))
  350. break;
  351. idx++;
  352. }
  353. }
  354. used_mesh_names.insert(mesh_name);
  355. pmc++;
  356. VoxelLightBaker::LightMapData lm;
  357. Error err;
  358. if (bake_step_function) {
  359. BakeTimeData btd;
  360. btd.text = RTR("Lighting Meshes: ") + mesh_name + " (" + itos(pmc) + "/" + itos(mesh_list.size()) + ")";
  361. btd.pass = step;
  362. btd.last_step = 0;
  363. err = baker.make_lightmap(E->get().local_xform, E->get().mesh, lm, _bake_time, &btd);
  364. if (err != OK) {
  365. bake_end_function();
  366. if (err == ERR_SKIP)
  367. return BAKE_ERROR_USER_ABORTED;
  368. return BAKE_ERROR_CANT_CREATE_IMAGE;
  369. }
  370. step += 100;
  371. } else {
  372. err = baker.make_lightmap(E->get().local_xform, E->get().mesh, lm);
  373. }
  374. if (err == OK) {
  375. Ref<Image> image;
  376. image.instance();
  377. uint32_t tex_flags = Texture::FLAGS_DEFAULT;
  378. if (hdr) {
  379. //just save a regular image
  380. PoolVector<uint8_t> data;
  381. int s = lm.light.size();
  382. data.resize(lm.light.size() * 2);
  383. {
  384. PoolVector<uint8_t>::Write w = data.write();
  385. PoolVector<float>::Read r = lm.light.read();
  386. uint16_t *hfw = (uint16_t *)w.ptr();
  387. for (int i = 0; i < s; i++) {
  388. hfw[i] = Math::make_half_float(r[i]);
  389. }
  390. }
  391. image->create(lm.width, lm.height, false, Image::FORMAT_RGBH, data);
  392. } else {
  393. //just save a regular image
  394. PoolVector<uint8_t> data;
  395. int s = lm.light.size();
  396. data.resize(lm.light.size());
  397. {
  398. PoolVector<uint8_t>::Write w = data.write();
  399. PoolVector<float>::Read r = lm.light.read();
  400. for (int i = 0; i < s; i += 3) {
  401. Color c(r[i + 0], r[i + 1], r[i + 2]);
  402. c = c.to_srgb();
  403. w[i + 0] = CLAMP(c.r * 255, 0, 255);
  404. w[i + 1] = CLAMP(c.g * 255, 0, 255);
  405. w[i + 2] = CLAMP(c.b * 255, 0, 255);
  406. }
  407. }
  408. image->create(lm.width, lm.height, false, Image::FORMAT_RGB8, data);
  409. //This texture is saved to SRGB for two reasons:
  410. // 1) first is so it looks better when doing the LINEAR->SRGB conversion (more accurate)
  411. // 2) So it can be used in the GLES2 backend, which does not support linkear workflow
  412. tex_flags |= Texture::FLAG_CONVERT_TO_LINEAR;
  413. }
  414. String image_path = save_path.plus_file(mesh_name);
  415. Ref<Texture> texture;
  416. if (ResourceLoader::import) {
  417. bool srgb = false;
  418. if (false && hdr) {
  419. //save hdr
  420. } else {
  421. image_path += ".png";
  422. print_line("image path saving png: " + image_path);
  423. image->save_png(image_path);
  424. srgb = true;
  425. }
  426. if (!FileAccess::exists(image_path + ".import")) {
  427. Ref<ConfigFile> config;
  428. config.instance();
  429. config->set_value("remap", "importer", "texture");
  430. config->set_value("remap", "type", "StreamTexture");
  431. config->set_value("params", "compress/mode", 2);
  432. config->set_value("params", "detect_3d", false);
  433. config->set_value("params", "flags/repeat", false);
  434. config->set_value("params", "flags/filter", true);
  435. config->set_value("params", "flags/mipmaps", false);
  436. config->set_value("params", "flags/srgb", srgb);
  437. config->save(image_path + ".import");
  438. }
  439. ResourceLoader::import(image_path);
  440. texture = ResourceLoader::load(image_path); //if already loaded, it will be updated on refocus?
  441. } else {
  442. image_path += ".text";
  443. Ref<ImageTexture> tex;
  444. bool set_path = true;
  445. if (ResourceCache::has(image_path)) {
  446. tex = Ref<Resource>((Resource *)ResourceCache::get(image_path));
  447. set_path = false;
  448. }
  449. if (!tex.is_valid()) {
  450. tex.instance();
  451. }
  452. tex->create_from_image(image, tex_flags);
  453. err = ResourceSaver::save(image_path, tex, ResourceSaver::FLAG_CHANGE_PATH);
  454. if (set_path) {
  455. tex->set_path(image_path);
  456. }
  457. texture = tex;
  458. }
  459. if (err != OK) {
  460. if (bake_end_function) {
  461. bake_end_function();
  462. }
  463. ERR_FAIL_COND_V(err != OK, BAKE_ERROR_CANT_CREATE_IMAGE);
  464. }
  465. new_light_data->add_user(E->get().path, texture, E->get().instance_idx);
  466. }
  467. }
  468. AABB bounds = AABB(-extents, extents * 2);
  469. new_light_data->set_cell_subdiv(capture_subdiv);
  470. new_light_data->set_bounds(bounds);
  471. new_light_data->set_octree(baker.create_capture_octree(capture_subdiv));
  472. {
  473. float bake_bound_size = bake_bounds.get_longest_axis_size();
  474. Transform to_bounds;
  475. to_bounds.basis.scale(Vector3(bake_bound_size, bake_bound_size, bake_bound_size));
  476. to_bounds.origin = bounds.position;
  477. Transform to_grid;
  478. to_grid.basis.scale(Vector3(1 << (capture_subdiv - 1), 1 << (capture_subdiv - 1), 1 << (capture_subdiv - 1)));
  479. Transform to_cell_space = to_grid * to_bounds.affine_inverse();
  480. new_light_data->set_cell_space_transform(to_cell_space);
  481. }
  482. if (bake_end_function) {
  483. bake_end_function();
  484. }
  485. //create the data for visual server
  486. if (p_create_visual_debug) {
  487. MultiMeshInstance *mmi = memnew(MultiMeshInstance);
  488. mmi->set_multimesh(baker.create_debug_multimesh(VoxelLightBaker::DEBUG_LIGHT));
  489. add_child(mmi);
  490. #ifdef TOOLS_ENABLED
  491. if (get_tree()->get_edited_scene_root() == this) {
  492. mmi->set_owner(this);
  493. } else {
  494. mmi->set_owner(get_owner());
  495. }
  496. #else
  497. mmi->set_owner(get_owner());
  498. #endif
  499. }
  500. set_light_data(new_light_data);
  501. return BAKE_ERROR_OK;
  502. }
  503. void BakedLightmap::_notification(int p_what) {
  504. if (p_what == NOTIFICATION_READY) {
  505. if (light_data.is_valid()) {
  506. _assign_lightmaps();
  507. }
  508. request_ready(); //will need ready again if re-enters tree
  509. }
  510. if (p_what == NOTIFICATION_EXIT_TREE) {
  511. if (light_data.is_valid()) {
  512. _clear_lightmaps();
  513. }
  514. }
  515. }
  516. void BakedLightmap::_assign_lightmaps() {
  517. ERR_FAIL_COND(!light_data.is_valid());
  518. for (int i = 0; i < light_data->get_user_count(); i++) {
  519. Ref<Texture> lightmap = light_data->get_user_lightmap(i);
  520. ERR_CONTINUE(!lightmap.is_valid());
  521. Node *node = get_node(light_data->get_user_path(i));
  522. int instance_idx = light_data->get_user_instance(i);
  523. if (instance_idx >= 0) {
  524. RID instance = node->call("get_bake_mesh_instance", instance_idx);
  525. if (instance.is_valid()) {
  526. VS::get_singleton()->instance_set_use_lightmap(instance, get_instance(), lightmap->get_rid());
  527. }
  528. } else {
  529. VisualInstance *vi = Object::cast_to<VisualInstance>(node);
  530. ERR_CONTINUE(!vi);
  531. VS::get_singleton()->instance_set_use_lightmap(vi->get_instance(), get_instance(), lightmap->get_rid());
  532. }
  533. }
  534. }
  535. void BakedLightmap::_clear_lightmaps() {
  536. ERR_FAIL_COND(!light_data.is_valid());
  537. for (int i = 0; i < light_data->get_user_count(); i++) {
  538. Node *node = get_node(light_data->get_user_path(i));
  539. int instance_idx = light_data->get_user_instance(i);
  540. if (instance_idx >= 0) {
  541. RID instance = node->call("get_bake_mesh_instance", instance_idx);
  542. if (instance.is_valid()) {
  543. VS::get_singleton()->instance_set_use_lightmap(instance, get_instance(), RID());
  544. }
  545. } else {
  546. VisualInstance *vi = Object::cast_to<VisualInstance>(node);
  547. ERR_CONTINUE(!vi);
  548. VS::get_singleton()->instance_set_use_lightmap(vi->get_instance(), get_instance(), RID());
  549. }
  550. }
  551. }
  552. void BakedLightmap::set_light_data(const Ref<BakedLightmapData> &p_data) {
  553. if (light_data.is_valid()) {
  554. if (is_inside_tree()) {
  555. _clear_lightmaps();
  556. }
  557. set_base(RID());
  558. }
  559. light_data = p_data;
  560. if (light_data.is_valid()) {
  561. set_base(light_data->get_rid());
  562. if (is_inside_tree()) {
  563. _assign_lightmaps();
  564. }
  565. }
  566. }
  567. Ref<BakedLightmapData> BakedLightmap::get_light_data() const {
  568. return light_data;
  569. }
  570. void BakedLightmap::_debug_bake() {
  571. bake(get_parent(), true);
  572. }
  573. void BakedLightmap::set_propagation(float p_propagation) {
  574. propagation = p_propagation;
  575. }
  576. float BakedLightmap::get_propagation() const {
  577. return propagation;
  578. }
  579. void BakedLightmap::set_energy(float p_energy) {
  580. energy = p_energy;
  581. }
  582. float BakedLightmap::get_energy() const {
  583. return energy;
  584. }
  585. void BakedLightmap::set_bake_quality(BakeQuality p_quality) {
  586. bake_quality = p_quality;
  587. }
  588. BakedLightmap::BakeQuality BakedLightmap::get_bake_quality() const {
  589. return bake_quality;
  590. }
  591. void BakedLightmap::set_bake_mode(BakeMode p_mode) {
  592. bake_mode = p_mode;
  593. }
  594. BakedLightmap::BakeMode BakedLightmap::get_bake_mode() const {
  595. return bake_mode;
  596. }
  597. void BakedLightmap::set_image_path(const String &p_path) {
  598. image_path = p_path;
  599. }
  600. String BakedLightmap::get_image_path() const {
  601. return image_path;
  602. }
  603. AABB BakedLightmap::get_aabb() const {
  604. return AABB(-extents, extents * 2);
  605. }
  606. PoolVector<Face3> BakedLightmap::get_faces(uint32_t p_usage_flags) const {
  607. return PoolVector<Face3>();
  608. }
  609. void BakedLightmap::_bind_methods() {
  610. ClassDB::bind_method(D_METHOD("set_light_data", "data"), &BakedLightmap::set_light_data);
  611. ClassDB::bind_method(D_METHOD("get_light_data"), &BakedLightmap::get_light_data);
  612. ClassDB::bind_method(D_METHOD("set_bake_cell_size", "bake_cell_size"), &BakedLightmap::set_bake_cell_size);
  613. ClassDB::bind_method(D_METHOD("get_bake_cell_size"), &BakedLightmap::get_bake_cell_size);
  614. ClassDB::bind_method(D_METHOD("set_capture_cell_size", "capture_cell_size"), &BakedLightmap::set_capture_cell_size);
  615. ClassDB::bind_method(D_METHOD("get_capture_cell_size"), &BakedLightmap::get_capture_cell_size);
  616. ClassDB::bind_method(D_METHOD("set_bake_quality", "bake_quality"), &BakedLightmap::set_bake_quality);
  617. ClassDB::bind_method(D_METHOD("get_bake_quality"), &BakedLightmap::get_bake_quality);
  618. ClassDB::bind_method(D_METHOD("set_bake_mode", "bake_mode"), &BakedLightmap::set_bake_mode);
  619. ClassDB::bind_method(D_METHOD("get_bake_mode"), &BakedLightmap::get_bake_mode);
  620. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &BakedLightmap::set_extents);
  621. ClassDB::bind_method(D_METHOD("get_extents"), &BakedLightmap::get_extents);
  622. ClassDB::bind_method(D_METHOD("set_propagation", "propagation"), &BakedLightmap::set_propagation);
  623. ClassDB::bind_method(D_METHOD("get_propagation"), &BakedLightmap::get_propagation);
  624. ClassDB::bind_method(D_METHOD("set_energy", "energy"), &BakedLightmap::set_energy);
  625. ClassDB::bind_method(D_METHOD("get_energy"), &BakedLightmap::get_energy);
  626. ClassDB::bind_method(D_METHOD("set_hdr", "hdr"), &BakedLightmap::set_hdr);
  627. ClassDB::bind_method(D_METHOD("is_hdr"), &BakedLightmap::is_hdr);
  628. ClassDB::bind_method(D_METHOD("set_image_path", "image_path"), &BakedLightmap::set_image_path);
  629. ClassDB::bind_method(D_METHOD("get_image_path"), &BakedLightmap::get_image_path);
  630. ClassDB::bind_method(D_METHOD("bake", "from_node", "create_visual_debug"), &BakedLightmap::bake, DEFVAL(Variant()), DEFVAL(false));
  631. ClassDB::bind_method(D_METHOD("debug_bake"), &BakedLightmap::_debug_bake);
  632. ClassDB::set_method_flags(get_class_static(), _scs_create("debug_bake"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  633. ADD_GROUP("Bake", "bake_");
  634. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bake_cell_size", PROPERTY_HINT_RANGE, "0.01,64,0.01"), "set_bake_cell_size", "get_bake_cell_size");
  635. ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_quality", PROPERTY_HINT_ENUM, "Low,Medium,High"), "set_bake_quality", "get_bake_quality");
  636. ADD_PROPERTY(PropertyInfo(Variant::INT, "bake_mode", PROPERTY_HINT_ENUM, "ConeTrace,RayTrace"), "set_bake_mode", "get_bake_mode");
  637. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bake_propagation", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_propagation", "get_propagation");
  638. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bake_energy", PROPERTY_HINT_RANGE, "0,32,0.01"), "set_energy", "get_energy");
  639. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bake_hdr"), "set_hdr", "is_hdr");
  640. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "bake_extents"), "set_extents", "get_extents");
  641. ADD_GROUP("Capture", "capture_");
  642. ADD_PROPERTY(PropertyInfo(Variant::REAL, "capture_cell_size", PROPERTY_HINT_RANGE, "0.01,64,0.01"), "set_capture_cell_size", "get_capture_cell_size");
  643. ADD_GROUP("Data", "");
  644. ADD_PROPERTY(PropertyInfo(Variant::STRING, "image_path", PROPERTY_HINT_DIR), "set_image_path", "get_image_path");
  645. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "light_data", PROPERTY_HINT_RESOURCE_TYPE, "BakedLightmapData"), "set_light_data", "get_light_data");
  646. BIND_ENUM_CONSTANT(BAKE_QUALITY_LOW);
  647. BIND_ENUM_CONSTANT(BAKE_QUALITY_MEDIUM);
  648. BIND_ENUM_CONSTANT(BAKE_QUALITY_HIGH);
  649. BIND_ENUM_CONSTANT(BAKE_MODE_CONE_TRACE);
  650. BIND_ENUM_CONSTANT(BAKE_MODE_RAY_TRACE);
  651. BIND_ENUM_CONSTANT(BAKE_ERROR_OK);
  652. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_SAVE_PATH);
  653. BIND_ENUM_CONSTANT(BAKE_ERROR_NO_MESHES);
  654. BIND_ENUM_CONSTANT(BAKE_ERROR_CANT_CREATE_IMAGE);
  655. BIND_ENUM_CONSTANT(BAKE_ERROR_USER_ABORTED);
  656. }
  657. BakedLightmap::BakedLightmap() {
  658. extents = Vector3(10, 10, 10);
  659. bake_cell_size = 0.25;
  660. capture_cell_size = 0.5;
  661. bake_quality = BAKE_QUALITY_MEDIUM;
  662. bake_mode = BAKE_MODE_CONE_TRACE;
  663. energy = 1;
  664. propagation = 1;
  665. hdr = false;
  666. image_path = ".";
  667. set_disable_scale(true);
  668. }