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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/json/json_file_value_serializer.h" | 11 #include "base/json/json_file_value_serializer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "net/base/data_url.h" | 14 #include "net/base/data_url.h" |
15 #include "net/base/filename_util.h" | 15 #include "net/base/filename_util.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 namespace vr_shell { | 18 namespace vr_shell { |
19 | 19 |
20 GltfParser::GltfParser() {} | 20 GltfParser::GltfParser() {} |
21 | 21 |
22 GltfParser::~GltfParser() = default; | 22 GltfParser::~GltfParser() = default; |
23 | 23 |
24 std::unique_ptr<gltf::Asset> GltfParser::Parse( | 24 std::unique_ptr<gltf::Asset> GltfParser::Parse( |
25 const base::DictionaryValue& dict, | 25 const base::DictionaryValue& dict, |
| 26 std::vector<std::unique_ptr<gltf::Buffer>>* buffers, |
26 const base::FilePath& path) { | 27 const base::FilePath& path) { |
27 path_ = path; | 28 path_ = path; |
28 asset_ = base::MakeUnique<gltf::Asset>(); | 29 asset_ = base::MakeUnique<gltf::Asset>(); |
29 | 30 |
30 base::ScopedClosureRunner runner( | 31 base::ScopedClosureRunner runner( |
31 base::Bind(&GltfParser::Clear, base::Unretained(this))); | 32 base::Bind(&GltfParser::Clear, base::Unretained(this))); |
32 | 33 |
33 if (!ParseInternal(dict)) | 34 if (!ParseInternal(dict, buffers)) |
34 return nullptr; | 35 return nullptr; |
35 | 36 |
36 return std::move(asset_); | 37 return std::move(asset_); |
37 } | 38 } |
38 | 39 |
39 std::unique_ptr<gltf::Asset> GltfParser::Parse( | 40 std::unique_ptr<gltf::Asset> GltfParser::Parse( |
40 const base::FilePath& gltf_path) { | 41 const base::FilePath& gltf_path, |
| 42 std::vector<std::unique_ptr<gltf::Buffer>>* buffers) { |
41 JSONFileValueDeserializer json_deserializer(gltf_path); | 43 JSONFileValueDeserializer json_deserializer(gltf_path); |
42 int error_code; | 44 int error_code; |
43 std::string error_msg; | 45 std::string error_msg; |
44 auto asset_value = json_deserializer.Deserialize(&error_code, &error_msg); | 46 auto asset_value = json_deserializer.Deserialize(&error_code, &error_msg); |
45 if (!asset_value) | 47 if (!asset_value) |
46 return nullptr; | 48 return nullptr; |
47 base::DictionaryValue* asset; | 49 base::DictionaryValue* asset; |
48 if (!asset_value->GetAsDictionary(&asset)) | 50 if (!asset_value->GetAsDictionary(&asset)) |
49 return nullptr; | 51 return nullptr; |
50 return Parse(*asset, gltf_path); | 52 return Parse(*asset, buffers, gltf_path); |
51 } | 53 } |
52 | 54 |
53 bool GltfParser::ParseInternal(const base::DictionaryValue& dict) { | 55 bool GltfParser::ParseInternal( |
| 56 const base::DictionaryValue& dict, |
| 57 std::vector<std::unique_ptr<gltf::Buffer>>* buffers) { |
54 std::string gltf_version; | 58 std::string gltf_version; |
55 if (!dict.GetString("asset.version", &gltf_version) || gltf_version != "1.0") | 59 if (!dict.GetString("asset.version", &gltf_version) || gltf_version != "1.0") |
56 return false; | 60 return false; |
57 | 61 |
58 const base::DictionaryValue* sub_dict; | 62 const base::DictionaryValue* sub_dict; |
59 if (dict.GetDictionary("buffers", &sub_dict) && !SetBuffers(*sub_dict)) | 63 if (dict.GetDictionary("buffers", &sub_dict) && |
| 64 !SetBuffers(*sub_dict, buffers)) |
60 return false; | 65 return false; |
61 if (dict.GetDictionary("bufferViews", &sub_dict) && | 66 if (dict.GetDictionary("bufferViews", &sub_dict) && |
62 !SetBufferViews(*sub_dict)) | 67 !SetBufferViews(*sub_dict)) |
63 return false; | 68 return false; |
64 if (dict.GetDictionary("accessors", &sub_dict) && !SetAccessors(*sub_dict)) | 69 if (dict.GetDictionary("accessors", &sub_dict) && !SetAccessors(*sub_dict)) |
65 return false; | 70 return false; |
66 if (dict.GetDictionary("meshes", &sub_dict) && !SetMeshes(*sub_dict)) | 71 if (dict.GetDictionary("meshes", &sub_dict) && !SetMeshes(*sub_dict)) |
67 return false; | 72 return false; |
68 if (dict.GetDictionary("nodes", &sub_dict) && !SetNodes(*sub_dict)) | 73 if (dict.GetDictionary("nodes", &sub_dict) && !SetNodes(*sub_dict)) |
69 return false; | 74 return false; |
70 if (dict.GetDictionary("scenes", &sub_dict) && !SetScenes(*sub_dict)) | 75 if (dict.GetDictionary("scenes", &sub_dict) && !SetScenes(*sub_dict)) |
71 return false; | 76 return false; |
72 | 77 |
73 std::string scene_key; | 78 std::string scene_key; |
74 if (dict.GetString("scene", &scene_key)) { | 79 if (dict.GetString("scene", &scene_key)) { |
75 auto scene_it = scene_ids_.find(scene_key); | 80 auto scene_it = scene_ids_.find(scene_key); |
76 if (scene_it == scene_ids_.end()) | 81 if (scene_it == scene_ids_.end()) |
77 return false; | 82 return false; |
78 asset_->SetMainScene(asset_->GetScene(scene_it->second)); | 83 asset_->SetMainScene(asset_->GetScene(scene_it->second)); |
79 } | 84 } |
80 | 85 |
81 return true; | 86 return true; |
82 } | 87 } |
83 | 88 |
84 bool GltfParser::SetBuffers(const base::DictionaryValue& dict) { | 89 bool GltfParser::SetBuffers( |
| 90 const base::DictionaryValue& dict, |
| 91 std::vector<std::unique_ptr<gltf::Buffer>>* buffers) { |
| 92 buffers->clear(); |
85 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { | 93 for (base::DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { |
86 const base::DictionaryValue* buffer_dict; | 94 const base::DictionaryValue* buffer_dict; |
87 if (!it.value().GetAsDictionary(&buffer_dict)) | 95 if (!it.value().GetAsDictionary(&buffer_dict)) |
88 return false; | 96 return false; |
89 | 97 |
90 std::string uri_str; | 98 std::string uri_str; |
91 if (!buffer_dict->GetString("uri", &uri_str)) | 99 if (!buffer_dict->GetString("uri", &uri_str)) |
92 return false; | 100 return false; |
93 auto buffer = ProcessUri(uri_str); | 101 auto buffer = ProcessUri(uri_str); |
94 if (!buffer) | 102 if (!buffer) |
95 return false; | 103 return false; |
96 | 104 |
97 int byte_length; | 105 int byte_length; |
98 if (buffer_dict->GetInteger("byteLength", &byte_length) && | 106 if (buffer_dict->GetInteger("byteLength", &byte_length) && |
99 static_cast<int>(buffer->length()) != byte_length) | 107 static_cast<int>(buffer->length()) != byte_length) |
100 return false; | 108 return false; |
101 | 109 |
102 buffer_ids_[it.key()] = asset_->AddBuffer(std::move(buffer)); | 110 buffer_ids_[it.key()] = buffers->size(); |
| 111 buffers->push_back(std::move(buffer)); |
103 } | 112 } |
104 return true; | 113 return true; |
105 } | 114 } |
106 | 115 |
107 std::unique_ptr<gltf::Buffer> GltfParser::ProcessUri( | 116 std::unique_ptr<gltf::Buffer> GltfParser::ProcessUri( |
108 const std::string& uri_str) { | 117 const std::string& uri_str) { |
109 auto uri = path_.empty() ? GURL(uri_str) | 118 auto uri = path_.empty() ? GURL(uri_str) |
110 : net::FilePathToFileURL(path_).Resolve(uri_str); | 119 : net::FilePathToFileURL(path_).Resolve(uri_str); |
111 if (!uri.is_valid()) | 120 if (!uri.is_valid()) |
112 return nullptr; | 121 return nullptr; |
(...skipping 21 matching lines...) Expand all Loading... |
134 if (!it.value().GetAsDictionary(&buffer_view_dict)) | 143 if (!it.value().GetAsDictionary(&buffer_view_dict)) |
135 return false; | 144 return false; |
136 | 145 |
137 auto buffer_view = base::MakeUnique<gltf::BufferView>(); | 146 auto buffer_view = base::MakeUnique<gltf::BufferView>(); |
138 std::string buffer_key; | 147 std::string buffer_key; |
139 if (!buffer_view_dict->GetString("buffer", &buffer_key)) | 148 if (!buffer_view_dict->GetString("buffer", &buffer_key)) |
140 return false; | 149 return false; |
141 auto buffer_it = buffer_ids_.find(buffer_key); | 150 auto buffer_it = buffer_ids_.find(buffer_key); |
142 if (buffer_it == buffer_ids_.end()) | 151 if (buffer_it == buffer_ids_.end()) |
143 return false; | 152 return false; |
144 buffer_view->buffer = asset_->GetBuffer(buffer_it->second); | 153 buffer_view->buffer = buffer_it->second; |
145 if (!buffer_view_dict->GetInteger("byteOffset", &buffer_view->byte_offset)) | 154 if (!buffer_view_dict->GetInteger("byteOffset", &buffer_view->byte_offset)) |
146 return false; | 155 return false; |
147 buffer_view_dict->GetInteger("byteLength", &buffer_view->byte_length); | 156 buffer_view_dict->GetInteger("byteLength", &buffer_view->byte_length); |
148 buffer_view_dict->GetInteger("target", &buffer_view->target); | 157 buffer_view_dict->GetInteger("target", &buffer_view->target); |
149 | 158 |
150 buffer_view_ids_[it.key()] = asset_->AddBufferView(std::move(buffer_view)); | 159 buffer_view_ids_[it.key()] = asset_->AddBufferView(std::move(buffer_view)); |
151 } | 160 } |
152 return true; | 161 return true; |
153 } | 162 } |
154 | 163 |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 path_.clear(); | 327 path_.clear(); |
319 buffer_ids_.clear(); | 328 buffer_ids_.clear(); |
320 buffer_view_ids_.clear(); | 329 buffer_view_ids_.clear(); |
321 accessor_ids_.clear(); | 330 accessor_ids_.clear(); |
322 node_ids_.clear(); | 331 node_ids_.clear(); |
323 mesh_ids_.clear(); | 332 mesh_ids_.clear(); |
324 scene_ids_.clear(); | 333 scene_ids_.clear(); |
325 } | 334 } |
326 | 335 |
327 } // namespace vr_shell | 336 } // namespace vr_shell |
OLD | NEW |