mesh.vala 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. public struct Mesh
  8. {
  9. public Project _project;
  10. public Gee.ArrayList<string> _nodes;
  11. public Mesh(Project project)
  12. {
  13. _project = project;
  14. _nodes = new Gee.ArrayList<string>();
  15. }
  16. private void decode_node(Hashtable node)
  17. {
  18. foreach (var e in node.entries) {
  19. if (e.key == "matrix_local") {
  20. // Do nothing.
  21. } else if (e.key == "geometry") {
  22. // Do nothing.
  23. } else if (e.key == "children") {
  24. decode_nodes((Hashtable)e.value);
  25. }
  26. }
  27. }
  28. private void decode_nodes(Hashtable nodes)
  29. {
  30. foreach (var e in nodes.entries) {
  31. _nodes.add(e.key);
  32. decode_node((Hashtable)e.value);
  33. }
  34. }
  35. public void decode(Hashtable mesh)
  36. {
  37. if (mesh.has_key("source")) {
  38. ufbx.Error error = {};
  39. ufbx.LoadOpts load_opts = {};
  40. load_opts.ignore_all_content = true;
  41. ufbx.Scene? scene = ufbx.Scene.load_file(_project.absolute_path((string)mesh["source"]), load_opts, ref error);
  42. for (size_t i = 0; i < scene.nodes.data.length; ++i) {
  43. unowned ufbx.Node node = scene.nodes.data[i];
  44. if (node.mesh != null)
  45. _nodes.add((string)node.name.data);
  46. }
  47. } else {
  48. foreach (var e in mesh.entries) {
  49. if (e.key == "nodes") {
  50. decode_nodes((Hashtable)e.value);
  51. } else if (e.key == "geometries") {
  52. // Do nothing.
  53. }
  54. }
  55. }
  56. }
  57. public static Mesh load_from_path(Project project, string path) throws JsonSyntaxError
  58. {
  59. Mesh mesh = Mesh(project);
  60. mesh.decode(SJSON.load_from_path(project.absolute_path(path)));
  61. return mesh;
  62. }
  63. }
  64. } /* namespace Crown */