ObjParser.hx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package arm.format;
  2. class ObjParser {
  3. public static var splitCode = "o".code; // Object split, "g" for groups, "u"semtl for materials
  4. public var posa: kha.arrays.Int16Array = null;
  5. public var nora: kha.arrays.Int16Array = null;
  6. public var texa: kha.arrays.Int16Array = null;
  7. public var inda: kha.arrays.Uint32Array = null;
  8. public var udims: Array<kha.arrays.Uint32Array> = null; // Indices split per udim tile
  9. public var udimsU = 1; // Number of horizontal udim tiles
  10. public var scalePos = 1.0;
  11. public var scaleTex = 1.0;
  12. public var name = "";
  13. public var hasNext = false; // File contains multiple objects
  14. public var pos = 0;
  15. var posTemp: Array<Float>;
  16. var uvTemp: Array<Float>;
  17. var norTemp: Array<Float>;
  18. var va: kha.arrays.Uint32Array;
  19. var ua: kha.arrays.Uint32Array;
  20. var na: kha.arrays.Uint32Array;
  21. var vi = 0;
  22. var ui = 0;
  23. var ni = 0;
  24. var buf: haxe.io.UInt8Array = null;
  25. static var vindOff = 0;
  26. static var tindOff = 0;
  27. static var nindOff = 0;
  28. static var bytes: haxe.io.Bytes = null;
  29. static var posFirst: Array<Float>;
  30. static var uvFirst: Array<Float>;
  31. static var norFirst: Array<Float>;
  32. public function new(blob: kha.Blob, startPos = 0, udim = false) {
  33. pos = startPos;
  34. var posIndices: Array<Int> = [];
  35. var uvIndices: Array<Int> = [];
  36. var norIndices: Array<Int> = [];
  37. var readingFaces = false;
  38. var readingObject = false;
  39. var fullAttrib = false;
  40. bytes = blob.bytes;
  41. posTemp = [];
  42. uvTemp = [];
  43. norTemp = [];
  44. va = new kha.arrays.Uint32Array(60);
  45. ua = new kha.arrays.Uint32Array(60);
  46. na = new kha.arrays.Uint32Array(60);
  47. buf = new haxe.io.UInt8Array(64);
  48. if (splitCode == "u".code && startPos > 0) {
  49. posTemp = posFirst;
  50. norTemp = norFirst;
  51. uvTemp = uvFirst;
  52. }
  53. while (true) {
  54. if (pos >= bytes.length) break;
  55. var c0 = bytes.get(pos++);
  56. if (readingObject && readingFaces && (c0 == "v".code || c0 == splitCode)) {
  57. pos--;
  58. hasNext = true;
  59. break;
  60. }
  61. if (c0 == "v".code) {
  62. var c1 = bytes.get(pos++);
  63. if (c1 == " ".code) {
  64. // Some exporters put additional space directly after "v"
  65. if (bytes.get(pos) == " ".code) pos++;
  66. posTemp.push(readFloat());
  67. pos++; // Space
  68. posTemp.push(readFloat());
  69. pos++; // Space
  70. posTemp.push(readFloat());
  71. }
  72. else if (c1 == "t".code) {
  73. pos++; // Space
  74. uvTemp.push(readFloat());
  75. pos++; // Space
  76. uvTemp.push(readFloat());
  77. if (norTemp.length > 0) fullAttrib = true;
  78. }
  79. else if (c1 == "n".code) {
  80. pos++; // Space
  81. norTemp.push(readFloat());
  82. pos++; // Space
  83. norTemp.push(readFloat());
  84. pos++; // Space
  85. norTemp.push(readFloat());
  86. if (uvTemp.length > 0) fullAttrib = true;
  87. }
  88. }
  89. else if (c0 == "f".code) {
  90. pos++; // Space
  91. readingFaces = true;
  92. vi = 0;
  93. ui = 0;
  94. ni = 0;
  95. fullAttrib ? readFaceFast() : readFace();
  96. posIndices.push(va[0]);
  97. posIndices.push(va[1]);
  98. posIndices.push(va[2]);
  99. for (i in 3...vi) {
  100. posIndices.push(va[0]);
  101. posIndices.push(va[i - 1]);
  102. posIndices.push(va[i]);
  103. }
  104. if (uvTemp.length > 0) {
  105. uvIndices.push(ua[0]);
  106. uvIndices.push(ua[1]);
  107. uvIndices.push(ua[2]);
  108. for (i in 3...ui) {
  109. uvIndices.push(ua[0]);
  110. uvIndices.push(ua[i - 1]);
  111. uvIndices.push(ua[i]);
  112. }
  113. }
  114. if (norTemp.length > 0) {
  115. norIndices.push(na[0]);
  116. norIndices.push(na[1]);
  117. norIndices.push(na[2]);
  118. for (i in 3...ni) {
  119. norIndices.push(na[0]);
  120. norIndices.push(na[i - 1]);
  121. norIndices.push(na[i]);
  122. }
  123. }
  124. }
  125. else if (c0 == splitCode) {
  126. if (splitCode == "u".code) pos += 5; // "u"semtl
  127. pos++; // Space
  128. if (!udim) readingObject = true;
  129. name = readString();
  130. }
  131. nextLine();
  132. }
  133. if (startPos > 0) {
  134. if (splitCode != "u".code) {
  135. for (i in 0...posIndices.length) posIndices[i] -= vindOff;
  136. for (i in 0...uvIndices.length) uvIndices[i] -= tindOff;
  137. for (i in 0...norIndices.length) norIndices[i] -= nindOff;
  138. }
  139. }
  140. else {
  141. vindOff = tindOff = nindOff = 0;
  142. if (splitCode == "u".code) {
  143. posFirst = posTemp;
  144. norFirst = norTemp;
  145. uvFirst = uvTemp;
  146. }
  147. }
  148. vindOff += Std.int(posTemp.length / 3); // Assumes separate vertex data per object
  149. tindOff += Std.int(uvTemp.length / 2);
  150. nindOff += Std.int(norTemp.length / 3);
  151. // Pack positions to (-1, 1) range
  152. scalePos = 0.0;
  153. for (i in 0...posTemp.length) {
  154. var f = Math.abs(posTemp[i]);
  155. if (scalePos < f) scalePos = f;
  156. }
  157. var inv = 32767 * (1 / scalePos);
  158. posa = new kha.arrays.Int16Array(posIndices.length * 4);
  159. inda = new kha.arrays.Uint32Array(posIndices.length);
  160. for (i in 0...posIndices.length) {
  161. posa[i * 4 ] = Std.int( posTemp[posIndices[i] * 3 ] * inv);
  162. posa[i * 4 + 1] = Std.int(-posTemp[posIndices[i] * 3 + 2] * inv);
  163. posa[i * 4 + 2] = Std.int( posTemp[posIndices[i] * 3 + 1] * inv);
  164. inda[i] = i;
  165. }
  166. if (norIndices.length > 0) {
  167. nora = new kha.arrays.Int16Array(norIndices.length * 2);
  168. for (i in 0...posIndices.length) {
  169. nora[i * 2 ] = Std.int( norTemp[norIndices[i] * 3 ] * 32767);
  170. nora[i * 2 + 1] = Std.int(-norTemp[norIndices[i] * 3 + 2] * 32767);
  171. posa[i * 4 + 3] = Std.int( norTemp[norIndices[i] * 3 + 1] * 32767);
  172. }
  173. }
  174. else {
  175. // Calc normals
  176. nora = new kha.arrays.Int16Array(inda.length * 2);
  177. var va = new iron.math.Vec4();
  178. var vb = new iron.math.Vec4();
  179. var vc = new iron.math.Vec4();
  180. var cb = new iron.math.Vec4();
  181. var ab = new iron.math.Vec4();
  182. for (i in 0...Std.int(inda.length / 3)) {
  183. var i1 = inda[i * 3 ];
  184. var i2 = inda[i * 3 + 1];
  185. var i3 = inda[i * 3 + 2];
  186. va.set(posa[i1 * 4], posa[i1 * 4 + 1], posa[i1 * 4 + 2]);
  187. vb.set(posa[i2 * 4], posa[i2 * 4 + 1], posa[i2 * 4 + 2]);
  188. vc.set(posa[i3 * 4], posa[i3 * 4 + 1], posa[i3 * 4 + 2]);
  189. cb.subvecs(vc, vb);
  190. ab.subvecs(va, vb);
  191. cb.cross(ab);
  192. cb.normalize();
  193. nora[i1 * 2 ] = Std.int(cb.x * 32767);
  194. nora[i1 * 2 + 1] = Std.int(cb.y * 32767);
  195. posa[i1 * 4 + 3] = Std.int(cb.z * 32767);
  196. nora[i2 * 2 ] = Std.int(cb.x * 32767);
  197. nora[i2 * 2 + 1] = Std.int(cb.y * 32767);
  198. posa[i2 * 4 + 3] = Std.int(cb.z * 32767);
  199. nora[i3 * 2 ] = Std.int(cb.x * 32767);
  200. nora[i3 * 2 + 1] = Std.int(cb.y * 32767);
  201. posa[i3 * 4 + 3] = Std.int(cb.z * 32767);
  202. }
  203. }
  204. if (uvIndices.length > 0) {
  205. if (udim) {
  206. // Find number of tiles
  207. var tilesU = 1;
  208. var tilesV = 1;
  209. for (i in 0...Std.int(uvTemp.length / 2)) {
  210. while (uvTemp[i * 2 ] > tilesU) tilesU++;
  211. while (uvTemp[i * 2 + 1] > tilesV) tilesV++;
  212. }
  213. function getTile(i1: Int, i2: Int, i3: Int): Int {
  214. var u1 = uvTemp[uvIndices[i1] * 2 ];
  215. var v1 = uvTemp[uvIndices[i1] * 2 + 1];
  216. var u2 = uvTemp[uvIndices[i2] * 2 ];
  217. var v2 = uvTemp[uvIndices[i2] * 2 + 1];
  218. var u3 = uvTemp[uvIndices[i3] * 2 ];
  219. var v3 = uvTemp[uvIndices[i3] * 2 + 1];
  220. var tileU = Std.int((u1 + u2 + u3) / 3);
  221. var tileV = Std.int((v1 + v2 + v3) / 3);
  222. return tileU + tileV * tilesU;
  223. }
  224. // Amount of indices pre tile
  225. var num = new kha.arrays.Uint32Array(tilesU * tilesV);
  226. for (i in 0...Std.int(inda.length / 3)) {
  227. var tile = getTile(inda[i * 3], inda[i * 3 + 1], inda[i * 3 + 2]);
  228. num[tile] += 3;
  229. }
  230. // Split indices per tile
  231. udims = [];
  232. udimsU = tilesU;
  233. for (i in 0...tilesU * tilesV) { udims.push(new kha.arrays.Uint32Array(num[i])); num[i] = 0; }
  234. for (i in 0...Std.int(inda.length / 3)) {
  235. var i1 = inda[i * 3 ];
  236. var i2 = inda[i * 3 + 1];
  237. var i3 = inda[i * 3 + 2];
  238. var tile = getTile(i1, i2, i3);
  239. udims[tile][num[tile]++] = i1;
  240. udims[tile][num[tile]++] = i2;
  241. udims[tile][num[tile]++] = i3;
  242. }
  243. // Normalize uvs to 0-1 range
  244. var uvtiles = new kha.arrays.Int16Array(uvTemp.length);
  245. for (i in 0...Std.int(inda.length / 3)) { // TODO: merge loops
  246. var i1 = inda[i * 3 ];
  247. var i2 = inda[i * 3 + 1];
  248. var i3 = inda[i * 3 + 2];
  249. var tile = getTile(i1, i2, i3);
  250. var tileU = tile % tilesU;
  251. var tileV = Std.int(tile / tilesU);
  252. uvtiles[uvIndices[i1] * 2 ] = tileU;
  253. uvtiles[uvIndices[i1] * 2 + 1] = tileV;
  254. uvtiles[uvIndices[i2] * 2 ] = tileU;
  255. uvtiles[uvIndices[i2] * 2 + 1] = tileV;
  256. uvtiles[uvIndices[i3] * 2 ] = tileU;
  257. uvtiles[uvIndices[i3] * 2 + 1] = tileV;
  258. }
  259. for (i in 0...uvtiles.length) uvTemp[i] -= uvtiles[i];
  260. }
  261. texa = new kha.arrays.Int16Array(uvIndices.length * 2);
  262. for (i in 0...posIndices.length) {
  263. texa[i * 2 ] = Std.int( uvTemp[uvIndices[i] * 2 ] * 32767);
  264. texa[i * 2 + 1] = Std.int((1.0 - uvTemp[uvIndices[i] * 2 + 1]) * 32767);
  265. }
  266. }
  267. bytes = null;
  268. if (!hasNext) { posFirst = norFirst = uvFirst = null; }
  269. }
  270. function readFaceFast() {
  271. while (true) {
  272. va[vi++] = readInt() - 1;
  273. pos++; // "/"
  274. ua[ui++] = readInt() - 1;
  275. pos++; // "/"
  276. na[ni++] = readInt() - 1;
  277. if (bytes.get(pos) == "\n".code || bytes.get(pos) == "\r".code) break;
  278. pos++; // " "
  279. // Some exporters put space at the end of "f" line
  280. if (vi >= 3 && (bytes.get(pos) == "\n".code || bytes.get(pos) == "\r".code)) break;
  281. }
  282. }
  283. function readFace() {
  284. while (true) {
  285. va[vi++] = readInt() - 1;
  286. if (uvTemp.length > 0 || norTemp.length > 0) {
  287. pos++; // "/"
  288. }
  289. if (uvTemp.length > 0) {
  290. ua[ui++] = readInt() - 1;
  291. }
  292. if (norTemp.length > 0) {
  293. pos++; // "/"
  294. na[ni++] = readInt() - 1;
  295. }
  296. if (bytes.get(pos) == "\n".code || bytes.get(pos) == "\r".code) break;
  297. pos++; // " "
  298. // Some exporters put space at the end of "f" line
  299. if (vi >= 3 && (bytes.get(pos) == "\n".code || bytes.get(pos) == "\r".code)) break;
  300. }
  301. }
  302. function readFloat(): Float {
  303. var bi = 0;
  304. while (true) { // Read into buffer
  305. var c = bytes.get(pos);
  306. if (c == " ".code || c == "\n".code || c == "\r".code) break;
  307. if (c == "E".code || c == "e".code) {
  308. while (true) {
  309. pos++;
  310. c = bytes.get(pos);
  311. if (c == " ".code || c == "\n".code || c == "\r".code) break;
  312. }
  313. return 0.0; // Assume number close to zero for now
  314. }
  315. pos++;
  316. buf[bi++] = c;
  317. }
  318. var res = 0.0; // Parse buffer into float
  319. var dot = 1;
  320. var dec = 1;
  321. var off = buf[0] == "-".code ? 1 : 0;
  322. var len = bi - 1;
  323. for (i in 0...bi - off) {
  324. var c = buf[len - i];
  325. if (c == ".".code) { dot = dec; continue; }
  326. res += (c - 48) * dec;
  327. dec *= 10;
  328. }
  329. off > 0 ? res /= -dot : res /= dot;
  330. return res;
  331. }
  332. function readInt(): Int {
  333. var bi = 0;
  334. while (true) { // Read into buffer
  335. var c = bytes.get(pos);
  336. if (c == "/".code || c == "\n".code || c == "\r".code || c == " ".code) break;
  337. pos++;
  338. buf[bi++] = c;
  339. }
  340. var res = 0; // Parse buffer into int
  341. var dec = 1;
  342. var off = buf[0] == "-".code ? 1 : 0;
  343. var len = bi - 1;
  344. for (i in 0...bi - off) {
  345. res += (buf[len - i] - 48) * dec;
  346. dec *= 10;
  347. }
  348. if (off > 0) res *= -1;
  349. return res;
  350. }
  351. function readString(): String {
  352. var s = "";
  353. while (true) {
  354. var c = bytes.get(pos);
  355. if (c == "\n".code || c == "\r".code || c == " ".code) break;
  356. pos++;
  357. s += String.fromCharCode(c);
  358. }
  359. return s;
  360. }
  361. function nextLine() {
  362. while (true) {
  363. var c = bytes.get(pos++);
  364. if (c == "\n".code || pos >= bytes.length) break; // \n, \r\n
  365. }
  366. }
  367. }