gi_probe.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /*************************************************************************/
  2. /* gi_probe.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "gi_probe.h"
  31. #include "mesh_instance.h"
  32. void GIProbeData::set_bounds(const Rect3 &p_bounds) {
  33. VS::get_singleton()->gi_probe_set_bounds(probe, p_bounds);
  34. }
  35. Rect3 GIProbeData::get_bounds() const {
  36. return VS::get_singleton()->gi_probe_get_bounds(probe);
  37. }
  38. void GIProbeData::set_cell_size(float p_size) {
  39. VS::get_singleton()->gi_probe_set_cell_size(probe, p_size);
  40. }
  41. float GIProbeData::get_cell_size() const {
  42. return VS::get_singleton()->gi_probe_get_cell_size(probe);
  43. }
  44. void GIProbeData::set_to_cell_xform(const Transform &p_xform) {
  45. VS::get_singleton()->gi_probe_set_to_cell_xform(probe, p_xform);
  46. }
  47. Transform GIProbeData::get_to_cell_xform() const {
  48. return VS::get_singleton()->gi_probe_get_to_cell_xform(probe);
  49. }
  50. void GIProbeData::set_dynamic_data(const PoolVector<int> &p_data) {
  51. VS::get_singleton()->gi_probe_set_dynamic_data(probe, p_data);
  52. }
  53. PoolVector<int> GIProbeData::get_dynamic_data() const {
  54. return VS::get_singleton()->gi_probe_get_dynamic_data(probe);
  55. }
  56. void GIProbeData::set_dynamic_range(int p_range) {
  57. VS::get_singleton()->gi_probe_set_dynamic_range(probe, p_range);
  58. }
  59. void GIProbeData::set_energy(float p_range) {
  60. VS::get_singleton()->gi_probe_set_energy(probe, p_range);
  61. }
  62. float GIProbeData::get_energy() const {
  63. return VS::get_singleton()->gi_probe_get_energy(probe);
  64. }
  65. void GIProbeData::set_bias(float p_range) {
  66. VS::get_singleton()->gi_probe_set_bias(probe, p_range);
  67. }
  68. float GIProbeData::get_bias() const {
  69. return VS::get_singleton()->gi_probe_get_bias(probe);
  70. }
  71. void GIProbeData::set_normal_bias(float p_range) {
  72. VS::get_singleton()->gi_probe_set_normal_bias(probe, p_range);
  73. }
  74. float GIProbeData::get_normal_bias() const {
  75. return VS::get_singleton()->gi_probe_get_normal_bias(probe);
  76. }
  77. void GIProbeData::set_propagation(float p_range) {
  78. VS::get_singleton()->gi_probe_set_propagation(probe, p_range);
  79. }
  80. float GIProbeData::get_propagation() const {
  81. return VS::get_singleton()->gi_probe_get_propagation(probe);
  82. }
  83. void GIProbeData::set_interior(bool p_enable) {
  84. VS::get_singleton()->gi_probe_set_interior(probe, p_enable);
  85. }
  86. bool GIProbeData::is_interior() const {
  87. return VS::get_singleton()->gi_probe_is_interior(probe);
  88. }
  89. bool GIProbeData::is_compressed() const {
  90. return VS::get_singleton()->gi_probe_is_compressed(probe);
  91. }
  92. void GIProbeData::set_compress(bool p_enable) {
  93. VS::get_singleton()->gi_probe_set_compress(probe, p_enable);
  94. }
  95. int GIProbeData::get_dynamic_range() const {
  96. return VS::get_singleton()->gi_probe_get_dynamic_range(probe);
  97. }
  98. RID GIProbeData::get_rid() const {
  99. return probe;
  100. }
  101. void GIProbeData::_bind_methods() {
  102. ClassDB::bind_method(D_METHOD("set_bounds", "bounds"), &GIProbeData::set_bounds);
  103. ClassDB::bind_method(D_METHOD("get_bounds"), &GIProbeData::get_bounds);
  104. ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &GIProbeData::set_cell_size);
  105. ClassDB::bind_method(D_METHOD("get_cell_size"), &GIProbeData::get_cell_size);
  106. ClassDB::bind_method(D_METHOD("set_to_cell_xform", "to_cell_xform"), &GIProbeData::set_to_cell_xform);
  107. ClassDB::bind_method(D_METHOD("get_to_cell_xform"), &GIProbeData::get_to_cell_xform);
  108. ClassDB::bind_method(D_METHOD("set_dynamic_data", "dynamic_data"), &GIProbeData::set_dynamic_data);
  109. ClassDB::bind_method(D_METHOD("get_dynamic_data"), &GIProbeData::get_dynamic_data);
  110. ClassDB::bind_method(D_METHOD("set_dynamic_range", "dynamic_range"), &GIProbeData::set_dynamic_range);
  111. ClassDB::bind_method(D_METHOD("get_dynamic_range"), &GIProbeData::get_dynamic_range);
  112. ClassDB::bind_method(D_METHOD("set_energy", "energy"), &GIProbeData::set_energy);
  113. ClassDB::bind_method(D_METHOD("get_energy"), &GIProbeData::get_energy);
  114. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &GIProbeData::set_bias);
  115. ClassDB::bind_method(D_METHOD("get_bias"), &GIProbeData::get_bias);
  116. ClassDB::bind_method(D_METHOD("set_normal_bias", "bias"), &GIProbeData::set_normal_bias);
  117. ClassDB::bind_method(D_METHOD("get_normal_bias"), &GIProbeData::get_normal_bias);
  118. ClassDB::bind_method(D_METHOD("set_propagation", "propagation"), &GIProbeData::set_propagation);
  119. ClassDB::bind_method(D_METHOD("get_propagation"), &GIProbeData::get_propagation);
  120. ClassDB::bind_method(D_METHOD("set_interior", "interior"), &GIProbeData::set_interior);
  121. ClassDB::bind_method(D_METHOD("is_interior"), &GIProbeData::is_interior);
  122. ClassDB::bind_method(D_METHOD("set_compress", "compress"), &GIProbeData::set_compress);
  123. ClassDB::bind_method(D_METHOD("is_compressed"), &GIProbeData::is_compressed);
  124. ADD_PROPERTY(PropertyInfo(Variant::RECT3, "bounds", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_bounds", "get_bounds");
  125. ADD_PROPERTY(PropertyInfo(Variant::REAL, "cell_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_cell_size", "get_cell_size");
  126. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "to_cell_xform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_to_cell_xform", "get_to_cell_xform");
  127. ADD_PROPERTY(PropertyInfo(Variant::POOL_INT_ARRAY, "dynamic_data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_dynamic_data", "get_dynamic_data");
  128. ADD_PROPERTY(PropertyInfo(Variant::INT, "dynamic_range", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_dynamic_range", "get_dynamic_range");
  129. ADD_PROPERTY(PropertyInfo(Variant::REAL, "energy", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_energy", "get_energy");
  130. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bias", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_bias", "get_bias");
  131. ADD_PROPERTY(PropertyInfo(Variant::REAL, "normal_bias", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_normal_bias", "get_normal_bias");
  132. ADD_PROPERTY(PropertyInfo(Variant::REAL, "propagation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_propagation", "get_propagation");
  133. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_interior", "is_interior");
  134. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "compress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR), "set_compress", "is_compressed");
  135. }
  136. GIProbeData::GIProbeData() {
  137. probe = VS::get_singleton()->gi_probe_create();
  138. }
  139. GIProbeData::~GIProbeData() {
  140. VS::get_singleton()->free(probe);
  141. }
  142. //////////////////////
  143. //////////////////////
  144. void GIProbe::set_probe_data(const Ref<GIProbeData> &p_data) {
  145. if (p_data.is_valid()) {
  146. VS::get_singleton()->instance_set_base(get_instance(), p_data->get_rid());
  147. } else {
  148. VS::get_singleton()->instance_set_base(get_instance(), RID());
  149. }
  150. probe_data = p_data;
  151. }
  152. Ref<GIProbeData> GIProbe::get_probe_data() const {
  153. return probe_data;
  154. }
  155. void GIProbe::set_subdiv(Subdiv p_subdiv) {
  156. ERR_FAIL_INDEX(p_subdiv, SUBDIV_MAX);
  157. subdiv = p_subdiv;
  158. update_gizmo();
  159. }
  160. GIProbe::Subdiv GIProbe::get_subdiv() const {
  161. return subdiv;
  162. }
  163. void GIProbe::set_extents(const Vector3 &p_extents) {
  164. extents = p_extents;
  165. update_gizmo();
  166. }
  167. Vector3 GIProbe::get_extents() const {
  168. return extents;
  169. }
  170. void GIProbe::set_dynamic_range(int p_dynamic_range) {
  171. dynamic_range = p_dynamic_range;
  172. }
  173. int GIProbe::get_dynamic_range() const {
  174. return dynamic_range;
  175. }
  176. void GIProbe::set_energy(float p_energy) {
  177. energy = p_energy;
  178. if (probe_data.is_valid()) {
  179. probe_data->set_energy(energy);
  180. }
  181. }
  182. float GIProbe::get_energy() const {
  183. return energy;
  184. }
  185. void GIProbe::set_bias(float p_bias) {
  186. bias = p_bias;
  187. if (probe_data.is_valid()) {
  188. probe_data->set_bias(bias);
  189. }
  190. }
  191. float GIProbe::get_bias() const {
  192. return bias;
  193. }
  194. void GIProbe::set_normal_bias(float p_normal_bias) {
  195. normal_bias = p_normal_bias;
  196. if (probe_data.is_valid()) {
  197. probe_data->set_normal_bias(normal_bias);
  198. }
  199. }
  200. float GIProbe::get_normal_bias() const {
  201. return normal_bias;
  202. }
  203. void GIProbe::set_propagation(float p_propagation) {
  204. propagation = p_propagation;
  205. if (probe_data.is_valid()) {
  206. probe_data->set_propagation(propagation);
  207. }
  208. }
  209. float GIProbe::get_propagation() const {
  210. return propagation;
  211. }
  212. void GIProbe::set_interior(bool p_enable) {
  213. interior = p_enable;
  214. if (probe_data.is_valid()) {
  215. probe_data->set_interior(p_enable);
  216. }
  217. }
  218. bool GIProbe::is_interior() const {
  219. return interior;
  220. }
  221. void GIProbe::set_compress(bool p_enable) {
  222. compress = p_enable;
  223. if (probe_data.is_valid()) {
  224. probe_data->set_compress(p_enable);
  225. }
  226. }
  227. bool GIProbe::is_compressed() const {
  228. return compress;
  229. }
  230. #include "math.h"
  231. #define FINDMINMAX(x0, x1, x2, min, max) \
  232. min = max = x0; \
  233. if (x1 < min) min = x1; \
  234. if (x1 > max) max = x1; \
  235. if (x2 < min) min = x2; \
  236. if (x2 > max) max = x2;
  237. static bool planeBoxOverlap(Vector3 normal, float d, Vector3 maxbox) {
  238. int q;
  239. Vector3 vmin, vmax;
  240. for (q = 0; q <= 2; q++) {
  241. if (normal[q] > 0.0f) {
  242. vmin[q] = -maxbox[q];
  243. vmax[q] = maxbox[q];
  244. } else {
  245. vmin[q] = maxbox[q];
  246. vmax[q] = -maxbox[q];
  247. }
  248. }
  249. if (normal.dot(vmin) + d > 0.0f) return false;
  250. if (normal.dot(vmax) + d >= 0.0f) return true;
  251. return false;
  252. }
  253. /*======================== X-tests ========================*/
  254. #define AXISTEST_X01(a, b, fa, fb) \
  255. p0 = a * v0.y - b * v0.z; \
  256. p2 = a * v2.y - b * v2.z; \
  257. if (p0 < p2) { \
  258. min = p0; \
  259. max = p2; \
  260. } else { \
  261. min = p2; \
  262. max = p0; \
  263. } \
  264. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  265. if (min > rad || max < -rad) return false;
  266. #define AXISTEST_X2(a, b, fa, fb) \
  267. p0 = a * v0.y - b * v0.z; \
  268. p1 = a * v1.y - b * v1.z; \
  269. if (p0 < p1) { \
  270. min = p0; \
  271. max = p1; \
  272. } else { \
  273. min = p1; \
  274. max = p0; \
  275. } \
  276. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  277. if (min > rad || max < -rad) return false;
  278. /*======================== Y-tests ========================*/
  279. #define AXISTEST_Y02(a, b, fa, fb) \
  280. p0 = -a * v0.x + b * v0.z; \
  281. p2 = -a * v2.x + b * v2.z; \
  282. if (p0 < p2) { \
  283. min = p0; \
  284. max = p2; \
  285. } else { \
  286. min = p2; \
  287. max = p0; \
  288. } \
  289. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  290. if (min > rad || max < -rad) return false;
  291. #define AXISTEST_Y1(a, b, fa, fb) \
  292. p0 = -a * v0.x + b * v0.z; \
  293. p1 = -a * v1.x + b * v1.z; \
  294. if (p0 < p1) { \
  295. min = p0; \
  296. max = p1; \
  297. } else { \
  298. min = p1; \
  299. max = p0; \
  300. } \
  301. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  302. if (min > rad || max < -rad) return false;
  303. /*======================== Z-tests ========================*/
  304. #define AXISTEST_Z12(a, b, fa, fb) \
  305. p1 = a * v1.x - b * v1.y; \
  306. p2 = a * v2.x - b * v2.y; \
  307. if (p2 < p1) { \
  308. min = p2; \
  309. max = p1; \
  310. } else { \
  311. min = p1; \
  312. max = p2; \
  313. } \
  314. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  315. if (min > rad || max < -rad) return false;
  316. #define AXISTEST_Z0(a, b, fa, fb) \
  317. p0 = a * v0.x - b * v0.y; \
  318. p1 = a * v1.x - b * v1.y; \
  319. if (p0 < p1) { \
  320. min = p0; \
  321. max = p1; \
  322. } else { \
  323. min = p1; \
  324. max = p0; \
  325. } \
  326. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  327. if (min > rad || max < -rad) return false;
  328. static bool fast_tri_box_overlap(const Vector3 &boxcenter, const Vector3 boxhalfsize, const Vector3 *triverts) {
  329. /* use separating axis theorem to test overlap between triangle and box */
  330. /* need to test for overlap in these directions: */
  331. /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  332. /* we do not even need to test these) */
  333. /* 2) normal of the triangle */
  334. /* 3) crossproduct(edge from tri, {x,y,z}-directin) */
  335. /* this gives 3x3=9 more tests */
  336. Vector3 v0, v1, v2;
  337. float min, max, d, p0, p1, p2, rad, fex, fey, fez;
  338. Vector3 normal, e0, e1, e2;
  339. /* This is the fastest branch on Sun */
  340. /* move everything so that the boxcenter is in (0,0,0) */
  341. v0 = triverts[0] - boxcenter;
  342. v1 = triverts[1] - boxcenter;
  343. v2 = triverts[2] - boxcenter;
  344. /* compute triangle edges */
  345. e0 = v1 - v0; /* tri edge 0 */
  346. e1 = v2 - v1; /* tri edge 1 */
  347. e2 = v0 - v2; /* tri edge 2 */
  348. /* Bullet 3: */
  349. /* test the 9 tests first (this was faster) */
  350. fex = Math::abs(e0.x);
  351. fey = Math::abs(e0.y);
  352. fez = Math::abs(e0.z);
  353. AXISTEST_X01(e0.z, e0.y, fez, fey);
  354. AXISTEST_Y02(e0.z, e0.x, fez, fex);
  355. AXISTEST_Z12(e0.y, e0.x, fey, fex);
  356. fex = Math::abs(e1.x);
  357. fey = Math::abs(e1.y);
  358. fez = Math::abs(e1.z);
  359. AXISTEST_X01(e1.z, e1.y, fez, fey);
  360. AXISTEST_Y02(e1.z, e1.x, fez, fex);
  361. AXISTEST_Z0(e1.y, e1.x, fey, fex);
  362. fex = Math::abs(e2.x);
  363. fey = Math::abs(e2.y);
  364. fez = Math::abs(e2.z);
  365. AXISTEST_X2(e2.z, e2.y, fez, fey);
  366. AXISTEST_Y1(e2.z, e2.x, fez, fex);
  367. AXISTEST_Z12(e2.y, e2.x, fey, fex);
  368. /* Bullet 1: */
  369. /* first test overlap in the {x,y,z}-directions */
  370. /* find min, max of the triangle each direction, and test for overlap in */
  371. /* that direction -- this is equivalent to testing a minimal AABB around */
  372. /* the triangle against the AABB */
  373. /* test in X-direction */
  374. FINDMINMAX(v0.x, v1.x, v2.x, min, max);
  375. if (min > boxhalfsize.x || max < -boxhalfsize.x) return false;
  376. /* test in Y-direction */
  377. FINDMINMAX(v0.y, v1.y, v2.y, min, max);
  378. if (min > boxhalfsize.y || max < -boxhalfsize.y) return false;
  379. /* test in Z-direction */
  380. FINDMINMAX(v0.z, v1.z, v2.z, min, max);
  381. if (min > boxhalfsize.z || max < -boxhalfsize.z) return false;
  382. /* Bullet 2: */
  383. /* test if the box intersects the plane of the triangle */
  384. /* compute plane equation of triangle: normal*x+d=0 */
  385. normal = e0.cross(e1);
  386. d = -normal.dot(v0); /* plane eq: normal.x+d=0 */
  387. if (!planeBoxOverlap(normal, d, boxhalfsize)) return false;
  388. return true; /* box and triangle overlaps */
  389. }
  390. static _FORCE_INLINE_ Vector2 get_uv(const Vector3 &p_pos, const Vector3 *p_vtx, const Vector2 *p_uv) {
  391. if (p_pos.distance_squared_to(p_vtx[0]) < CMP_EPSILON2)
  392. return p_uv[0];
  393. if (p_pos.distance_squared_to(p_vtx[1]) < CMP_EPSILON2)
  394. return p_uv[1];
  395. if (p_pos.distance_squared_to(p_vtx[2]) < CMP_EPSILON2)
  396. return p_uv[2];
  397. Vector3 v0 = p_vtx[1] - p_vtx[0];
  398. Vector3 v1 = p_vtx[2] - p_vtx[0];
  399. Vector3 v2 = p_pos - p_vtx[0];
  400. float d00 = v0.dot(v0);
  401. float d01 = v0.dot(v1);
  402. float d11 = v1.dot(v1);
  403. float d20 = v2.dot(v0);
  404. float d21 = v2.dot(v1);
  405. float denom = (d00 * d11 - d01 * d01);
  406. if (denom == 0)
  407. return p_uv[0];
  408. float v = (d11 * d20 - d01 * d21) / denom;
  409. float w = (d00 * d21 - d01 * d20) / denom;
  410. float u = 1.0f - v - w;
  411. return p_uv[0] * u + p_uv[1] * v + p_uv[2] * w;
  412. }
  413. void GIProbe::_plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, const Vector3 *p_vtx, const Vector2 *p_uv, const Baker::MaterialCache &p_material, const Rect3 &p_aabb, Baker *p_baker) {
  414. if (p_level == p_baker->cell_subdiv - 1) {
  415. //plot the face by guessing it's albedo and emission value
  416. //find best axis to map to, for scanning values
  417. int closest_axis = 0;
  418. float closest_dot = 0;
  419. Plane plane = Plane(p_vtx[0], p_vtx[1], p_vtx[2]);
  420. Vector3 normal = plane.normal;
  421. for (int i = 0; i < 3; i++) {
  422. Vector3 axis;
  423. axis[i] = 1.0;
  424. float dot = ABS(normal.dot(axis));
  425. if (i == 0 || dot > closest_dot) {
  426. closest_axis = i;
  427. closest_dot = dot;
  428. }
  429. }
  430. Vector3 axis;
  431. axis[closest_axis] = 1.0;
  432. Vector3 t1;
  433. t1[(closest_axis + 1) % 3] = 1.0;
  434. Vector3 t2;
  435. t2[(closest_axis + 2) % 3] = 1.0;
  436. t1 *= p_aabb.size[(closest_axis + 1) % 3] / float(color_scan_cell_width);
  437. t2 *= p_aabb.size[(closest_axis + 2) % 3] / float(color_scan_cell_width);
  438. Color albedo_accum;
  439. Color emission_accum;
  440. Vector3 normal_accum;
  441. float alpha = 0.0;
  442. //map to a grid average in the best axis for this face
  443. for (int i = 0; i < color_scan_cell_width; i++) {
  444. Vector3 ofs_i = float(i) * t1;
  445. for (int j = 0; j < color_scan_cell_width; j++) {
  446. Vector3 ofs_j = float(j) * t2;
  447. Vector3 from = p_aabb.position + ofs_i + ofs_j;
  448. Vector3 to = from + t1 + t2 + axis * p_aabb.size[closest_axis];
  449. Vector3 half = (to - from) * 0.5;
  450. //is in this cell?
  451. if (!fast_tri_box_overlap(from + half, half, p_vtx)) {
  452. continue; //face does not span this cell
  453. }
  454. //go from -size to +size*2 to avoid skipping collisions
  455. Vector3 ray_from = from + (t1 + t2) * 0.5 - axis * p_aabb.size[closest_axis];
  456. Vector3 ray_to = ray_from + axis * p_aabb.size[closest_axis] * 2;
  457. if (normal.dot(ray_from - ray_to) < 0) {
  458. SWAP(ray_from, ray_to);
  459. }
  460. Vector3 intersection;
  461. if (!plane.intersects_segment(ray_from, ray_to, &intersection)) {
  462. if (ABS(plane.distance_to(ray_from)) < ABS(plane.distance_to(ray_to))) {
  463. intersection = plane.project(ray_from);
  464. } else {
  465. intersection = plane.project(ray_to);
  466. }
  467. }
  468. intersection = Face3(p_vtx[0], p_vtx[1], p_vtx[2]).get_closest_point_to(intersection);
  469. Vector2 uv = get_uv(intersection, p_vtx, p_uv);
  470. int uv_x = CLAMP(Math::fposmod(uv.x, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
  471. int uv_y = CLAMP(Math::fposmod(uv.y, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
  472. int ofs = uv_y * bake_texture_size + uv_x;
  473. albedo_accum.r += p_material.albedo[ofs].r;
  474. albedo_accum.g += p_material.albedo[ofs].g;
  475. albedo_accum.b += p_material.albedo[ofs].b;
  476. albedo_accum.a += p_material.albedo[ofs].a;
  477. emission_accum.r += p_material.emission[ofs].r;
  478. emission_accum.g += p_material.emission[ofs].g;
  479. emission_accum.b += p_material.emission[ofs].b;
  480. normal_accum += normal;
  481. alpha += 1.0;
  482. }
  483. }
  484. if (alpha == 0) {
  485. //could not in any way get texture information.. so use closest point to center
  486. Face3 f(p_vtx[0], p_vtx[1], p_vtx[2]);
  487. Vector3 inters = f.get_closest_point_to(p_aabb.position + p_aabb.size * 0.5);
  488. Vector2 uv = get_uv(inters, p_vtx, p_uv);
  489. int uv_x = CLAMP(Math::fposmod(uv.x, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
  490. int uv_y = CLAMP(Math::fposmod(uv.y, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
  491. int ofs = uv_y * bake_texture_size + uv_x;
  492. alpha = 1.0 / (color_scan_cell_width * color_scan_cell_width);
  493. albedo_accum.r = p_material.albedo[ofs].r * alpha;
  494. albedo_accum.g = p_material.albedo[ofs].g * alpha;
  495. albedo_accum.b = p_material.albedo[ofs].b * alpha;
  496. albedo_accum.a = p_material.albedo[ofs].a * alpha;
  497. emission_accum.r = p_material.emission[ofs].r * alpha;
  498. emission_accum.g = p_material.emission[ofs].g * alpha;
  499. emission_accum.b = p_material.emission[ofs].b * alpha;
  500. normal_accum *= alpha;
  501. } else {
  502. float accdiv = 1.0 / (color_scan_cell_width * color_scan_cell_width);
  503. alpha *= accdiv;
  504. albedo_accum.r *= accdiv;
  505. albedo_accum.g *= accdiv;
  506. albedo_accum.b *= accdiv;
  507. albedo_accum.a *= accdiv;
  508. emission_accum.r *= accdiv;
  509. emission_accum.g *= accdiv;
  510. emission_accum.b *= accdiv;
  511. normal_accum *= accdiv;
  512. }
  513. //put this temporarily here, corrected in a later step
  514. p_baker->bake_cells[p_idx].albedo[0] += albedo_accum.r;
  515. p_baker->bake_cells[p_idx].albedo[1] += albedo_accum.g;
  516. p_baker->bake_cells[p_idx].albedo[2] += albedo_accum.b;
  517. p_baker->bake_cells[p_idx].emission[0] += emission_accum.r;
  518. p_baker->bake_cells[p_idx].emission[1] += emission_accum.g;
  519. p_baker->bake_cells[p_idx].emission[2] += emission_accum.b;
  520. p_baker->bake_cells[p_idx].normal[0] += normal_accum.x;
  521. p_baker->bake_cells[p_idx].normal[1] += normal_accum.y;
  522. p_baker->bake_cells[p_idx].normal[2] += normal_accum.z;
  523. p_baker->bake_cells[p_idx].alpha += alpha;
  524. } else {
  525. //go down
  526. int half = (1 << (p_baker->cell_subdiv - 1)) >> (p_level + 1);
  527. for (int i = 0; i < 8; i++) {
  528. Rect3 aabb = p_aabb;
  529. aabb.size *= 0.5;
  530. int nx = p_x;
  531. int ny = p_y;
  532. int nz = p_z;
  533. if (i & 1) {
  534. aabb.position.x += aabb.size.x;
  535. nx += half;
  536. }
  537. if (i & 2) {
  538. aabb.position.y += aabb.size.y;
  539. ny += half;
  540. }
  541. if (i & 4) {
  542. aabb.position.z += aabb.size.z;
  543. nz += half;
  544. }
  545. //make sure to not plot beyond limits
  546. if (nx < 0 || nx >= p_baker->axis_cell_size[0] || ny < 0 || ny >= p_baker->axis_cell_size[1] || nz < 0 || nz >= p_baker->axis_cell_size[2])
  547. continue;
  548. {
  549. Rect3 test_aabb = aabb;
  550. //test_aabb.grow_by(test_aabb.get_longest_axis_size()*0.05); //grow a bit to avoid numerical error in real-time
  551. Vector3 qsize = test_aabb.size * 0.5; //quarter size, for fast aabb test
  552. if (!fast_tri_box_overlap(test_aabb.position + qsize, qsize, p_vtx)) {
  553. //if (!Face3(p_vtx[0],p_vtx[1],p_vtx[2]).intersects_aabb2(aabb)) {
  554. //does not fit in child, go on
  555. continue;
  556. }
  557. }
  558. if (p_baker->bake_cells[p_idx].childs[i] == Baker::CHILD_EMPTY) {
  559. //sub cell must be created
  560. uint32_t child_idx = p_baker->bake_cells.size();
  561. p_baker->bake_cells[p_idx].childs[i] = child_idx;
  562. p_baker->bake_cells.resize(p_baker->bake_cells.size() + 1);
  563. p_baker->bake_cells[child_idx].level = p_level + 1;
  564. }
  565. _plot_face(p_baker->bake_cells[p_idx].childs[i], p_level + 1, nx, ny, nz, p_vtx, p_uv, p_material, aabb, p_baker);
  566. }
  567. }
  568. }
  569. void GIProbe::_fixup_plot(int p_idx, int p_level, int p_x, int p_y, int p_z, Baker *p_baker) {
  570. if (p_level == p_baker->cell_subdiv - 1) {
  571. p_baker->leaf_voxel_count++;
  572. float alpha = p_baker->bake_cells[p_idx].alpha;
  573. p_baker->bake_cells[p_idx].albedo[0] /= alpha;
  574. p_baker->bake_cells[p_idx].albedo[1] /= alpha;
  575. p_baker->bake_cells[p_idx].albedo[2] /= alpha;
  576. //transfer emission to light
  577. p_baker->bake_cells[p_idx].emission[0] /= alpha;
  578. p_baker->bake_cells[p_idx].emission[1] /= alpha;
  579. p_baker->bake_cells[p_idx].emission[2] /= alpha;
  580. p_baker->bake_cells[p_idx].normal[0] /= alpha;
  581. p_baker->bake_cells[p_idx].normal[1] /= alpha;
  582. p_baker->bake_cells[p_idx].normal[2] /= alpha;
  583. Vector3 n(p_baker->bake_cells[p_idx].normal[0], p_baker->bake_cells[p_idx].normal[1], p_baker->bake_cells[p_idx].normal[2]);
  584. if (n.length() < 0.01) {
  585. //too much fight over normal, zero it
  586. p_baker->bake_cells[p_idx].normal[0] = 0;
  587. p_baker->bake_cells[p_idx].normal[1] = 0;
  588. p_baker->bake_cells[p_idx].normal[2] = 0;
  589. } else {
  590. n.normalize();
  591. p_baker->bake_cells[p_idx].normal[0] = n.x;
  592. p_baker->bake_cells[p_idx].normal[1] = n.y;
  593. p_baker->bake_cells[p_idx].normal[2] = n.z;
  594. }
  595. p_baker->bake_cells[p_idx].alpha = 1.0;
  596. /*
  597. //remove neighbours from used sides
  598. for(int n=0;n<6;n++) {
  599. int ofs[3]={0,0,0};
  600. ofs[n/2]=(n&1)?1:-1;
  601. //convert to x,y,z on this level
  602. int x=p_x;
  603. int y=p_y;
  604. int z=p_z;
  605. x+=ofs[0];
  606. y+=ofs[1];
  607. z+=ofs[2];
  608. int ofs_x=0;
  609. int ofs_y=0;
  610. int ofs_z=0;
  611. int size = 1<<p_level;
  612. int half=size/2;
  613. if (x<0 || x>=size || y<0 || y>=size || z<0 || z>=size) {
  614. //neighbour is out, can't use it
  615. p_baker->bake_cells[p_idx].used_sides&=~(1<<uint32_t(n));
  616. continue;
  617. }
  618. uint32_t neighbour=0;
  619. for(int i=0;i<p_baker->cell_subdiv-1;i++) {
  620. Baker::Cell *bc = &p_baker->bake_cells[neighbour];
  621. int child = 0;
  622. if (x >= ofs_x + half) {
  623. child|=1;
  624. ofs_x+=half;
  625. }
  626. if (y >= ofs_y + half) {
  627. child|=2;
  628. ofs_y+=half;
  629. }
  630. if (z >= ofs_z + half) {
  631. child|=4;
  632. ofs_z+=half;
  633. }
  634. neighbour = bc->childs[child];
  635. if (neighbour==Baker::CHILD_EMPTY) {
  636. break;
  637. }
  638. half>>=1;
  639. }
  640. if (neighbour!=Baker::CHILD_EMPTY) {
  641. p_baker->bake_cells[p_idx].used_sides&=~(1<<uint32_t(n));
  642. }
  643. }
  644. */
  645. } else {
  646. //go down
  647. float alpha_average = 0;
  648. int half = (1 << (p_baker->cell_subdiv - 1)) >> (p_level + 1);
  649. for (int i = 0; i < 8; i++) {
  650. uint32_t child = p_baker->bake_cells[p_idx].childs[i];
  651. if (child == Baker::CHILD_EMPTY)
  652. continue;
  653. int nx = p_x;
  654. int ny = p_y;
  655. int nz = p_z;
  656. if (i & 1)
  657. nx += half;
  658. if (i & 2)
  659. ny += half;
  660. if (i & 4)
  661. nz += half;
  662. _fixup_plot(child, p_level + 1, nx, ny, nz, p_baker);
  663. alpha_average += p_baker->bake_cells[child].alpha;
  664. }
  665. p_baker->bake_cells[p_idx].alpha = alpha_average / 8.0;
  666. p_baker->bake_cells[p_idx].emission[0] = 0;
  667. p_baker->bake_cells[p_idx].emission[1] = 0;
  668. p_baker->bake_cells[p_idx].emission[2] = 0;
  669. p_baker->bake_cells[p_idx].normal[0] = 0;
  670. p_baker->bake_cells[p_idx].normal[1] = 0;
  671. p_baker->bake_cells[p_idx].normal[2] = 0;
  672. p_baker->bake_cells[p_idx].albedo[0] = 0;
  673. p_baker->bake_cells[p_idx].albedo[1] = 0;
  674. p_baker->bake_cells[p_idx].albedo[2] = 0;
  675. }
  676. }
  677. Vector<Color> GIProbe::_get_bake_texture(Ref<Image> p_image, const Color &p_color) {
  678. Vector<Color> ret;
  679. if (p_image.is_null() || p_image->empty()) {
  680. ret.resize(bake_texture_size * bake_texture_size);
  681. for (int i = 0; i < bake_texture_size * bake_texture_size; i++) {
  682. ret[i] = p_color;
  683. }
  684. return ret;
  685. }
  686. p_image = p_image->duplicate();
  687. if (p_image->is_compressed()) {
  688. print_line("DECOMPRESSING!!!!");
  689. p_image->decompress();
  690. }
  691. p_image->convert(Image::FORMAT_RGBA8);
  692. p_image->resize(bake_texture_size, bake_texture_size, Image::INTERPOLATE_CUBIC);
  693. PoolVector<uint8_t>::Read r = p_image->get_data().read();
  694. ret.resize(bake_texture_size * bake_texture_size);
  695. for (int i = 0; i < bake_texture_size * bake_texture_size; i++) {
  696. Color c;
  697. c.r = (r[i * 4 + 0] / 255.0) * p_color.r;
  698. c.g = (r[i * 4 + 1] / 255.0) * p_color.g;
  699. c.b = (r[i * 4 + 2] / 255.0) * p_color.b;
  700. c.a = r[i * 4 + 3] / 255.0;
  701. ret[i] = c;
  702. }
  703. return ret;
  704. }
  705. GIProbe::Baker::MaterialCache GIProbe::_get_material_cache(Ref<Material> p_material, Baker *p_baker) {
  706. //this way of obtaining materials is inaccurate and also does not support some compressed formats very well
  707. Ref<SpatialMaterial> mat = p_material;
  708. Ref<Material> material = mat; //hack for now
  709. if (p_baker->material_cache.has(material)) {
  710. return p_baker->material_cache[material];
  711. }
  712. Baker::MaterialCache mc;
  713. if (mat.is_valid()) {
  714. Ref<Texture> albedo_tex = mat->get_texture(SpatialMaterial::TEXTURE_ALBEDO);
  715. Ref<Image> img_albedo;
  716. if (albedo_tex.is_valid()) {
  717. img_albedo = albedo_tex->get_data();
  718. } else {
  719. }
  720. mc.albedo = _get_bake_texture(img_albedo, mat->get_albedo());
  721. Ref<ImageTexture> emission_tex = mat->get_texture(SpatialMaterial::TEXTURE_EMISSION);
  722. Color emission_col = mat->get_emission();
  723. emission_col.r *= mat->get_emission_energy();
  724. emission_col.g *= mat->get_emission_energy();
  725. emission_col.b *= mat->get_emission_energy();
  726. Ref<Image> img_emission;
  727. if (emission_tex.is_valid()) {
  728. img_emission = emission_tex->get_data();
  729. }
  730. mc.emission = _get_bake_texture(img_emission, emission_col);
  731. } else {
  732. Ref<Image> empty;
  733. mc.albedo = _get_bake_texture(empty, Color(0.7, 0.7, 0.7));
  734. mc.emission = _get_bake_texture(empty, Color(0, 0, 0));
  735. }
  736. p_baker->material_cache[p_material] = mc;
  737. return mc;
  738. }
  739. void GIProbe::_plot_mesh(const Transform &p_xform, Ref<Mesh> &p_mesh, Baker *p_baker, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material) {
  740. for (int i = 0; i < p_mesh->get_surface_count(); i++) {
  741. if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  742. continue; //only triangles
  743. Ref<Material> src_material;
  744. if (p_override_material.is_valid()) {
  745. src_material = p_override_material;
  746. } else if (i < p_materials.size() && p_materials[i].is_valid()) {
  747. src_material = p_materials[i];
  748. } else {
  749. src_material = p_mesh->surface_get_material(i);
  750. }
  751. Baker::MaterialCache material = _get_material_cache(src_material, p_baker);
  752. Array a = p_mesh->surface_get_arrays(i);
  753. PoolVector<Vector3> vertices = a[Mesh::ARRAY_VERTEX];
  754. PoolVector<Vector3>::Read vr = vertices.read();
  755. PoolVector<Vector2> uv = a[Mesh::ARRAY_TEX_UV];
  756. PoolVector<Vector2>::Read uvr;
  757. PoolVector<int> index = a[Mesh::ARRAY_INDEX];
  758. bool read_uv = false;
  759. if (uv.size()) {
  760. uvr = uv.read();
  761. read_uv = true;
  762. }
  763. if (index.size()) {
  764. int facecount = index.size() / 3;
  765. PoolVector<int>::Read ir = index.read();
  766. for (int j = 0; j < facecount; j++) {
  767. Vector3 vtxs[3];
  768. Vector2 uvs[3];
  769. for (int k = 0; k < 3; k++) {
  770. vtxs[k] = p_xform.xform(vr[ir[j * 3 + k]]);
  771. }
  772. if (read_uv) {
  773. for (int k = 0; k < 3; k++) {
  774. uvs[k] = uvr[ir[j * 3 + k]];
  775. }
  776. }
  777. //test against original bounds
  778. if (!fast_tri_box_overlap(-extents, extents * 2, vtxs))
  779. continue;
  780. //plot
  781. _plot_face(0, 0, 0, 0, 0, vtxs, uvs, material, p_baker->po2_bounds, p_baker);
  782. }
  783. } else {
  784. int facecount = vertices.size() / 3;
  785. for (int j = 0; j < facecount; j++) {
  786. Vector3 vtxs[3];
  787. Vector2 uvs[3];
  788. for (int k = 0; k < 3; k++) {
  789. vtxs[k] = p_xform.xform(vr[j * 3 + k]);
  790. }
  791. if (read_uv) {
  792. for (int k = 0; k < 3; k++) {
  793. uvs[k] = uvr[j * 3 + k];
  794. }
  795. }
  796. //test against original bounds
  797. if (!fast_tri_box_overlap(-extents, extents * 2, vtxs))
  798. continue;
  799. //plot face
  800. _plot_face(0, 0, 0, 0, 0, vtxs, uvs, material, p_baker->po2_bounds, p_baker);
  801. }
  802. }
  803. }
  804. }
  805. void GIProbe::_find_meshes(Node *p_at_node, Baker *p_baker) {
  806. MeshInstance *mi = Object::cast_to<MeshInstance>(p_at_node);
  807. if (mi && mi->get_flag(GeometryInstance::FLAG_USE_BAKED_LIGHT) && mi->is_visible_in_tree()) {
  808. Ref<Mesh> mesh = mi->get_mesh();
  809. if (mesh.is_valid()) {
  810. Rect3 aabb = mesh->get_aabb();
  811. Transform xf = get_global_transform().affine_inverse() * mi->get_global_transform();
  812. if (Rect3(-extents, extents * 2).intersects(xf.xform(aabb))) {
  813. Baker::PlotMesh pm;
  814. pm.local_xform = xf;
  815. pm.mesh = mesh;
  816. for (int i = 0; i < mesh->get_surface_count(); i++) {
  817. pm.instance_materials.push_back(mi->get_surface_material(i));
  818. }
  819. pm.override_material = mi->get_material_override();
  820. p_baker->mesh_list.push_back(pm);
  821. }
  822. }
  823. }
  824. Spatial *s = Object::cast_to<Spatial>(p_at_node);
  825. if (s) {
  826. if (s->is_visible_in_tree()) {
  827. Array meshes = p_at_node->call("get_meshes");
  828. for (int i = 0; i < meshes.size(); i += 2) {
  829. Transform mxf = meshes[i];
  830. Ref<Mesh> mesh = meshes[i + 1];
  831. if (!mesh.is_valid())
  832. continue;
  833. Rect3 aabb = mesh->get_aabb();
  834. Transform xf = get_global_transform().affine_inverse() * (s->get_global_transform() * mxf);
  835. if (Rect3(-extents, extents * 2).intersects(xf.xform(aabb))) {
  836. Baker::PlotMesh pm;
  837. pm.local_xform = xf;
  838. pm.mesh = mesh;
  839. p_baker->mesh_list.push_back(pm);
  840. }
  841. }
  842. }
  843. }
  844. for (int i = 0; i < p_at_node->get_child_count(); i++) {
  845. Node *child = p_at_node->get_child(i);
  846. if (!child->get_owner())
  847. continue; //maybe a helper
  848. _find_meshes(child, p_baker);
  849. }
  850. }
  851. GIProbe::BakeBeginFunc GIProbe::bake_begin_function = NULL;
  852. GIProbe::BakeStepFunc GIProbe::bake_step_function = NULL;
  853. GIProbe::BakeEndFunc GIProbe::bake_end_function = NULL;
  854. void GIProbe::bake(Node *p_from_node, bool p_create_visual_debug) {
  855. Baker baker;
  856. static const int subdiv_value[SUBDIV_MAX] = { 7, 8, 9, 10 };
  857. baker.cell_subdiv = subdiv_value[subdiv];
  858. baker.bake_cells.resize(1);
  859. //find out the actual real bounds, power of 2, which gets the highest subdivision
  860. baker.po2_bounds = Rect3(-extents, extents * 2.0);
  861. int longest_axis = baker.po2_bounds.get_longest_axis_index();
  862. baker.axis_cell_size[longest_axis] = (1 << (baker.cell_subdiv - 1));
  863. baker.leaf_voxel_count = 0;
  864. for (int i = 0; i < 3; i++) {
  865. if (i == longest_axis)
  866. continue;
  867. baker.axis_cell_size[i] = baker.axis_cell_size[longest_axis];
  868. float axis_size = baker.po2_bounds.size[longest_axis];
  869. //shrink until fit subdiv
  870. while (axis_size / 2.0 >= baker.po2_bounds.size[i]) {
  871. axis_size /= 2.0;
  872. baker.axis_cell_size[i] >>= 1;
  873. }
  874. baker.po2_bounds.size[i] = baker.po2_bounds.size[longest_axis];
  875. }
  876. Transform to_bounds;
  877. to_bounds.basis.scale(Vector3(baker.po2_bounds.size[longest_axis], baker.po2_bounds.size[longest_axis], baker.po2_bounds.size[longest_axis]));
  878. to_bounds.origin = baker.po2_bounds.position;
  879. Transform to_grid;
  880. to_grid.basis.scale(Vector3(baker.axis_cell_size[longest_axis], baker.axis_cell_size[longest_axis], baker.axis_cell_size[longest_axis]));
  881. baker.to_cell_space = to_grid * to_bounds.affine_inverse();
  882. _find_meshes(p_from_node ? p_from_node : get_parent(), &baker);
  883. if (bake_begin_function) {
  884. bake_begin_function(baker.mesh_list.size() + 1);
  885. }
  886. int pmc = 0;
  887. for (List<Baker::PlotMesh>::Element *E = baker.mesh_list.front(); E; E = E->next()) {
  888. if (bake_step_function) {
  889. bake_step_function(pmc, RTR("Plotting Meshes") + " " + itos(pmc) + "/" + itos(baker.mesh_list.size()));
  890. }
  891. pmc++;
  892. _plot_mesh(E->get().local_xform, E->get().mesh, &baker, E->get().instance_materials, E->get().override_material);
  893. }
  894. if (bake_step_function) {
  895. bake_step_function(pmc++, RTR("Finishing Plot"));
  896. }
  897. _fixup_plot(0, 0, 0, 0, 0, &baker);
  898. //create the data for visual server
  899. PoolVector<int> data;
  900. data.resize(16 + (8 + 1 + 1 + 1 + 1) * baker.bake_cells.size()); //4 for header, rest for rest.
  901. {
  902. PoolVector<int>::Write w = data.write();
  903. uint32_t *w32 = (uint32_t *)w.ptr();
  904. w32[0] = 0; //version
  905. w32[1] = baker.cell_subdiv; //subdiv
  906. w32[2] = baker.axis_cell_size[0];
  907. w32[3] = baker.axis_cell_size[1];
  908. w32[4] = baker.axis_cell_size[2];
  909. w32[5] = baker.bake_cells.size();
  910. w32[6] = baker.leaf_voxel_count;
  911. int ofs = 16;
  912. for (int i = 0; i < baker.bake_cells.size(); i++) {
  913. for (int j = 0; j < 8; j++) {
  914. w32[ofs++] = baker.bake_cells[i].childs[j];
  915. }
  916. { //albedo
  917. uint32_t rgba = uint32_t(CLAMP(baker.bake_cells[i].albedo[0] * 255.0, 0, 255)) << 16;
  918. rgba |= uint32_t(CLAMP(baker.bake_cells[i].albedo[1] * 255.0, 0, 255)) << 8;
  919. rgba |= uint32_t(CLAMP(baker.bake_cells[i].albedo[2] * 255.0, 0, 255)) << 0;
  920. w32[ofs++] = rgba;
  921. }
  922. { //emission
  923. Vector3 e(baker.bake_cells[i].emission[0], baker.bake_cells[i].emission[1], baker.bake_cells[i].emission[2]);
  924. float l = e.length();
  925. if (l > 0) {
  926. e.normalize();
  927. l = CLAMP(l / 8.0, 0, 1.0);
  928. }
  929. uint32_t em = uint32_t(CLAMP(e[0] * 255, 0, 255)) << 24;
  930. em |= uint32_t(CLAMP(e[1] * 255, 0, 255)) << 16;
  931. em |= uint32_t(CLAMP(e[2] * 255, 0, 255)) << 8;
  932. em |= uint32_t(CLAMP(l * 255, 0, 255));
  933. w32[ofs++] = em;
  934. }
  935. //w32[ofs++]=baker.bake_cells[i].used_sides;
  936. { //normal
  937. Vector3 n(baker.bake_cells[i].normal[0], baker.bake_cells[i].normal[1], baker.bake_cells[i].normal[2]);
  938. n = n * Vector3(0.5, 0.5, 0.5) + Vector3(0.5, 0.5, 0.5);
  939. uint32_t norm = 0;
  940. norm |= uint32_t(CLAMP(n.x * 255.0, 0, 255)) << 16;
  941. norm |= uint32_t(CLAMP(n.y * 255.0, 0, 255)) << 8;
  942. norm |= uint32_t(CLAMP(n.z * 255.0, 0, 255)) << 0;
  943. w32[ofs++] = norm;
  944. }
  945. {
  946. uint16_t alpha = CLAMP(uint32_t(baker.bake_cells[i].alpha * 65535.0), 0, 65535);
  947. uint16_t level = baker.bake_cells[i].level;
  948. w32[ofs++] = (uint32_t(level) << 16) | uint32_t(alpha);
  949. }
  950. }
  951. }
  952. if (p_create_visual_debug) {
  953. _create_debug_mesh(&baker);
  954. } else {
  955. Ref<GIProbeData> probe_data;
  956. probe_data.instance();
  957. probe_data->set_bounds(Rect3(-extents, extents * 2.0));
  958. probe_data->set_cell_size(baker.po2_bounds.size[longest_axis] / baker.axis_cell_size[longest_axis]);
  959. probe_data->set_dynamic_data(data);
  960. probe_data->set_dynamic_range(dynamic_range);
  961. probe_data->set_energy(energy);
  962. probe_data->set_bias(bias);
  963. probe_data->set_normal_bias(normal_bias);
  964. probe_data->set_propagation(propagation);
  965. probe_data->set_interior(interior);
  966. probe_data->set_compress(compress);
  967. probe_data->set_to_cell_xform(baker.to_cell_space);
  968. set_probe_data(probe_data);
  969. }
  970. if (bake_end_function) {
  971. bake_end_function();
  972. }
  973. }
  974. void GIProbe::_debug_mesh(int p_idx, int p_level, const Rect3 &p_aabb, Ref<MultiMesh> &p_multimesh, int &idx, Baker *p_baker) {
  975. if (p_level == p_baker->cell_subdiv - 1) {
  976. Vector3 center = p_aabb.position + p_aabb.size * 0.5;
  977. Transform xform;
  978. xform.origin = center;
  979. xform.basis.scale(p_aabb.size * 0.5);
  980. p_multimesh->set_instance_transform(idx, xform);
  981. Color col = Color(p_baker->bake_cells[p_idx].albedo[0], p_baker->bake_cells[p_idx].albedo[1], p_baker->bake_cells[p_idx].albedo[2]);
  982. //Color col = Color(p_baker->bake_cells[p_idx].emission[0], p_baker->bake_cells[p_idx].emission[1], p_baker->bake_cells[p_idx].emission[2]);
  983. p_multimesh->set_instance_color(idx, col);
  984. idx++;
  985. } else {
  986. for (int i = 0; i < 8; i++) {
  987. if (p_baker->bake_cells[p_idx].childs[i] == Baker::CHILD_EMPTY)
  988. continue;
  989. Rect3 aabb = p_aabb;
  990. aabb.size *= 0.5;
  991. if (i & 1)
  992. aabb.position.x += aabb.size.x;
  993. if (i & 2)
  994. aabb.position.y += aabb.size.y;
  995. if (i & 4)
  996. aabb.position.z += aabb.size.z;
  997. _debug_mesh(p_baker->bake_cells[p_idx].childs[i], p_level + 1, aabb, p_multimesh, idx, p_baker);
  998. }
  999. }
  1000. }
  1001. void GIProbe::_create_debug_mesh(Baker *p_baker) {
  1002. Ref<MultiMesh> mm;
  1003. mm.instance();
  1004. mm->set_transform_format(MultiMesh::TRANSFORM_3D);
  1005. mm->set_color_format(MultiMesh::COLOR_8BIT);
  1006. print_line("leaf voxels: " + itos(p_baker->leaf_voxel_count));
  1007. mm->set_instance_count(p_baker->leaf_voxel_count);
  1008. Ref<ArrayMesh> mesh;
  1009. mesh.instance();
  1010. {
  1011. Array arr;
  1012. arr.resize(Mesh::ARRAY_MAX);
  1013. PoolVector<Vector3> vertices;
  1014. PoolVector<Color> colors;
  1015. int vtx_idx = 0;
  1016. #define ADD_VTX(m_idx) \
  1017. ; \
  1018. vertices.push_back(face_points[m_idx]); \
  1019. colors.push_back(Color(1, 1, 1, 1)); \
  1020. vtx_idx++;
  1021. for (int i = 0; i < 6; i++) {
  1022. Vector3 face_points[4];
  1023. for (int j = 0; j < 4; j++) {
  1024. float v[3];
  1025. v[0] = 1.0;
  1026. v[1] = 1 - 2 * ((j >> 1) & 1);
  1027. v[2] = v[1] * (1 - 2 * (j & 1));
  1028. for (int k = 0; k < 3; k++) {
  1029. if (i < 3)
  1030. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  1031. else
  1032. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  1033. }
  1034. }
  1035. //tri 1
  1036. ADD_VTX(0);
  1037. ADD_VTX(1);
  1038. ADD_VTX(2);
  1039. //tri 2
  1040. ADD_VTX(2);
  1041. ADD_VTX(3);
  1042. ADD_VTX(0);
  1043. }
  1044. arr[Mesh::ARRAY_VERTEX] = vertices;
  1045. arr[Mesh::ARRAY_COLOR] = colors;
  1046. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  1047. }
  1048. {
  1049. Ref<SpatialMaterial> fsm;
  1050. fsm.instance();
  1051. fsm->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  1052. fsm->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1053. fsm->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1054. fsm->set_albedo(Color(1, 1, 1, 1));
  1055. mesh->surface_set_material(0, fsm);
  1056. }
  1057. mm->set_mesh(mesh);
  1058. int idx = 0;
  1059. _debug_mesh(0, 0, p_baker->po2_bounds, mm, idx, p_baker);
  1060. MultiMeshInstance *mmi = memnew(MultiMeshInstance);
  1061. mmi->set_multimesh(mm);
  1062. add_child(mmi);
  1063. #ifdef TOOLS_ENABLED
  1064. if (get_tree()->get_edited_scene_root() == this) {
  1065. mmi->set_owner(this);
  1066. } else {
  1067. mmi->set_owner(get_owner());
  1068. }
  1069. #else
  1070. mmi->set_owner(get_owner());
  1071. #endif
  1072. }
  1073. void GIProbe::_debug_bake() {
  1074. bake(NULL, true);
  1075. }
  1076. Rect3 GIProbe::get_aabb() const {
  1077. return Rect3(-extents, extents * 2);
  1078. }
  1079. PoolVector<Face3> GIProbe::get_faces(uint32_t p_usage_flags) const {
  1080. return PoolVector<Face3>();
  1081. }
  1082. void GIProbe::_bind_methods() {
  1083. ClassDB::bind_method(D_METHOD("set_probe_data", "data"), &GIProbe::set_probe_data);
  1084. ClassDB::bind_method(D_METHOD("get_probe_data"), &GIProbe::get_probe_data);
  1085. ClassDB::bind_method(D_METHOD("set_subdiv", "subdiv"), &GIProbe::set_subdiv);
  1086. ClassDB::bind_method(D_METHOD("get_subdiv"), &GIProbe::get_subdiv);
  1087. ClassDB::bind_method(D_METHOD("set_extents", "extents"), &GIProbe::set_extents);
  1088. ClassDB::bind_method(D_METHOD("get_extents"), &GIProbe::get_extents);
  1089. ClassDB::bind_method(D_METHOD("set_dynamic_range", "max"), &GIProbe::set_dynamic_range);
  1090. ClassDB::bind_method(D_METHOD("get_dynamic_range"), &GIProbe::get_dynamic_range);
  1091. ClassDB::bind_method(D_METHOD("set_energy", "max"), &GIProbe::set_energy);
  1092. ClassDB::bind_method(D_METHOD("get_energy"), &GIProbe::get_energy);
  1093. ClassDB::bind_method(D_METHOD("set_bias", "max"), &GIProbe::set_bias);
  1094. ClassDB::bind_method(D_METHOD("get_bias"), &GIProbe::get_bias);
  1095. ClassDB::bind_method(D_METHOD("set_normal_bias", "max"), &GIProbe::set_normal_bias);
  1096. ClassDB::bind_method(D_METHOD("get_normal_bias"), &GIProbe::get_normal_bias);
  1097. ClassDB::bind_method(D_METHOD("set_propagation", "max"), &GIProbe::set_propagation);
  1098. ClassDB::bind_method(D_METHOD("get_propagation"), &GIProbe::get_propagation);
  1099. ClassDB::bind_method(D_METHOD("set_interior", "enable"), &GIProbe::set_interior);
  1100. ClassDB::bind_method(D_METHOD("is_interior"), &GIProbe::is_interior);
  1101. ClassDB::bind_method(D_METHOD("set_compress", "enable"), &GIProbe::set_compress);
  1102. ClassDB::bind_method(D_METHOD("is_compressed"), &GIProbe::is_compressed);
  1103. ClassDB::bind_method(D_METHOD("bake", "from_node", "create_visual_debug"), &GIProbe::bake, DEFVAL(Variant()), DEFVAL(false));
  1104. ClassDB::bind_method(D_METHOD("debug_bake"), &GIProbe::_debug_bake);
  1105. ClassDB::set_method_flags(get_class_static(), _scs_create("debug_bake"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
  1106. ADD_PROPERTY(PropertyInfo(Variant::INT, "subdiv", PROPERTY_HINT_ENUM, "64,128,256,512"), "set_subdiv", "get_subdiv");
  1107. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "extents"), "set_extents", "get_extents");
  1108. ADD_PROPERTY(PropertyInfo(Variant::INT, "dynamic_range", PROPERTY_HINT_RANGE, "1,16,1"), "set_dynamic_range", "get_dynamic_range");
  1109. ADD_PROPERTY(PropertyInfo(Variant::REAL, "energy", PROPERTY_HINT_RANGE, "0,16,0.01"), "set_energy", "get_energy");
  1110. ADD_PROPERTY(PropertyInfo(Variant::REAL, "propagation", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_propagation", "get_propagation");
  1111. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bias", PROPERTY_HINT_RANGE, "0,4,0.001"), "set_bias", "get_bias");
  1112. ADD_PROPERTY(PropertyInfo(Variant::REAL, "normal_bias", PROPERTY_HINT_RANGE, "0,4,0.001"), "set_normal_bias", "get_normal_bias");
  1113. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "interior"), "set_interior", "is_interior");
  1114. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "compress"), "set_compress", "is_compressed");
  1115. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "GIProbeData"), "set_probe_data", "get_probe_data");
  1116. BIND_ENUM_CONSTANT(SUBDIV_64);
  1117. BIND_ENUM_CONSTANT(SUBDIV_128);
  1118. BIND_ENUM_CONSTANT(SUBDIV_256);
  1119. BIND_ENUM_CONSTANT(SUBDIV_512);
  1120. BIND_ENUM_CONSTANT(SUBDIV_MAX);
  1121. }
  1122. GIProbe::GIProbe() {
  1123. subdiv = SUBDIV_128;
  1124. dynamic_range = 4;
  1125. energy = 1.0;
  1126. bias = 1.5;
  1127. normal_bias = 0.0;
  1128. propagation = 1.0;
  1129. extents = Vector3(10, 10, 10);
  1130. color_scan_cell_width = 4;
  1131. bake_texture_size = 128;
  1132. interior = false;
  1133. compress = false;
  1134. gi_probe = VS::get_singleton()->gi_probe_create();
  1135. }
  1136. GIProbe::~GIProbe() {
  1137. }