2
2
#include " threepp/loaders/URDFLoader.hpp"
3
3
4
4
#include " pugixml.hpp"
5
- #include " threepp/materials/MeshBasicMaterial.hpp"
5
+ #include " threepp/materials/MeshStandardMaterial.hpp"
6
+ #include " threepp/math/MathUtils.hpp"
6
7
#include " threepp/objects/Group.hpp"
7
8
#include " threepp/objects/Mesh.hpp"
8
9
#include " threepp/utils/StringUtils.hpp"
@@ -38,6 +39,25 @@ namespace {
38
39
-utils::parseFloat (xyz[2 ]));
39
40
}
40
41
42
+ std::shared_ptr<Material> getMaterial (const pugi::xml_node& node) {
43
+ const auto color = node.child (" color" );
44
+ const auto diffuse = color.attribute (" rgba" ).value ();
45
+ const auto diffuseArray = utils::split (diffuse, ' ' );
46
+
47
+ const auto mtl = MeshStandardMaterial::create ();
48
+ mtl->color .setRGB (
49
+ utils::parseFloat (diffuseArray[0 ]),
50
+ utils::parseFloat (diffuseArray[1 ]),
51
+ utils::parseFloat (diffuseArray[2 ]));
52
+
53
+ if (float alpha = utils::parseFloat (diffuseArray[3 ]) < 1 ) {
54
+ mtl->transparent = true ;
55
+ mtl->opacity = alpha;
56
+ }
57
+
58
+ return mtl;
59
+ }
60
+
41
61
JointType getType (const std::string& type) {
42
62
if (type == " revolute" ) {
43
63
return JointType::Revolute;
@@ -78,7 +98,7 @@ namespace {
78
98
79
99
// find parent path with package.xml
80
100
bool packageFound = false ;
81
- auto packagePath = basePath. parent_path () ;
101
+ auto packagePath = basePath;
82
102
for (int i = 0 ; i < 10 ; ++i) {
83
103
if (exists (packagePath / " package.xml" )) {
84
104
packageFound = true ;
@@ -93,7 +113,7 @@ namespace {
93
113
return packagePath.parent_path ().string () + " /" + fileName;
94
114
}
95
115
96
- return fileName;
116
+ return basePath / fileName;
97
117
}
98
118
99
119
}// namespace
@@ -110,55 +130,50 @@ struct URDFLoader::Impl {
110
130
const auto root = doc.child (" robot" );
111
131
if (!root) return nullptr ;
112
132
113
-
114
133
auto robot = std::make_shared<Robot>();
115
134
robot->name = root.attribute (" name" ).as_string (" robot" );
116
135
117
136
for (const auto link : root.children (" link" )) {
118
137
119
- auto linkObject = std::make_shared<Object3D>();
138
+ const auto linkObject = std::make_shared<Object3D>();
120
139
linkObject->name = link .attribute (" name" ).value ();
121
140
122
141
for (const auto visual : link .children (" visual" )) {
123
142
124
- auto visualObject = std::make_shared<Object3D>();
125
- visualObject->name = visual.attribute (" name" ).value ();
143
+ if (const auto geometry = visual.child (" geometry" )) {
144
+
145
+ const auto mesh = geometry.child (" mesh" );
146
+ const auto fileName = getModelPath (path.parent_path (), mesh.attribute (" filename" ).value ());
147
+
148
+ if (auto visualObject = loader.load (fileName)) {
149
+ visualObject->name = visual.attribute (" name" ).value ();
150
+ visualObject->position .copy (getXYZ (visual));
151
+ visualObject->rotation .copy (getRPY (visual));
126
152
127
- visualObject->position .copy (getXYZ (visual));
128
- visualObject->rotation .copy (getRPY (visual));
153
+ if (const auto material = visual.child (" material" )) {
129
154
130
- for ( const auto geometry : visual. children ( " geometry " )) {
155
+ const auto mtl = getMaterial (material);
131
156
132
- auto mesh = geometry.child (" mesh" );
133
- auto fileName = getModelPath (path, mesh.attribute (" filename" ).value ());
157
+ visualObject->traverseType <Mesh>([&](Mesh& mesh) {
158
+ mesh.setMaterial (mtl);
159
+ });
160
+ }
134
161
135
- if (auto load = loader.load (fileName)) {
136
- visualObject->add (load);
162
+ if (fileName.extension ().string () == " .stl" || fileName.extension ().string () == " .STL" ) {
163
+ visualObject->rotateX (-math::PI / 2 );
164
+ }
165
+
166
+ linkObject->add (visualObject);
137
167
}
138
168
}
139
-
140
- // for (auto material : visual.children("material")) {
141
- //
142
- // auto color = material.child("color");
143
- // auto diffuse = color.attribute("rgba").value();
144
- // auto diffuseArray = utils::split(diffuse, ' ');
145
- //
146
- // auto mtl = MeshBasicMaterial::create();
147
- // mtl->color.setRGB(
148
- // utils::parseFloat(diffuseArray[0]),
149
- // utils::parseFloat(diffuseArray[1]),
150
- // utils::parseFloat(diffuseArray[2]));
151
- // }
152
-
153
- linkObject->add (visualObject);
154
169
}
155
170
156
171
robot->addLink (linkObject);
157
172
}
158
173
159
174
for (const auto joint : root.children (" joint" )) {
160
175
161
- auto jointObject = std::make_shared<Object3D>();
176
+ const auto jointObject = std::make_shared<Object3D>();
162
177
jointObject->name = joint.attribute (" name" ).value ();
163
178
164
179
jointObject->position .copy (getXYZ (joint));
0 commit comments