Skip to content

Commit 854c256

Browse files
sevendevseventhX
andauthored
Feature/import material (Synesthesias#184)
* BuildingのLOD4 import 対応 * Revert "BuildingのLOD4 import 対応" This reverts commit addbe37. * Material Import * libcitygml hash added * libcitygml hash update * mac build fix. * mesh_merger sjis -> utf8 Material.cs build error fix / Material as IEquatable --------- Co-authored-by: COREI5\taka <seventhdim@gmail.com>
1 parent 3f8a777 commit 854c256

File tree

14 files changed

+260
-28
lines changed

14 files changed

+260
-28
lines changed

include/plateau/polygon_mesh/mesh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ namespace plateau::polygonMesh {
7272
* 代わりに extendLastSubMesh を実行します。
7373
* なぜなら、同じテクスチャであればサブメッシュを分けるのは無意味で描画負荷を増やすだけと思われるためです。
7474
*/
75-
void addSubMesh(const std::string& texture_path, size_t sub_mesh_start_index, size_t sub_mesh_end_index);
75+
void addSubMesh(const std::string& texture_path, std::shared_ptr<const citygml::Material> material, size_t sub_mesh_start_index, size_t sub_mesh_end_index);
7676

7777
/**
7878
* 直前の SubMesh の範囲を拡大し、範囲の終わりがindicesリストの最後を指すようにします。

include/plateau/polygon_mesh/sub_mesh.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44
#include <vector>
55
#include <libplateau_api.h>
6+
#include "citygml/material.h"
67

78
namespace plateau::polygonMesh {
89
/**
@@ -13,20 +14,22 @@ namespace plateau::polygonMesh {
1314
*/
1415
class LIBPLATEAU_EXPORT SubMesh {
1516
public:
16-
SubMesh(size_t start_index, size_t end_index, const std::string& texture_path);
17+
SubMesh(size_t start_index, size_t end_index, const std::string& texture_path, std::shared_ptr<const citygml::Material> material);
1718

1819
/**
1920
* 引数で与えられた SubMesh の vector に SubMesh を追加します。
2021
*/
2122
static void addSubMesh(size_t start_index, size_t end_index,
22-
const std::string& texture_path, std::vector<SubMesh>& vector);
23+
const std::string& texture_path, std::shared_ptr<const citygml::Material> material, std::vector<SubMesh>& vector);
2324

2425
size_t getStartIndex() const;
2526
size_t getEndIndex() const;
2627

2728
/// テクスチャパスを取得します。 テクスチャがないときは空文字とします。
2829
const std::string& getTexturePath() const;
2930

31+
std::shared_ptr<const citygml::Material> getMaterial() const;
32+
3033
void setTexturePath(std::string file_path);
3134

3235
void setEndIndex(int end_index);
@@ -42,5 +45,6 @@ namespace plateau::polygonMesh {
4245
size_t start_index_;
4346
size_t end_index_;
4447
std::string texture_path_;
48+
std::shared_ptr<const citygml::Material> material_;
4549
};
4650
}

src/c_wrapper/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ add_library(c_wrapper OBJECT
2222
"appearancetargetdefinition_c.cpp"
2323
"materialtargetdefinition_c.cpp"
2424
"texture_c.cpp"
25+
"material_c.cpp"
2526
"appearance_c.cpp"
2627
"plateau_dll_logger_c.cpp"
2728
"mesh_extractor_c.cpp"
@@ -49,7 +50,7 @@ add_library(c_wrapper OBJECT
4950
"string_c.cpp"
5051
"udx_sub_folder_c.cpp"
5152
"fbx_writer_c.cpp"
52-
"city_object_list_c.cpp")
53+
"city_object_list_c.cpp" "material_c.cpp")
5354

5455
#target_link_libraries(c_wrapper PRIVATE citygml)
5556
target_include_directories(c_wrapper PUBLIC "${CMAKE_SOURCE_DIR}/include" "${LIBCITYGML_INCLUDE}")

src/c_wrapper/material_c.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <citygml/material.h>
2+
#include "libplateau_c.h"
3+
4+
using namespace citygml;
5+
using namespace libplateau;
6+
extern "C" {
7+
8+
DLL_VALUE_FUNC(plateau_material_get_diffuse,
9+
Material,
10+
TVec3f,
11+
handle->getDiffuse())
12+
13+
DLL_VALUE_FUNC(plateau_material_get_emissive,
14+
Material,
15+
TVec3f,
16+
handle->getEmissive())
17+
18+
DLL_VALUE_FUNC(plateau_material_get_specular,
19+
Material,
20+
TVec3f,
21+
handle->getSpecular())
22+
23+
DLL_VALUE_FUNC(plateau_material_get_ambient_intensity,
24+
Material,
25+
float,
26+
handle->getAmbientIntensity())
27+
28+
DLL_VALUE_FUNC(plateau_material_get_shininess,
29+
Material,
30+
float,
31+
handle->getShininess())
32+
33+
DLL_VALUE_FUNC(plateau_material_get_transparency,
34+
Material,
35+
float,
36+
handle->getTransparency())
37+
38+
DLL_VALUE_FUNC(plateau_material_is_smooth,
39+
Material,
40+
bool,
41+
handle->isSmooth())
42+
43+
}

src/c_wrapper/mesh_c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extern "C" {
8080
int sub_mesh_end_index
8181
) {
8282
API_TRY{
83-
mesh->addSubMesh(texture_path, sub_mesh_start_index, sub_mesh_end_index);
83+
mesh->addSubMesh(texture_path, nullptr, sub_mesh_start_index, sub_mesh_end_index);
8484
return APIResult::Success;
8585
} API_CATCH
8686
return APIResult::ErrorUnknown;

src/c_wrapper/sub_mesh_c.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "libplateau_c.h"
22
#include <plateau/polygon_mesh/sub_mesh.h>
3+
#include <citygml/material.h>
34
using namespace libplateau;
45
using namespace plateau::polygonMesh;
56
extern "C"{
@@ -18,13 +19,18 @@ extern "C"{
1819
SubMesh,
1920
handle->getTexturePath())
2021

22+
DLL_PTR_FUNC(plateau_sub_mesh_get_material,
23+
SubMesh,
24+
citygml::Material,
25+
handle->getMaterial().get())
26+
2127
LIBPLATEAU_C_EXPORT APIResult LIBPLATEAU_C_API plateau_create_sub_mesh(
2228
SubMesh** out_sub_mesh_ptr,
2329
const int start_index,
2430
const int end_index,
2531
const char* const texture_path
2632
) {
27-
*out_sub_mesh_ptr = new SubMesh(start_index, end_index, texture_path);
33+
*out_sub_mesh_ptr = new SubMesh(start_index, end_index, texture_path, nullptr);
2834
return APIResult::Success;
2935
}
3036

src/polygon_mesh/mesh.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ namespace plateau::polygonMesh {
129129
}
130130
}
131131

132-
void Mesh::addSubMesh(const std::string& texture_path, size_t sub_mesh_start_index, size_t sub_mesh_end_index) {
132+
void Mesh::addSubMesh(const std::string& texture_path, std::shared_ptr<const citygml::Material> material, size_t sub_mesh_start_index, size_t sub_mesh_end_index) {
133133
// テクスチャが異なる場合は追加します。
134134
// TODO テクスチャありのポリゴン と なしのポリゴン が交互にマージされることで、テクスチャなしのサブメッシュが大量に生成されるので描画負荷に改善の余地ありです。
135135
// テクスチャなしのサブメッシュは1つにまとめたいところです。テクスチャなしのポリゴンを連続してマージすることで1つにまとまるはずです。
@@ -141,11 +141,20 @@ namespace plateau::polygonMesh {
141141
} else {
142142
auto& last_texture_path = sub_meshes_.rbegin()->getTexturePath();
143143
is_different_tex = texture_path != last_texture_path;
144+
145+
// 前と同じマテリアルかどうか判定します。
146+
if (!is_different_tex) {
147+
auto last_material = sub_meshes_.rbegin()->getMaterial();
148+
if (material != nullptr && last_material != nullptr)
149+
is_different_tex = material->getId() != last_material->getId();
150+
else if ((material == nullptr && last_material != nullptr) || (material != nullptr && last_material == nullptr))
151+
is_different_tex = true;
152+
}
144153
}
145154

146155
if (is_different_tex) {
147156
// テクスチャが違うなら、サブメッシュを追加します。
148-
SubMesh::addSubMesh(sub_mesh_start_index, sub_mesh_end_index, texture_path, sub_meshes_);
157+
SubMesh::addSubMesh(sub_mesh_start_index, sub_mesh_end_index, texture_path, material, sub_meshes_);
149158
} else {
150159
// テクスチャが同じなら、最後のサブメッシュの範囲を延長して新しい部分の終わりに合わせます。
151160
extendLastSubMesh(sub_mesh_end_index);
@@ -154,7 +163,7 @@ namespace plateau::polygonMesh {
154163

155164
void Mesh::extendLastSubMesh(size_t sub_mesh_end_index) {
156165
if (sub_meshes_.empty()) {
157-
sub_meshes_.emplace_back(0, sub_mesh_end_index, "");
166+
sub_meshes_.emplace_back(0, sub_mesh_end_index, "", nullptr);
158167
} else {
159168
sub_meshes_.at(sub_meshes_.size() - 1).setEndIndex(sub_mesh_end_index);
160169
}

src/polygon_mesh/mesh_factory.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <plateau/polygon_mesh/mesh_factory.h>
22
#include "citygml/texture.h"
3+
#include "citygml/material.h"
34
#include <plateau/geometry/geo_coordinate.h>
45
#include <plateau/geometry/geo_reference.h>
56
#include <cassert>
@@ -8,6 +9,8 @@
89

910
#include "plateau/polygon_mesh/mesh_merger.h"
1011

12+
13+
1114
namespace plateau::polygonMesh {
1215
using namespace citygml;
1316
using namespace plateau::geometry;
@@ -89,7 +92,16 @@ namespace plateau::polygonMesh {
8992
}
9093
}
9194

92-
out_mesh.addSubMesh(texture_path, 0, out_mesh.getIndices().size() - 1);
95+
// Materialを取得し SubMesh に設定します。設定されていない場合nullptrが設定されます。
96+
auto material = polygon.getMaterialFor("rgbTexture");
97+
if (material == nullptr) {
98+
// rgbTextureのthemeが存在しない場合
99+
auto themes = polygon.getAllMaterialThemes(true);
100+
if (!themes.empty())
101+
material = polygon.getMaterialFor(themes.at(0));
102+
}
103+
104+
out_mesh.addSubMesh(texture_path, material, 0, out_mesh.getIndices().size() - 1);
93105
}
94106

95107
void findAllPolygonsInGeometry(

src/polygon_mesh/mesh_merger.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace plateau::polygonMesh {
1111
}
1212

1313
/**
14-
* @brief SubMesh以外の形状情報をマージします。
14+
* @brief SubMesh以外の形状情報をマージします。
1515
*/
1616
void mergeShape(Mesh& mesh, const Mesh& other_mesh, const bool invert_mesh_front_back) {
1717
const auto vertex_count = mesh.getVertices().size();
@@ -24,8 +24,8 @@ namespace plateau::polygonMesh {
2424
}
2525

2626
/**
27-
* merge関数 のテクスチャあり版です。
28-
* テクスチャについては、マージした結果、範囲とテクスチャを対応付ける SubMesh が追加されます。
27+
* merge関数 のテクスチャあり版です。
28+
* テクスチャについては、マージした結果、範囲とテクスチャを対応付ける SubMesh が追加されます。
2929
*/
3030
void mergeWithTexture(Mesh& mesh, const Mesh& other_mesh, const bool invert_mesh_front_back) {
3131
if (!isValidMesh(other_mesh)) return;
@@ -37,18 +37,19 @@ namespace plateau::polygonMesh {
3737
size_t offset = prev_indices_count;
3838
for (const auto& other_sub_mesh : other_sub_meshes) {
3939
const auto& texture_path = other_sub_mesh.getTexturePath();
40+
auto material = other_sub_mesh.getMaterial();
4041
size_t start_index = other_sub_mesh.getStartIndex() + offset;
4142
size_t end_index = other_sub_mesh.getEndIndex() + offset;
4243
assert(start_index <= end_index);
4344
assert(end_index < mesh.getIndices().size());
4445
assert((end_index - start_index + 1) % 3 == 0);
45-
mesh.addSubMesh(texture_path, start_index, end_index);
46+
mesh.addSubMesh(texture_path, material, start_index, end_index);
4647
}
4748
}
4849

4950
/**
50-
* merge関数 のテクスチャ無し版です。
51-
* 生成される Mesh の SubMesh はただ1つであり、そのテクスチャパスは空文字列となります。
51+
* merge関数 のテクスチャ無し版です。
52+
* 生成される Mesh の SubMesh はただ1つであり、そのテクスチャパスは空文字列となります。
5253
*/
5354
void mergeWithoutTexture(
5455
Mesh& mesh, const Mesh& other_mesh, const bool invert_mesh_front_back) {

src/polygon_mesh/sub_mesh.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
namespace plateau::polygonMesh {
77

8-
SubMesh::SubMesh(size_t start_index, size_t end_index, const std::string& texture_path) :
8+
SubMesh::SubMesh(size_t start_index, size_t end_index, const std::string& texture_path, std::shared_ptr<const citygml::Material> material) :
99
start_index_(start_index),
1010
end_index_(end_index),
11-
texture_path_(texture_path) {}
11+
texture_path_(texture_path) ,
12+
material_(material) {}
13+
1214

1315
void
14-
SubMesh::addSubMesh(size_t start_index, size_t end_index, const std::string& texture_path, std::vector<SubMesh>& vector) {
16+
SubMesh::addSubMesh(size_t start_index, size_t end_index, const std::string& texture_path, std::shared_ptr<const citygml::Material> material, std::vector<SubMesh>& vector) {
1517
if (end_index <= start_index) throw std::logic_error("addSubMesh : Index is invalid.");
16-
vector.emplace_back(start_index, end_index, texture_path);
18+
vector.emplace_back(start_index, end_index, texture_path, material);
1719
}
1820

1921
size_t SubMesh::getStartIndex() const {
@@ -28,6 +30,10 @@ namespace plateau::polygonMesh {
2830
return texture_path_;
2931
}
3032

33+
std::shared_ptr<const citygml::Material> SubMesh::getMaterial() const {
34+
return material_;
35+
}
36+
3137
void SubMesh::setTexturePath(std::string file_path) {
3238
texture_path_ = file_path;
3339
}

0 commit comments

Comments
 (0)