Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Unified Diff: chrome/browser/android/vr_shell/gltf_parser.cc

Issue 2757213003: Implementing glTF 1.0 parser (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..04afb6490a7a5eab31ae9756ca29a64b33a75ba8
--- /dev/null
+++ b/chrome/browser/android/vr_shell/gltf_parser.cc
@@ -0,0 +1,250 @@
+// Copyright 2016 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;
+
+namespace gltf {
+
+const std::unordered_map<std::string, Type> kTypeMap = {
+ {"SCALAR", SCALAR}, {"VEC2", VEC2}, {"VEC3", VEC3}, {"VEC4", VEC4},
+ {"MAT2", MAT2}, {"MAT3", MAT3}, {"MAT4", MAT4},
+};
+
+Type GetType(const std::string& type) {
+ auto it = kTypeMap.find(type);
+ CHECK(it != kTypeMap.end());
+ return it->second;
+}
+
+Primitive::Primitive() : indices(nullptr), mode(0) {}
+
+Primitive::~Primitive() = default;
+
+Mesh::Mesh() {}
+
+Mesh::~Mesh() = default;
+
+Node::Node() {}
+
+Node::~Node() = default;
+
+Scene::Scene() {}
+
+Scene::~Scene() = default;
+
+Asset::Asset() : scene_(nullptr) {}
+
+Asset::~Asset() = default;
+
+} // namespace gltf
+
+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");
+
+ asset_ = base::MakeUnique<gltf::Asset>();
+
+ const base::DictionaryValue* sub_dict;
+ CHECK(dict.GetDictionary("buffers", &sub_dict));
+ SetBuffers(*sub_dict);
+ CHECK(dict.GetDictionary("bufferViews", &sub_dict));
+ SetBufferViews(*sub_dict);
+ CHECK(dict.GetDictionary("accessors", &sub_dict));
+ SetAccessors(*sub_dict);
+ CHECK(dict.GetDictionary("meshes", &sub_dict));
+ SetMeshes(*sub_dict);
+ CHECK(dict.GetDictionary("nodes", &sub_dict));
+ SetNodes(*sub_dict);
+ CHECK(dict.GetDictionary("scenes", &sub_dict));
+ SetScenes(*sub_dict);
+ std::string scene_key;
+ CHECK(dict.GetString("scene", &scene_key));
+ auto scene_it = scene_ids_.find(scene_key);
+ CHECK(scene_it != scene_ids_.end());
+ 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));
+ // Only inline data is supported
mthiesse 2017/03/21 16:06:54 nit: add period.
+ CHECK(uri.substr(0, kBase64HeaderSize) == kBase64Header);
+
+ auto buffer = base::MakeUnique<gltf::Buffer>();
+ CHECK(base::Base64Decode(uri.substr(kBase64HeaderSize), buffer.get()));
+
+ int byte_length;
+ CHECK(buffer_dict->GetInteger("byteLength", &byte_length));
mthiesse 2017/03/21 16:06:54 byteLength isn't required by the spec, add a comme
acondor_ 2017/03/21 19:31:29 Actually we don't need it.
+ CHECK(int{buffer->length()} == byte_length);
mthiesse 2017/03/21 16:06:54 static_cast<int>(buffer->length())
acondor_ 2017/03/21 19:31:29 Done.
+
+ 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("byteLength", &buffer_view->byte_length));
mthiesse 2017/03/21 16:06:54 byteLength and target are not required per the spe
acondor_ 2017/03/21 19:31:29 Acknowledged.
+ CHECK(
+ buffer_view_dict->GetInteger("byteOffset", &buffer_view->byte_offset));
+ CHECK(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));
+ CHECK(accessor_dict->GetInteger("byteStride", &accessor->byte_stride));
mthiesse 2017/03/21 16:06:54 Also not required per spec, add comment?
acondor_ 2017/03/21 19:31:29 Acknowledged.
+ 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;
+ CHECK(mesh_dict->GetList("primitives", &list));
+ for (const auto& primitive_value : *list) {
mthiesse 2017/03/21 16:06:54 nit: Move parsing of mesh.primitive into separate
+ const base::DictionaryValue* primitive_dict;
mthiesse 2017/03/21 16:06:54 Add comment that we ignore the required material a
acondor_ 2017/03/21 19:31:29 Done.
+ CHECK(primitive_value->GetAsDictionary(&primitive_dict));
+
+ auto primitive = base::MakeUnique<gltf::Primitive>();
+ std::string indices_key;
+ const base::DictionaryValue* attributes;
+ CHECK(primitive_dict->GetString("indices", &indices_key));
mthiesse 2017/03/21 16:06:54 Add comment, not required per spec.
acondor_ 2017/03/21 19:31:29 Acknowledged.
+ auto accessor_it = accessor_ids_.find(indices_key);
+ CHECK(accessor_it != accessor_ids_.end());
+ primitive->indices = asset_->GetAccessor(accessor_it->second);
+ CHECK(primitive_dict->GetInteger("mode", &primitive->mode));
mthiesse 2017/03/21 16:06:54 Add comment, not required per spec.
acondor_ 2017/03/21 19:31:29 Acknowledged.
+ CHECK(primitive_dict->GetDictionary("attributes", &attributes));
mthiesse 2017/03/21 16:06:54 Attributes isn't required per spec. Do we need it,
acondor_ 2017/03/21 19:31:29 We can handle them. Recall that the only model we
+ for (base::DictionaryValue::Iterator it2(*attributes); !it2.IsAtEnd();
+ it2.Advance()) {
+ std::string accessor_key;
+ CHECK(it2.value().GetAsString(&accessor_key));
+ 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;
+ CHECK(scene_dict->GetList("nodes", &list));
mthiesse 2017/03/21 16:06:54 Not required per spec
acondor_ 2017/03/21 19:31:29 Acknowledged.
+ 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

Powered by Google App Engine
This is Rietveld 408576698