-
Notifications
You must be signed in to change notification settings - Fork 472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for vrm. #199
Support for vrm. #199
Changes from all commits
c06f2e5
c77fc9f
aea5e4d
8c15632
4353d11
31c6825
8d6fe5a
7fb67b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -357,6 +357,7 @@ static void process(cgltf_data* data, const char* input_path, const char* output | |
std::string json_accessors; | ||
std::string json_meshes; | ||
std::string json_nodes; | ||
std::string json_extensions; | ||
std::string json_skins; | ||
std::vector<std::string> json_roots(data->scenes_count); | ||
std::string json_animations; | ||
|
@@ -440,7 +441,15 @@ static void process(cgltf_data* data, const char* input_path, const char* output | |
const Mesh& mesh = meshes[i]; | ||
|
||
comma(json_meshes); | ||
append(json_meshes, "{\"primitives\":["); | ||
append(json_meshes, "{"); | ||
if (!mesh.name.empty()) | ||
{ | ||
append(json_meshes, "\"name\":\""); | ||
append(json_meshes, mesh.name); | ||
append(json_meshes, "\""); | ||
comma(json_meshes); | ||
} | ||
append(json_meshes, "\"primitives\":["); | ||
|
||
size_t pi = i; | ||
for (; pi < meshes.size(); ++pi) | ||
|
@@ -462,7 +471,8 @@ static void process(cgltf_data* data, const char* input_path, const char* output | |
const QuantizationTexture& qt = prim.material ? qt_materials[prim.material - data->materials] : qt_dummy; | ||
|
||
comma(json_meshes); | ||
append(json_meshes, "{\"attributes\":{"); | ||
append(json_meshes, "{"); | ||
append(json_meshes, "\"attributes\":{"); | ||
writeMeshAttributes(json_meshes, views, json_accessors, accr_offset, prim, 0, qp, qt, settings); | ||
append(json_meshes, "}"); | ||
append(json_meshes, ",\"mode\":"); | ||
|
@@ -488,6 +498,11 @@ static void process(cgltf_data* data, const char* input_path, const char* output | |
append(json_meshes, ",\"indices\":"); | ||
append(json_meshes, index_accr); | ||
} | ||
if (settings.keep_extras && !prim.extras.empty()) | ||
{ | ||
append(json_meshes, ",\"extras\":"); | ||
append(json_meshes, prim.extras); | ||
} | ||
|
||
if (prim.material) | ||
{ | ||
|
@@ -592,7 +607,9 @@ static void process(cgltf_data* data, const char* input_path, const char* output | |
append(json_nodes, "{"); | ||
writeNode(json_nodes, node, nodes, data); | ||
if (settings.keep_extras) | ||
{ | ||
writeExtras(json_nodes, extras, node.extras); | ||
} | ||
append(json_nodes, "}"); | ||
} | ||
|
||
|
@@ -650,7 +667,7 @@ static void process(cgltf_data* data, const char* input_path, const char* output | |
const ExtensionInfo extensions[] = { | ||
{"KHR_mesh_quantization", settings.quantize, true}, | ||
{"EXT_meshopt_compression", settings.compress, !settings.fallback}, | ||
{"KHR_texture_transform", settings.quantize && !json_textures.empty(), false}, | ||
{"KHR_texture_transform", (settings.quantize && !json_textures.empty()) || (settings.vrm && !json_textures.empty()), false}, | ||
{"KHR_materials_pbrSpecularGlossiness", ext_pbr_specular_glossiness, false}, | ||
{"KHR_materials_clearcoat", ext_clearcoat, false}, | ||
{"KHR_materials_transmission", ext_transmission, false}, | ||
|
@@ -661,10 +678,39 @@ static void process(cgltf_data* data, const char* input_path, const char* output | |
{"KHR_lights_punctual", data->lights_count > 0, false}, | ||
{"KHR_texture_basisu", !json_textures.empty() && settings.texture_ktx2, true}, | ||
{"EXT_mesh_gpu_instancing", ext_instancing, true}, | ||
{"VRM", settings.vrm, false}, | ||
}; | ||
|
||
writeExtensions(json, extensions, sizeof(extensions) / sizeof(extensions[0])); | ||
|
||
comma(json_extensions); | ||
append(json_extensions, "\"extensions\":{"); | ||
if (settings.vrm) | ||
{ | ||
|
||
std::string extension_vrm; | ||
extension_vrm.assign("VRM"); | ||
for (size_t i = 0; i < data->data_extensions_count; ++i) | ||
{ | ||
cgltf_extension extension = data->data_extensions[i]; | ||
if (!extension.name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This & above & below can be replaced with |
||
{ | ||
continue; | ||
} | ||
std::string extension_tested; | ||
extension_tested.assign(extension.name); | ||
if (extension_vrm != extension_tested) | ||
{ | ||
break; | ||
} | ||
append(json_extensions, "\""); | ||
append(json_extensions, extension.name); | ||
append(json_extensions, "\":"); | ||
append(json_extensions, extension.data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use |
||
} | ||
} | ||
append(json_extensions, "}"); | ||
|
||
std::string json_views; | ||
finalizeBufferViews(json_views, views, bin, settings.fallback ? &fallback : NULL, fallback_size); | ||
|
||
|
@@ -676,6 +722,8 @@ static void process(cgltf_data* data, const char* input_path, const char* output | |
writeArray(json, "meshes", json_meshes); | ||
writeArray(json, "skins", json_skins); | ||
writeArray(json, "animations", json_animations); | ||
comma(json); | ||
append(json, json_extensions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will always emit "extensions" and also causes a conflict with top-level KHR_lights_punctual extension since there's now going to be two extension blocks for files that use that. Instead what should happen is:
|
||
writeArray(json, "nodes", json_nodes); | ||
|
||
if (!json_roots.empty()) | ||
|
@@ -791,7 +839,12 @@ int gltfpack(const char* input, const char* output, const char* report, Settings | |
const char* iext = strrchr(input, '.'); | ||
const char* oext = output ? strrchr(output, '.') : NULL; | ||
|
||
if (iext && (strcmp(iext, ".gltf") == 0 || strcmp(iext, ".GLTF") == 0 || strcmp(iext, ".glb") == 0 || strcmp(iext, ".GLB") == 0)) | ||
if (iext && (strcmp(iext, ".vrm") == 0 || strcmp(iext, ".VRM") == 0)) | ||
{ | ||
settings.vrm = true; | ||
} | ||
|
||
if (iext && (strcmp(iext, ".vrm") == 0 || strcmp(iext, ".VRM") == 0 || strcmp(iext, ".gltf") == 0 || strcmp(iext, ".GLTF") == 0 || strcmp(iext, ".glb") == 0 || strcmp(iext, ".GLB") == 0)) | ||
{ | ||
const char* error = 0; | ||
data = parseGltf(input, meshes, animations, extras, &error); | ||
|
@@ -843,6 +896,13 @@ int gltfpack(const char* input, const char* output, const char* report, Settings | |
settings.texture_embed = true; | ||
} | ||
|
||
if (oext && (strcmp(oext, ".vrm") == 0 || strcmp(oext, ".VRM") == 0)) | ||
{ | ||
settings.vrm = true; | ||
settings.keep_extras = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to omit these, esp. |
||
settings.quantize = false; | ||
} | ||
|
||
std::string json, bin, fallback; | ||
size_t fallback_size = 0; | ||
process(data, input, output, report, meshes, animations, extras, settings, json, bin, fallback, fallback_size); | ||
|
@@ -896,7 +956,7 @@ int gltfpack(const char* input, const char* output, const char* report, Settings | |
return 4; | ||
} | ||
} | ||
else if (oext && (strcmp(oext, ".glb") == 0 || strcmp(oext, ".GLB") == 0)) | ||
else if (oext && (strcmp(oext, ".vrm") == 0 || strcmp(oext, ".VRM") == 0 || strcmp(oext, ".glb") == 0 || strcmp(oext, ".GLB") == 0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this also be added to the condition above that sets |
||
{ | ||
std::string fbpath = output; | ||
fbpath.replace(fbpath.size() - 4, 4, ".fallback.bin"); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,16 @@ static void fixupIndices(std::vector<unsigned int>& indices, cgltf_primitive_typ | |
} | ||
} | ||
|
||
static void evacuateExtras(cgltf_data* data, std::string& extras, cgltf_extras& item) | ||
{ | ||
size_t offset = extras.size(); | ||
|
||
extras.append(data->json + item.start_offset, item.end_offset - item.start_offset); | ||
|
||
item.start_offset = offset; | ||
item.end_offset = extras.size(); | ||
} | ||
|
||
static void parseMeshesGltf(cgltf_data* data, std::vector<Mesh>& meshes, std::vector<std::pair<size_t, size_t> >& mesh_remap) | ||
{ | ||
size_t total_primitives = 0; | ||
|
@@ -154,7 +164,7 @@ static void parseMeshesGltf(cgltf_data* data, std::vector<Mesh>& meshes, std::ve | |
|
||
for (size_t pi = 0; pi < mesh.primitives_count; ++pi) | ||
{ | ||
const cgltf_primitive& primitive = mesh.primitives[pi]; | ||
cgltf_primitive& primitive = mesh.primitives[pi]; | ||
|
||
if (primitive.type == cgltf_primitive_type_points && primitive.indices) | ||
{ | ||
|
@@ -165,6 +175,8 @@ static void parseMeshesGltf(cgltf_data* data, std::vector<Mesh>& meshes, std::ve | |
meshes.push_back(Mesh()); | ||
Mesh& result = meshes.back(); | ||
|
||
result.name = mesh.name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be a problem with variable lifetime |
||
|
||
result.scene = -1; | ||
|
||
result.material = primitive.material; | ||
|
@@ -244,6 +256,7 @@ static void parseMeshesGltf(cgltf_data* data, std::vector<Mesh>& meshes, std::ve | |
result.targets = primitive.targets_count; | ||
result.target_weights.assign(mesh.weights, mesh.weights + mesh.weights_count); | ||
result.target_names.assign(mesh.target_names, mesh.target_names + mesh.target_names_count); | ||
evacuateExtras(data, result.extras, primitive.extras); | ||
} | ||
|
||
mesh_remap[mi] = std::make_pair(remap_offset, meshes.size()); | ||
|
@@ -378,16 +391,6 @@ static bool needsDummyBuffers(cgltf_data* data) | |
return false; | ||
} | ||
|
||
static void evacuateExtras(cgltf_data* data, std::string& extras, cgltf_extras& item) | ||
{ | ||
size_t offset = extras.size(); | ||
|
||
extras.append(data->json + item.start_offset, item.end_offset - item.start_offset); | ||
|
||
item.start_offset = offset; | ||
item.end_offset = extras.size(); | ||
} | ||
|
||
static void evacuateExtras(cgltf_data* data, std::string& extras) | ||
{ | ||
size_t size = 0; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this necessary? AFAICT the only "extras" node in the .VRM file from the issue is on the mesh, not on primitive, and that contains targetNames which should already be preserved by gltfpack.