diff --git a/libraries/fbx/src/GLTFSerializer.cpp b/libraries/fbx/src/GLTFSerializer.cpp index 51f721e985..d6de3d4b25 100755 --- a/libraries/fbx/src/GLTFSerializer.cpp +++ b/libraries/fbx/src/GLTFSerializer.cpp @@ -124,6 +124,31 @@ bool GLTFSerializer::getObjectArrayVal(const QJsonObject& object, const QString& return _defined; } +QByteArray GLTFSerializer::setGLBChunks(const QByteArray& data) { + int byte = 4; + int jsonStart = data.indexOf("JSON", Qt::CaseSensitive); + int binStart = data.indexOf("BIN", Qt::CaseSensitive); + int jsonLength, binLength; + QByteArray jsonLengthChunk, binLengthChunk; + + jsonLengthChunk = data.mid(jsonStart - byte, byte); + QDataStream tempJsonLen(jsonLengthChunk); + tempJsonLen.setByteOrder(QDataStream::LittleEndian); + tempJsonLen >> jsonLength; + QByteArray jsonChunk = data.mid(jsonStart + byte, jsonLength); + + if (binStart != -1) { + binLengthChunk = data.mid(binStart - byte, byte); + + QDataStream tempBinLen(binLengthChunk); + tempBinLen.setByteOrder(QDataStream::LittleEndian); + tempBinLen >> binLength; + + _glbBinary = data.mid(binStart + byte, binLength); + } + return jsonChunk; +} + int GLTFSerializer::getMeshPrimitiveRenderingMode(const QString& type) { if (type == "POINTS") { @@ -309,6 +334,14 @@ bool GLTFSerializer::addBuffer(const QJsonObject& object) { GLTFBuffer buffer; getIntVal(object, "byteLength", buffer.byteLength, buffer.defined); + + if (_url.toString().endsWith("glb")) { + if (!_glbBinary.isEmpty()) { + buffer.blob = _glbBinary; + } else { + return false; + } + } if (getStringVal(object, "uri", buffer.uri, buffer.defined)) { if (!readBinary(buffer.uri, buffer.blob)) { return false; @@ -535,9 +568,16 @@ bool GLTFSerializer::addTexture(const QJsonObject& object) { bool GLTFSerializer::parseGLTF(const QByteArray& data) { PROFILE_RANGE_EX(resource_parse, __FUNCTION__, 0xffff0000, nullptr); - - QJsonDocument d = QJsonDocument::fromJson(data); + + QByteArray jsonChunk = data; + + if (_url.toString().endsWith("glb") && data.indexOf("glTF") == 0 && data.contains("JSON")) { + jsonChunk = setGLBChunks(data); + } + + QJsonDocument d = QJsonDocument::fromJson(jsonChunk); QJsonObject jsFile = d.object(); + bool success = setAsset(jsFile); if (success) { QJsonArray accessors; @@ -924,6 +964,10 @@ MediaType GLTFSerializer::getMediaType() const { MediaType mediaType("gltf"); mediaType.extensions.push_back("gltf"); mediaType.webMediaTypes.push_back("model/gltf+json"); + + mediaType.extensions.push_back("glb"); + mediaType.webMediaTypes.push_back("model/gltf-binary"); + return mediaType; } @@ -932,9 +976,9 @@ std::unique_ptr GLTFSerializer::getFactory() const { } HFMModel::Pointer GLTFSerializer::read(const QByteArray& data, const QVariantHash& mapping, const QUrl& url) { - - _url = url; + _url = url; + // Normalize url for local files QUrl normalizeUrl = DependencyManager::get()->normalizeURL(_url); if (normalizeUrl.scheme().isEmpty() || (normalizeUrl.scheme() == "file")) { @@ -1032,7 +1076,7 @@ QNetworkReply* GLTFSerializer::request(QUrl& url, bool isTest) { HFMTexture GLTFSerializer::getHFMTexture(const GLTFTexture& texture) { HFMTexture fbxtex = HFMTexture(); fbxtex.texcoordSet = 0; - + if (texture.defined["source"]) { QString url = _file.images[texture.source].uri; @@ -1041,6 +1085,17 @@ HFMTexture GLTFSerializer::getHFMTexture(const GLTFTexture& texture) { qCDebug(modelformat) << "fname: " << fname; fbxtex.name = fname; fbxtex.filename = textureUrl.toEncoded(); + + if (_url.toString().endsWith("glb") && !_glbBinary.isEmpty()) { + int bufferView = _file.images[texture.source].bufferView; + + GLTFBufferView& imagesBufferview = _file.bufferviews[bufferView]; + int offset = imagesBufferview.byteOffset; + int length = imagesBufferview.byteLength; + + fbxtex.content = _glbBinary.mid(offset, length); + fbxtex.filename = textureUrl.toEncoded().append(texture.source); + } if (url.contains("data:image/jpeg;base64,") || url.contains("data:image/png;base64,")) { fbxtex.content = requestEmbeddedData(url); diff --git a/libraries/fbx/src/GLTFSerializer.h b/libraries/fbx/src/GLTFSerializer.h index 57ea126a7b..a361e09fa6 100755 --- a/libraries/fbx/src/GLTFSerializer.h +++ b/libraries/fbx/src/GLTFSerializer.h @@ -709,6 +709,7 @@ public: private: GLTFFile _file; QUrl _url; + QByteArray _glbBinary; glm::mat4 getModelTransform(const GLTFNode& node); @@ -731,6 +732,8 @@ private: QVector& values, QMap& defined); bool getObjectArrayVal(const QJsonObject& object, const QString& fieldname, QJsonArray& objects, QMap& defined); + + QByteArray setGLBChunks(const QByteArray& data); int getMaterialAlphaMode(const QString& type); int getAccessorType(const QString& type);