fix compile.

This commit is contained in:
Syoyo Fujita
2025-08-23 08:52:42 +09:00
parent 93150cc189
commit 14ec29a86a
9 changed files with 164 additions and 73 deletions

View File

@@ -434,6 +434,7 @@ set(TINYUSDZ_SOURCES
${PROJECT_SOURCE_DIR}/src/uuid-gen.cc
${PROJECT_SOURCE_DIR}/src/uuid-gen.hh
${PROJECT_SOURCE_DIR}/src/parser-timing.cc
${PROJECT_SOURCE_DIR}/src/sha256.cc
)
@@ -508,11 +509,13 @@ if(TINYUSDZ_WITH_USDMTLX)
endif()
if(TINYUSDZ_WITH_JSON)
list(APPEND TINYUSDZ_SOURCES
${PROJECT_SOURCE_DIR}/src/json-to-usd.cc
${PROJECT_SOURCE_DIR}/src/usd-to-json.cc
${PROJECT_SOURCE_DIR}/src/json-writer.cc
${PROJECT_SOURCE_DIR}/src/json-util.cc)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/external/yyjson.c)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/json-to-usd.cc)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/usd-to-json.cc)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/json-writer.cc)
list(APPEND TINYUSDZ_DEP_SOURCES ${PROJECT_SOURCE_DIR}/src/json-util.cc)
endif()
if(TINYUSDZ_WITH_USDFBX)

View File

@@ -225,21 +225,24 @@ bool DeserializeArrayFromBase64(const std::string& base64_data, std::vector<T>*
return true;
}
#if 0
// Specialized versions for different types
bool DeserializeIntArrayFromBase64(const std::string& base64_data, std::vector<int>* result) {
static bool DeserializeIntArrayFromBase64(const std::string& base64_data, std::vector<int>* result) {
return DeserializeArrayFromBase64(base64_data, result);
}
#endif
static bool DeserializeFloatArrayFromBase64(const std::string& base64_data, std::vector<float>* result) {
return DeserializeArrayFromBase64(base64_data, result);
}
bool DeserializeFloatArrayFromBase64(const std::string& base64_data, std::vector<float>* result) {
return DeserializeArrayFromBase64(base64_data, result);
}
bool DeserializeDoubleArrayFromBase64(const std::string& base64_data, std::vector<double>* result) {
#if 0 // TODO
static bool DeserializeDoubleArrayFromBase64(const std::string& base64_data, std::vector<double>* result) {
return DeserializeArrayFromBase64(base64_data, result);
}
// Vector deserialization helpers for integer types
bool DeserializeInt2ArrayFromBase64(const std::string& base64_data, std::vector<value::int2>* result) {
static bool DeserializeInt2ArrayFromBase64(const std::string& base64_data, std::vector<value::int2>* result) {
if (!result) {
return false;
}
@@ -264,7 +267,7 @@ bool DeserializeInt2ArrayFromBase64(const std::string& base64_data, std::vector<
return true;
}
bool DeserializeInt3ArrayFromBase64(const std::string& base64_data, std::vector<value::int3>* result) {
static bool DeserializeInt3ArrayFromBase64(const std::string& base64_data, std::vector<value::int3>* result) {
if (!result) {
return false;
}
@@ -290,7 +293,7 @@ bool DeserializeInt3ArrayFromBase64(const std::string& base64_data, std::vector<
return true;
}
bool DeserializeInt4ArrayFromBase64(const std::string& base64_data, std::vector<value::int4>* result) {
static bool DeserializeInt4ArrayFromBase64(const std::string& base64_data, std::vector<value::int4>* result) {
if (!result) {
return false;
}
@@ -318,7 +321,7 @@ bool DeserializeInt4ArrayFromBase64(const std::string& base64_data, std::vector<
}
// Vector deserialization helpers for float types
bool DeserializeFloat2ArrayFromBase64(const std::string& base64_data, std::vector<value::float2>* result) {
static bool DeserializeFloat2ArrayFromBase64(const std::string& base64_data, std::vector<value::float2>* result) {
if (!result) {
return false;
}
@@ -343,7 +346,7 @@ bool DeserializeFloat2ArrayFromBase64(const std::string& base64_data, std::vecto
return true;
}
bool DeserializeFloat4ArrayFromBase64(const std::string& base64_data, std::vector<value::float4>* result) {
static bool DeserializeFloat4ArrayFromBase64(const std::string& base64_data, std::vector<value::float4>* result) {
if (!result) {
return false;
}
@@ -371,7 +374,7 @@ bool DeserializeFloat4ArrayFromBase64(const std::string& base64_data, std::vecto
}
// Vector deserialization helpers for half types
bool DeserializeHalf2ArrayFromBase64(const std::string& base64_data, std::vector<value::half2>* result) {
static bool DeserializeHalf2ArrayFromBase64(const std::string& base64_data, std::vector<value::half2>* result) {
if (!result) {
return false;
}
@@ -396,7 +399,7 @@ bool DeserializeHalf2ArrayFromBase64(const std::string& base64_data, std::vector
return true;
}
bool DeserializeHalf3ArrayFromBase64(const std::string& base64_data, std::vector<value::half3>* result) {
static bool DeserializeHalf3ArrayFromBase64(const std::string& base64_data, std::vector<value::half3>* result) {
if (!result) {
return false;
}
@@ -422,7 +425,7 @@ bool DeserializeHalf3ArrayFromBase64(const std::string& base64_data, std::vector
return true;
}
bool DeserializeHalf4ArrayFromBase64(const std::string& base64_data, std::vector<value::half4>* result) {
static bool DeserializeHalf4ArrayFromBase64(const std::string& base64_data, std::vector<value::half4>* result) {
if (!result) {
return false;
}
@@ -448,9 +451,11 @@ bool DeserializeHalf4ArrayFromBase64(const std::string& base64_data, std::vector
return true;
}
#endif
// Attribute metadata deserialization
bool DeserializeAttributeMetadata(const nlohmann::json& metadata_json, AttrMetas* metas, std::string* err = nullptr) {
static bool DeserializeAttributeMetadata(const nlohmann::json& metadata_json, AttrMetas* metas, std::string* err = nullptr) {
(void)err;
if (!metas || !metadata_json.is_object()) {
return false;
}
@@ -500,7 +505,7 @@ bool DeserializeAttributeMetadata(const nlohmann::json& metadata_json, AttrMetas
}
// Helper function to parse array data from JSON (supports both base64 and accessor modes)
bool ParseArrayFromJSON(const nlohmann::json& j, JSONToUSDContext* context, std::string* base64_data,
static bool ParseArrayFromJSON(const nlohmann::json& j, JSONToUSDContext* context, std::string* base64_data,
size_t* accessor_index, size_t* count, std::string* type, std::string* err) {
if (!j.is_object()) {
if (err) {
@@ -575,7 +580,7 @@ bool ParseArrayFromJSON(const nlohmann::json& j, JSONToUSDContext* context, std:
// Template helper for parsing and deserializing arrays
template<typename T>
bool ParseAndDeserializeArray(const nlohmann::json& array_json, JSONToUSDContext* context,
static bool ParseAndDeserializeArray(const nlohmann::json& array_json, JSONToUSDContext* context,
const std::string& expected_type, std::vector<T>* result, std::string* err) {
std::string base64_data, type;
size_t accessor_index, count;
@@ -636,7 +641,7 @@ bool ParseAndDeserializeArrayWithMetadata(const nlohmann::json& array_json, JSON
}
// Specialized metadata-aware parsing for point3f arrays
bool ParsePoint3fArrayWithMetadata(const nlohmann::json& array_json, JSONToUSDContext* context,
static bool ParsePoint3fArrayWithMetadata(const nlohmann::json& array_json, JSONToUSDContext* context,
std::vector<value::point3f>* result, AttrMetas* metas, std::string* err) {
if (!array_json.is_object()) {
if (err) (*err) = "Points array must be an object";
@@ -691,7 +696,7 @@ bool ParsePoint3fArrayWithMetadata(const nlohmann::json& array_json, JSONToUSDCo
}
// Specialized metadata-aware parsing for normal3f arrays
bool ParseNormal3fArrayWithMetadata(const nlohmann::json& array_json, JSONToUSDContext* context,
static bool ParseNormal3fArrayWithMetadata(const nlohmann::json& array_json, JSONToUSDContext* context,
std::vector<value::normal3f>* result, AttrMetas* metas, std::string* err) {
if (!array_json.is_object()) {
if (err) (*err) = "Normals array must be an object";
@@ -727,7 +732,9 @@ bool ParseNormal3fArrayWithMetadata(const nlohmann::json& array_json, JSONToUSDC
}
// JSON to GeomMesh conversion (with context support)
bool JSONToGeomMesh(const nlohmann::json& j, GeomMesh* mesh, JSONToUSDContext* context, std::string* warn, std::string* err) {
static bool JSONToGeomMesh(const nlohmann::json& j, GeomMesh* mesh, JSONToUSDContext* context, std::string* warn, std::string* err) {
(void)warn;
if (!mesh) {
if (err) {
(*err) = "Internal error: mesh is null";
@@ -846,12 +853,10 @@ bool JSONToGeomMesh(const nlohmann::json& j, GeomMesh* mesh, JSONToUSDContext* c
} // namespace detail
// Original JSONToGeomMesh without context (for backward compatibility)
bool JSONToGeomMesh(const nlohmann::json& j, GeomMesh* mesh, std::string* warn, std::string* err) {
return detail::JSONToGeomMesh(j, mesh, nullptr, warn, err);
}
#if 0
static bool ParseStringArray(const nlohmann::json &j, std::vector<std::string> *result, std::string *warn, std::string *err) {
(void)warn;
bool ParseStringArray(const nlohmann::json &j, std::vector<std::string> *result, std::string *warn, std::string *err) {
if (!result) {
if (err) {
(*err) = "Internal error.";
@@ -885,7 +890,10 @@ bool ParseStringArray(const nlohmann::json &j, std::vector<std::string> *result,
return true;
}
bool JSONToPropertyImpl(const nlohmann::json &j, PrimSpec *ps, std::string *warn, std::string *err) {
static bool JSONToPropertyImpl(const nlohmann::json &j, PrimSpec *ps, std::string *warn, std::string *err) {
(void)warn;
(void)ps;
// TODO:
// [ ] timeSamples
// [ ] None
@@ -902,8 +910,13 @@ bool JSONToPropertyImpl(const nlohmann::json &j, PrimSpec *ps, std::string *warn
return true;
}
#endif
static bool JSONToPrimSpecImpl(const nlohmann::json &j, PrimSpec *ps, std::string *warn, std::string *err) {
(void)err;
(void)warn;
(void)ps;
bool JSONToPrimSpecImpl(const nlohmann::json &j, PrimSpec *ps, std::string *warn, std::string *err) {
if (j.contains("metadata")) {
nlohmann::json meta = j["metadata"];
@@ -1132,4 +1145,4 @@ bool JSONToGeomMesh(const std::string &j_str, GeomMesh *mesh, std::string *warn,
return detail::JSONToGeomMesh(j, mesh, nullptr, warn, err);
}
} // namespace tinyusdz
} // namespace tinyusdz

View File

@@ -28,15 +28,18 @@ namespace json {
namespace detail {
#if 0
inline std::string dtos(const double v) {
char buf[64];
dtoa_milo(v, buf);
return std::string(buf);
}
#endif
bool WriteInt(const int value, std::string *out_json) {
#if 0
static bool WriteInt(const int value, std::string *out_json) {
if (!out_json) return false;
(*out_json) = detail::dtos(double(value));
@@ -45,7 +48,7 @@ bool WriteInt(const int value, std::string *out_json) {
}
bool WriteString(const std::string &str, std::string *out_json) {
static bool WriteString(const std::string &str, std::string *out_json) {
if (!out_json) return false;
// Escape quotes and backslashes
@@ -63,7 +66,7 @@ bool WriteString(const std::string &str, std::string *out_json) {
// Base122 encoding using 122 printable ASCII characters
// Excludes: " (34), \ (92), DEL (127), and non-printable control characters
std::string EncodeBase122(const std::vector<uint8_t> &data) {
static std::string EncodeBase122(const std::vector<uint8_t> &data) {
// Base122 character set (122 printable ASCII characters)
static const char kBase122Chars[] =
"!#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`"
@@ -111,7 +114,7 @@ std::string EncodeBase122(const std::vector<uint8_t> &data) {
}
// Base64 encoding using standard base64 character set
std::string EncodeBase64(const std::vector<uint8_t> &data) {
static std::string EncodeBase64(const std::vector<uint8_t> &data) {
static const char kBase64Chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -127,9 +130,9 @@ std::string EncodeBase64(const std::vector<uint8_t> &data) {
int padding = 0;
// Pack 3 bytes into 24 bits
buffer = (data[i] << 16);
buffer = (uint32_t(data[i]) << 16);
if (i + 1 < data.size()) {
buffer |= (data[i + 1] << 8);
buffer |= (uint32_t(data[i + 1]) << 8);
} else {
padding++;
}
@@ -148,11 +151,15 @@ std::string EncodeBase64(const std::vector<uint8_t> &data) {
return result;
}
#endif
} // namespace detal
bool JsonWriter::to_json(const tinyusdz::Layer &layer, std::string *out_json) {
(void)layer;
(void)out_json;
// TODO
return false;
}

View File

@@ -128,7 +128,7 @@ std::string sha256(const char *binary, size_t size) {
}
for (int i = 0; i < 8; i++) {
msg[new_len + i] = static_cast<uint8_t>((bit_len >> (56 - i * 8)) & 0xff);
msg[new_len + size_t(i)] = static_cast<uint8_t>((bit_len >> (56 - i * 8)) & 0xff);
}
for (size_t i = 0; i < new_len + 8; i += 64) {
@@ -145,4 +145,4 @@ std::string sha256(const char *binary, size_t size) {
return ss.str();
}
} // namespace tinyusdz
} // namespace tinyusdz

View File

@@ -7,4 +7,5 @@ namespace tinyusdz {
std::string sha256(const char *binary, size_t size);
} // namespace tinyusdz
} // namespace tinyusdz

View File

@@ -86,19 +86,24 @@ std::string SerializeArrayToBase64(const std::vector<T>& array) {
return base64_encode(bytes, static_cast<unsigned int>(byte_size));
}
#if 0
// Specialized versions for different types
std::string SerializeIntArrayToBase64(const std::vector<int>& array) {
return SerializeArrayToBase64(array);
}
#endif
std::string SerializeFloatArrayToBase64(const std::vector<float>& array) {
return SerializeArrayToBase64(array);
}
#if 0
std::string SerializeDoubleArrayToBase64(const std::vector<double>& array) {
return SerializeArrayToBase64(array);
}
#endif
#if 0
// Helper functions for mixed-mode serialization
template<typename T>
json SerializeArrayData(const std::vector<T>& array, USDToJSONContext* context,
@@ -133,6 +138,7 @@ json SerializeArrayData(const std::vector<T>& array, USDToJSONContext* context,
};
}
}
#endif
// Helper function to serialize attribute metadata
json SerializeAttributeMetadata(const AttrMetas& metas) {
@@ -156,7 +162,8 @@ json SerializeAttributeMetadata(const AttrMetas& metas) {
case Interpolation::FaceVarying:
metadata["interpolation"] = "faceVarying";
break;
default:
case Interpolation::Invalid:
metadata["interpolation"] = "[[invalid]]";
break;
}
}
@@ -254,6 +261,7 @@ json SerializeAttributeMetadata(const AttrMetas& metas) {
return metadata;
}
#if 0
// Specialized array serialization functions
json SerializeIntArray(const std::vector<int>& array, USDToJSONContext* context = nullptr) {
return SerializeArrayData(array, context, "UNSIGNED_INT", "SCALAR");
@@ -266,7 +274,9 @@ json SerializeFloatArray(const std::vector<float>& array, USDToJSONContext* cont
json SerializeDoubleArray(const std::vector<double>& array, USDToJSONContext* context = nullptr) {
return SerializeArrayData(array, context, "FLOAT", "SCALAR"); // Note: JSON doesn't distinguish float/double
}
#endif
#if 0
// Overloaded functions with attribute metadata support
template<typename T>
json SerializeArrayDataWithMetadata(const std::vector<T>& array, const AttrMetas* metas, USDToJSONContext* context,
@@ -283,16 +293,22 @@ json SerializeArrayDataWithMetadata(const std::vector<T>& array, const AttrMetas
return result;
}
#endif
#if 0
// Metadata-aware array serialization functions
json SerializeIntArrayWithMetadata(const std::vector<int>& array, const AttrMetas* metas = nullptr, USDToJSONContext* context = nullptr) {
return SerializeArrayDataWithMetadata(array, metas, context, "UNSIGNED_INT", "SCALAR");
}
#endif
#if 0
json SerializeFloatArrayWithMetadata(const std::vector<float>& array, const AttrMetas* metas = nullptr, USDToJSONContext* context = nullptr) {
return SerializeArrayDataWithMetadata(array, metas, context, "FLOAT", "SCALAR");
}
#endif
#if 0
json SerializeDoubleArrayWithMetadata(const std::vector<double>& array, const AttrMetas* metas = nullptr, USDToJSONContext* context = nullptr) {
return SerializeArrayDataWithMetadata(array, metas, context, "FLOAT", "SCALAR");
}
@@ -680,6 +696,7 @@ json SerializeHalf4Array(const std::vector<value::half4>& vectors, USDToJSONCont
};
}
}
#endif
// Metadata-aware vector serialization functions
json SerializePoint3fArrayWithMetadata(const std::vector<value::point3f>& points, const AttrMetas* metas = nullptr, USDToJSONContext* context = nullptr) {
@@ -731,6 +748,7 @@ json SerializePoint3fArrayWithMetadata(const std::vector<value::point3f>& points
return result;
}
#if 0
json SerializeNormal3fArrayWithMetadata(const std::vector<value::normal3f>& normals, const AttrMetas* metas = nullptr, USDToJSONContext* context = nullptr) {
if (normals.empty()) {
return json::object();
@@ -779,7 +797,9 @@ json SerializeNormal3fArrayWithMetadata(const std::vector<value::normal3f>& norm
return result;
}
#endif
#if 0
// Matrix array serialization
template<typename MatrixType>
std::string SerializeMatrixArrayToBase64(const std::vector<MatrixType>& array) {
@@ -888,6 +908,7 @@ std::string SerializeMatrix4dArrayToBase64(const std::vector<value::matrix4d>& a
}
return SerializeDoubleArrayToBase64(double_data);
}
#endif
json ToJSON(tinyusdz::Xform& xform) {
json j;
@@ -913,6 +934,7 @@ json ToJSON(tinyusdz::GeomMesh& mesh) {
return ToJSON(mesh, nullptr); // Use base64 mode by default
}
#if 0
json ToJSON(tinyusdz::GeomMesh& mesh, USDToJSONContext* context) {
json j;
@@ -1018,10 +1040,30 @@ json ToJSON(tinyusdz::GeomMesh& mesh, USDToJSONContext* context) {
return j;
}
#endif
json ToJSON(tinyusdz::GeomBasisCurves& curves) {
json j;
j["name"] = curves.name;
j["typeName"] = "GeomBasisCurves";
// TODO
USDToJSONContext *context = nullptr;
// Points array (point3f[])
if (curves.points.authored()) {
auto points_opt = curves.points.get_value();
if (points_opt) {
std::vector<value::point3f> points_data;
if (points_opt.value().get(value::TimeCode::Default(), &points_data)) {
j["points"] = SerializePoint3fArrayWithMetadata(points_data, &curves.points.metas(), context);
}
}
}
// TODO: Serialize other attribs
return j;
}
@@ -1135,6 +1177,11 @@ json SerializeContextToJSON(const USDToJSONContext& context) {
return j;
}
} // namespace
json ToJSON(const tinyusdz::Layer& layer) {
USDToJSONContext context; // Default context (base64 mode)
return ToJSON(layer, context);
@@ -1151,7 +1198,6 @@ json ToJSON(const tinyusdz::Layer& layer, USDToJSONContext& context) {
const LayerMetas& metas = layer.metas();
json layerMetas;
// Basic layer properties
if (metas.upAxis.authored()) {
layerMetas["upAxis"] = to_string(metas.upAxis.get_value());
@@ -1282,9 +1328,6 @@ json ToJSON(const tinyusdz::Layer& layer, USDToJSONContext& context) {
return j;
}
} // namespace
nonstd::expected<std::string, std::string> ToJSON(
const tinyusdz::Stage& stage) {
json j; // root
@@ -1325,4 +1368,31 @@ nonstd::expected<std::string, std::string> ToJSON(
return str;
}
bool to_json_string(const tinyusdz::Layer &layer, std::string *json_str, std::string *warn, std::string *err) {
if (!json_str) {
return false;
}
(void)warn;
(void)err;
USDToJSONContext context; // Default context (base64 mode)
json j = ToJSON(layer, context);
(*json_str) = j.dump();
return true;
}
bool to_json_string(const tinyusdz::Layer &layer, const USDToJSONOptions& options, std::string *json_str, std::string *warn, std::string *err) {
// TODO: options
(void)options;
return to_json_string(layer, json_str, warn, err);
}
} // namespace tinyusdz

View File

@@ -130,4 +130,4 @@ bool to_json_string(const tinyusdz::Layer &layer, const USDToJSONOptions& option
///
nlohmann::json ToJSON(tinyusdz::GeomMesh& mesh, USDToJSONContext* context);
} // namespace tinyusd
} // namespace tinyusdz

View File

@@ -1395,12 +1395,8 @@ class TinyUSDZLoaderNative {
const tinyusdz::Layer &curr = composited_ ? composed_layer_ : layer_;
try {
nlohmann::json json_obj = tinyusdz::ToJSON(curr);
return json_obj.dump(2); // Pretty print with 2 spaces
} catch (const std::exception& e) {
return "{\"error\": \"Failed to convert layer to JSON: " + std::string(e.what()) + "\"}";
}
nlohmann::json json_obj = tinyusdz::ToJSON(curr);
return json_obj.dump(2); // Pretty print with 2 spaces
}
std::string layerToJSONWithOptions(bool embedBuffers, const std::string& arrayMode) const {
@@ -1410,27 +1406,23 @@ class TinyUSDZLoaderNative {
const tinyusdz::Layer &curr = composited_ ? composed_layer_ : layer_;
try {
tinyusdz::USDToJSONOptions options;
options.embedBuffers = embedBuffers;
if (arrayMode == "buffer") {
options.arrayMode = tinyusdz::ArraySerializationMode::Buffer;
} else {
options.arrayMode = tinyusdz::ArraySerializationMode::Base64;
}
std::string json_str, warn, err;
bool success = tinyusdz::to_json_string(curr, options, &json_str, &warn, &err);
if (!success) {
return "{\"error\": \"Failed to convert layer to JSON: " + err + "\"}";
}
return json_str;
} catch (const std::exception& e) {
return "{\"error\": \"Failed to convert layer to JSON: " + std::string(e.what()) + "\"}";
tinyusdz::USDToJSONOptions options;
options.embedBuffers = embedBuffers;
if (arrayMode == "buffer") {
options.arrayMode = tinyusdz::ArraySerializationMode::Buffer;
} else {
options.arrayMode = tinyusdz::ArraySerializationMode::Base64;
}
std::string json_str, warn, err;
bool success = tinyusdz::to_json_string(curr, options, &json_str, &warn, &err);
if (!success) {
return "{\"error\": \"Failed to convert layer to JSON: " + err + "\"}";
}
return json_str;
}
bool loadLayerFromJSON(const std::string& json_string) {

5
web/bootstrap-linux-wasm64.sh Executable file
View File

@@ -0,0 +1,5 @@
builddir=build_64
rm -rf ${builddir}
mkdir ${builddir}
emcmake cmake -DCMAKE_BUILD_TYPE=MinSizeRel -DCMAKE_VERBOSE_MAKEFILE=1 -DTINYUSDZ_WASM64=1 -B${builddir}