| 18 | } |
| 19 | |
| 20 | void scene::import_environment(const std::string & import_path) |
| 21 | { |
| 22 | manual_timer t; |
| 23 | t.start(); |
| 24 | |
| 25 | const std::string json_txt = read_file_text(import_path); |
| 26 | const json env_doc = json::parse(json_txt); |
| 27 | |
| 28 | // Track parent-child relationships to establish after all entities created |
| 29 | struct parent_child_link |
| 30 | { |
| 31 | entity child_entity; |
| 32 | entity parent_entity; |
| 33 | }; |
| 34 | std::vector<parent_child_link> parent_child_links; |
| 35 | |
| 36 | for (auto entityIterator = env_doc.begin(); entityIterator != env_doc.end(); ++entityIterator) |
| 37 | { |
| 38 | const std::string entity_key = entityIterator.key(); |
| 39 | const json & entity_json = entityIterator.value(); |
| 40 | |
| 41 | // Create entity with the parsed key as entity ID (must be valid GUID format) |
| 42 | entity parsed_entity = entity(entity_key); |
| 43 | base_object obj(parsed_entity); |
| 44 | |
| 45 | // Parse identifier_component for name |
| 46 | if (entity_json.contains("@identifier_component")) |
| 47 | { |
| 48 | const json & id_json = entity_json["@identifier_component"]; |
| 49 | if (id_json.contains("id")) obj.name = id_json["id"].get<std::string>(); |
| 50 | } |
| 51 | |
| 52 | // Parse local_transform_component |
| 53 | if (entity_json.contains("@local_transform_component")) |
| 54 | { |
| 55 | const json & xform_json = entity_json["@local_transform_component"]; |
| 56 | |
| 57 | transform_component xform_c; |
| 58 | |
| 59 | if (xform_json.contains("local_pose")) xform_c.local_pose = xform_json["local_pose"].get<transform>(); |
| 60 | if (xform_json.contains("local_scale")) xform_c.local_scale = xform_json["local_scale"].get<float3>(); |
| 61 | |
| 62 | obj.add_component(xform_c); |
| 63 | |
| 64 | // Track parent relationship for later (parent should be GUID string or empty) |
| 65 | if (xform_json.contains("parent")) |
| 66 | { |
| 67 | const json & parent_val = xform_json["parent"]; |
| 68 | entity parent_ent = kInvalidEntity; |
| 69 | |
| 70 | if (parent_val.is_string()) |
| 71 | { |
| 72 | std::string parent_str = parent_val.get<std::string>(); |
| 73 | if (!parent_str.empty()) parent_ent = entity(parent_str); |
| 74 | } |
| 75 | |
| 76 | if (parent_ent != kInvalidEntity) parent_child_links.push_back({parsed_entity, parent_ent}); |
| 77 | } |
no test coverage detected