cgltf_write.h 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283
  1. /**
  2. * cgltf_write - a single-file glTF 2.0 writer written in C99.
  3. *
  4. * Version: 1.9
  5. *
  6. * Website: https://github.com/jkuhlmann/cgltf
  7. *
  8. * Distributed under the MIT License, see notice at the end of this file.
  9. *
  10. * Building:
  11. * Include this file where you need the struct and function
  12. * declarations. Have exactly one source file where you define
  13. * `CGLTF_WRITE_IMPLEMENTATION` before including this file to get the
  14. * function definitions.
  15. *
  16. * Reference:
  17. * `cgltf_result cgltf_write_file(const cgltf_options* options, const char*
  18. * path, const cgltf_data* data)` writes JSON to the given file path. Buffer
  19. * files and external images are not written out. `data` is not deallocated.
  20. *
  21. * `cgltf_size cgltf_write(const cgltf_options* options, char* buffer,
  22. * cgltf_size size, const cgltf_data* data)` writes JSON into the given memory
  23. * buffer. Returns the number of bytes written to `buffer`, including a null
  24. * terminator. If buffer is null, returns the number of bytes that would have
  25. * been written. `data` is not deallocated.
  26. *
  27. * To write custom JSON into the `extras` field, aggregate all the custom JSON
  28. * into a single buffer, then set `file_data` to this buffer. By supplying
  29. * start_offset and end_offset values for various objects, you can select a
  30. * range of characters within the aggregated buffer.
  31. */
  32. #ifndef CGLTF_WRITE_H_INCLUDED__
  33. #define CGLTF_WRITE_H_INCLUDED__
  34. #include "cgltf.h"
  35. #include <stddef.h>
  36. #include <stdbool.h>
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, const cgltf_data* data);
  41. cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size size, const cgltf_data* data);
  42. #ifdef __cplusplus
  43. }
  44. #endif
  45. #endif /* #ifndef CGLTF_WRITE_H_INCLUDED__ */
  46. /*
  47. *
  48. * Stop now, if you are only interested in the API.
  49. * Below, you find the implementation.
  50. *
  51. */
  52. #if defined(__INTELLISENSE__) || defined(__JETBRAINS_IDE__)
  53. /* This makes MSVC/CLion intellisense work. */
  54. #define CGLTF_IMPLEMENTATION
  55. #endif
  56. #ifdef CGLTF_WRITE_IMPLEMENTATION
  57. #include <stdio.h>
  58. #include <stdint.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <float.h>
  62. #define CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM (1 << 0)
  63. #define CGLTF_EXTENSION_FLAG_MATERIALS_UNLIT (1 << 1)
  64. #define CGLTF_EXTENSION_FLAG_SPECULAR_GLOSSINESS (1 << 2)
  65. #define CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL (1 << 3)
  66. #define CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION (1 << 4)
  67. #define CGLTF_EXTENSION_FLAG_MATERIALS_CLEARCOAT (1 << 5)
  68. #define CGLTF_EXTENSION_FLAG_MATERIALS_IOR (1 << 6)
  69. #define CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR (1 << 7)
  70. #define CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION (1 << 8)
  71. #define CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN (1 << 9)
  72. #define CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS (1 << 10)
  73. #define CGLTF_EXTENSION_FLAG_MATERIALS_VOLUME (1 << 11)
  74. typedef struct {
  75. char* buffer;
  76. cgltf_size buffer_size;
  77. cgltf_size remaining;
  78. char* cursor;
  79. cgltf_size tmp;
  80. cgltf_size chars_written;
  81. const cgltf_data* data;
  82. int depth;
  83. const char* indent;
  84. int needs_comma;
  85. uint32_t extension_flags;
  86. uint32_t required_extension_flags;
  87. } cgltf_write_context;
  88. #define CGLTF_MIN(a, b) (a < b ? a : b)
  89. #ifdef FLT_DECIMAL_DIG
  90. // FLT_DECIMAL_DIG is C11
  91. #define CGLTF_DECIMAL_DIG (FLT_DECIMAL_DIG)
  92. #else
  93. #define CGLTF_DECIMAL_DIG 9
  94. #endif
  95. #define CGLTF_SPRINTF(...) { \
  96. context->tmp = snprintf ( context->cursor, context->remaining, __VA_ARGS__ ); \
  97. context->chars_written += context->tmp; \
  98. if (context->cursor) { \
  99. context->cursor += context->tmp; \
  100. context->remaining -= context->tmp; \
  101. } }
  102. #define CGLTF_SNPRINTF(length, ...) { \
  103. context->tmp = snprintf ( context->cursor, CGLTF_MIN(length + 1, context->remaining), __VA_ARGS__ ); \
  104. context->chars_written += length; \
  105. if (context->cursor) { \
  106. context->cursor += length; \
  107. context->remaining -= length; \
  108. } }
  109. #define CGLTF_WRITE_IDXPROP(label, val, start) if (val) { \
  110. cgltf_write_indent(context); \
  111. CGLTF_SPRINTF("\"%s\": %d", label, (int) (val - start)); \
  112. context->needs_comma = 1; }
  113. #define CGLTF_WRITE_IDXARRPROP(label, dim, vals, start) if (vals) { \
  114. cgltf_write_indent(context); \
  115. CGLTF_SPRINTF("\"%s\": [", label); \
  116. for (int i = 0; i < (int)(dim); ++i) { \
  117. int idx = (int) (vals[i] - start); \
  118. if (i != 0) CGLTF_SPRINTF(","); \
  119. CGLTF_SPRINTF(" %d", idx); \
  120. } \
  121. CGLTF_SPRINTF(" ]"); \
  122. context->needs_comma = 1; }
  123. #define CGLTF_WRITE_TEXTURE_INFO(label, info) if (info.texture) { \
  124. cgltf_write_line(context, "\"" label "\": {"); \
  125. CGLTF_WRITE_IDXPROP("index", info.texture, context->data->textures); \
  126. cgltf_write_intprop(context, "texCoord", info.texcoord, 0); \
  127. cgltf_write_floatprop(context, "scale", info.scale, 1.0f); \
  128. if (info.has_transform) { \
  129. context->extension_flags |= CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM; \
  130. cgltf_write_texture_transform(context, &info.transform); \
  131. } \
  132. cgltf_write_extras(context, &info.extras); \
  133. cgltf_write_line(context, "}"); }
  134. static void cgltf_write_indent(cgltf_write_context* context)
  135. {
  136. if (context->needs_comma)
  137. {
  138. CGLTF_SPRINTF(",\n");
  139. context->needs_comma = 0;
  140. }
  141. else
  142. {
  143. CGLTF_SPRINTF("\n");
  144. }
  145. for (int i = 0; i < context->depth; ++i)
  146. {
  147. CGLTF_SPRINTF("%s", context->indent);
  148. }
  149. }
  150. static void cgltf_write_line(cgltf_write_context* context, const char* line)
  151. {
  152. if (line[0] == ']' || line[0] == '}')
  153. {
  154. --context->depth;
  155. context->needs_comma = 0;
  156. }
  157. cgltf_write_indent(context);
  158. CGLTF_SPRINTF("%s", line);
  159. cgltf_size last = (cgltf_size)(strlen(line) - 1);
  160. if (line[0] == ']' || line[0] == '}')
  161. {
  162. context->needs_comma = 1;
  163. }
  164. if (line[last] == '[' || line[last] == '{')
  165. {
  166. ++context->depth;
  167. context->needs_comma = 0;
  168. }
  169. }
  170. static void cgltf_write_strprop(cgltf_write_context* context, const char* label, const char* val)
  171. {
  172. if (val)
  173. {
  174. cgltf_write_indent(context);
  175. CGLTF_SPRINTF("\"%s\": \"%s\"", label, val);
  176. context->needs_comma = 1;
  177. }
  178. }
  179. static void cgltf_write_extras(cgltf_write_context* context, const cgltf_extras* extras)
  180. {
  181. cgltf_size length = extras->end_offset - extras->start_offset;
  182. if (length > 0 && context->data->file_data)
  183. {
  184. char* json_string = ((char*) context->data->file_data) + extras->start_offset;
  185. cgltf_write_indent(context);
  186. CGLTF_SPRINTF("%s", "\"extras\": ");
  187. CGLTF_SNPRINTF(length, "%.*s", (int)(extras->end_offset - extras->start_offset), json_string);
  188. context->needs_comma = 1;
  189. }
  190. }
  191. static void cgltf_write_stritem(cgltf_write_context* context, const char* item)
  192. {
  193. cgltf_write_indent(context);
  194. CGLTF_SPRINTF("\"%s\"", item);
  195. context->needs_comma = 1;
  196. }
  197. static void cgltf_write_intprop(cgltf_write_context* context, const char* label, int val, int def)
  198. {
  199. if (val != def)
  200. {
  201. cgltf_write_indent(context);
  202. CGLTF_SPRINTF("\"%s\": %d", label, val);
  203. context->needs_comma = 1;
  204. }
  205. }
  206. static void cgltf_write_floatprop(cgltf_write_context* context, const char* label, float val, float def)
  207. {
  208. if (val != def)
  209. {
  210. cgltf_write_indent(context);
  211. CGLTF_SPRINTF("\"%s\": ", label);
  212. CGLTF_SPRINTF("%.*g", CGLTF_DECIMAL_DIG, val);
  213. context->needs_comma = 1;
  214. if (context->cursor)
  215. {
  216. char *decimal_comma = strchr(context->cursor - context->tmp, ',');
  217. if (decimal_comma)
  218. {
  219. *decimal_comma = '.';
  220. }
  221. }
  222. }
  223. }
  224. static void cgltf_write_boolprop_optional(cgltf_write_context* context, const char* label, bool val, bool def)
  225. {
  226. if (val != def)
  227. {
  228. cgltf_write_indent(context);
  229. CGLTF_SPRINTF("\"%s\": %s", label, val ? "true" : "false");
  230. context->needs_comma = 1;
  231. }
  232. }
  233. static void cgltf_write_floatarrayprop(cgltf_write_context* context, const char* label, const cgltf_float* vals, cgltf_size dim)
  234. {
  235. cgltf_write_indent(context);
  236. CGLTF_SPRINTF("\"%s\": [", label);
  237. for (cgltf_size i = 0; i < dim; ++i)
  238. {
  239. if (i != 0)
  240. {
  241. CGLTF_SPRINTF(", %.*g", CGLTF_DECIMAL_DIG, vals[i]);
  242. }
  243. else
  244. {
  245. CGLTF_SPRINTF("%.*g", CGLTF_DECIMAL_DIG, vals[i]);
  246. }
  247. }
  248. CGLTF_SPRINTF("]");
  249. context->needs_comma = 1;
  250. }
  251. static bool cgltf_check_floatarray(const float* vals, int dim, float val) {
  252. while (dim--)
  253. {
  254. if (vals[dim] != val)
  255. {
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261. static int cgltf_int_from_component_type(cgltf_component_type ctype)
  262. {
  263. switch (ctype)
  264. {
  265. case cgltf_component_type_r_8: return 5120;
  266. case cgltf_component_type_r_8u: return 5121;
  267. case cgltf_component_type_r_16: return 5122;
  268. case cgltf_component_type_r_16u: return 5123;
  269. case cgltf_component_type_r_32u: return 5125;
  270. case cgltf_component_type_r_32f: return 5126;
  271. default: return 0;
  272. }
  273. }
  274. static const char* cgltf_str_from_alpha_mode(cgltf_alpha_mode alpha_mode)
  275. {
  276. switch (alpha_mode)
  277. {
  278. case cgltf_alpha_mode_mask: return "MASK";
  279. case cgltf_alpha_mode_blend: return "BLEND";
  280. default: return NULL;
  281. }
  282. }
  283. static const char* cgltf_str_from_type(cgltf_type type)
  284. {
  285. switch (type)
  286. {
  287. case cgltf_type_scalar: return "SCALAR";
  288. case cgltf_type_vec2: return "VEC2";
  289. case cgltf_type_vec3: return "VEC3";
  290. case cgltf_type_vec4: return "VEC4";
  291. case cgltf_type_mat2: return "MAT2";
  292. case cgltf_type_mat3: return "MAT3";
  293. case cgltf_type_mat4: return "MAT4";
  294. default: return NULL;
  295. }
  296. }
  297. static cgltf_size cgltf_dim_from_type(cgltf_type type)
  298. {
  299. switch (type)
  300. {
  301. case cgltf_type_scalar: return 1;
  302. case cgltf_type_vec2: return 2;
  303. case cgltf_type_vec3: return 3;
  304. case cgltf_type_vec4: return 4;
  305. case cgltf_type_mat2: return 4;
  306. case cgltf_type_mat3: return 9;
  307. case cgltf_type_mat4: return 16;
  308. default: return 0;
  309. }
  310. }
  311. static const char* cgltf_str_from_camera_type(cgltf_camera_type camera_type)
  312. {
  313. switch (camera_type)
  314. {
  315. case cgltf_camera_type_perspective: return "perspective";
  316. case cgltf_camera_type_orthographic: return "orthographic";
  317. default: return NULL;
  318. }
  319. }
  320. static const char* cgltf_str_from_light_type(cgltf_light_type light_type)
  321. {
  322. switch (light_type)
  323. {
  324. case cgltf_light_type_directional: return "directional";
  325. case cgltf_light_type_point: return "point";
  326. case cgltf_light_type_spot: return "spot";
  327. default: return NULL;
  328. }
  329. }
  330. static void cgltf_write_texture_transform(cgltf_write_context* context, const cgltf_texture_transform* transform)
  331. {
  332. cgltf_write_line(context, "\"extensions\": {");
  333. cgltf_write_line(context, "\"KHR_texture_transform\": {");
  334. if (cgltf_check_floatarray(transform->offset, 2, 0.0f))
  335. {
  336. cgltf_write_floatarrayprop(context, "offset", transform->offset, 2);
  337. }
  338. cgltf_write_floatprop(context, "rotation", transform->rotation, 0.0f);
  339. if (cgltf_check_floatarray(transform->scale, 2, 1.0f))
  340. {
  341. cgltf_write_floatarrayprop(context, "scale", transform->scale, 2);
  342. }
  343. cgltf_write_intprop(context, "texCoord", transform->texcoord, 0);
  344. cgltf_write_line(context, "}");
  345. cgltf_write_line(context, "}");
  346. }
  347. static void cgltf_write_asset(cgltf_write_context* context, const cgltf_asset* asset)
  348. {
  349. cgltf_write_line(context, "\"asset\": {");
  350. cgltf_write_strprop(context, "copyright", asset->copyright);
  351. cgltf_write_strprop(context, "generator", asset->generator);
  352. cgltf_write_strprop(context, "version", asset->version);
  353. cgltf_write_strprop(context, "min_version", asset->min_version);
  354. cgltf_write_extras(context, &asset->extras);
  355. cgltf_write_line(context, "}");
  356. }
  357. static void cgltf_write_primitive(cgltf_write_context* context, const cgltf_primitive* prim)
  358. {
  359. cgltf_write_intprop(context, "mode", (int) prim->type, 4);
  360. CGLTF_WRITE_IDXPROP("indices", prim->indices, context->data->accessors);
  361. CGLTF_WRITE_IDXPROP("material", prim->material, context->data->materials);
  362. cgltf_write_line(context, "\"attributes\": {");
  363. for (cgltf_size i = 0; i < prim->attributes_count; ++i)
  364. {
  365. const cgltf_attribute* attr = prim->attributes + i;
  366. CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
  367. }
  368. cgltf_write_line(context, "}");
  369. if (prim->targets_count)
  370. {
  371. cgltf_write_line(context, "\"targets\": [");
  372. for (cgltf_size i = 0; i < prim->targets_count; ++i)
  373. {
  374. cgltf_write_line(context, "{");
  375. for (cgltf_size j = 0; j < prim->targets[i].attributes_count; ++j)
  376. {
  377. const cgltf_attribute* attr = prim->targets[i].attributes + j;
  378. CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
  379. }
  380. cgltf_write_line(context, "}");
  381. }
  382. cgltf_write_line(context, "]");
  383. }
  384. cgltf_write_extras(context, &prim->extras);
  385. if (prim->has_draco_mesh_compression || prim->mappings_count > 0)
  386. {
  387. cgltf_write_line(context, "\"extensions\": {");
  388. if (prim->has_draco_mesh_compression)
  389. {
  390. context->extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
  391. if (prim->attributes_count == 0 || prim->indices == 0)
  392. {
  393. context->required_extension_flags |= CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION;
  394. }
  395. cgltf_write_line(context, "\"KHR_draco_mesh_compression\": {");
  396. CGLTF_WRITE_IDXPROP("bufferView", prim->draco_mesh_compression.buffer_view, context->data->buffer_views);
  397. cgltf_write_line(context, "\"attributes\": {");
  398. for (cgltf_size i = 0; i < prim->draco_mesh_compression.attributes_count; ++i)
  399. {
  400. const cgltf_attribute* attr = prim->draco_mesh_compression.attributes + i;
  401. CGLTF_WRITE_IDXPROP(attr->name, attr->data, context->data->accessors);
  402. }
  403. cgltf_write_line(context, "}");
  404. cgltf_write_line(context, "}");
  405. }
  406. if (prim->mappings_count > 0)
  407. {
  408. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS;
  409. cgltf_write_line(context, "\"KHR_materials_variants\": {");
  410. cgltf_write_line(context, "\"mappings\": [");
  411. for (cgltf_size i = 0; i < prim->mappings_count; ++i)
  412. {
  413. const cgltf_material_mapping* map = prim->mappings + i;
  414. cgltf_write_line(context, "{");
  415. CGLTF_WRITE_IDXPROP("material", map->material, context->data->materials);
  416. cgltf_write_indent(context);
  417. CGLTF_SPRINTF("\"variants\": [%d]", (int)map->variant);
  418. context->needs_comma = 1;
  419. cgltf_write_extras(context, &map->extras);
  420. cgltf_write_line(context, "}");
  421. }
  422. cgltf_write_line(context, "]");
  423. cgltf_write_line(context, "}");
  424. }
  425. cgltf_write_line(context, "}");
  426. }
  427. }
  428. static void cgltf_write_mesh(cgltf_write_context* context, const cgltf_mesh* mesh)
  429. {
  430. cgltf_write_line(context, "{");
  431. cgltf_write_strprop(context, "name", mesh->name);
  432. cgltf_write_line(context, "\"primitives\": [");
  433. for (cgltf_size i = 0; i < mesh->primitives_count; ++i)
  434. {
  435. cgltf_write_line(context, "{");
  436. cgltf_write_primitive(context, mesh->primitives + i);
  437. cgltf_write_line(context, "}");
  438. }
  439. cgltf_write_line(context, "]");
  440. if (mesh->weights_count > 0)
  441. {
  442. cgltf_write_floatarrayprop(context, "weights", mesh->weights, mesh->weights_count);
  443. }
  444. cgltf_write_extras(context, &mesh->extras);
  445. cgltf_write_line(context, "}");
  446. }
  447. static void cgltf_write_buffer_view(cgltf_write_context* context, const cgltf_buffer_view* view)
  448. {
  449. cgltf_write_line(context, "{");
  450. CGLTF_WRITE_IDXPROP("buffer", view->buffer, context->data->buffers);
  451. cgltf_write_intprop(context, "byteLength", (int)view->size, -1);
  452. cgltf_write_intprop(context, "byteOffset", (int)view->offset, 0);
  453. cgltf_write_intprop(context, "byteStride", (int)view->stride, 0);
  454. // NOTE: We skip writing "target" because the spec says its usage can be inferred.
  455. cgltf_write_extras(context, &view->extras);
  456. cgltf_write_line(context, "}");
  457. }
  458. static void cgltf_write_buffer(cgltf_write_context* context, const cgltf_buffer* buffer)
  459. {
  460. cgltf_write_line(context, "{");
  461. cgltf_write_strprop(context, "uri", buffer->uri);
  462. cgltf_write_intprop(context, "byteLength", (int)buffer->size, -1);
  463. cgltf_write_extras(context, &buffer->extras);
  464. cgltf_write_line(context, "}");
  465. }
  466. static void cgltf_write_material(cgltf_write_context* context, const cgltf_material* material)
  467. {
  468. cgltf_write_line(context, "{");
  469. cgltf_write_strprop(context, "name", material->name);
  470. cgltf_write_floatprop(context, "alphaCutoff", material->alpha_cutoff, 0.5f);
  471. cgltf_write_boolprop_optional(context, "doubleSided", material->double_sided, false);
  472. // cgltf_write_boolprop_optional(context, "unlit", material->unlit, false);
  473. if (material->unlit)
  474. {
  475. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_UNLIT;
  476. }
  477. if (material->has_pbr_specular_glossiness)
  478. {
  479. context->extension_flags |= CGLTF_EXTENSION_FLAG_SPECULAR_GLOSSINESS;
  480. }
  481. if (material->has_clearcoat)
  482. {
  483. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_CLEARCOAT;
  484. }
  485. if (material->has_transmission)
  486. {
  487. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION;
  488. }
  489. if (material->has_volume)
  490. {
  491. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VOLUME;
  492. }
  493. if (material->has_ior)
  494. {
  495. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_IOR;
  496. }
  497. if (material->has_specular)
  498. {
  499. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR;
  500. }
  501. if (material->has_sheen)
  502. {
  503. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN;
  504. }
  505. if (material->has_pbr_metallic_roughness)
  506. {
  507. const cgltf_pbr_metallic_roughness* params = &material->pbr_metallic_roughness;
  508. cgltf_write_line(context, "\"pbrMetallicRoughness\": {");
  509. CGLTF_WRITE_TEXTURE_INFO("baseColorTexture", params->base_color_texture);
  510. CGLTF_WRITE_TEXTURE_INFO("metallicRoughnessTexture", params->metallic_roughness_texture);
  511. cgltf_write_floatprop(context, "metallicFactor", params->metallic_factor, 1.0f);
  512. cgltf_write_floatprop(context, "roughnessFactor", params->roughness_factor, 1.0f);
  513. if (cgltf_check_floatarray(params->base_color_factor, 4, 1.0f))
  514. {
  515. cgltf_write_floatarrayprop(context, "baseColorFactor", params->base_color_factor, 4);
  516. }
  517. cgltf_write_extras(context, &params->extras);
  518. cgltf_write_line(context, "}");
  519. }
  520. if (material->unlit || material->has_pbr_specular_glossiness || material->has_clearcoat || material->has_ior || material->has_specular || material->has_transmission || material->has_sheen || material->has_volume)
  521. {
  522. cgltf_write_line(context, "\"extensions\": {");
  523. if (material->has_clearcoat)
  524. {
  525. const cgltf_clearcoat* params = &material->clearcoat;
  526. cgltf_write_line(context, "\"KHR_materials_clearcoat\": {");
  527. CGLTF_WRITE_TEXTURE_INFO("clearcoatTexture", params->clearcoat_texture);
  528. CGLTF_WRITE_TEXTURE_INFO("clearcoatRoughnessTexture", params->clearcoat_roughness_texture);
  529. CGLTF_WRITE_TEXTURE_INFO("clearcoatNormalTexture", params->clearcoat_normal_texture);
  530. cgltf_write_floatprop(context, "clearcoatFactor", params->clearcoat_factor, 0.0f);
  531. cgltf_write_floatprop(context, "clearcoatRoughnessFactor", params->clearcoat_roughness_factor, 0.0f);
  532. cgltf_write_line(context, "}");
  533. }
  534. if (material->has_ior)
  535. {
  536. const cgltf_ior* params = &material->ior;
  537. cgltf_write_line(context, "\"KHR_materials_ior\": {");
  538. cgltf_write_floatprop(context, "ior", params->ior, 1.5f);
  539. cgltf_write_line(context, "}");
  540. }
  541. if (material->has_specular)
  542. {
  543. const cgltf_specular* params = &material->specular;
  544. cgltf_write_line(context, "\"KHR_materials_specular\": {");
  545. CGLTF_WRITE_TEXTURE_INFO("specularTexture", params->specular_texture);
  546. CGLTF_WRITE_TEXTURE_INFO("specularColorTexture", params->specular_color_texture);
  547. cgltf_write_floatprop(context, "specularFactor", params->specular_factor, 1.0f);
  548. if (cgltf_check_floatarray(params->specular_color_factor, 3, 1.0f))
  549. {
  550. cgltf_write_floatarrayprop(context, "specularColorFactor", params->specular_color_factor, 3);
  551. }
  552. cgltf_write_line(context, "}");
  553. }
  554. if (material->has_transmission)
  555. {
  556. const cgltf_transmission* params = &material->transmission;
  557. cgltf_write_line(context, "\"KHR_materials_transmission\": {");
  558. CGLTF_WRITE_TEXTURE_INFO("transmissionTexture", params->transmission_texture);
  559. cgltf_write_floatprop(context, "transmissionFactor", params->transmission_factor, 0.0f);
  560. cgltf_write_line(context, "}");
  561. }
  562. if (material->has_volume)
  563. {
  564. const cgltf_volume* params = &material->volume;
  565. cgltf_write_line(context, "\"KHR_materials_volume\": {");
  566. CGLTF_WRITE_TEXTURE_INFO("thicknessTexture", params->thickness_texture);
  567. cgltf_write_floatprop(context, "thicknessFactor", params->thickness_factor, 0.0f);
  568. if (cgltf_check_floatarray(params->attenuation_color, 3, 1.0f))
  569. {
  570. cgltf_write_floatarrayprop(context, "attenuationColor", params->attenuation_color, 3);
  571. }
  572. if (params->attenuation_distance < FLT_MAX)
  573. {
  574. cgltf_write_floatprop(context, "attenuationDistance", params->attenuation_distance, FLT_MAX);
  575. }
  576. cgltf_write_line(context, "}");
  577. }
  578. if (material->has_sheen)
  579. {
  580. const cgltf_sheen* params = &material->sheen;
  581. cgltf_write_line(context, "\"KHR_materials_sheen\": {");
  582. CGLTF_WRITE_TEXTURE_INFO("sheenColorTexture", params->sheen_color_texture);
  583. CGLTF_WRITE_TEXTURE_INFO("sheenRoughnessTexture", params->sheen_roughness_texture);
  584. if (cgltf_check_floatarray(params->sheen_color_factor, 3, 0.0f))
  585. {
  586. cgltf_write_floatarrayprop(context, "sheenColorFactor", params->sheen_color_factor, 3);
  587. }
  588. cgltf_write_floatprop(context, "sheenRoughnessFactor", params->sheen_roughness_factor, 0.0f);
  589. cgltf_write_line(context, "}");
  590. }
  591. if (material->has_pbr_specular_glossiness)
  592. {
  593. const cgltf_pbr_specular_glossiness* params = &material->pbr_specular_glossiness;
  594. cgltf_write_line(context, "\"KHR_materials_pbrSpecularGlossiness\": {");
  595. CGLTF_WRITE_TEXTURE_INFO("diffuseTexture", params->diffuse_texture);
  596. CGLTF_WRITE_TEXTURE_INFO("specularGlossinessTexture", params->specular_glossiness_texture);
  597. if (cgltf_check_floatarray(params->diffuse_factor, 4, 1.0f))
  598. {
  599. cgltf_write_floatarrayprop(context, "dffuseFactor", params->diffuse_factor, 4);
  600. }
  601. if (cgltf_check_floatarray(params->specular_factor, 3, 1.0f))
  602. {
  603. cgltf_write_floatarrayprop(context, "specularFactor", params->specular_factor, 3);
  604. }
  605. cgltf_write_floatprop(context, "glossinessFactor", params->glossiness_factor, 1.0f);
  606. cgltf_write_line(context, "}");
  607. }
  608. if (material->unlit)
  609. {
  610. cgltf_write_line(context, "\"KHR_materials_unlit\": {}");
  611. }
  612. cgltf_write_line(context, "}");
  613. }
  614. CGLTF_WRITE_TEXTURE_INFO("normalTexture", material->normal_texture);
  615. CGLTF_WRITE_TEXTURE_INFO("occlusionTexture", material->occlusion_texture);
  616. CGLTF_WRITE_TEXTURE_INFO("emissiveTexture", material->emissive_texture);
  617. if (cgltf_check_floatarray(material->emissive_factor, 3, 0.0f))
  618. {
  619. cgltf_write_floatarrayprop(context, "emissiveFactor", material->emissive_factor, 3);
  620. }
  621. cgltf_write_strprop(context, "alphaMode", cgltf_str_from_alpha_mode(material->alpha_mode));
  622. cgltf_write_extras(context, &material->extras);
  623. cgltf_write_line(context, "}");
  624. }
  625. static void cgltf_write_image(cgltf_write_context* context, const cgltf_image* image)
  626. {
  627. cgltf_write_line(context, "{");
  628. cgltf_write_strprop(context, "name", image->name);
  629. cgltf_write_strprop(context, "uri", image->uri);
  630. CGLTF_WRITE_IDXPROP("bufferView", image->buffer_view, context->data->buffer_views);
  631. cgltf_write_strprop(context, "mimeType", image->mime_type);
  632. cgltf_write_extras(context, &image->extras);
  633. cgltf_write_line(context, "}");
  634. }
  635. static void cgltf_write_texture(cgltf_write_context* context, const cgltf_texture* texture)
  636. {
  637. cgltf_write_line(context, "{");
  638. cgltf_write_strprop(context, "name", texture->name);
  639. CGLTF_WRITE_IDXPROP("source", texture->image, context->data->images);
  640. CGLTF_WRITE_IDXPROP("sampler", texture->sampler, context->data->samplers);
  641. cgltf_write_extras(context, &texture->extras);
  642. cgltf_write_line(context, "}");
  643. }
  644. static void cgltf_write_skin(cgltf_write_context* context, const cgltf_skin* skin)
  645. {
  646. cgltf_write_line(context, "{");
  647. CGLTF_WRITE_IDXPROP("skeleton", skin->skeleton, context->data->nodes);
  648. CGLTF_WRITE_IDXPROP("inverseBindMatrices", skin->inverse_bind_matrices, context->data->accessors);
  649. CGLTF_WRITE_IDXARRPROP("joints", skin->joints_count, skin->joints, context->data->nodes);
  650. cgltf_write_strprop(context, "name", skin->name);
  651. cgltf_write_extras(context, &skin->extras);
  652. cgltf_write_line(context, "}");
  653. }
  654. static const char* cgltf_write_str_path_type(cgltf_animation_path_type path_type)
  655. {
  656. switch (path_type)
  657. {
  658. case cgltf_animation_path_type_translation:
  659. return "translation";
  660. case cgltf_animation_path_type_rotation:
  661. return "rotation";
  662. case cgltf_animation_path_type_scale:
  663. return "scale";
  664. case cgltf_animation_path_type_weights:
  665. return "weights";
  666. case cgltf_animation_path_type_invalid:
  667. break;
  668. }
  669. return "invalid";
  670. }
  671. static const char* cgltf_write_str_interpolation_type(cgltf_interpolation_type interpolation_type)
  672. {
  673. switch (interpolation_type)
  674. {
  675. case cgltf_interpolation_type_linear:
  676. return "LINEAR";
  677. case cgltf_interpolation_type_step:
  678. return "STEP";
  679. case cgltf_interpolation_type_cubic_spline:
  680. return "CUBICSPLINE";
  681. }
  682. return "invalid";
  683. }
  684. static void cgltf_write_path_type(cgltf_write_context* context, const char *label, cgltf_animation_path_type path_type)
  685. {
  686. cgltf_write_strprop(context, label, cgltf_write_str_path_type(path_type));
  687. }
  688. static void cgltf_write_interpolation_type(cgltf_write_context* context, const char *label, cgltf_interpolation_type interpolation_type)
  689. {
  690. cgltf_write_strprop(context, label, cgltf_write_str_interpolation_type(interpolation_type));
  691. }
  692. static void cgltf_write_animation_sampler(cgltf_write_context* context, const cgltf_animation_sampler* animation_sampler)
  693. {
  694. cgltf_write_line(context, "{");
  695. cgltf_write_interpolation_type(context, "interpolation", animation_sampler->interpolation);
  696. CGLTF_WRITE_IDXPROP("input", animation_sampler->input, context->data->accessors);
  697. CGLTF_WRITE_IDXPROP("output", animation_sampler->output, context->data->accessors);
  698. cgltf_write_extras(context, &animation_sampler->extras);
  699. cgltf_write_line(context, "}");
  700. }
  701. static void cgltf_write_animation_channel(cgltf_write_context* context, const cgltf_animation* animation, const cgltf_animation_channel* animation_channel)
  702. {
  703. cgltf_write_line(context, "{");
  704. CGLTF_WRITE_IDXPROP("sampler", animation_channel->sampler, animation->samplers);
  705. cgltf_write_line(context, "\"target\": {");
  706. CGLTF_WRITE_IDXPROP("node", animation_channel->target_node, context->data->nodes);
  707. cgltf_write_path_type(context, "path", animation_channel->target_path);
  708. cgltf_write_line(context, "}");
  709. cgltf_write_extras(context, &animation_channel->extras);
  710. cgltf_write_line(context, "}");
  711. }
  712. static void cgltf_write_animation(cgltf_write_context* context, const cgltf_animation* animation)
  713. {
  714. cgltf_write_line(context, "{");
  715. cgltf_write_strprop(context, "name", animation->name);
  716. if (animation->samplers_count > 0)
  717. {
  718. cgltf_write_line(context, "\"samplers\": [");
  719. for (cgltf_size i = 0; i < animation->samplers_count; ++i)
  720. {
  721. cgltf_write_animation_sampler(context, animation->samplers + i);
  722. }
  723. cgltf_write_line(context, "]");
  724. }
  725. if (animation->channels_count > 0)
  726. {
  727. cgltf_write_line(context, "\"channels\": [");
  728. for (cgltf_size i = 0; i < animation->channels_count; ++i)
  729. {
  730. cgltf_write_animation_channel(context, animation, animation->channels + i);
  731. }
  732. cgltf_write_line(context, "]");
  733. }
  734. cgltf_write_extras(context, &animation->extras);
  735. cgltf_write_line(context, "}");
  736. }
  737. static void cgltf_write_sampler(cgltf_write_context* context, const cgltf_sampler* sampler)
  738. {
  739. cgltf_write_line(context, "{");
  740. cgltf_write_intprop(context, "magFilter", sampler->mag_filter, 0);
  741. cgltf_write_intprop(context, "minFilter", sampler->min_filter, 0);
  742. cgltf_write_intprop(context, "wrapS", sampler->wrap_s, 10497);
  743. cgltf_write_intprop(context, "wrapT", sampler->wrap_t, 10497);
  744. cgltf_write_extras(context, &sampler->extras);
  745. cgltf_write_line(context, "}");
  746. }
  747. static void cgltf_write_node(cgltf_write_context* context, const cgltf_node* node)
  748. {
  749. cgltf_write_line(context, "{");
  750. CGLTF_WRITE_IDXARRPROP("children", node->children_count, node->children, context->data->nodes);
  751. CGLTF_WRITE_IDXPROP("mesh", node->mesh, context->data->meshes);
  752. cgltf_write_strprop(context, "name", node->name);
  753. if (node->has_matrix)
  754. {
  755. cgltf_write_floatarrayprop(context, "matrix", node->matrix, 16);
  756. }
  757. if (node->has_translation)
  758. {
  759. cgltf_write_floatarrayprop(context, "translation", node->translation, 3);
  760. }
  761. if (node->has_rotation)
  762. {
  763. cgltf_write_floatarrayprop(context, "rotation", node->rotation, 4);
  764. }
  765. if (node->has_scale)
  766. {
  767. cgltf_write_floatarrayprop(context, "scale", node->scale, 3);
  768. }
  769. if (node->skin)
  770. {
  771. CGLTF_WRITE_IDXPROP("skin", node->skin, context->data->skins);
  772. }
  773. if (node->light)
  774. {
  775. context->extension_flags |= CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL;
  776. cgltf_write_line(context, "\"extensions\": {");
  777. cgltf_write_line(context, "\"KHR_lights_punctual\": {");
  778. CGLTF_WRITE_IDXPROP("light", node->light, context->data->lights);
  779. cgltf_write_line(context, "}");
  780. cgltf_write_line(context, "}");
  781. }
  782. if (node->weights_count > 0)
  783. {
  784. cgltf_write_floatarrayprop(context, "weights", node->weights, node->weights_count);
  785. }
  786. if (node->camera)
  787. {
  788. CGLTF_WRITE_IDXPROP("camera", node->camera, context->data->cameras);
  789. }
  790. cgltf_write_extras(context, &node->extras);
  791. cgltf_write_line(context, "}");
  792. }
  793. static void cgltf_write_scene(cgltf_write_context* context, const cgltf_scene* scene)
  794. {
  795. cgltf_write_line(context, "{");
  796. cgltf_write_strprop(context, "name", scene->name);
  797. CGLTF_WRITE_IDXARRPROP("nodes", scene->nodes_count, scene->nodes, context->data->nodes);
  798. cgltf_write_extras(context, &scene->extras);
  799. cgltf_write_line(context, "}");
  800. }
  801. static void cgltf_write_accessor(cgltf_write_context* context, const cgltf_accessor* accessor)
  802. {
  803. cgltf_write_line(context, "{");
  804. CGLTF_WRITE_IDXPROP("bufferView", accessor->buffer_view, context->data->buffer_views);
  805. cgltf_write_intprop(context, "componentType", cgltf_int_from_component_type(accessor->component_type), 0);
  806. cgltf_write_strprop(context, "type", cgltf_str_from_type(accessor->type));
  807. cgltf_size dim = cgltf_dim_from_type(accessor->type);
  808. cgltf_write_boolprop_optional(context, "normalized", accessor->normalized, false);
  809. cgltf_write_intprop(context, "byteOffset", (int)accessor->offset, 0);
  810. cgltf_write_intprop(context, "count", (int)accessor->count, -1);
  811. if (accessor->has_min)
  812. {
  813. cgltf_write_floatarrayprop(context, "min", accessor->min, dim);
  814. }
  815. if (accessor->has_max)
  816. {
  817. cgltf_write_floatarrayprop(context, "max", accessor->max, dim);
  818. }
  819. if (accessor->is_sparse)
  820. {
  821. cgltf_write_line(context, "\"sparse\": {");
  822. cgltf_write_intprop(context, "count", (int)accessor->sparse.count, 0);
  823. cgltf_write_line(context, "\"indices\": {");
  824. cgltf_write_intprop(context, "byteOffset", (int)accessor->sparse.indices_byte_offset, 0);
  825. CGLTF_WRITE_IDXPROP("bufferView", accessor->sparse.indices_buffer_view, context->data->buffer_views);
  826. cgltf_write_intprop(context, "componentType", cgltf_int_from_component_type(accessor->sparse.indices_component_type), 0);
  827. cgltf_write_extras(context, &accessor->sparse.indices_extras);
  828. cgltf_write_line(context, "}");
  829. cgltf_write_line(context, "\"values\": {");
  830. cgltf_write_intprop(context, "byteOffset", (int)accessor->sparse.values_byte_offset, 0);
  831. CGLTF_WRITE_IDXPROP("bufferView", accessor->sparse.values_buffer_view, context->data->buffer_views);
  832. cgltf_write_extras(context, &accessor->sparse.values_extras);
  833. cgltf_write_line(context, "}");
  834. cgltf_write_extras(context, &accessor->sparse.extras);
  835. cgltf_write_line(context, "}");
  836. }
  837. cgltf_write_extras(context, &accessor->extras);
  838. cgltf_write_line(context, "}");
  839. }
  840. static void cgltf_write_camera(cgltf_write_context* context, const cgltf_camera* camera)
  841. {
  842. cgltf_write_line(context, "{");
  843. cgltf_write_strprop(context, "type", cgltf_str_from_camera_type(camera->type));
  844. if (camera->name)
  845. {
  846. cgltf_write_strprop(context, "name", camera->name);
  847. }
  848. if (camera->type == cgltf_camera_type_orthographic)
  849. {
  850. cgltf_write_line(context, "\"orthographic\": {");
  851. cgltf_write_floatprop(context, "xmag", camera->data.orthographic.xmag, -1.0f);
  852. cgltf_write_floatprop(context, "ymag", camera->data.orthographic.ymag, -1.0f);
  853. cgltf_write_floatprop(context, "zfar", camera->data.orthographic.zfar, -1.0f);
  854. cgltf_write_floatprop(context, "znear", camera->data.orthographic.znear, -1.0f);
  855. cgltf_write_extras(context, &camera->data.orthographic.extras);
  856. cgltf_write_line(context, "}");
  857. }
  858. else if (camera->type == cgltf_camera_type_perspective)
  859. {
  860. cgltf_write_line(context, "\"perspective\": {");
  861. cgltf_write_floatprop(context, "aspectRatio", camera->data.perspective.aspect_ratio, -1.0f);
  862. cgltf_write_floatprop(context, "yfov", camera->data.perspective.yfov, -1.0f);
  863. cgltf_write_floatprop(context, "zfar", camera->data.perspective.zfar, -1.0f);
  864. cgltf_write_floatprop(context, "znear", camera->data.perspective.znear, -1.0f);
  865. cgltf_write_extras(context, &camera->data.perspective.extras);
  866. cgltf_write_line(context, "}");
  867. }
  868. cgltf_write_extras(context, &camera->extras);
  869. cgltf_write_line(context, "}");
  870. }
  871. static void cgltf_write_light(cgltf_write_context* context, const cgltf_light* light)
  872. {
  873. context->extension_flags |= CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL;
  874. cgltf_write_line(context, "{");
  875. cgltf_write_strprop(context, "type", cgltf_str_from_light_type(light->type));
  876. if (light->name)
  877. {
  878. cgltf_write_strprop(context, "name", light->name);
  879. }
  880. if (cgltf_check_floatarray(light->color, 3, 1.0f))
  881. {
  882. cgltf_write_floatarrayprop(context, "color", light->color, 3);
  883. }
  884. cgltf_write_floatprop(context, "intensity", light->intensity, 1.0f);
  885. cgltf_write_floatprop(context, "range", light->range, 0.0f);
  886. if (light->type == cgltf_light_type_spot)
  887. {
  888. cgltf_write_line(context, "\"spot\": {");
  889. cgltf_write_floatprop(context, "innerConeAngle", light->spot_inner_cone_angle, 0.0f);
  890. cgltf_write_floatprop(context, "outerConeAngle", light->spot_outer_cone_angle, 3.14159265358979323846f/4.0f);
  891. cgltf_write_line(context, "}");
  892. }
  893. cgltf_write_line(context, "}");
  894. }
  895. static void cgltf_write_variant(cgltf_write_context* context, const cgltf_material_variant* variant)
  896. {
  897. context->extension_flags |= CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS;
  898. cgltf_write_line(context, "{");
  899. cgltf_write_strprop(context, "name", variant->name);
  900. cgltf_write_extras(context, &variant->extras);
  901. cgltf_write_line(context, "}");
  902. }
  903. cgltf_result cgltf_write_file(const cgltf_options* options, const char* path, const cgltf_data* data)
  904. {
  905. cgltf_size expected = cgltf_write(options, NULL, 0, data);
  906. char* buffer = (char*) malloc(expected);
  907. cgltf_size actual = cgltf_write(options, buffer, expected, data);
  908. if (expected != actual) {
  909. fprintf(stderr, "Error: expected %zu bytes but wrote %zu bytes.\n", expected, actual);
  910. }
  911. FILE* file = fopen(path, "wt");
  912. if (!file)
  913. {
  914. return cgltf_result_file_not_found;
  915. }
  916. // Note that cgltf_write() includes a null terminator, which we omit from the file content.
  917. fwrite(buffer, actual - 1, 1, file);
  918. fclose(file);
  919. free(buffer);
  920. return cgltf_result_success;
  921. }
  922. static void cgltf_write_extensions(cgltf_write_context* context, uint32_t extension_flags)
  923. {
  924. if (extension_flags & CGLTF_EXTENSION_FLAG_TEXTURE_TRANSFORM) {
  925. cgltf_write_stritem(context, "KHR_texture_transform");
  926. }
  927. if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_UNLIT) {
  928. cgltf_write_stritem(context, "KHR_materials_unlit");
  929. }
  930. if (extension_flags & CGLTF_EXTENSION_FLAG_SPECULAR_GLOSSINESS) {
  931. cgltf_write_stritem(context, "KHR_materials_pbrSpecularGlossiness");
  932. }
  933. if (extension_flags & CGLTF_EXTENSION_FLAG_LIGHTS_PUNCTUAL) {
  934. cgltf_write_stritem(context, "KHR_lights_punctual");
  935. }
  936. if (extension_flags & CGLTF_EXTENSION_FLAG_DRACO_MESH_COMPRESSION) {
  937. cgltf_write_stritem(context, "KHR_draco_mesh_compression");
  938. }
  939. if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_CLEARCOAT) {
  940. cgltf_write_stritem(context, "KHR_materials_clearcoat");
  941. }
  942. if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_IOR) {
  943. cgltf_write_stritem(context, "KHR_materials_ior");
  944. }
  945. if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_SPECULAR) {
  946. cgltf_write_stritem(context, "KHR_materials_specular");
  947. }
  948. if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_TRANSMISSION) {
  949. cgltf_write_stritem(context, "KHR_materials_transmission");
  950. }
  951. if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_SHEEN) {
  952. cgltf_write_stritem(context, "KHR_materials_sheen");
  953. }
  954. if (extension_flags & CGLTF_EXTENSION_FLAG_MATERIALS_VARIANTS) {
  955. cgltf_write_stritem(context, "KHR_materials_variants");
  956. }
  957. }
  958. cgltf_size cgltf_write(const cgltf_options* options, char* buffer, cgltf_size size, const cgltf_data* data)
  959. {
  960. (void)options;
  961. cgltf_write_context ctx;
  962. ctx.buffer = buffer;
  963. ctx.buffer_size = size;
  964. ctx.remaining = size;
  965. ctx.cursor = buffer;
  966. ctx.chars_written = 0;
  967. ctx.data = data;
  968. ctx.depth = 1;
  969. ctx.indent = " ";
  970. ctx.needs_comma = 0;
  971. ctx.extension_flags = 0;
  972. ctx.required_extension_flags = 0;
  973. cgltf_write_context* context = &ctx;
  974. CGLTF_SPRINTF("{");
  975. if (data->accessors_count > 0)
  976. {
  977. cgltf_write_line(context, "\"accessors\": [");
  978. for (cgltf_size i = 0; i < data->accessors_count; ++i)
  979. {
  980. cgltf_write_accessor(context, data->accessors + i);
  981. }
  982. cgltf_write_line(context, "]");
  983. }
  984. cgltf_write_asset(context, &data->asset);
  985. if (data->buffer_views_count > 0)
  986. {
  987. cgltf_write_line(context, "\"bufferViews\": [");
  988. for (cgltf_size i = 0; i < data->buffer_views_count; ++i)
  989. {
  990. cgltf_write_buffer_view(context, data->buffer_views + i);
  991. }
  992. cgltf_write_line(context, "]");
  993. }
  994. if (data->buffers_count > 0)
  995. {
  996. cgltf_write_line(context, "\"buffers\": [");
  997. for (cgltf_size i = 0; i < data->buffers_count; ++i)
  998. {
  999. cgltf_write_buffer(context, data->buffers + i);
  1000. }
  1001. cgltf_write_line(context, "]");
  1002. }
  1003. if (data->images_count > 0)
  1004. {
  1005. cgltf_write_line(context, "\"images\": [");
  1006. for (cgltf_size i = 0; i < data->images_count; ++i)
  1007. {
  1008. cgltf_write_image(context, data->images + i);
  1009. }
  1010. cgltf_write_line(context, "]");
  1011. }
  1012. if (data->meshes_count > 0)
  1013. {
  1014. cgltf_write_line(context, "\"meshes\": [");
  1015. for (cgltf_size i = 0; i < data->meshes_count; ++i)
  1016. {
  1017. cgltf_write_mesh(context, data->meshes + i);
  1018. }
  1019. cgltf_write_line(context, "]");
  1020. }
  1021. if (data->materials_count > 0)
  1022. {
  1023. cgltf_write_line(context, "\"materials\": [");
  1024. for (cgltf_size i = 0; i < data->materials_count; ++i)
  1025. {
  1026. cgltf_write_material(context, data->materials + i);
  1027. }
  1028. cgltf_write_line(context, "]");
  1029. }
  1030. if (data->nodes_count > 0)
  1031. {
  1032. cgltf_write_line(context, "\"nodes\": [");
  1033. for (cgltf_size i = 0; i < data->nodes_count; ++i)
  1034. {
  1035. cgltf_write_node(context, data->nodes + i);
  1036. }
  1037. cgltf_write_line(context, "]");
  1038. }
  1039. if (data->samplers_count > 0)
  1040. {
  1041. cgltf_write_line(context, "\"samplers\": [");
  1042. for (cgltf_size i = 0; i < data->samplers_count; ++i)
  1043. {
  1044. cgltf_write_sampler(context, data->samplers + i);
  1045. }
  1046. cgltf_write_line(context, "]");
  1047. }
  1048. CGLTF_WRITE_IDXPROP("scene", data->scene, data->scenes);
  1049. if (data->scenes_count > 0)
  1050. {
  1051. cgltf_write_line(context, "\"scenes\": [");
  1052. for (cgltf_size i = 0; i < data->scenes_count; ++i)
  1053. {
  1054. cgltf_write_scene(context, data->scenes + i);
  1055. }
  1056. cgltf_write_line(context, "]");
  1057. }
  1058. if (data->textures_count > 0)
  1059. {
  1060. cgltf_write_line(context, "\"textures\": [");
  1061. for (cgltf_size i = 0; i < data->textures_count; ++i)
  1062. {
  1063. cgltf_write_texture(context, data->textures + i);
  1064. }
  1065. cgltf_write_line(context, "]");
  1066. }
  1067. if (data->skins_count > 0)
  1068. {
  1069. cgltf_write_line(context, "\"skins\": [");
  1070. for (cgltf_size i = 0; i < data->skins_count; ++i)
  1071. {
  1072. cgltf_write_skin(context, data->skins + i);
  1073. }
  1074. cgltf_write_line(context, "]");
  1075. }
  1076. if (data->animations_count > 0)
  1077. {
  1078. cgltf_write_line(context, "\"animations\": [");
  1079. for (cgltf_size i = 0; i < data->animations_count; ++i)
  1080. {
  1081. cgltf_write_animation(context, data->animations + i);
  1082. }
  1083. cgltf_write_line(context, "]");
  1084. }
  1085. if (data->cameras_count > 0)
  1086. {
  1087. cgltf_write_line(context, "\"cameras\": [");
  1088. for (cgltf_size i = 0; i < data->cameras_count; ++i)
  1089. {
  1090. cgltf_write_camera(context, data->cameras + i);
  1091. }
  1092. cgltf_write_line(context, "]");
  1093. }
  1094. if (data->lights_count > 0 || data->variants_count > 0)
  1095. {
  1096. cgltf_write_line(context, "\"extensions\": {");
  1097. if (data->lights_count > 0)
  1098. {
  1099. cgltf_write_line(context, "\"KHR_lights_punctual\": {");
  1100. cgltf_write_line(context, "\"lights\": [");
  1101. for (cgltf_size i = 0; i < data->lights_count; ++i)
  1102. {
  1103. cgltf_write_light(context, data->lights + i);
  1104. }
  1105. cgltf_write_line(context, "]");
  1106. cgltf_write_line(context, "}");
  1107. }
  1108. if (data->variants_count)
  1109. {
  1110. cgltf_write_line(context, "\"KHR_materials_variants\": {");
  1111. cgltf_write_line(context, "\"variants\": [");
  1112. for (cgltf_size i = 0; i < data->variants_count; ++i)
  1113. {
  1114. cgltf_write_variant(context, data->variants + i);
  1115. }
  1116. cgltf_write_line(context, "]");
  1117. cgltf_write_line(context, "}");
  1118. }
  1119. cgltf_write_line(context, "}");
  1120. }
  1121. if (context->extension_flags != 0)
  1122. {
  1123. cgltf_write_line(context, "\"extensionsUsed\": [");
  1124. cgltf_write_extensions(context, context->extension_flags);
  1125. cgltf_write_line(context, "]");
  1126. }
  1127. if (context->required_extension_flags != 0)
  1128. {
  1129. cgltf_write_line(context, "\"extensionsRequired\": [");
  1130. cgltf_write_extensions(context, context->required_extension_flags);
  1131. cgltf_write_line(context, "]");
  1132. }
  1133. cgltf_write_extras(context, &data->extras);
  1134. CGLTF_SPRINTF("\n}\n");
  1135. // snprintf does not include the null terminator in its return value, so be sure to include it
  1136. // in the returned byte count.
  1137. return 1 + ctx.chars_written;
  1138. }
  1139. #endif /* #ifdef CGLTF_WRITE_IMPLEMENTATION */
  1140. /* cgltf is distributed under MIT license:
  1141. *
  1142. * Copyright (c) 2019 Philip Rideout
  1143. * Permission is hereby granted, free of charge, to any person obtaining a copy
  1144. * of this software and associated documentation files (the "Software"), to deal
  1145. * in the Software without restriction, including without limitation the rights
  1146. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  1147. * copies of the Software, and to permit persons to whom the Software is
  1148. * furnished to do so, subject to the following conditions:
  1149. * The above copyright notice and this permission notice shall be included in all
  1150. * copies or substantial portions of the Software.
  1151. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1152. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1153. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1154. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1155. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1156. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1157. * SOFTWARE.
  1158. */