voxelizer.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /*************************************************************************/
  2. /* voxelizer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "voxelizer.h"
  31. #include "core/os/os.h"
  32. #include "core/os/threaded_array_processor.h"
  33. #include <stdlib.h>
  34. #define FINDMINMAX(x0, x1, x2, min, max) \
  35. min = max = x0; \
  36. if (x1 < min) min = x1; \
  37. if (x1 > max) max = x1; \
  38. if (x2 < min) min = x2; \
  39. if (x2 > max) max = x2;
  40. static bool planeBoxOverlap(Vector3 normal, float d, Vector3 maxbox) {
  41. int q;
  42. Vector3 vmin, vmax;
  43. for (q = 0; q <= 2; q++) {
  44. if (normal[q] > 0.0f) {
  45. vmin[q] = -maxbox[q];
  46. vmax[q] = maxbox[q];
  47. } else {
  48. vmin[q] = maxbox[q];
  49. vmax[q] = -maxbox[q];
  50. }
  51. }
  52. if (normal.dot(vmin) + d > 0.0f) return false;
  53. if (normal.dot(vmax) + d >= 0.0f) return true;
  54. return false;
  55. }
  56. /*======================== X-tests ========================*/
  57. #define AXISTEST_X01(a, b, fa, fb) \
  58. p0 = a * v0.y - b * v0.z; \
  59. p2 = a * v2.y - b * v2.z; \
  60. if (p0 < p2) { \
  61. min = p0; \
  62. max = p2; \
  63. } else { \
  64. min = p2; \
  65. max = p0; \
  66. } \
  67. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  68. if (min > rad || max < -rad) return false;
  69. #define AXISTEST_X2(a, b, fa, fb) \
  70. p0 = a * v0.y - b * v0.z; \
  71. p1 = a * v1.y - b * v1.z; \
  72. if (p0 < p1) { \
  73. min = p0; \
  74. max = p1; \
  75. } else { \
  76. min = p1; \
  77. max = p0; \
  78. } \
  79. rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
  80. if (min > rad || max < -rad) return false;
  81. /*======================== Y-tests ========================*/
  82. #define AXISTEST_Y02(a, b, fa, fb) \
  83. p0 = -a * v0.x + b * v0.z; \
  84. p2 = -a * v2.x + b * v2.z; \
  85. if (p0 < p2) { \
  86. min = p0; \
  87. max = p2; \
  88. } else { \
  89. min = p2; \
  90. max = p0; \
  91. } \
  92. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  93. if (min > rad || max < -rad) return false;
  94. #define AXISTEST_Y1(a, b, fa, fb) \
  95. p0 = -a * v0.x + b * v0.z; \
  96. p1 = -a * v1.x + b * v1.z; \
  97. if (p0 < p1) { \
  98. min = p0; \
  99. max = p1; \
  100. } else { \
  101. min = p1; \
  102. max = p0; \
  103. } \
  104. rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
  105. if (min > rad || max < -rad) return false;
  106. /*======================== Z-tests ========================*/
  107. #define AXISTEST_Z12(a, b, fa, fb) \
  108. p1 = a * v1.x - b * v1.y; \
  109. p2 = a * v2.x - b * v2.y; \
  110. if (p2 < p1) { \
  111. min = p2; \
  112. max = p1; \
  113. } else { \
  114. min = p1; \
  115. max = p2; \
  116. } \
  117. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  118. if (min > rad || max < -rad) return false;
  119. #define AXISTEST_Z0(a, b, fa, fb) \
  120. p0 = a * v0.x - b * v0.y; \
  121. p1 = a * v1.x - b * v1.y; \
  122. if (p0 < p1) { \
  123. min = p0; \
  124. max = p1; \
  125. } else { \
  126. min = p1; \
  127. max = p0; \
  128. } \
  129. rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
  130. if (min > rad || max < -rad) return false;
  131. static bool fast_tri_box_overlap(const Vector3 &boxcenter, const Vector3 boxhalfsize, const Vector3 *triverts) {
  132. /* use separating axis theorem to test overlap between triangle and box */
  133. /* need to test for overlap in these directions: */
  134. /* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
  135. /* we do not even need to test these) */
  136. /* 2) normal of the triangle */
  137. /* 3) crossproduct(edge from tri, {x,y,z}-directin) */
  138. /* this gives 3x3=9 more tests */
  139. Vector3 v0, v1, v2;
  140. float min, max, d, p0, p1, p2, rad, fex, fey, fez;
  141. Vector3 normal, e0, e1, e2;
  142. /* This is the fastest branch on Sun */
  143. /* move everything so that the boxcenter is in (0,0,0) */
  144. v0 = triverts[0] - boxcenter;
  145. v1 = triverts[1] - boxcenter;
  146. v2 = triverts[2] - boxcenter;
  147. /* compute triangle edges */
  148. e0 = v1 - v0; /* tri edge 0 */
  149. e1 = v2 - v1; /* tri edge 1 */
  150. e2 = v0 - v2; /* tri edge 2 */
  151. /* Bullet 3: */
  152. /* test the 9 tests first (this was faster) */
  153. fex = Math::abs(e0.x);
  154. fey = Math::abs(e0.y);
  155. fez = Math::abs(e0.z);
  156. AXISTEST_X01(e0.z, e0.y, fez, fey);
  157. AXISTEST_Y02(e0.z, e0.x, fez, fex);
  158. AXISTEST_Z12(e0.y, e0.x, fey, fex);
  159. fex = Math::abs(e1.x);
  160. fey = Math::abs(e1.y);
  161. fez = Math::abs(e1.z);
  162. AXISTEST_X01(e1.z, e1.y, fez, fey);
  163. AXISTEST_Y02(e1.z, e1.x, fez, fex);
  164. AXISTEST_Z0(e1.y, e1.x, fey, fex);
  165. fex = Math::abs(e2.x);
  166. fey = Math::abs(e2.y);
  167. fez = Math::abs(e2.z);
  168. AXISTEST_X2(e2.z, e2.y, fez, fey);
  169. AXISTEST_Y1(e2.z, e2.x, fez, fex);
  170. AXISTEST_Z12(e2.y, e2.x, fey, fex);
  171. /* Bullet 1: */
  172. /* first test overlap in the {x,y,z}-directions */
  173. /* find min, max of the triangle each direction, and test for overlap in */
  174. /* that direction -- this is equivalent to testing a minimal AABB around */
  175. /* the triangle against the AABB */
  176. /* test in X-direction */
  177. FINDMINMAX(v0.x, v1.x, v2.x, min, max);
  178. if (min > boxhalfsize.x || max < -boxhalfsize.x) return false;
  179. /* test in Y-direction */
  180. FINDMINMAX(v0.y, v1.y, v2.y, min, max);
  181. if (min > boxhalfsize.y || max < -boxhalfsize.y) return false;
  182. /* test in Z-direction */
  183. FINDMINMAX(v0.z, v1.z, v2.z, min, max);
  184. if (min > boxhalfsize.z || max < -boxhalfsize.z) return false;
  185. /* Bullet 2: */
  186. /* test if the box intersects the plane of the triangle */
  187. /* compute plane equation of triangle: normal*x+d=0 */
  188. normal = e0.cross(e1);
  189. d = -normal.dot(v0); /* plane eq: normal.x+d=0 */
  190. return planeBoxOverlap(normal, d, boxhalfsize); /* if true, box and triangle overlaps */
  191. }
  192. static _FORCE_INLINE_ void get_uv_and_normal(const Vector3 &p_pos, const Vector3 *p_vtx, const Vector2 *p_uv, const Vector3 *p_normal, Vector2 &r_uv, Vector3 &r_normal) {
  193. if (p_pos.distance_squared_to(p_vtx[0]) < CMP_EPSILON2) {
  194. r_uv = p_uv[0];
  195. r_normal = p_normal[0];
  196. return;
  197. }
  198. if (p_pos.distance_squared_to(p_vtx[1]) < CMP_EPSILON2) {
  199. r_uv = p_uv[1];
  200. r_normal = p_normal[1];
  201. return;
  202. }
  203. if (p_pos.distance_squared_to(p_vtx[2]) < CMP_EPSILON2) {
  204. r_uv = p_uv[2];
  205. r_normal = p_normal[2];
  206. return;
  207. }
  208. Vector3 v0 = p_vtx[1] - p_vtx[0];
  209. Vector3 v1 = p_vtx[2] - p_vtx[0];
  210. Vector3 v2 = p_pos - p_vtx[0];
  211. float d00 = v0.dot(v0);
  212. float d01 = v0.dot(v1);
  213. float d11 = v1.dot(v1);
  214. float d20 = v2.dot(v0);
  215. float d21 = v2.dot(v1);
  216. float denom = (d00 * d11 - d01 * d01);
  217. if (denom == 0) {
  218. r_uv = p_uv[0];
  219. r_normal = p_normal[0];
  220. return;
  221. }
  222. float v = (d11 * d20 - d01 * d21) / denom;
  223. float w = (d00 * d21 - d01 * d20) / denom;
  224. float u = 1.0f - v - w;
  225. r_uv = p_uv[0] * u + p_uv[1] * v + p_uv[2] * w;
  226. r_normal = (p_normal[0] * u + p_normal[1] * v + p_normal[2] * w).normalized();
  227. }
  228. void Voxelizer::_plot_face(int p_idx, int p_level, int p_x, int p_y, int p_z, const Vector3 *p_vtx, const Vector3 *p_normal, const Vector2 *p_uv, const MaterialCache &p_material, const AABB &p_aabb) {
  229. if (p_level == cell_subdiv) {
  230. //plot the face by guessing its albedo and emission value
  231. //find best axis to map to, for scanning values
  232. int closest_axis = 0;
  233. float closest_dot = 0;
  234. Plane plane = Plane(p_vtx[0], p_vtx[1], p_vtx[2]);
  235. Vector3 normal = plane.normal;
  236. for (int i = 0; i < 3; i++) {
  237. Vector3 axis;
  238. axis[i] = 1.0;
  239. float dot = ABS(normal.dot(axis));
  240. if (i == 0 || dot > closest_dot) {
  241. closest_axis = i;
  242. closest_dot = dot;
  243. }
  244. }
  245. Vector3 axis;
  246. axis[closest_axis] = 1.0;
  247. Vector3 t1;
  248. t1[(closest_axis + 1) % 3] = 1.0;
  249. Vector3 t2;
  250. t2[(closest_axis + 2) % 3] = 1.0;
  251. t1 *= p_aabb.size[(closest_axis + 1) % 3] / float(color_scan_cell_width);
  252. t2 *= p_aabb.size[(closest_axis + 2) % 3] / float(color_scan_cell_width);
  253. Color albedo_accum;
  254. Color emission_accum;
  255. Vector3 normal_accum;
  256. float alpha = 0.0;
  257. //map to a grid average in the best axis for this face
  258. for (int i = 0; i < color_scan_cell_width; i++) {
  259. Vector3 ofs_i = float(i) * t1;
  260. for (int j = 0; j < color_scan_cell_width; j++) {
  261. Vector3 ofs_j = float(j) * t2;
  262. Vector3 from = p_aabb.position + ofs_i + ofs_j;
  263. Vector3 to = from + t1 + t2 + axis * p_aabb.size[closest_axis];
  264. Vector3 half = (to - from) * 0.5;
  265. //is in this cell?
  266. if (!fast_tri_box_overlap(from + half, half, p_vtx)) {
  267. continue; //face does not span this cell
  268. }
  269. //go from -size to +size*2 to avoid skipping collisions
  270. Vector3 ray_from = from + (t1 + t2) * 0.5 - axis * p_aabb.size[closest_axis];
  271. Vector3 ray_to = ray_from + axis * p_aabb.size[closest_axis] * 2;
  272. if (normal.dot(ray_from - ray_to) < 0) {
  273. SWAP(ray_from, ray_to);
  274. }
  275. Vector3 intersection;
  276. if (!plane.intersects_segment(ray_from, ray_to, &intersection)) {
  277. if (ABS(plane.distance_to(ray_from)) < ABS(plane.distance_to(ray_to))) {
  278. intersection = plane.project(ray_from);
  279. } else {
  280. intersection = plane.project(ray_to);
  281. }
  282. }
  283. intersection = Face3(p_vtx[0], p_vtx[1], p_vtx[2]).get_closest_point_to(intersection);
  284. Vector2 uv;
  285. Vector3 lnormal;
  286. get_uv_and_normal(intersection, p_vtx, p_uv, p_normal, uv, lnormal);
  287. if (lnormal == Vector3()) //just in case normal as nor provided
  288. lnormal = normal;
  289. int uv_x = CLAMP(int(Math::fposmod(uv.x, 1.0f) * bake_texture_size), 0, bake_texture_size - 1);
  290. int uv_y = CLAMP(int(Math::fposmod(uv.y, 1.0f) * bake_texture_size), 0, bake_texture_size - 1);
  291. int ofs = uv_y * bake_texture_size + uv_x;
  292. albedo_accum.r += p_material.albedo[ofs].r;
  293. albedo_accum.g += p_material.albedo[ofs].g;
  294. albedo_accum.b += p_material.albedo[ofs].b;
  295. albedo_accum.a += p_material.albedo[ofs].a;
  296. emission_accum.r += p_material.emission[ofs].r;
  297. emission_accum.g += p_material.emission[ofs].g;
  298. emission_accum.b += p_material.emission[ofs].b;
  299. normal_accum += lnormal;
  300. alpha += 1.0;
  301. }
  302. }
  303. if (alpha == 0) {
  304. //could not in any way get texture information.. so use closest point to center
  305. Face3 f(p_vtx[0], p_vtx[1], p_vtx[2]);
  306. Vector3 inters = f.get_closest_point_to(p_aabb.position + p_aabb.size * 0.5);
  307. Vector3 lnormal;
  308. Vector2 uv;
  309. get_uv_and_normal(inters, p_vtx, p_uv, p_normal, uv, normal);
  310. if (lnormal == Vector3()) //just in case normal as nor provided
  311. lnormal = normal;
  312. int uv_x = CLAMP(Math::fposmod(uv.x, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
  313. int uv_y = CLAMP(Math::fposmod(uv.y, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
  314. int ofs = uv_y * bake_texture_size + uv_x;
  315. alpha = 1.0 / (color_scan_cell_width * color_scan_cell_width);
  316. albedo_accum.r = p_material.albedo[ofs].r * alpha;
  317. albedo_accum.g = p_material.albedo[ofs].g * alpha;
  318. albedo_accum.b = p_material.albedo[ofs].b * alpha;
  319. albedo_accum.a = p_material.albedo[ofs].a * alpha;
  320. emission_accum.r = p_material.emission[ofs].r * alpha;
  321. emission_accum.g = p_material.emission[ofs].g * alpha;
  322. emission_accum.b = p_material.emission[ofs].b * alpha;
  323. normal_accum = lnormal * alpha;
  324. } else {
  325. float accdiv = 1.0 / (color_scan_cell_width * color_scan_cell_width);
  326. alpha *= accdiv;
  327. albedo_accum.r *= accdiv;
  328. albedo_accum.g *= accdiv;
  329. albedo_accum.b *= accdiv;
  330. albedo_accum.a *= accdiv;
  331. emission_accum.r *= accdiv;
  332. emission_accum.g *= accdiv;
  333. emission_accum.b *= accdiv;
  334. normal_accum *= accdiv;
  335. }
  336. //put this temporarily here, corrected in a later step
  337. bake_cells.write[p_idx].albedo[0] += albedo_accum.r;
  338. bake_cells.write[p_idx].albedo[1] += albedo_accum.g;
  339. bake_cells.write[p_idx].albedo[2] += albedo_accum.b;
  340. bake_cells.write[p_idx].emission[0] += emission_accum.r;
  341. bake_cells.write[p_idx].emission[1] += emission_accum.g;
  342. bake_cells.write[p_idx].emission[2] += emission_accum.b;
  343. bake_cells.write[p_idx].normal[0] += normal_accum.x;
  344. bake_cells.write[p_idx].normal[1] += normal_accum.y;
  345. bake_cells.write[p_idx].normal[2] += normal_accum.z;
  346. bake_cells.write[p_idx].alpha += alpha;
  347. } else {
  348. //go down
  349. int half = (1 << cell_subdiv) >> (p_level + 1);
  350. for (int i = 0; i < 8; i++) {
  351. AABB aabb = p_aabb;
  352. aabb.size *= 0.5;
  353. int nx = p_x;
  354. int ny = p_y;
  355. int nz = p_z;
  356. if (i & 1) {
  357. aabb.position.x += aabb.size.x;
  358. nx += half;
  359. }
  360. if (i & 2) {
  361. aabb.position.y += aabb.size.y;
  362. ny += half;
  363. }
  364. if (i & 4) {
  365. aabb.position.z += aabb.size.z;
  366. nz += half;
  367. }
  368. //make sure to not plot beyond limits
  369. if (nx < 0 || nx >= axis_cell_size[0] || ny < 0 || ny >= axis_cell_size[1] || nz < 0 || nz >= axis_cell_size[2])
  370. continue;
  371. {
  372. AABB test_aabb = aabb;
  373. //test_aabb.grow_by(test_aabb.get_longest_axis_size()*0.05); //grow a bit to avoid numerical error in real-time
  374. Vector3 qsize = test_aabb.size * 0.5; //quarter size, for fast aabb test
  375. if (!fast_tri_box_overlap(test_aabb.position + qsize, qsize, p_vtx)) {
  376. //if (!Face3(p_vtx[0],p_vtx[1],p_vtx[2]).intersects_aabb2(aabb)) {
  377. //does not fit in child, go on
  378. continue;
  379. }
  380. }
  381. if (bake_cells[p_idx].children[i] == CHILD_EMPTY) {
  382. //sub cell must be created
  383. uint32_t child_idx = bake_cells.size();
  384. bake_cells.write[p_idx].children[i] = child_idx;
  385. bake_cells.resize(bake_cells.size() + 1);
  386. bake_cells.write[child_idx].level = p_level + 1;
  387. bake_cells.write[child_idx].x = nx / half;
  388. bake_cells.write[child_idx].y = ny / half;
  389. bake_cells.write[child_idx].z = nz / half;
  390. }
  391. _plot_face(bake_cells[p_idx].children[i], p_level + 1, nx, ny, nz, p_vtx, p_normal, p_uv, p_material, aabb);
  392. }
  393. }
  394. }
  395. Vector<Color> Voxelizer::_get_bake_texture(Ref<Image> p_image, const Color &p_color_mul, const Color &p_color_add) {
  396. Vector<Color> ret;
  397. if (p_image.is_null() || p_image->empty()) {
  398. ret.resize(bake_texture_size * bake_texture_size);
  399. for (int i = 0; i < bake_texture_size * bake_texture_size; i++) {
  400. ret.write[i] = p_color_add;
  401. }
  402. return ret;
  403. }
  404. p_image = p_image->duplicate();
  405. if (p_image->is_compressed()) {
  406. p_image->decompress();
  407. }
  408. p_image->convert(Image::FORMAT_RGBA8);
  409. p_image->resize(bake_texture_size, bake_texture_size, Image::INTERPOLATE_CUBIC);
  410. PoolVector<uint8_t>::Read r = p_image->get_data().read();
  411. ret.resize(bake_texture_size * bake_texture_size);
  412. for (int i = 0; i < bake_texture_size * bake_texture_size; i++) {
  413. Color c;
  414. c.r = (r[i * 4 + 0] / 255.0) * p_color_mul.r + p_color_add.r;
  415. c.g = (r[i * 4 + 1] / 255.0) * p_color_mul.g + p_color_add.g;
  416. c.b = (r[i * 4 + 2] / 255.0) * p_color_mul.b + p_color_add.b;
  417. c.a = r[i * 4 + 3] / 255.0;
  418. ret.write[i] = c;
  419. }
  420. return ret;
  421. }
  422. Voxelizer::MaterialCache Voxelizer::_get_material_cache(Ref<Material> p_material) {
  423. //this way of obtaining materials is inaccurate and also does not support some compressed formats very well
  424. Ref<StandardMaterial3D> mat = p_material;
  425. Ref<Material> material = mat; //hack for now
  426. if (material_cache.has(material)) {
  427. return material_cache[material];
  428. }
  429. MaterialCache mc;
  430. if (mat.is_valid()) {
  431. Ref<Texture2D> albedo_tex = mat->get_texture(StandardMaterial3D::TEXTURE_ALBEDO);
  432. Ref<Image> img_albedo;
  433. if (albedo_tex.is_valid()) {
  434. img_albedo = albedo_tex->get_data();
  435. mc.albedo = _get_bake_texture(img_albedo, mat->get_albedo(), Color(0, 0, 0)); // albedo texture, color is multiplicative
  436. } else {
  437. mc.albedo = _get_bake_texture(img_albedo, Color(1, 1, 1), mat->get_albedo()); // no albedo texture, color is additive
  438. }
  439. Ref<Texture2D> emission_tex = mat->get_texture(StandardMaterial3D::TEXTURE_EMISSION);
  440. Color emission_col = mat->get_emission();
  441. float emission_energy = mat->get_emission_energy();
  442. Ref<Image> img_emission;
  443. if (emission_tex.is_valid()) {
  444. img_emission = emission_tex->get_data();
  445. }
  446. if (mat->get_emission_operator() == StandardMaterial3D::EMISSION_OP_ADD) {
  447. mc.emission = _get_bake_texture(img_emission, Color(1, 1, 1) * emission_energy, emission_col * emission_energy);
  448. } else {
  449. mc.emission = _get_bake_texture(img_emission, emission_col * emission_energy, Color(0, 0, 0));
  450. }
  451. } else {
  452. Ref<Image> empty;
  453. mc.albedo = _get_bake_texture(empty, Color(0, 0, 0), Color(1, 1, 1));
  454. mc.emission = _get_bake_texture(empty, Color(0, 0, 0), Color(0, 0, 0));
  455. }
  456. material_cache[p_material] = mc;
  457. return mc;
  458. }
  459. void Voxelizer::plot_mesh(const Transform &p_xform, Ref<Mesh> &p_mesh, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material) {
  460. for (int i = 0; i < p_mesh->get_surface_count(); i++) {
  461. if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
  462. continue; //only triangles
  463. Ref<Material> src_material;
  464. if (p_override_material.is_valid()) {
  465. src_material = p_override_material;
  466. } else if (i < p_materials.size() && p_materials[i].is_valid()) {
  467. src_material = p_materials[i];
  468. } else {
  469. src_material = p_mesh->surface_get_material(i);
  470. }
  471. MaterialCache material = _get_material_cache(src_material);
  472. Array a = p_mesh->surface_get_arrays(i);
  473. PoolVector<Vector3> vertices = a[Mesh::ARRAY_VERTEX];
  474. PoolVector<Vector3>::Read vr = vertices.read();
  475. PoolVector<Vector2> uv = a[Mesh::ARRAY_TEX_UV];
  476. PoolVector<Vector2>::Read uvr;
  477. PoolVector<Vector3> normals = a[Mesh::ARRAY_NORMAL];
  478. PoolVector<Vector3>::Read nr;
  479. PoolVector<int> index = a[Mesh::ARRAY_INDEX];
  480. bool read_uv = false;
  481. bool read_normals = false;
  482. if (uv.size()) {
  483. uvr = uv.read();
  484. read_uv = true;
  485. }
  486. if (normals.size()) {
  487. read_normals = true;
  488. nr = normals.read();
  489. }
  490. if (index.size()) {
  491. int facecount = index.size() / 3;
  492. PoolVector<int>::Read ir = index.read();
  493. for (int j = 0; j < facecount; j++) {
  494. Vector3 vtxs[3];
  495. Vector2 uvs[3];
  496. Vector3 normal[3];
  497. for (int k = 0; k < 3; k++) {
  498. vtxs[k] = p_xform.xform(vr[ir[j * 3 + k]]);
  499. }
  500. if (read_uv) {
  501. for (int k = 0; k < 3; k++) {
  502. uvs[k] = uvr[ir[j * 3 + k]];
  503. }
  504. }
  505. if (read_normals) {
  506. for (int k = 0; k < 3; k++) {
  507. normal[k] = nr[ir[j * 3 + k]];
  508. }
  509. }
  510. //test against original bounds
  511. if (!fast_tri_box_overlap(original_bounds.position + original_bounds.size * 0.5, original_bounds.size * 0.5, vtxs))
  512. continue;
  513. //plot
  514. _plot_face(0, 0, 0, 0, 0, vtxs, normal, uvs, material, po2_bounds);
  515. }
  516. } else {
  517. int facecount = vertices.size() / 3;
  518. for (int j = 0; j < facecount; j++) {
  519. Vector3 vtxs[3];
  520. Vector2 uvs[3];
  521. Vector3 normal[3];
  522. for (int k = 0; k < 3; k++) {
  523. vtxs[k] = p_xform.xform(vr[j * 3 + k]);
  524. }
  525. if (read_uv) {
  526. for (int k = 0; k < 3; k++) {
  527. uvs[k] = uvr[j * 3 + k];
  528. }
  529. }
  530. if (read_normals) {
  531. for (int k = 0; k < 3; k++) {
  532. normal[k] = nr[j * 3 + k];
  533. }
  534. }
  535. //test against original bounds
  536. if (!fast_tri_box_overlap(original_bounds.position + original_bounds.size * 0.5, original_bounds.size * 0.5, vtxs))
  537. continue;
  538. //plot face
  539. _plot_face(0, 0, 0, 0, 0, vtxs, normal, uvs, material, po2_bounds);
  540. }
  541. }
  542. }
  543. max_original_cells = bake_cells.size();
  544. }
  545. void Voxelizer::_sort() {
  546. // cells need to be sorted by level and coordinates
  547. // it is important that level has more priority (for compute), and that Z has the least,
  548. // given it may aid older implementations plot using GPU
  549. Vector<CellSort> sorted_cells;
  550. uint32_t cell_count = bake_cells.size();
  551. sorted_cells.resize(cell_count);
  552. {
  553. CellSort *sort_cellsp = sorted_cells.ptrw();
  554. const Cell *bake_cellsp = bake_cells.ptr();
  555. for (uint32_t i = 0; i < cell_count; i++) {
  556. sort_cellsp[i].x = bake_cellsp[i].x;
  557. sort_cellsp[i].y = bake_cellsp[i].y;
  558. sort_cellsp[i].z = bake_cellsp[i].z;
  559. sort_cellsp[i].level = bake_cellsp[i].level;
  560. sort_cellsp[i].index = i;
  561. }
  562. }
  563. sorted_cells.sort();
  564. //verify just in case, index 0 must be level 0
  565. ERR_FAIL_COND(sorted_cells[0].level != 0);
  566. Vector<Cell> new_bake_cells;
  567. new_bake_cells.resize(cell_count);
  568. Vector<uint32_t> reverse_map;
  569. {
  570. reverse_map.resize(cell_count);
  571. const CellSort *sort_cellsp = sorted_cells.ptr();
  572. uint32_t *reverse_mapp = reverse_map.ptrw();
  573. for (uint32_t i = 0; i < cell_count; i++) {
  574. reverse_mapp[sort_cellsp[i].index] = i;
  575. }
  576. }
  577. {
  578. const CellSort *sort_cellsp = sorted_cells.ptr();
  579. const Cell *bake_cellsp = bake_cells.ptr();
  580. const uint32_t *reverse_mapp = reverse_map.ptr();
  581. Cell *new_bake_cellsp = new_bake_cells.ptrw();
  582. for (uint32_t i = 0; i < cell_count; i++) {
  583. //copy to new cell
  584. new_bake_cellsp[i] = bake_cellsp[sort_cellsp[i].index];
  585. //remap children
  586. for (uint32_t j = 0; j < 8; j++) {
  587. if (new_bake_cellsp[i].children[j] != CHILD_EMPTY) {
  588. new_bake_cellsp[i].children[j] = reverse_mapp[new_bake_cellsp[i].children[j]];
  589. }
  590. }
  591. }
  592. }
  593. bake_cells = new_bake_cells;
  594. sorted = true;
  595. }
  596. void Voxelizer::_fixup_plot(int p_idx, int p_level) {
  597. if (p_level == cell_subdiv) {
  598. leaf_voxel_count++;
  599. float alpha = bake_cells[p_idx].alpha;
  600. bake_cells.write[p_idx].albedo[0] /= alpha;
  601. bake_cells.write[p_idx].albedo[1] /= alpha;
  602. bake_cells.write[p_idx].albedo[2] /= alpha;
  603. //transfer emission to light
  604. bake_cells.write[p_idx].emission[0] /= alpha;
  605. bake_cells.write[p_idx].emission[1] /= alpha;
  606. bake_cells.write[p_idx].emission[2] /= alpha;
  607. bake_cells.write[p_idx].normal[0] /= alpha;
  608. bake_cells.write[p_idx].normal[1] /= alpha;
  609. bake_cells.write[p_idx].normal[2] /= alpha;
  610. Vector3 n(bake_cells[p_idx].normal[0], bake_cells[p_idx].normal[1], bake_cells[p_idx].normal[2]);
  611. if (n.length() < 0.01) {
  612. //too much fight over normal, zero it
  613. bake_cells.write[p_idx].normal[0] = 0;
  614. bake_cells.write[p_idx].normal[1] = 0;
  615. bake_cells.write[p_idx].normal[2] = 0;
  616. } else {
  617. n.normalize();
  618. bake_cells.write[p_idx].normal[0] = n.x;
  619. bake_cells.write[p_idx].normal[1] = n.y;
  620. bake_cells.write[p_idx].normal[2] = n.z;
  621. }
  622. bake_cells.write[p_idx].alpha = 1.0;
  623. /*if (bake_light.size()) {
  624. for(int i=0;i<6;i++) {
  625. }
  626. }*/
  627. } else {
  628. //go down
  629. bake_cells.write[p_idx].emission[0] = 0;
  630. bake_cells.write[p_idx].emission[1] = 0;
  631. bake_cells.write[p_idx].emission[2] = 0;
  632. bake_cells.write[p_idx].normal[0] = 0;
  633. bake_cells.write[p_idx].normal[1] = 0;
  634. bake_cells.write[p_idx].normal[2] = 0;
  635. bake_cells.write[p_idx].albedo[0] = 0;
  636. bake_cells.write[p_idx].albedo[1] = 0;
  637. bake_cells.write[p_idx].albedo[2] = 0;
  638. float alpha_average = 0;
  639. int children_found = 0;
  640. for (int i = 0; i < 8; i++) {
  641. uint32_t child = bake_cells[p_idx].children[i];
  642. if (child == CHILD_EMPTY)
  643. continue;
  644. _fixup_plot(child, p_level + 1);
  645. alpha_average += bake_cells[child].alpha;
  646. children_found++;
  647. }
  648. bake_cells.write[p_idx].alpha = alpha_average / 8.0;
  649. }
  650. }
  651. void Voxelizer::begin_bake(int p_subdiv, const AABB &p_bounds) {
  652. sorted = false;
  653. original_bounds = p_bounds;
  654. cell_subdiv = p_subdiv;
  655. bake_cells.resize(1);
  656. material_cache.clear();
  657. print_line("subdiv: " + itos(p_subdiv));
  658. //find out the actual real bounds, power of 2, which gets the highest subdivision
  659. po2_bounds = p_bounds;
  660. int longest_axis = po2_bounds.get_longest_axis_index();
  661. axis_cell_size[longest_axis] = 1 << cell_subdiv;
  662. leaf_voxel_count = 0;
  663. for (int i = 0; i < 3; i++) {
  664. if (i == longest_axis)
  665. continue;
  666. axis_cell_size[i] = axis_cell_size[longest_axis];
  667. float axis_size = po2_bounds.size[longest_axis];
  668. //shrink until fit subdiv
  669. while (axis_size / 2.0 >= po2_bounds.size[i]) {
  670. axis_size /= 2.0;
  671. axis_cell_size[i] >>= 1;
  672. }
  673. po2_bounds.size[i] = po2_bounds.size[longest_axis];
  674. }
  675. Transform to_bounds;
  676. to_bounds.basis.scale(Vector3(po2_bounds.size[longest_axis], po2_bounds.size[longest_axis], po2_bounds.size[longest_axis]));
  677. to_bounds.origin = po2_bounds.position;
  678. Transform to_grid;
  679. to_grid.basis.scale(Vector3(axis_cell_size[longest_axis], axis_cell_size[longest_axis], axis_cell_size[longest_axis]));
  680. to_cell_space = to_grid * to_bounds.affine_inverse();
  681. cell_size = po2_bounds.size[longest_axis] / axis_cell_size[longest_axis];
  682. }
  683. void Voxelizer::end_bake() {
  684. if (!sorted) {
  685. _sort();
  686. }
  687. _fixup_plot(0, 0);
  688. }
  689. //create the data for visual server
  690. int Voxelizer::get_gi_probe_octree_depth() const {
  691. return cell_subdiv;
  692. }
  693. Vector3i Voxelizer::get_giprobe_octree_size() const {
  694. return Vector3i(axis_cell_size[0], axis_cell_size[1], axis_cell_size[2]);
  695. }
  696. int Voxelizer::get_giprobe_cell_count() const {
  697. return bake_cells.size();
  698. }
  699. PoolVector<uint8_t> Voxelizer::get_giprobe_octree_cells() const {
  700. PoolVector<uint8_t> data;
  701. data.resize((8 * 4) * bake_cells.size()); //8 uint32t values
  702. {
  703. PoolVector<uint8_t>::Write w = data.write();
  704. uint32_t *children_cells = (uint32_t *)w.ptr();
  705. const Cell *cells = bake_cells.ptr();
  706. uint32_t cell_count = bake_cells.size();
  707. for (uint32_t i = 0; i < cell_count; i++) {
  708. for (uint32_t j = 0; j < 8; j++) {
  709. children_cells[i * 8 + j] = cells[i].children[j];
  710. }
  711. }
  712. }
  713. return data;
  714. }
  715. PoolVector<uint8_t> Voxelizer::get_giprobe_data_cells() const {
  716. PoolVector<uint8_t> data;
  717. data.resize((4 * 4) * bake_cells.size()); //8 uint32t values
  718. {
  719. PoolVector<uint8_t>::Write w = data.write();
  720. uint32_t *dataptr = (uint32_t *)w.ptr();
  721. const Cell *cells = bake_cells.ptr();
  722. uint32_t cell_count = bake_cells.size();
  723. for (uint32_t i = 0; i < cell_count; i++) {
  724. { //position
  725. uint32_t x = cells[i].x;
  726. uint32_t y = cells[i].y;
  727. uint32_t z = cells[i].z;
  728. uint32_t position = x;
  729. position |= y << 11;
  730. position |= z << 21;
  731. dataptr[i * 4 + 0] = position;
  732. }
  733. { //albedo + alpha
  734. uint32_t rgba = uint32_t(CLAMP(cells[i].alpha * 255.0, 0, 255)) << 24; //a
  735. rgba |= uint32_t(CLAMP(cells[i].albedo[2] * 255.0, 0, 255)) << 16; //b
  736. rgba |= uint32_t(CLAMP(cells[i].albedo[1] * 255.0, 0, 255)) << 8; //g
  737. rgba |= uint32_t(CLAMP(cells[i].albedo[0] * 255.0, 0, 255)); //r
  738. dataptr[i * 4 + 1] = rgba;
  739. }
  740. { //emission, as rgbe9995
  741. Color emission = Color(cells[i].emission[0], cells[i].emission[1], cells[i].emission[2]);
  742. dataptr[i * 4 + 2] = emission.to_rgbe9995();
  743. }
  744. { //normal
  745. Vector3 n(bake_cells[i].normal[0], bake_cells[i].normal[1], bake_cells[i].normal[2]);
  746. n.normalize();
  747. uint32_t normal = uint32_t(uint8_t(int8_t(CLAMP(n.x * 127.0, -128, 127))));
  748. normal |= uint32_t(uint8_t(int8_t(CLAMP(n.y * 127.0, -128, 127)))) << 8;
  749. normal |= uint32_t(uint8_t(int8_t(CLAMP(n.z * 127.0, -128, 127)))) << 16;
  750. dataptr[i * 4 + 3] = normal;
  751. }
  752. }
  753. }
  754. return data;
  755. }
  756. PoolVector<int> Voxelizer::get_giprobe_level_cell_count() const {
  757. uint32_t cell_count = bake_cells.size();
  758. const Cell *cells = bake_cells.ptr();
  759. PoolVector<int> level_count;
  760. level_count.resize(cell_subdiv + 1); //remember, always x+1 levels for x subdivisions
  761. {
  762. PoolVector<int>::Write w = level_count.write();
  763. for (int i = 0; i < cell_subdiv + 1; i++) {
  764. w[i] = 0;
  765. }
  766. for (uint32_t i = 0; i < cell_count; i++) {
  767. w[cells[i].level]++;
  768. }
  769. }
  770. return level_count;
  771. }
  772. // euclidean distance computation based on:
  773. // https://prideout.net/blog/distance_fields/
  774. #define square(m_s) ((m_s) * (m_s))
  775. #define INF 1e20
  776. /* dt of 1d function using squared distance */
  777. static void edt(float *f, int stride, int n) {
  778. float *d = (float *)alloca(sizeof(float) * n + sizeof(int) * n + sizeof(float) * (n + 1));
  779. int *v = (int *)&(d[n]);
  780. float *z = (float *)&v[n];
  781. int k = 0;
  782. v[0] = 0;
  783. z[0] = -INF;
  784. z[1] = +INF;
  785. for (int q = 1; q <= n - 1; q++) {
  786. float s = ((f[q * stride] + square(q)) - (f[v[k] * stride] + square(v[k]))) / (2 * q - 2 * v[k]);
  787. while (s <= z[k]) {
  788. k--;
  789. s = ((f[q * stride] + square(q)) - (f[v[k] * stride] + square(v[k]))) / (2 * q - 2 * v[k]);
  790. }
  791. k++;
  792. v[k] = q;
  793. z[k] = s;
  794. z[k + 1] = +INF;
  795. }
  796. k = 0;
  797. for (int q = 0; q <= n - 1; q++) {
  798. while (z[k + 1] < q)
  799. k++;
  800. d[q] = square(q - v[k]) + f[v[k] * stride];
  801. }
  802. for (int i = 0; i < n; i++) {
  803. f[i * stride] = d[i];
  804. }
  805. }
  806. #undef square
  807. PoolVector<uint8_t> Voxelizer::get_sdf_3d_image() const {
  808. Vector3i octree_size = get_giprobe_octree_size();
  809. uint32_t float_count = octree_size.x * octree_size.y * octree_size.z;
  810. float *work_memory = memnew_arr(float, float_count);
  811. for (uint32_t i = 0; i < float_count; i++) {
  812. work_memory[i] = INF;
  813. }
  814. uint32_t y_mult = octree_size.x;
  815. uint32_t z_mult = y_mult * octree_size.y;
  816. //plot solid cells
  817. {
  818. const Cell *cells = bake_cells.ptr();
  819. uint32_t cell_count = bake_cells.size();
  820. for (uint32_t i = 0; i < cell_count; i++) {
  821. if (cells[i].level < (cell_subdiv - 1)) {
  822. continue; //do not care about this level
  823. }
  824. work_memory[cells[i].x + cells[i].y * y_mult + cells[i].z * z_mult] = 0;
  825. }
  826. }
  827. //process in each direction
  828. //xy->z
  829. for (int i = 0; i < octree_size.x; i++) {
  830. for (int j = 0; j < octree_size.y; j++) {
  831. edt(&work_memory[i + j * y_mult], z_mult, octree_size.z);
  832. }
  833. }
  834. //xz->y
  835. for (int i = 0; i < octree_size.x; i++) {
  836. for (int j = 0; j < octree_size.z; j++) {
  837. edt(&work_memory[i + j * z_mult], y_mult, octree_size.y);
  838. }
  839. }
  840. //yz->x
  841. for (int i = 0; i < octree_size.y; i++) {
  842. for (int j = 0; j < octree_size.z; j++) {
  843. edt(&work_memory[i * y_mult + j * z_mult], 1, octree_size.x);
  844. }
  845. }
  846. PoolVector<uint8_t> image3d;
  847. image3d.resize(float_count);
  848. {
  849. PoolVector<uint8_t>::Write w = image3d.write();
  850. for (uint32_t i = 0; i < float_count; i++) {
  851. uint32_t d = uint32_t(Math::sqrt(work_memory[i]));
  852. if (d == 0) {
  853. w[i] = 0;
  854. } else {
  855. w[i] = CLAMP(d, 0, 254) + 1;
  856. }
  857. }
  858. }
  859. return image3d;
  860. }
  861. #undef INF
  862. void Voxelizer::_debug_mesh(int p_idx, int p_level, const AABB &p_aabb, Ref<MultiMesh> &p_multimesh, int &idx) {
  863. if (p_level == cell_subdiv - 1) {
  864. Vector3 center = p_aabb.position + p_aabb.size * 0.5;
  865. Transform xform;
  866. xform.origin = center;
  867. xform.basis.scale(p_aabb.size * 0.5);
  868. p_multimesh->set_instance_transform(idx, xform);
  869. Color col;
  870. col = Color(bake_cells[p_idx].albedo[0], bake_cells[p_idx].albedo[1], bake_cells[p_idx].albedo[2]);
  871. //Color col = Color(bake_cells[p_idx].emission[0], bake_cells[p_idx].emission[1], bake_cells[p_idx].emission[2]);
  872. p_multimesh->set_instance_color(idx, col);
  873. idx++;
  874. } else {
  875. for (int i = 0; i < 8; i++) {
  876. uint32_t child = bake_cells[p_idx].children[i];
  877. if (child == CHILD_EMPTY || child >= (uint32_t)max_original_cells)
  878. continue;
  879. AABB aabb = p_aabb;
  880. aabb.size *= 0.5;
  881. if (i & 1)
  882. aabb.position.x += aabb.size.x;
  883. if (i & 2)
  884. aabb.position.y += aabb.size.y;
  885. if (i & 4)
  886. aabb.position.z += aabb.size.z;
  887. _debug_mesh(bake_cells[p_idx].children[i], p_level + 1, aabb, p_multimesh, idx);
  888. }
  889. }
  890. }
  891. Ref<MultiMesh> Voxelizer::create_debug_multimesh() {
  892. Ref<MultiMesh> mm;
  893. mm.instance();
  894. mm->set_transform_format(MultiMesh::TRANSFORM_3D);
  895. mm->set_use_colors(true);
  896. mm->set_instance_count(leaf_voxel_count);
  897. Ref<ArrayMesh> mesh;
  898. mesh.instance();
  899. {
  900. Array arr;
  901. arr.resize(Mesh::ARRAY_MAX);
  902. PoolVector<Vector3> vertices;
  903. PoolVector<Color> colors;
  904. #define ADD_VTX(m_idx) \
  905. vertices.push_back(face_points[m_idx]); \
  906. colors.push_back(Color(1, 1, 1, 1));
  907. for (int i = 0; i < 6; i++) {
  908. Vector3 face_points[4];
  909. for (int j = 0; j < 4; j++) {
  910. float v[3];
  911. v[0] = 1.0;
  912. v[1] = 1 - 2 * ((j >> 1) & 1);
  913. v[2] = v[1] * (1 - 2 * (j & 1));
  914. for (int k = 0; k < 3; k++) {
  915. if (i < 3)
  916. face_points[j][(i + k) % 3] = v[k];
  917. else
  918. face_points[3 - j][(i + k) % 3] = -v[k];
  919. }
  920. }
  921. //tri 1
  922. ADD_VTX(0);
  923. ADD_VTX(1);
  924. ADD_VTX(2);
  925. //tri 2
  926. ADD_VTX(2);
  927. ADD_VTX(3);
  928. ADD_VTX(0);
  929. }
  930. arr[Mesh::ARRAY_VERTEX] = vertices;
  931. arr[Mesh::ARRAY_COLOR] = colors;
  932. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
  933. }
  934. {
  935. Ref<StandardMaterial3D> fsm;
  936. fsm.instance();
  937. fsm->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  938. fsm->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  939. fsm->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  940. fsm->set_albedo(Color(1, 1, 1, 1));
  941. mesh->surface_set_material(0, fsm);
  942. }
  943. mm->set_mesh(mesh);
  944. int idx = 0;
  945. _debug_mesh(0, 0, po2_bounds, mm, idx);
  946. return mm;
  947. }
  948. Transform Voxelizer::get_to_cell_space_xform() const {
  949. return to_cell_space;
  950. }
  951. Voxelizer::Voxelizer() {
  952. sorted = false;
  953. color_scan_cell_width = 4;
  954. bake_texture_size = 128;
  955. }