k3d_format.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>K3D Format Specification</title>
  5. <style type="text/css">
  6. html,
  7. body {
  8. font-family: Geneva, Verdana, Arial, Helvetica, sans-serif;
  9. background-color: #222;
  10. color: #ddd;
  11. }
  12. h1,
  13. h2,
  14. h3,
  15. h4 {
  16. border-bottom: 1px solid #ccc;
  17. }
  18. a,
  19. a:visited,
  20. a:active,
  21. a:focus {
  22. color: #ff6600;
  23. }
  24. #toc {
  25. box-sizing: border-box;
  26. width: 150px;
  27. border: 1px solid #ccc;
  28. padding: 5px;
  29. background-color: #333;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <h1>K3D (Kohi 3D model format)</h1>
  35. <div id="toc">
  36. <h2 id="toctitle">Contents</h2>
  37. <ol>
  38. <li><a href="#Introduction">Introduction</a></li>
  39. <li><a href="#K3D_Format">K3D Format</a></li>
  40. <li><a href="#Header">Header</a></li>
  41. <li><a href="#Submeshes">Submeshes</a></li>
  42. <li><a href="#Bones">Bones</a></li>
  43. <li><a href="#Nodes">Nodes</a></li>
  44. <li><a href="#Animations">Animations</a></li>
  45. <li><a href="#Animation_Channels">Animation Channels</a></li>
  46. <li><a href="#Strings">Strings</a></li>
  47. </ol>
  48. </div>
  49. <a name="Introduction"></a>
  50. <h2>Introduction</h2>
  51. <p>
  52. K3D is the native, binary format used by Kohi to store mesh shape,
  53. animation, and skeletal data. This document serves as a basic
  54. specification for the K3D format.
  55. </p>
  56. <a name="K3D_Format"></a>
  57. <h2>K3D Format</h2>
  58. <a name="Header"></a>
  59. <h3>Header</h3>
  60. <table border="1">
  61. <tr>
  62. <th>Field Name</th>
  63. <th>Bytes</th>
  64. <th>Data Type</th>
  65. <th>Description</th>
  66. </tr>
  67. <tr>
  68. <td>Kohi Magic</td>
  69. <td>4</td>
  70. <td>u32</td>
  71. <td>Marks file as Kohi Asset. Value must be 0xDECAFBAD</td>
  72. </tr>
  73. <tr>
  74. <td>Asset Type</td>
  75. <td>4</td>
  76. <td>u32</td>
  77. <td>Should match the enum value of KASSET_TYPE_MODEL</td>
  78. </tr>
  79. <tr>
  80. <td>Version</td>
  81. <td>1</td>
  82. <td>u8</td>
  83. <td>The K3D asset version.</td>
  84. </tr>
  85. <tr>
  86. <td>Exp. Type</td>
  87. <td>1</td>
  88. <td>u32</td>
  89. <td>
  90. Used to track the type of exporter used. 0x00000001 means imported via
  91. Kohi's importer toolchain. Ignored by the engine, mostly used for
  92. debugging purposes.
  93. </td>
  94. </tr>
  95. <tr>
  96. <td>Exp. Ver.</td>
  97. <td>1</td>
  98. <td>u8</td>
  99. <td>
  100. Used to track version of the exporter used. Ignored by the engine,
  101. mostly used for debugging purposes.
  102. </td>
  103. </tr>
  104. <tr>
  105. <td>Extents</td>
  106. <td>24</td>
  107. <td>extents_3d</td>
  108. <td>The combined extents of all submeshes.</td>
  109. </tr>
  110. <tr>
  111. <td>Center</td>
  112. <td>12</td>
  113. <td>vec3</td>
  114. <td>The center point of all submeshes.</td>
  115. </tr>
  116. <tr>
  117. <td>Inverse Global Transform</td>
  118. <td>64</td>
  119. <td>mat4</td>
  120. <td>The inverse global transform matrix.</td>
  121. </tr>
  122. <tr>
  123. <td>Submesh Count</td>
  124. <td>2</td>
  125. <td>u16</td>
  126. <td>Number of submeshes present.</td>
  127. </tr>
  128. <tr>
  129. <td>Bone count</td>
  130. <td>2</td>
  131. <td>u16</td>
  132. <td>Number of bones present.</td>
  133. </tr>
  134. <tr>
  135. <td>Node count</td>
  136. <td>2</td>
  137. <td>u16</td>
  138. <td>Number of nodes present.</td>
  139. </tr>
  140. <tr>
  141. <td>Animation count</td>
  142. <td>2</td>
  143. <td>u16</td>
  144. <td>Number of animations present.</td>
  145. </tr>
  146. <tr>
  147. <td>String table offset</td>
  148. <td>4</td>
  149. <td>u32</td>
  150. <td>
  151. Offset in bytes in the entire file where the string table begins.
  152. </td>
  153. </tr>
  154. </table>
  155. <a name="Submeshes"></a>
  156. <h3>Submeshes</h3>
  157. <p>NOTE: This section should only exist if Submesh Count > 0.</p>
  158. <table border="1">
  159. <tr>
  160. <th>Field Name</th>
  161. <th>Bytes</th>
  162. <th>Data Type</th>
  163. <th>Description</th>
  164. </tr>
  165. <tr>
  166. <td>SUBMESHES BEGIN GUARD</td>
  167. <td>4</td>
  168. <td>u32</td>
  169. <td>
  170. Serves as a checkpoint to ensure the beginning of the submeshes
  171. section is where it belongs. Value should indicate section type =
  172. 0x00000001.
  173. </td>
  174. </tr>
  175. <tr>
  176. <td>Name Ids</td>
  177. <td>2 * Submesh Count</td>
  178. <td>u16*</td>
  179. <td>
  180. An array of string ids of the names of the submeshes (length = Submesh
  181. Count).
  182. </td>
  183. </tr>
  184. <tr>
  185. <td>Material Name Ids</td>
  186. <td>2 * Submesh Count</td>
  187. <td>u16*</td>
  188. <td>
  189. An array of string ids of the material names of the submeshes (length
  190. = Submesh Count).
  191. </td>
  192. </tr>
  193. <tr>
  194. <td>Vertex Counts</td>
  195. <td>4 * Submesh Count</td>
  196. <td>u32*</td>
  197. <td>
  198. An array of counts of vertices per Submesh (length = Submesh Count).
  199. </td>
  200. </tr>
  201. <tr>
  202. <td>Index Counts</td>
  203. <td>4 * Submesh Count</td>
  204. <td>u32*</td>
  205. <td>
  206. An array of counts of indices per Submesh (length = Submesh Count).
  207. </td>
  208. </tr>
  209. <tr>
  210. <td>Mesh Types</td>
  211. <td>1 * Submesh Count</td>
  212. <td>u8*</td>
  213. <td>
  214. An array of types of vertex data for a submesh. I.e. 0 = vertex_3d
  215. format, 1 = skinned_vertex_3d format, etc.
  216. </td>
  217. </tr>
  218. <tr>
  219. <td>Centers</td>
  220. <td>12 * Submesh Count</td>
  221. <td>vec3*</td>
  222. <td>
  223. An array of center positions per Submesh (length = Submesh Count).
  224. </td>
  225. </tr>
  226. <tr>
  227. <td>Extents</td>
  228. <td>24 * Submesh Count</td>
  229. <td>extents_3d*</td>
  230. <td>An array of extents per Submesh (length = Submesh Count).</td>
  231. </tr>
  232. <tr>
  233. <td>Vertex Data Buffer</td>
  234. <td>4</td>
  235. <td>f32* or i32*</td>
  236. <td>
  237. A buffer of f32s that stores all vertex data for all submeshes,
  238. regardless of format. The overall size of this is determined by the
  239. vertex format of each submesh, how many vertices that submesh has, as
  240. well as how many submeshes there are.
  241. </td>
  242. </tr>
  243. <tr>
  244. <td>Index Data Buffer</td>
  245. <td>4</td>
  246. <td>u32*</td>
  247. <td>
  248. A buffer of u32s that stores all index data for all submeshes. The
  249. overall size of this is determined by the how many indices each
  250. submesh has, as well as how many submeshes there are.
  251. </td>
  252. </tr>
  253. </table>
  254. <a name="Bones"></a>
  255. <h3>Bones</h3>
  256. <p>NOTE: This section should only exist if Bone Count > 0.</p>
  257. <table border="1">
  258. <tr>
  259. <th>Field Name</th>
  260. <th>Bytes</th>
  261. <th>Data Type</th>
  262. <th>Description</th>
  263. </tr>
  264. <tr>
  265. <td>BONES BEGIN GUARD</td>
  266. <td>4</td>
  267. <td>u32</td>
  268. <td>
  269. Serves as a checkpoint to ensure the beginning of the bones section is
  270. where it belongs. Value should indicate section type = 0x00000002.
  271. </td>
  272. </tr>
  273. <tr>
  274. <td>Name Ids</td>
  275. <td>2 * Bone Count</td>
  276. <td>u16*</td>
  277. <td>
  278. An array of string ids of the names of the bones (length = Bone
  279. Count).
  280. </td>
  281. </tr>
  282. <tr>
  283. <td>Offset Matrices</td>
  284. <td>64 * Bone Count</td>
  285. <td>mat4*</td>
  286. <td>An array of offset matrices of the bones (length = Bone Count).</td>
  287. </tr>
  288. </table>
  289. <a name="Nodes"></a>
  290. <h3>Nodes</h3>
  291. <p>NOTE: This section should only exist if Node Count > 0.</p>
  292. <table border="1">
  293. <tr>
  294. <th>Field Name</th>
  295. <th>Bytes</th>
  296. <th>Data Type</th>
  297. <th>Description</th>
  298. </tr>
  299. <tr>
  300. <td>NODES BEGIN GUARD</td>
  301. <td>4</td>
  302. <td>u32</td>
  303. <td>
  304. Serves as a checkpoint to ensure the beginning of the nodes section is
  305. where it belongs. Value should indicate section type = 0x00000003.
  306. </td>
  307. </tr>
  308. <tr>
  309. <td>Name Ids</td>
  310. <td>2 * Node Count</td>
  311. <td>u16*</td>
  312. <td>
  313. An array of string ids of the names of the nodes (length = Node
  314. Count).
  315. </td>
  316. </tr>
  317. <tr>
  318. <td>Parent Indices</td>
  319. <td>2 * Node Count</td>
  320. <td>u16*</td>
  321. <td>
  322. An array of indices of parents of the nodes (length = Node Count).
  323. INVALID_ID_U16 = root node (no parent)
  324. </td>
  325. </tr>
  326. <tr>
  327. <td>Local transforms</td>
  328. <td>64 * Node Count</td>
  329. <td>mat4*</td>
  330. <td>
  331. An array of local transform matrices of the nodes (length = Node
  332. Count).
  333. </td>
  334. </tr>
  335. </table>
  336. <a name="Animations"></a>
  337. <h3>Animations</h3>
  338. <p>NOTE: This section should only exist if Animation Count > 0.</p>
  339. <table border="1">
  340. <tr>
  341. <th>Field Name</th>
  342. <th>Bytes</th>
  343. <th>Data Type</th>
  344. <th>Description</th>
  345. </tr>
  346. <tr>
  347. <td>ANIMATIONS BEGIN GUARD</td>
  348. <td>4</td>
  349. <td>u32</td>
  350. <td>
  351. Serves as a checkpoint to ensure the beginning of the animations
  352. section is where it belongs. Value should indicate section type =
  353. 0x00000004.
  354. </td>
  355. </tr>
  356. <tr>
  357. <td>Total Channel Count</td>
  358. <td>2</td>
  359. <td>u16</td>
  360. <td>The total number of channels for all animations</td>
  361. </tr>
  362. <tr>
  363. <td>Name Ids</td>
  364. <td>2 * Animation Count</td>
  365. <td>u16*</td>
  366. <td>
  367. An array of string ids of the names of the animations (length =
  368. Animation Count).
  369. </td>
  370. </tr>
  371. <tr>
  372. <td>Durations</td>
  373. <td>4 * Animation Count</td>
  374. <td>f32*</td>
  375. <td>
  376. An array of durations of each animation (length = Animation Count).
  377. </td>
  378. </tr>
  379. <tr>
  380. <td>Ticks Per Seconds</td>
  381. <td>4 * Animation Count</td>
  382. <td>f32*</td>
  383. <td>
  384. An array of ticks-per-second of each animation (length = Animation
  385. Count).
  386. </td>
  387. </tr>
  388. <tr>
  389. <td>Channel Counts</td>
  390. <td>2 * Animation Count</td>
  391. <td>u16*</td>
  392. <td>
  393. An array of channel counts of each animation (length = Animation
  394. Count).
  395. </td>
  396. </tr>
  397. </table>
  398. <a name="Animation_Channels"></a>
  399. <h3>Animation Channels</h3>
  400. <p>
  401. NOTE: This section should only exist if Animation Count > 0 AND Total
  402. Channel Count > 0.
  403. </p>
  404. <table border="1">
  405. <tr>
  406. <th>Field Name</th>
  407. <th>Bytes</th>
  408. <th>Data Type</th>
  409. <th>Description</th>
  410. </tr>
  411. <tr>
  412. <td>ANIMATION CHANNELS BEGIN GUARD</td>
  413. <td>4</td>
  414. <td>u32</td>
  415. <td>
  416. Serves as a checkpoint to ensure the beginning of the animation
  417. channels section is where it belongs. Value should indicate section
  418. type = 0x00000005.
  419. </td>
  420. </tr>
  421. <tr>
  422. <td>Channel Anim. Ids</td>
  423. <td>2 * Total Channel Count</td>
  424. <td>u16*</td>
  425. <td>
  426. An array of animation ids for each channel (length = Total Channel
  427. Count). Used to link a channel back to an animation.
  428. </td>
  429. </tr>
  430. <tr>
  431. <td>Channel Name Ids</td>
  432. <td>2 * Total Channel Count</td>
  433. <td>u16*</td>
  434. <td>
  435. An array of string ids of the names of the animation channels (length
  436. = Total Channel Count).
  437. </td>
  438. </tr>
  439. <tr>
  440. <td>Channel Position Counts</td>
  441. <td>4 * Total Channel Count</td>
  442. <td>u32*</td>
  443. <td>
  444. An array of position counts for each channel (length = Total Channel
  445. Count).
  446. </td>
  447. </tr>
  448. <tr>
  449. <td>Channel Position Offsets</td>
  450. <td>4 * Total Channel Count</td>
  451. <td>u32*</td>
  452. <td>
  453. An offset into the positions array for each channel (length = Total
  454. Channel Count).
  455. </td>
  456. </tr>
  457. <tr>
  458. <td>Channel Rotation Counts</td>
  459. <td>4 * Total Channel Count</td>
  460. <td>u32*</td>
  461. <td>
  462. An array of rotation counts for each channel (length = Total Channel
  463. Count).
  464. </td>
  465. </tr>
  466. <tr>
  467. <td>Channel Rotation Offsets</td>
  468. <td>4 * Total Channel Count</td>
  469. <td>u32*</td>
  470. <td>
  471. An offset into the rotations array for each channel (length = Total
  472. Channel Count).
  473. </td>
  474. </tr>
  475. <tr>
  476. <td>Channel Scale Counts</td>
  477. <td>4 * Total Channel Count</td>
  478. <td>u32*</td>
  479. <td>
  480. An array of scale counts for each channel (length = Total Channel
  481. Count).
  482. </td>
  483. </tr>
  484. <tr>
  485. <td>Channel Scale Offsets</td>
  486. <td>4 * Total Channel Count</td>
  487. <td>u32*</td>
  488. <td>
  489. An offset into the scales array for each channel (length = Total
  490. Channel Count).
  491. </td>
  492. </tr>
  493. <tr>
  494. <td>Channel Data Buffer</td>
  495. <td>4</td>
  496. <td>f32*</td>
  497. <td>
  498. A buffer of f32s that stores all position/rotation/scale data for all
  499. animation channels. The overall size of this is determined by the how
  500. many keys each channel has, as well as how many channels there are.
  501. Bytes extracted from this vary by the type of key data being extracted
  502. (i.e a key_vec3 extracts 4 f32s whereas a key_quat extracts 5. Each
  503. channel is stored in the order of position, rotation, then scale.
  504. </td>
  505. </tr>
  506. </table>
  507. <a name="Strings"></a>
  508. <h3>Strings</h3>
  509. <table border="1">
  510. <tr>
  511. <th>Field Name</th>
  512. <th>Bytes</th>
  513. <th>Data Type</th>
  514. <th>Description</th>
  515. </tr>
  516. <tr>
  517. <td>STRINGS BEGIN GUARD</td>
  518. <td>4</td>
  519. <td>u32</td>
  520. <td>
  521. Serves as a checkpoint to ensure the beginning of the strings section
  522. is where it belongs. Value should indicate section type = 0x00000006.
  523. </td>
  524. </tr>
  525. </table>
  526. <p>
  527. NOTE: Strings are handled by a binary_string_table that is serialized into
  528. one large block of memory and is appended to the end of the file.
  529. </p>
  530. </body>
  531. </html>