Chromium Code Reviews| Index: chrome/browser/android/vr_shell/gltf_parser.cc |
| diff --git a/chrome/browser/android/vr_shell/gltf_parser.cc b/chrome/browser/android/vr_shell/gltf_parser.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b01b55a83467af656ac35a94f2dd1737c1897ea1 |
| --- /dev/null |
| +++ b/chrome/browser/android/vr_shell/gltf_parser.cc |
| @@ -0,0 +1,220 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/vr_shell/gltf_parser.h" |
| + |
| +#include "base/base64.h" |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| + |
| +namespace vr_shell { |
| + |
| +constexpr char kBase64Header[] = "data:application/octet-stream;base64,"; |
| +constexpr size_t kBase64HeaderSize = 37; |
| + |
| +GltfParser::GltfParser() {} |
| + |
| +GltfParser::~GltfParser() = default; |
| + |
| +std::unique_ptr<gltf::Asset> GltfParser::Parse( |
| + const base::DictionaryValue& dict) { |
| + std::string gltf_version; |
| + CHECK(dict.GetString("asset.version", &gltf_version)); |
| + CHECK(gltf_version == "1.0"); |
|
cjgrant
2017/03/21 16:16:47
General comment for here and many other spots: Fe
acondor_
2017/03/22 16:25:28
Done.
|
| + |
| + asset_ = base::MakeUnique<gltf::Asset>(); |
| + |
| + const base::DictionaryValue* sub_dict; |
| + if (dict.GetDictionary("buffers", &sub_dict)) |
| + SetBuffers(*sub_dict); |
| + if (dict.GetDictionary("bufferViews", &sub_dict)) |
| + SetBufferViews(*sub_dict); |
| + if (dict.GetDictionary("accessors", &sub_dict)) |
| + SetAccessors(*sub_dict); |
| + if (dict.GetDictionary("meshes", &sub_dict)) |
| + SetMeshes(*sub_dict); |
| + if (dict.GetDictionary("nodes", &sub_dict)) |
| + SetNodes(*sub_dict); |
| + if (dict.GetDictionary("scenes", &sub_dict)) |
| + SetScenes(*sub_dict); |
| + |
| + std::string scene_key; |
| + if (dict.GetString("scene", &scene_key)) { |
| + auto scene_it = scene_ids_.find(scene_key); |
| + CHECK(scene_it != scene_ids_.end()); |
|
cjgrant
2017/03/21 16:16:47
Same question as above - anything that could fail
acondor_
2017/03/22 16:25:28
Done.
|
| + asset_->SetScene(asset_->GetScene(scene_it->second)); |
| + } |
| + |
| + return std::move(asset_); |
| +} |
| + |
| +void GltfParser::SetBuffers(const base::DictionaryValue& dict) { |
| + for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| + const base::DictionaryValue* buffer_dict; |
| + CHECK(it.value().GetAsDictionary(&buffer_dict)); |
| + |
| + std::string uri; |
| + CHECK(buffer_dict->GetString("uri", &uri)); |
| + // TODO(acondor): Support files. Only inline data is supported now. |
| + CHECK(uri.substr(0, kBase64HeaderSize) == kBase64Header); |
|
bajones
2017/03/21 15:44:24
This is a smart simplification for a first pass, b
acondor_
2017/03/21 19:31:30
For reference, our current model is < 20k and we a
|
| + |
| + auto buffer = base::MakeUnique<gltf::Buffer>(); |
| + CHECK(base::Base64Decode(uri.substr(kBase64HeaderSize), buffer.get())); |
| + |
| + int byte_length; |
| + CHECK(buffer_dict->GetInteger("byteLength", &byte_length)); |
| + CHECK(int{buffer->length()} == byte_length); |
| + |
| + buffer_ids_[it.key()] = asset_->AddBuffer(std::move(buffer)); |
| + } |
| +} |
| + |
| +void GltfParser::SetBufferViews(const base::DictionaryValue& dict) { |
| + for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| + const base::DictionaryValue* buffer_view_dict; |
| + CHECK(it.value().GetAsDictionary(&buffer_view_dict)); |
| + |
| + auto buffer_view = base::MakeUnique<gltf::BufferView>(); |
| + std::string buffer_key; |
| + CHECK(buffer_view_dict->GetString("buffer", &buffer_key)); |
| + auto buffer_it = buffer_ids_.find(buffer_key); |
| + CHECK(buffer_it != buffer_ids_.end()); |
| + buffer_view->buffer = asset_->GetBuffer(buffer_it->second); |
| + CHECK( |
| + buffer_view_dict->GetInteger("byteOffset", &buffer_view->byte_offset)); |
| + buffer_view_dict->GetInteger("byteLength", &buffer_view->byte_length); |
| + buffer_view_dict->GetInteger("target", &buffer_view->target); |
| + |
| + buffer_view_ids_[it.key()] = asset_->AddBufferView(std::move(buffer_view)); |
| + } |
| +} |
| + |
| +void GltfParser::SetAccessors(const base::DictionaryValue& dict) { |
| + for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| + const base::DictionaryValue* accessor_dict; |
| + CHECK(it.value().GetAsDictionary(&accessor_dict)); |
| + |
| + auto accessor = base::MakeUnique<gltf::Accessor>(); |
| + std::string buffer_view_key; |
| + std::string type_str; |
| + CHECK(accessor_dict->GetString("bufferView", &buffer_view_key)); |
| + auto buffer_view_it = buffer_view_ids_.find(buffer_view_key); |
| + CHECK(buffer_view_it != buffer_view_ids_.end()); |
| + accessor->buffer_view = asset_->GetBufferView(buffer_view_it->second); |
| + CHECK(accessor_dict->GetInteger("byteOffset", &accessor->byte_offset)); |
| + accessor_dict->GetInteger("byteStride", &accessor->byte_stride); |
| + CHECK( |
| + accessor_dict->GetInteger("componentType", &accessor->component_type)); |
| + CHECK(accessor_dict->GetInteger("count", &accessor->count)); |
| + CHECK(accessor_dict->GetString("type", &type_str)); |
| + accessor->type = gltf::GetType(type_str); |
| + |
| + accessor_ids_[it.key()] = asset_->AddAccessor(std::move(accessor)); |
| + } |
| +} |
| + |
| +void GltfParser::SetMeshes(const base::DictionaryValue& dict) { |
| + for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| + const base::DictionaryValue* mesh_dict; |
| + CHECK(it.value().GetAsDictionary(&mesh_dict)); |
| + |
| + auto mesh = base::MakeUnique<gltf::Mesh>(); |
| + const base::ListValue* list; |
| + if (mesh_dict->GetList("primitives", &list)) { |
| + for (const auto& primitive_value : *list) { |
| + const base::DictionaryValue* primitive_dict; |
| + CHECK(primitive_value->GetAsDictionary(&primitive_dict)); |
| + |
| + auto primitive = base::MakeUnique<gltf::Mesh::Primitive>(); |
| + std::string indices_key; |
| + const base::DictionaryValue* attributes; |
| + if (primitive_dict->GetString("indices", &indices_key)) { |
| + auto accessor_it = accessor_ids_.find(indices_key); |
| + CHECK(accessor_it != accessor_ids_.end()); |
| + primitive->indices = asset_->GetAccessor(accessor_it->second); |
| + } |
| + primitive_dict->GetInteger("mode", &primitive->mode); |
| + if (primitive_dict->GetDictionary("attributes", &attributes)) { |
| + for (base::DictionaryValue::Iterator it2(*attributes); !it2.IsAtEnd(); |
| + it2.Advance()) { |
| + std::string accessor_key; |
| + CHECK(it2.value().GetAsString(&accessor_key)); |
| + auto accessor_it = accessor_ids_.find(accessor_key); |
| + CHECK(accessor_it != accessor_ids_.end()); |
| + primitive->attributes[it2.key()] = |
| + asset_->GetAccessor(accessor_it->second); |
| + } |
| + } |
| + |
| + mesh->primitives.push_back(std::move(primitive)); |
| + } |
| + } |
| + |
| + mesh_ids_[it.key()] = asset_->AddMesh(std::move(mesh)); |
| + } |
| +} |
| + |
| +void GltfParser::SetNodes(const base::DictionaryValue& dict) { |
| + std::unordered_map<std::string, gltf::Node*> nodes; |
| + for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| + const base::DictionaryValue* node_dict; |
| + CHECK(it.value().GetAsDictionary(&node_dict)); |
| + |
| + auto node = base::MakeUnique<gltf::Node>(); |
| + const base::ListValue* list; |
| + if (node_dict->GetList("meshes", &list)) { |
| + std::string mesh_key; |
| + for (const auto& mesh_value : *list) { |
| + CHECK(mesh_value->GetAsString(&mesh_key)); |
| + auto mesh_it = mesh_ids_.find(mesh_key); |
| + CHECK(mesh_it != mesh_ids_.end()); |
| + node->meshes.push_back(asset_->GetMesh(mesh_it->second)); |
| + } |
| + } |
| + |
| + nodes[it.key()] = node.get(); |
| + node_ids_[it.key()] = asset_->AddNode(std::move(node)); |
| + } |
| + |
| + // Processing children after all nodes have been added to the asset. |
| + for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| + const base::DictionaryValue* node_dict; |
| + it.value().GetAsDictionary(&node_dict); |
| + |
| + gltf::Node* node = nodes[it.key()]; |
| + const base::ListValue* list; |
| + if (node_dict->GetList("children", &list)) { |
| + std::string node_key; |
| + for (const auto& mesh_value : *list) { |
| + CHECK(mesh_value->GetAsString(&node_key)); |
| + auto node_it = nodes.find(node_key); |
| + CHECK(node_it != nodes.end()); |
| + node->children.push_back(node_it->second); |
| + } |
| + } |
| + } |
| +} |
| + |
| +void GltfParser::SetScenes(const base::DictionaryValue& dict) { |
| + for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
| + const base::DictionaryValue* scene_dict; |
| + CHECK(it.value().GetAsDictionary(&scene_dict)); |
| + |
| + auto scene = base::MakeUnique<gltf::Scene>(); |
| + const base::ListValue* list; |
| + if (scene_dict->GetList("nodes", &list)) { |
| + std::string node_key; |
| + for (const auto& node_value : *list) { |
| + CHECK(node_value->GetAsString(&node_key)); |
| + auto node_it = node_ids_.find(node_key); |
| + CHECK(node_it != node_ids_.end()); |
| + scene->nodes.push_back(asset_->GetNode(node_it->second)); |
| + } |
| + } |
| + |
| + scene_ids_[it.key()] = asset_->AddScene(std::move(scene)); |
| + } |
| +} |
| + |
| +} // namespace vr_shell |