89 std::vector<VertexF> vertices;
90 std::vector<uint32_t> indices;
93 float minX = std::numeric_limits<float>::max();
94 float minY = std::numeric_limits<float>::max();
95 float minZ = std::numeric_limits<float>::max();
96 float maxX = std::numeric_limits<float>::lowest();
97 float maxY = std::numeric_limits<float>::lowest();
98 float maxZ = std::numeric_limits<float>::lowest();
100 tinyobj::attrib_t attrib;
101 std::vector<tinyobj::shape_t> shapes;
102 std::vector<tinyobj::material_t> materials;
105 std::istringstream input(modelAsset->content());
107 bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err,
108 &input,
nullptr,
true);
111 throw std::runtime_error(
"Failed to load/parse .obj. " + err);
113 for (
const auto &shape: shapes) {
114 for (
const auto &index: shape.mesh.indices) {
115 if (index.vertex_index < 0) {
119 const size_t vertexBase =
120 static_cast<size_t>(index.vertex_index) * 3;
121 if (vertexBase + 2 >= attrib.vertices.size()) {
130 attrib.vertices[vertexBase + 0],
131 attrib.vertices[vertexBase + 1],
132 attrib.vertices[vertexBase + 2]));
134 if (index.texcoord_index >= 0) {
135 const size_t texcoordBase =
136 static_cast<size_t>(index.texcoord_index) * 2;
137 if (texcoordBase + 1 < attrib.texcoords.size()) {
139 { attrib.texcoords[texcoordBase + 0],
140 attrib.texcoords[texcoordBase + 1] }));
144 if (index.normal_index >= 0) {
145 const size_t normalBase =
146 static_cast<size_t>(index.normal_index) * 3;
147 if (normalBase + 2 < attrib.normals.size()) {
149 { attrib.normals[normalBase + 0],
150 attrib.normals[normalBase + 1],
151 attrib.normals[normalBase + 2] }));
158 if (vertices.size() >=
static_cast<size_t>(
159 std::numeric_limits<uint32_t>::max())) {
160 throw std::runtime_error(
161 "Too many vertices for uint32_t indices");
164 minX = std::min(minX, attrib.vertices[vertexBase + 0]);
165 minY = std::min(minY, attrib.vertices[vertexBase + 1]);
166 minZ = std::min(minZ, attrib.vertices[vertexBase + 2]);
167 maxX = std::max(maxX, attrib.vertices[vertexBase + 0]);
168 maxY = std::max(maxY, attrib.vertices[vertexBase + 1]);
169 maxZ = std::max(maxZ, attrib.vertices[vertexBase + 2]);
171 const uint32_t newIndex =
172 static_cast<uint32_t
>(vertices.size());
173 vertices.push_back(vertex);
174 indices.push_back(newIndex);
179 if (!vertices.empty() && !indices.empty()) {
180 _meshes.push_back(std::make_shared<Mesh>(vertices, indices));