Skip to content

Commit cc02330

Browse files
committed
option to keep material map
1 parent ea1af1d commit cc02330

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/ofbx.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,10 @@ struct GeometryDataImpl : GeometryData {
11821182
Vec4Attributes getColors() const override { return patchAttributes<Vec4Attributes>(colors); }
11831183
Vec3Attributes getTangents() const override { return patchAttributes<Vec3Attributes>(tangents); }
11841184
int getPartitionCount() const override { return (int)partitions.size(); }
1185-
1185+
1186+
const int getMaterialMapSize() const override { return (int)materials.size(); }
1187+
const int* getMaterialMap() const override { return materials.empty() ? nullptr : &materials[0]; }
1188+
11861189
GeometryPartition getPartition(int index) const override {
11871190
if (index >= partitions.size()) return {nullptr, 0, 0, 0};
11881191
return {
@@ -3123,26 +3126,28 @@ static OptionalError<Object*> parseAnimationCurve(const Scene& scene, const Elem
31233126
return curve;
31243127
}
31253128

3126-
static OptionalError<Object*> parseGeometry(const Element& element, GeometryImpl& geom, std::vector<ParseDataJob> &jobs, Allocator& allocator) {
3129+
static OptionalError<Object*> parseGeometry(const Element& element, GeometryImpl& geom, std::vector<ParseDataJob> &jobs, bool ignore_geometry, Allocator& allocator) {
31273130
assert(element.first_property);
31283131

3132+
if (!parseGeometryMaterials(geom, element, jobs)) return Error("Invalid materials");
3133+
31293134
const Element* vertices_element = findChild(element, "Vertices");
3130-
if (!vertices_element || !vertices_element->first_property)
3131-
{
3135+
if (!vertices_element || !vertices_element->first_property) {
31323136
return &geom;
31333137
}
31343138

31353139
const Element* polys_element = findChild(element, "PolygonVertexIndex");
31363140
if (!polys_element || !polys_element->first_property) return Error("Indices missing");
31373141

3138-
if (!pushJob(jobs, *vertices_element->first_property, geom.positions.values)) return Error("Invalid vertices");
3139-
if (!pushJob(jobs, *polys_element->first_property, geom.positions.indices)) return Error("Invalid vertices");
3142+
if (!ignore_geometry) {
3143+
if (!pushJob(jobs, *vertices_element->first_property, geom.positions.values)) return Error("Invalid vertices");
3144+
if (!pushJob(jobs, *polys_element->first_property, geom.positions.indices)) return Error("Invalid vertices");
31403145

3141-
if (!parseGeometryMaterials(geom, element, jobs)) return Error("Invalid materials");
3142-
if (!parseGeometryUVs(geom, element, jobs)) return Error("Invalid vertex attributes");
3143-
if (!parseGeometryTangents(geom, element, jobs)) return Error("Invalid vertex attributes");
3144-
if (!parseGeometryColors(geom, element, jobs)) return Error("Invalid vertex attributes");
3145-
if (!parseGeometryNormals(geom, element, jobs)) return Error("Invalid vertex attributes");
3146+
if (!parseGeometryUVs(geom, element, jobs)) return Error("Invalid vertex attributes");
3147+
if (!parseGeometryTangents(geom, element, jobs)) return Error("Invalid vertex attributes");
3148+
if (!parseGeometryColors(geom, element, jobs)) return Error("Invalid vertex attributes");
3149+
if (!parseGeometryNormals(geom, element, jobs)) return Error("Invalid vertex attributes");
3150+
}
31463151

31473152
return &geom;
31483153
}
@@ -3406,6 +3411,7 @@ static bool parseObjects(const Element& root, Scene& scene, u16 flags, Allocator
34063411
const bool ignore_limbs = (flags & (u16)LoadFlags::IGNORE_LIMBS) != 0;
34073412
const bool ignore_meshes = (flags & (u16)LoadFlags::IGNORE_MESHES) != 0;
34083413
const bool ignore_models = (flags & (u16)LoadFlags::IGNORE_MODELS) != 0;
3414+
const bool keep_matertial_map = (flags & (u16)LoadFlags::KEEP_MATERIAL_MAP) != 0;
34093415

34103416
const Element* objs = findChild(root, "Objects");
34113417
if (!objs) return true;
@@ -3438,18 +3444,20 @@ static bool parseObjects(const Element& root, Scene& scene, u16 flags, Allocator
34383444

34393445
if (iter.second.object == scene.m_root) continue;
34403446

3441-
if (iter.second.element->id == "Geometry" && !ignore_geometry)
3447+
if (iter.second.element->id == "Geometry")
34423448
{
34433449
Property* last_prop = iter.second.element->first_property;
34443450
while (last_prop->next) last_prop = last_prop->next;
34453451
if (last_prop && last_prop->value == "Mesh")
34463452
{
34473453
GeometryImpl* geom = allocator.allocate<GeometryImpl>(scene, *iter.second.element);
3448-
parseGeometry(*iter.second.element, *geom, jobs, allocator);
3454+
if (!ignore_geometry || keep_matertial_map) {
3455+
parseGeometry(*iter.second.element, *geom, jobs, ignore_geometry, allocator);
3456+
}
34493457
obj = geom;
34503458
scene.m_geometries.push_back(geom);
34513459
}
3452-
else if (last_prop && last_prop->value == "Shape")
3460+
else if (last_prop && last_prop->value == "Shape" && !ignore_geometry)
34533461
{
34543462
obj = allocator.allocate<ShapeImpl>(scene, *iter.second.element);
34553463
}

src/ofbx.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ using JobProcessor = void (*)(JobFunction, void*, void*, u32, u32);
3030
enum class LoadFlags : u16
3131
{
3232
NONE = 0,
33-
UNUSED = 1 << 0, // can be reused
33+
KEEP_MATERIAL_MAP = 1 << 0, // keep material map even if IGNORE_GEOMETRY is used
3434
IGNORE_GEOMETRY = 1 << 1,
3535
IGNORE_BLEND_SHAPES = 1 << 2,
3636
IGNORE_CAMERAS = 1 << 3,
@@ -544,6 +544,8 @@ struct GeometryData {
544544
virtual Vec2Attributes getUVs(int index = 0) const = 0;
545545
virtual Vec4Attributes getColors() const = 0;
546546
virtual Vec3Attributes getTangents() const = 0;
547+
virtual const int getMaterialMapSize() const = 0;
548+
virtual const int* getMaterialMap() const = 0;
547549
virtual int getPartitionCount() const = 0;
548550
virtual GeometryPartition getPartition(int partition_index) const = 0;
549551
};

0 commit comments

Comments
 (0)