OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/gltf_parser.h" | 5 #include "chrome/browser/android/vr_shell/gltf_parser.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/files/file_util.h" | |
9 #include "base/json/json_file_value_serializer.h" | |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "net/base/data_url.h" | |
13 #include "url/gurl.h" | |
10 | 14 |
11 namespace vr_shell { | 15 namespace vr_shell { |
12 | 16 |
13 constexpr char kBase64Header[] = "data:application/octet-stream;base64,"; | |
14 constexpr size_t kBase64HeaderSize = 37; | |
15 | |
16 GltfParser::GltfParser() {} | 17 GltfParser::GltfParser() {} |
17 | 18 |
18 GltfParser::~GltfParser() = default; | 19 GltfParser::~GltfParser() = default; |
19 | 20 |
20 std::unique_ptr<gltf::Asset> GltfParser::Parse( | 21 std::unique_ptr<gltf::Asset> GltfParser::Parse( |
21 const base::DictionaryValue& dict) { | 22 const base::DictionaryValue& dict) { |
22 std::string gltf_version; | |
23 if (!dict.GetString("asset.version", &gltf_version) || gltf_version != "1.0") | |
24 return nullptr; | |
25 | |
26 asset_ = base::MakeUnique<gltf::Asset>(); | 23 asset_ = base::MakeUnique<gltf::Asset>(); |
27 | 24 |
28 if (!ParseInternal(dict)) { | 25 if (!ParseInternal(dict)) { |
29 asset_.reset(); | 26 Clear(); |
mthiesse
2017/03/23 15:00:10
You probably want to clear on the success case as
acondor_
2017/03/23 16:28:12
Done.
| |
30 return nullptr; | 27 return nullptr; |
31 } | 28 } |
32 | 29 |
33 return std::move(asset_); | 30 return std::move(asset_); |
34 } | 31 } |
35 | 32 |
33 // Use this method if the glTF file references external files. | |
mthiesse
2017/03/23 15:00:10
Comments should go in the header.
mthiesse
2017/03/23 15:00:10
It shouldn't matter whether your input is a dictio
acondor_
2017/03/23 15:29:21
Just to clarify, there would be no way to process
acondor_
2017/03/23 16:28:12
Done.
| |
34 // If so, use it in the FILE BrowserThread. | |
35 std::unique_ptr<gltf::Asset> GltfParser::Parse(const base::FilePath& path) { | |
36 JSONFileValueDeserializer json_deserializer(path); | |
37 int error_code; | |
38 std::string error_msg; | |
39 auto asset_value = json_deserializer.Deserialize(&error_code, &error_msg); | |
40 if (!asset_value) | |
41 return nullptr; | |
42 base::DictionaryValue* asset; | |
43 if (!asset_value->GetAsDictionary(&asset)) | |
44 return nullptr; | |
45 path_ = path; | |
46 return Parse(*asset); | |
47 } | |
48 | |
36 bool GltfParser::ParseInternal(const base::DictionaryValue& dict) { | 49 bool GltfParser::ParseInternal(const base::DictionaryValue& dict) { |
50 std::string gltf_version; | |
51 if (!dict.GetString("asset.version", &gltf_version) || gltf_version != "1.0") | |
52 return false; | |
53 | |
37 const base::DictionaryValue* sub_dict; | 54 const base::DictionaryValue* sub_dict; |
38 if (dict.GetDictionary("buffers", &sub_dict) && !SetBuffers(*sub_dict)) | 55 if (dict.GetDictionary("buffers", &sub_dict) && !SetBuffers(*sub_dict)) |
39 return false; | 56 return false; |
40 if (dict.GetDictionary("bufferViews", &sub_dict) && | 57 if (dict.GetDictionary("bufferViews", &sub_dict) && |
41 !SetBufferViews(*sub_dict)) | 58 !SetBufferViews(*sub_dict)) |
42 return false; | 59 return false; |
43 if (dict.GetDictionary("accessors", &sub_dict) && !SetAccessors(*sub_dict)) | 60 if (dict.GetDictionary("accessors", &sub_dict) && !SetAccessors(*sub_dict)) |
44 return false; | 61 return false; |
45 if (dict.GetDictionary("meshes", &sub_dict) && !SetMeshes(*sub_dict)) | 62 if (dict.GetDictionary("meshes", &sub_dict) && !SetMeshes(*sub_dict)) |
46 return false; | 63 return false; |
(...skipping 12 matching lines...) Expand all Loading... | |
59 | 76 |
60 return true; | 77 return true; |
61 } | 78 } |
62 | 79 |
63 bool GltfParser::SetBuffers(const base::DictionaryValue& dict) { | 80 bool GltfParser::SetBuffers(const base::DictionaryValue& dict) { |
64 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 81 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
65 const base::DictionaryValue* buffer_dict; | 82 const base::DictionaryValue* buffer_dict; |
66 if (!it.value().GetAsDictionary(&buffer_dict)) | 83 if (!it.value().GetAsDictionary(&buffer_dict)) |
67 return false; | 84 return false; |
68 | 85 |
69 std::string uri; | 86 std::string uri_str; |
70 // TODO(acondor): Support files. Only inline data is supported now. | 87 if (!buffer_dict->GetString("uri", &uri_str)) |
71 if (!buffer_dict->GetString("uri", &uri) || | |
72 uri.substr(0, kBase64HeaderSize) != kBase64Header) | |
73 return false; | 88 return false; |
74 | 89 auto buffer = ProcessUri(uri_str); |
75 auto buffer = base::MakeUnique<gltf::Buffer>(); | 90 if (!buffer) |
76 if (!base::Base64Decode(uri.substr(kBase64HeaderSize), buffer.get())) | |
77 return false; | 91 return false; |
78 | 92 |
79 int byte_length; | 93 int byte_length; |
80 if (buffer_dict->GetInteger("byteLength", &byte_length) && | 94 if (buffer_dict->GetInteger("byteLength", &byte_length) && |
81 static_cast<int>(buffer->length()) != byte_length) | 95 static_cast<int>(buffer->length()) != byte_length) |
82 return false; | 96 return false; |
83 | 97 |
84 buffer_ids_[it.key()] = asset_->AddBuffer(std::move(buffer)); | 98 buffer_ids_[it.key()] = asset_->AddBuffer(std::move(buffer)); |
85 } | 99 } |
86 return true; | 100 return true; |
87 } | 101 } |
88 | 102 |
103 std::unique_ptr<gltf::Buffer> GltfParser::ProcessUri( | |
104 const std::string& uri_str) { | |
105 auto uri = path_.empty() ? GURL(uri_str) | |
106 : GURL("file://" + path_.value()).Resolve(uri_str); | |
107 if (!uri.is_valid()) | |
108 return nullptr; | |
109 if (uri.SchemeIs(url::kDataScheme)) { | |
110 std::string mime_type; | |
111 std::string charset; | |
112 auto data = base::MakeUnique<gltf::Buffer>(); | |
113 if (!net::DataURL::Parse(uri, &mime_type, &charset, data.get())) | |
114 return nullptr; | |
115 return data; | |
116 } | |
117 if (uri.SchemeIsFile()) { | |
118 auto data = base::MakeUnique<gltf::Buffer>(); | |
119 if (!base::ReadFileToString(base::FilePath(uri.path()), data.get())) | |
120 return nullptr; | |
121 return data; | |
122 } | |
123 // No other schemes are supported yet. | |
124 return nullptr; | |
125 } | |
126 | |
89 bool GltfParser::SetBufferViews(const base::DictionaryValue& dict) { | 127 bool GltfParser::SetBufferViews(const base::DictionaryValue& dict) { |
90 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 128 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
91 const base::DictionaryValue* buffer_view_dict; | 129 const base::DictionaryValue* buffer_view_dict; |
92 if (!it.value().GetAsDictionary(&buffer_view_dict)) | 130 if (!it.value().GetAsDictionary(&buffer_view_dict)) |
93 return false; | 131 return false; |
94 | 132 |
95 auto buffer_view = base::MakeUnique<gltf::BufferView>(); | 133 auto buffer_view = base::MakeUnique<gltf::BufferView>(); |
96 std::string buffer_key; | 134 std::string buffer_key; |
97 if (!buffer_view_dict->GetString("buffer", &buffer_key)) | 135 if (!buffer_view_dict->GetString("buffer", &buffer_key)) |
98 return false; | 136 return false; |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 return false; | 302 return false; |
265 scene->nodes.push_back(asset_->GetNode(node_it->second)); | 303 scene->nodes.push_back(asset_->GetNode(node_it->second)); |
266 } | 304 } |
267 } | 305 } |
268 | 306 |
269 scene_ids_[it.key()] = asset_->AddScene(std::move(scene)); | 307 scene_ids_[it.key()] = asset_->AddScene(std::move(scene)); |
270 } | 308 } |
271 return true; | 309 return true; |
272 } | 310 } |
273 | 311 |
312 void GltfParser::Clear() { | |
313 asset_.reset(); | |
314 path_.clear(); | |
315 buffer_ids_.clear(); | |
316 buffer_view_ids_.clear(); | |
317 accessor_ids_.clear(); | |
318 node_ids_.clear(); | |
319 mesh_ids_.clear(); | |
320 scene_ids_.clear(); | |
321 } | |
322 | |
274 } // namespace vr_shell | 323 } // namespace vr_shell |
OLD | NEW |