export_obj.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. function export_obj_write_string(out: i32[], str: string) {
  2. for (let i: i32 = 0; i < str.length; ++i) {
  3. array_push(out, char_code_at(str, i));
  4. }
  5. }
  6. function export_obj_run(path: string, paint_objects: mesh_object_t[], apply_disp: bool = false) {
  7. let o: i32[] = [];
  8. export_obj_write_string(o, "# armorpaint.org\n");
  9. let poff: i32 = 0;
  10. let noff: i32 = 0;
  11. let toff: i32 = 0;
  12. for (let i: i32 = 0; i < paint_objects.length; ++i) {
  13. let p: mesh_object_t = paint_objects[i];
  14. let mesh: mesh_data_t = p.data;
  15. let inv: f32 = 1 / 32767;
  16. let sc: f32 = p.data.scale_pos * inv;
  17. let posa: i16_array_t = mesh.vertex_arrays[0].values;
  18. let nora: i16_array_t = mesh.vertex_arrays[1].values;
  19. let texa: i16_array_t = mesh.vertex_arrays[2].values;
  20. let len: i32 = math_floor(posa.length / 4);
  21. // Merge shared vertices and remap indices
  22. let posa2: i16_array_t = i16_array_create(len * 3);
  23. let nora2: i16_array_t = i16_array_create(len * 3);
  24. let texa2: i16_array_t = i16_array_create(len * 2);
  25. let posmap: map_t<i32, i32> = map_create();
  26. let normap: map_t<i32, i32> = map_create();
  27. let texmap: map_t<i32, i32> = map_create();
  28. let pi: i32 = 0;
  29. let ni: i32 = 0;
  30. let ti: i32 = 0;
  31. for (let i: i32 = 0; i < len; ++i) {
  32. let found: bool = false;
  33. for (let j: i32 = 0; j < pi; ++j) {
  34. if (posa2[j * 3 ] == posa[i * 4 ] &&
  35. posa2[j * 3 + 1] == posa[i * 4 + 1] &&
  36. posa2[j * 3 + 2] == posa[i * 4 + 2]) {
  37. map_set(posmap, i, j);
  38. found = true;
  39. break;
  40. }
  41. }
  42. if (!found) {
  43. map_set(posmap, i, pi);
  44. posa2[pi * 3 ] = posa[i * 4 ];
  45. posa2[pi * 3 + 1] = posa[i * 4 + 1];
  46. posa2[pi * 3 + 2] = posa[i * 4 + 2];
  47. pi++;
  48. }
  49. found = false;
  50. for (let j: i32 = 0; j < ni; ++j) {
  51. if (nora2[j * 3 ] == nora[i * 2 ] &&
  52. nora2[j * 3 + 1] == nora[i * 2 + 1] &&
  53. nora2[j * 3 + 2] == posa[i * 4 + 3]) {
  54. map_set(normap, i, j);
  55. found = true;
  56. break;
  57. }
  58. }
  59. if (!found) {
  60. map_set(normap, i, ni);
  61. nora2[ni * 3 ] = nora[i * 2 ];
  62. nora2[ni * 3 + 1] = nora[i * 2 + 1];
  63. nora2[ni * 3 + 2] = posa[i * 4 + 3];
  64. ni++;
  65. }
  66. found = false;
  67. for (let j: i32 = 0; j < ti; ++j) {
  68. if (texa2[j * 2 ] == texa[i * 2 ] &&
  69. texa2[j * 2 + 1] == texa[i * 2 + 1]) {
  70. map_set(texmap, i, j);
  71. found = true;
  72. break;
  73. }
  74. }
  75. if (!found) {
  76. map_set(texmap, i, ti);
  77. texa2[ti * 2 ] = texa[i * 2 ];
  78. texa2[ti * 2 + 1] = texa[i * 2 + 1];
  79. ti++;
  80. }
  81. }
  82. if (apply_disp) {
  83. // let height: buffer_t = layers[0].texpaint_pack.getPixels();
  84. // let res: i32 = layers[0].texpaint_pack.width;
  85. // let strength: f32 = 0.1;
  86. // for (let i: i32 = 0; i < len; ++i) {
  87. // let x: i32 = math_floor(texa2[i * 2 ] / 32767 * res);
  88. // let y: i32 = math_floor((1.0 - texa2[i * 2 + 1] / 32767) * res);
  89. // let h: f32 = (1.0 - height.get((y * res + x) * 4 + 3) / 255) * strength;
  90. // posa2[i * 3 ] -= math_floor(nora2[i * 3 ] * inv * h / sc);
  91. // posa2[i * 3 + 1] -= math_floor(nora2[i * 3 + 1] * inv * h / sc);
  92. // posa2[i * 3 + 2] -= math_floor(nora2[i * 3 + 2] * inv * h / sc);
  93. // }
  94. }
  95. export_obj_write_string(o, "o " + p.base.name + "\n");
  96. for (let i: i32 = 0; i < pi; ++i) {
  97. export_obj_write_string(o, "v ");
  98. export_obj_write_string(o, posa2[i * 3] * sc + "");
  99. export_obj_write_string(o, " ");
  100. export_obj_write_string(o, posa2[i * 3 + 2] * sc + "");
  101. export_obj_write_string(o, " ");
  102. export_obj_write_string(o, -posa2[i * 3 + 1] * sc + "");
  103. export_obj_write_string(o, "\n");
  104. }
  105. for (let i: i32 = 0; i < ni; ++i) {
  106. export_obj_write_string(o, "vn ");
  107. export_obj_write_string(o, nora2[i * 3] * inv + "");
  108. export_obj_write_string(o, " ");
  109. export_obj_write_string(o, nora2[i * 3 + 2] * inv + "");
  110. export_obj_write_string(o, " ");
  111. export_obj_write_string(o, -nora2[i * 3 + 1] * inv + "");
  112. export_obj_write_string(o, "\n");
  113. }
  114. for (let i: i32 = 0; i < ti; ++i) {
  115. export_obj_write_string(o, "vt ");
  116. export_obj_write_string(o, texa2[i * 2] * inv + "");
  117. export_obj_write_string(o, " ");
  118. export_obj_write_string(o, 1.0 - texa2[i * 2 + 1] * inv + "");
  119. export_obj_write_string(o, "\n");
  120. }
  121. let inda: u32_array_t = mesh.index_arrays[0].values;
  122. for (let i: i32 = 0; i < math_floor(inda.length / 3); ++i) {
  123. let pi1: i32 = map_get(posmap, inda[i * 3 ]) + 1 + poff;
  124. let pi2: i32 = map_get(posmap, inda[i * 3 + 1]) + 1 + poff;
  125. let pi3: i32 = map_get(posmap, inda[i * 3 + 2]) + 1 + poff;
  126. let ni1: i32 = map_get(normap, inda[i * 3 ]) + 1 + noff;
  127. let ni2: i32 = map_get(normap, inda[i * 3 + 1]) + 1 + noff;
  128. let ni3: i32 = map_get(normap, inda[i * 3 + 2]) + 1 + noff;
  129. let ti1: i32 = map_get(texmap, inda[i * 3 ]) + 1 + toff;
  130. let ti2: i32 = map_get(texmap, inda[i * 3 + 1]) + 1 + toff;
  131. let ti3: i32 = map_get(texmap, inda[i * 3 + 2]) + 1 + toff;
  132. export_obj_write_string(o, "f ");
  133. export_obj_write_string(o, pi1 + "");
  134. export_obj_write_string(o, "/");
  135. export_obj_write_string(o, ti1 + "");
  136. export_obj_write_string(o, "/");
  137. export_obj_write_string(o, ni1 + "");
  138. export_obj_write_string(o, " ");
  139. export_obj_write_string(o, pi2 + "");
  140. export_obj_write_string(o, "/");
  141. export_obj_write_string(o, ti2 + "");
  142. export_obj_write_string(o, "/");
  143. export_obj_write_string(o, ni2 + "");
  144. export_obj_write_string(o, " ");
  145. export_obj_write_string(o, pi3 + "");
  146. export_obj_write_string(o, "/");
  147. export_obj_write_string(o, ti3 + "");
  148. export_obj_write_string(o, "/");
  149. export_obj_write_string(o, ni3 + "");
  150. export_obj_write_string(o, "\n");
  151. }
  152. poff += pi;
  153. noff += ni;
  154. toff += ti;
  155. }
  156. if (!ends_with(path, ".obj")) {
  157. path += ".obj";
  158. }
  159. let b: buffer_t = u8_array_t.from(o).buffer;
  160. krom_file_save_bytes(path, b, buffer_size(b));
  161. }