Merge branch 'master' into gltf_mat-support

This commit is contained in:
sabrina-shanman 2019-02-11 17:46:23 -08:00
commit 2c5446dfb5
3 changed files with 69 additions and 14 deletions

View file

@ -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<hfm::Serializer::Factory> 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<ResourceManager>()->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);

View file

@ -709,6 +709,7 @@ public:
private:
GLTFFile _file;
QUrl _url;
QByteArray _glbBinary;
glm::mat4 getModelTransform(const GLTFNode& node);
@ -731,6 +732,8 @@ private:
QVector<double>& values, QMap<QString, bool>& defined);
bool getObjectArrayVal(const QJsonObject& object, const QString& fieldname,
QJsonArray& objects, QMap<QString, bool>& defined);
QByteArray setGLBChunks(const QByteArray& data);
int getMaterialAlphaMode(const QString& type);
int getAccessorType(const QString& type);

View file

@ -2328,7 +2328,7 @@ function createTextureProperty(property, elProperty) {
elInput.setAttribute("id", elementID);
elInput.setAttribute("type", "text");
let imageLoad = _.debounce(function (url) {
let imageLoad = function(url) {
if (url.slice(0, 5).toLowerCase() === "atp:/") {
elImage.src = "";
elImage.style.display = "none";
@ -2348,15 +2348,12 @@ function createTextureProperty(property, elProperty) {
elDiv.classList.remove("no-preview");
elDiv.classList.add("no-texture");
}
}, IMAGE_DEBOUNCE_TIMEOUT);
elInput.imageLoad = imageLoad;
elInput.oninput = function (event) {
// Add throttle
let url = event.target.value;
imageLoad(url);
updateProperty(property.name, url, property.isParticleProperty)
};
elInput.onchange = elInput.oninput;
elInput.imageLoad = imageLoad;
elInput.addEventListener('change', createEmitTextPropertyUpdateFunction(property));
elInput.addEventListener('change', function(ev) {
imageLoad(ev.target.value);
});
elProperty.appendChild(elInput);
elProperty.appendChild(elDiv);