OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/android/vr_shell/gltf_parser.h" | |
6 | |
7 #include "base/base64.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/ptr_util.h" | |
10 | |
11 namespace vr_shell { | |
12 | |
13 constexpr char kBase64Header[] = "data:application/octet-stream;base64,"; | |
14 constexpr size_t kBase64HeaderSize = 37; | |
15 | |
16 GltfParser::GltfParser() {} | |
17 | |
18 GltfParser::~GltfParser() = default; | |
19 | |
20 std::unique_ptr<gltf::Asset> GltfParser::Parse( | |
21 const base::DictionaryValue& dict) { | |
22 std::string gltf_version; | |
23 CHECK(dict.GetString("asset.version", &gltf_version)); | |
24 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.
| |
25 | |
26 asset_ = base::MakeUnique<gltf::Asset>(); | |
27 | |
28 const base::DictionaryValue* sub_dict; | |
29 if (dict.GetDictionary("buffers", &sub_dict)) | |
30 SetBuffers(*sub_dict); | |
31 if (dict.GetDictionary("bufferViews", &sub_dict)) | |
32 SetBufferViews(*sub_dict); | |
33 if (dict.GetDictionary("accessors", &sub_dict)) | |
34 SetAccessors(*sub_dict); | |
35 if (dict.GetDictionary("meshes", &sub_dict)) | |
36 SetMeshes(*sub_dict); | |
37 if (dict.GetDictionary("nodes", &sub_dict)) | |
38 SetNodes(*sub_dict); | |
39 if (dict.GetDictionary("scenes", &sub_dict)) | |
40 SetScenes(*sub_dict); | |
41 | |
42 std::string scene_key; | |
43 if (dict.GetString("scene", &scene_key)) { | |
44 auto scene_it = scene_ids_.find(scene_key); | |
45 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.
| |
46 asset_->SetScene(asset_->GetScene(scene_it->second)); | |
47 } | |
48 | |
49 return std::move(asset_); | |
50 } | |
51 | |
52 void GltfParser::SetBuffers(const base::DictionaryValue& dict) { | |
53 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | |
54 const base::DictionaryValue* buffer_dict; | |
55 CHECK(it.value().GetAsDictionary(&buffer_dict)); | |
56 | |
57 std::string uri; | |
58 CHECK(buffer_dict->GetString("uri", &uri)); | |
59 // TODO(acondor): Support files. Only inline data is supported now. | |
60 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
| |
61 | |
62 auto buffer = base::MakeUnique<gltf::Buffer>(); | |
63 CHECK(base::Base64Decode(uri.substr(kBase64HeaderSize), buffer.get())); | |
64 | |
65 int byte_length; | |
66 CHECK(buffer_dict->GetInteger("byteLength", &byte_length)); | |
67 CHECK(int{buffer->length()} == byte_length); | |
68 | |
69 buffer_ids_[it.key()] = asset_->AddBuffer(std::move(buffer)); | |
70 } | |
71 } | |
72 | |
73 void GltfParser::SetBufferViews(const base::DictionaryValue& dict) { | |
74 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | |
75 const base::DictionaryValue* buffer_view_dict; | |
76 CHECK(it.value().GetAsDictionary(&buffer_view_dict)); | |
77 | |
78 auto buffer_view = base::MakeUnique<gltf::BufferView>(); | |
79 std::string buffer_key; | |
80 CHECK(buffer_view_dict->GetString("buffer", &buffer_key)); | |
81 auto buffer_it = buffer_ids_.find(buffer_key); | |
82 CHECK(buffer_it != buffer_ids_.end()); | |
83 buffer_view->buffer = asset_->GetBuffer(buffer_it->second); | |
84 CHECK( | |
85 buffer_view_dict->GetInteger("byteOffset", &buffer_view->byte_offset)); | |
86 buffer_view_dict->GetInteger("byteLength", &buffer_view->byte_length); | |
87 buffer_view_dict->GetInteger("target", &buffer_view->target); | |
88 | |
89 buffer_view_ids_[it.key()] = asset_->AddBufferView(std::move(buffer_view)); | |
90 } | |
91 } | |
92 | |
93 void GltfParser::SetAccessors(const base::DictionaryValue& dict) { | |
94 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | |
95 const base::DictionaryValue* accessor_dict; | |
96 CHECK(it.value().GetAsDictionary(&accessor_dict)); | |
97 | |
98 auto accessor = base::MakeUnique<gltf::Accessor>(); | |
99 std::string buffer_view_key; | |
100 std::string type_str; | |
101 CHECK(accessor_dict->GetString("bufferView", &buffer_view_key)); | |
102 auto buffer_view_it = buffer_view_ids_.find(buffer_view_key); | |
103 CHECK(buffer_view_it != buffer_view_ids_.end()); | |
104 accessor->buffer_view = asset_->GetBufferView(buffer_view_it->second); | |
105 CHECK(accessor_dict->GetInteger("byteOffset", &accessor->byte_offset)); | |
106 accessor_dict->GetInteger("byteStride", &accessor->byte_stride); | |
107 CHECK( | |
108 accessor_dict->GetInteger("componentType", &accessor->component_type)); | |
109 CHECK(accessor_dict->GetInteger("count", &accessor->count)); | |
110 CHECK(accessor_dict->GetString("type", &type_str)); | |
111 accessor->type = gltf::GetType(type_str); | |
112 | |
113 accessor_ids_[it.key()] = asset_->AddAccessor(std::move(accessor)); | |
114 } | |
115 } | |
116 | |
117 void GltfParser::SetMeshes(const base::DictionaryValue& dict) { | |
118 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | |
119 const base::DictionaryValue* mesh_dict; | |
120 CHECK(it.value().GetAsDictionary(&mesh_dict)); | |
121 | |
122 auto mesh = base::MakeUnique<gltf::Mesh>(); | |
123 const base::ListValue* list; | |
124 if (mesh_dict->GetList("primitives", &list)) { | |
125 for (const auto& primitive_value : *list) { | |
126 const base::DictionaryValue* primitive_dict; | |
127 CHECK(primitive_value->GetAsDictionary(&primitive_dict)); | |
128 | |
129 auto primitive = base::MakeUnique<gltf::Mesh::Primitive>(); | |
130 std::string indices_key; | |
131 const base::DictionaryValue* attributes; | |
132 if (primitive_dict->GetString("indices", &indices_key)) { | |
133 auto accessor_it = accessor_ids_.find(indices_key); | |
134 CHECK(accessor_it != accessor_ids_.end()); | |
135 primitive->indices = asset_->GetAccessor(accessor_it->second); | |
136 } | |
137 primitive_dict->GetInteger("mode", &primitive->mode); | |
138 if (primitive_dict->GetDictionary("attributes", &attributes)) { | |
139 for (base::DictionaryValue::Iterator it2(*attributes); !it2.IsAtEnd(); | |
140 it2.Advance()) { | |
141 std::string accessor_key; | |
142 CHECK(it2.value().GetAsString(&accessor_key)); | |
143 auto accessor_it = accessor_ids_.find(accessor_key); | |
144 CHECK(accessor_it != accessor_ids_.end()); | |
145 primitive->attributes[it2.key()] = | |
146 asset_->GetAccessor(accessor_it->second); | |
147 } | |
148 } | |
149 | |
150 mesh->primitives.push_back(std::move(primitive)); | |
151 } | |
152 } | |
153 | |
154 mesh_ids_[it.key()] = asset_->AddMesh(std::move(mesh)); | |
155 } | |
156 } | |
157 | |
158 void GltfParser::SetNodes(const base::DictionaryValue& dict) { | |
159 std::unordered_map<std::string, gltf::Node*> nodes; | |
160 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | |
161 const base::DictionaryValue* node_dict; | |
162 CHECK(it.value().GetAsDictionary(&node_dict)); | |
163 | |
164 auto node = base::MakeUnique<gltf::Node>(); | |
165 const base::ListValue* list; | |
166 if (node_dict->GetList("meshes", &list)) { | |
167 std::string mesh_key; | |
168 for (const auto& mesh_value : *list) { | |
169 CHECK(mesh_value->GetAsString(&mesh_key)); | |
170 auto mesh_it = mesh_ids_.find(mesh_key); | |
171 CHECK(mesh_it != mesh_ids_.end()); | |
172 node->meshes.push_back(asset_->GetMesh(mesh_it->second)); | |
173 } | |
174 } | |
175 | |
176 nodes[it.key()] = node.get(); | |
177 node_ids_[it.key()] = asset_->AddNode(std::move(node)); | |
178 } | |
179 | |
180 // Processing children after all nodes have been added to the asset. | |
181 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | |
182 const base::DictionaryValue* node_dict; | |
183 it.value().GetAsDictionary(&node_dict); | |
184 | |
185 gltf::Node* node = nodes[it.key()]; | |
186 const base::ListValue* list; | |
187 if (node_dict->GetList("children", &list)) { | |
188 std::string node_key; | |
189 for (const auto& mesh_value : *list) { | |
190 CHECK(mesh_value->GetAsString(&node_key)); | |
191 auto node_it = nodes.find(node_key); | |
192 CHECK(node_it != nodes.end()); | |
193 node->children.push_back(node_it->second); | |
194 } | |
195 } | |
196 } | |
197 } | |
198 | |
199 void GltfParser::SetScenes(const base::DictionaryValue& dict) { | |
200 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | |
201 const base::DictionaryValue* scene_dict; | |
202 CHECK(it.value().GetAsDictionary(&scene_dict)); | |
203 | |
204 auto scene = base::MakeUnique<gltf::Scene>(); | |
205 const base::ListValue* list; | |
206 if (scene_dict->GetList("nodes", &list)) { | |
207 std::string node_key; | |
208 for (const auto& node_value : *list) { | |
209 CHECK(node_value->GetAsString(&node_key)); | |
210 auto node_it = node_ids_.find(node_key); | |
211 CHECK(node_it != node_ids_.end()); | |
212 scene->nodes.push_back(asset_->GetNode(node_it->second)); | |
213 } | |
214 } | |
215 | |
216 scene_ids_[it.key()] = asset_->AddScene(std::move(scene)); | |
217 } | |
218 } | |
219 | |
220 } // namespace vr_shell | |
OLD | NEW |