Port FBX module from commit 68013d23932688e57b489600f4517dd280edc464
Ports FBX module from 3.2 branch to 4.0
This is the only time the plugin will be updated from 3.2 and marks the final time we do this, from now on we will backport FBX to 3.2 with fixes.
Changelog:
- fixed crash importing files with buggy format (because of bad newlines in ASCII data, this is yet to be fixed fully)
- fixed const correctness with C++/C version change
- rewrote material handling to be simpler and better
- ports from 3.2 to 4.0 the fbx importer
+ Transform S = chain[TransformationComp_Scaling];
+
+ // 3DS Max Pivots
+ Transform OT = chain[TransformationComp_GeometricTranslation];
+ Transform OR = chain[TransformationComp_GeometricRotation];
+ Transform OS = chain[TransformationComp_GeometricScaling];
+
+ // Calculate 3DS max pivot transform - use geometric space (e.g doesn't effect children nodes only the current node)
+ geometric_transform = OT * OR * OS;
+ // Calculate standard maya pivots
+ return T * Roff * Rp * Rpre * R * Rpost.inverse() * Rp.inverse() * Soff * Sp * S * Sp.inverse();
+}
+```
+
+# Transform inheritance for FBX Nodes
+
+The goal of below is to explain why they implement this in the first place.
+
+The use case is to make nodes have an option to override their local scaling or to make scaling influenced by orientation, which i would imagine would be useful for when you need to rotate a node and the child to scale based on the orientation rather than setting on the rotation matrix planes.
+```cpp
+// not modified the formatting here since this code must remain clear
+enum TransformInheritance {
+ Transform_RrSs = 0,
+ // Parent Rotation * Local Rotation * Parent Scale * Local Scale -- Parent Rotation Offset * Parent ScalingOffset (Local scaling is offset by rotation of parent node)
+ Transform_RSrs = 1, // Parent Rotation * Parent Scale * Local Rotation * Local Scale -- Parent * Local (normal mode)
+ Transform_Rrs = 2, // Parent Rotation * Local Rotation * Local Scale -- Node transform scale is the only relevant component
+#### FBX FILE declares axis dynamically using FBX header
+Coord is X
+Up is Y
+Front is Z
+
+#### GODOT - constant reference point
+Coord is X positive,
+Y is up positive,
+Front is -Z negative
+
+### Explaining MeshGeometry indexing
+
+Reference type declared:
+- Direct (directly related to the mapping information type)
+- IndexToDirect (Map with key value, meaning depends on the MappingInformationType)
+
+ControlPoint is a vertex
+* None The mapping is undetermined.
+* ByVertex There will be one mapping coordinate for each surface control point/vertex.
+ * If you have direct reference type vertices [x]
+ * If you have IndexToDirect reference type the UV
+* ByPolygonVertex There will be one mapping coordinate for each vertex, for every polygon of which it is a part. This means that a vertex will have as many mapping coordinates as polygons of which it is a part. (Sorted by polygon, referencing vertex)
+* ByPolygon There can be only one mapping coordinate for the whole polygon.
+ * One mapping per polygon polygon x has this normal x
+ * For each vertex of the polygon then set the normal to x
+* ByEdge There will be one mapping coordinate for each unique edge in the mesh. This is meant to be used with smoothing layer elements. (Mapping is referencing the edge id)
+* AllSame There can be only one mapping coordinate for the whole surface.
+ ERR_FAIL_COND_V_MSG(val == nullptr, p_default, "The FBX is corrupted, the property `" + String(p_name.c_str()) + "` is a `" + String(typeid(*prop).name()) + "` but should be a " + p_type);
+ ERR_CONTINUE_MSG(file_extension.empty(), "your texture has no file extension so we had to ignore it, let us know if you think this is wrong file an issue on github! " + debug_string);
+ ERR_CONTINUE_MSG(fbx_texture_map.count(fbx_mapping_name) <= 0, "This material has a texture with mapping name: " + String(fbx_mapping_name.c_str()) + " which is not yet supported by this importer. Consider opening an issue so we can support it.");
+ ERR_CONTINUE_MSG(
+ file_extension_uppercase != "PNG" &&
+ file_extension_uppercase != "JPEG" &&
+ file_extension_uppercase != "JPG" &&
+ file_extension_uppercase != "TGA" &&
+ file_extension_uppercase != "WEBP" &&
+ file_extension_uppercase != "DDS",
+ "The FBX file contains a texture with an unrecognized extension: " + file_extension_uppercase);
+
+ print_verbose("Getting FBX mapping mode for " + String(fbx_mapping_name.c_str()));
+ print_verbose("Created texture from embedded image.");
+ } else {
+ ERR_CONTINUE_MSG(true, "The FBX texture, with name: `" + texture_name + "`, is not found into the project nor is stored as embedded file. Make sure to insert the texture as embedded file or into the project, then reimport.");
+ for (FBXDocParser::LazyPropertyMap::value_type iter : material->Props()->GetLazyProperties()) {
+ const std::string name = iter.first;
+
+ if (name.empty()) {
+ continue;
+ }
+
+ PropertyDesc desc = PROPERTY_DESC_NOT_FOUND;
+ if (fbx_properties_desc.count(name) > 0) {
+ desc = fbx_properties_desc.at(name);
+ }
+
+ // check if we can ignore this it will be done at the next phase
+ if (desc == PROPERTY_DESC_NOT_FOUND || desc == PROPERTY_DESC_IGNORE) {
+ // count the texture mapping references. Skip this one if it's found and we can't look up a property value.
+ if (fbx_texture_map.count(name) > 0) {
+ continue; // safe to ignore it's a texture mapping.
+ }
+ }
+
+ if (desc == PROPERTY_DESC_IGNORE) {
+ //WARN_PRINT("[Ignored] The FBX material parameter: `" + String(name.c_str()) + "` is ignored.");
+ continue;
+ } else {
+ print_verbose("FBX Material parameter: " + String(name.c_str()));
+
+ // Check for Diffuse material system / lambert materials / legacy basically
+ if (name == "Diffuse" && !warning_non_pbr_material) {
+ ValidationTracker::get_singleton()->add_validation_error(state.path, "Invalid material settings change to Ai Standard Surface shader, mat name: " + material_name.c_escape());
+ warning_non_pbr_material = true;
+ }
+ }
+
+ // DISABLE when adding support for all weird and wonderful material formats
+ if (desc == PROPERTY_DESC_NOT_FOUND) {
+ continue;
+ }
+
+ ERR_CONTINUE_MSG(desc == PROPERTY_DESC_NOT_FOUND, "The FBX material parameter: `" + String(name.c_str()) + "` was not recognized. Please open an issue so we can add the support to it.");
+ ERR_FAIL_COND_V_MSG(p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct && p_mapping_data.index.size() == 0, (HashMap<int, R>()), "FBX importer needs to map correctly to this field, please specify the override index name to fix this problem!");
+ case FBXDocParser::MeshGeometry::MapType::none: {
+ // No data nothing to do.
+ return (HashMap<int, R>());
+ }
+ case FBXDocParser::MeshGeometry::MapType::vertex: {
+ ERR_FAIL_COND_V_MSG(p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct, (HashMap<int, R>()), "We will support in future");
+
+ if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
+ ERR_FAIL_COND_V_MSG((polygon_index + 1) != polygon_count, (HashMap<int, R>()), "FBX file seems corrupted: #ERR16. Not all Polygons are present in the file.");
+ } else {
+ // The data is mapped per polygon using a reference.
+ // The indices array, contains a *reference_id for each polygon.
+ // * Note that the reference_id is the id of data into the data array.
+ ERR_FAIL_COND_V_MSG((polygon_index + 1) != polygon_count, (HashMap<int, R>()), "FBX file seems corrupted: #ERR22. Not all Polygons are present in the file.");
+ }
+ } break;
+ case FBXDocParser::MeshGeometry::MapType::edge: {
+ if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
+ ERR_FAIL_COND_V_MSG(p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct && p_fbx_data.data.size() == 0, (HashMap<int, T>()), "invalid index to direct array");
+ const int polygon_count = count_polygons(p_polygon_indices);
+
+ // Aggregate vertex data.
+ HashMap<int, Vector<T>> aggregate_polygon_data;
+
+ switch (p_fbx_data.map_type) {
+ case FBXDocParser::MeshGeometry::MapType::none: {
+ // No data nothing to do.
+ return (HashMap<int, T>());
+ }
+ case FBXDocParser::MeshGeometry::MapType::vertex: {
+ ERR_FAIL_V_MSG((HashMap<int, T>()), "This data can't be extracted and organized per polygon, since into the FBX is mapped per vertex. This should not happen.");
+ } break;
+ case FBXDocParser::MeshGeometry::MapType::polygon_vertex: {
+ ERR_FAIL_V_MSG((HashMap<int, T>()), "This data can't be extracted and organized per polygon, since into the FBX is mapped per polygon vertex. This should not happen.");
+ } break;
+ case FBXDocParser::MeshGeometry::MapType::polygon: {
+ if (p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct) {
+ // The data is stored efficiently index_to_direct allows less data in the FBX file.
+ for (int polygon_index = 0;
+ polygon_index < polygon_count;
+ polygon_index += 1) {
+ if (p_fbx_data.index.size() == 0) {
+ ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_fbx_data.data.size(), (HashMap<int, T>()), "FBX file is corrupted: #ERR62");
+ case FBXDocParser::MeshGeometry::MapType::edge: {
+ ERR_FAIL_V_MSG((HashMap<int, T>()), "This data can't be extracted and organized per polygon, since into the FBX is mapped per edge. This should not happen.");
+ } break;
+ case FBXDocParser::MeshGeometry::MapType::all_the_same: {
+ // No matter the mode, no matter the data size; The first always win
+ // todo: move to document shutdown (will need to be validated after moving; this code has been validated already)
+ for (FBXDocParser::TokenPtr token : tokens) {
+ if (token) {
+ delete token;
+ token = nullptr;
+ }
+ }
+
+ return spatial;
+
+ } else {
+ print_error("Cannot import file: " + p_path + " version of file is unsupported, please re-export in your modelling package file version is: " + itos(doc.FBXVersion()));
+ // note: this could actually be unsafe this means we should be careful about continuing here, if we see bizzare effects later we should disable this.
+ // I am not sure if this is unsafe or not, testing will tell us this.
+ print_error("[doc] invalid fbx target detected for this track");
+ continue;
+ }
+
+ // everything in FBX and Maya is a node therefore if this happens something is seriously broken.
+ if (!state.fbx_target_map.has(target_id)) {
+ print_error("unable to resolve this to an FBX object.");
+ // You can see how the edges are stored into the FBX here: https://gist.github.com/AndreaCatania/da81840f5aa3b2feedf189e26c5a87e6
+ for (size_t i = 0; i < m_edges.size(); i += 1) {
+ ERR_FAIL_INDEX_MSG((size_t)m_edges[i], m_face_indices.size(), "The edge is pointing to a weird location in the face indices. The FBX is corrupted.");
+ int polygon_vertex_0 = m_face_indices[m_edges[i]];
+ int polygon_vertex_1;
+ if (polygon_vertex_0 < 0) {
+ // The polygon_vertex_0 points to the end of a polygon, so it's
+ // connected with the beginning of polygon in the edge list.
+
+ // Fist invert the vertex.
+ polygon_vertex_0 = ~polygon_vertex_0;
+
+ // Search the start vertex of the polygon.
+ // Iterate from the polygon_vertex_index backward till the start of
+ // the polygon is found.
+ ERR_FAIL_COND_MSG(m_edges[i] - 1 < 0, "The polygon is not yet started and we already need the final vertex. This FBX is corrupted.");
+ bool found_it = false;
+ for (int x = m_edges[i] - 1; x >= 0; x -= 1) {
+ if (x == 0) {
+ // This for sure is the start.
+ polygon_vertex_1 = m_face_indices[x];
+ found_it = true;
+ break;
+ } else if (m_face_indices[x] < 0) {
+ // This is the end of the previous polygon, so the next is
+ // the start of the polygon we need.
+ polygon_vertex_1 = m_face_indices[x + 1];
+ found_it = true;
+ break;
+ }
+ }
+ // As the algorithm above, this check is useless. Because the first
+ // ever vertex is always considered the begining of a polygon.
+ ERR_FAIL_COND_MSG(found_it == false, "Was not possible to find the first vertex of this polygon. FBX file is corrupted.");
+
+ } else {
+ ERR_FAIL_INDEX_MSG((size_t)(m_edges[i] + 1), m_face_indices.size(), "FBX The other FBX edge seems to point to an invalid vertices. This FBX file is corrupted.");
+ // We don't care if the `polygon_vertex_1` is the end of the polygon,
+ // for `polygon_vertex_1` so we can just invert it.
+ polygon_vertex_1 = ~polygon_vertex_1;
+ }
+
+ ERR_FAIL_COND_MSG(polygon_vertex_0 == polygon_vertex_1, "The vertices of this edge can't be the same, Is this a point???. This FBX file is corrupted.");
+/// Map Geometry stores the FBX file information.
+///
+/// # FBX doc.
+/// ## Reference type declared:
+/// - Direct (directly related to the mapping information type)
+/// - IndexToDirect (Map with key value, meaning depends on the MappingInformationType)
+///
+/// ## Map Type:
+/// * None The mapping is undetermined.
+/// * ByVertex There will be one mapping coordinate for each surface control point/vertex (ControlPoint is a vertex).
+/// * If you have direct reference type verticies[x]
+/// * If you have IndexToDirect reference type the UV
+/// * ByPolygonVertex There will be one mapping coordinate for each vertex, for every polygon of which it is a part. This means that a vertex will have as many mapping coordinates as polygons of which it is a part. (Sorted by polygon, referencing vertex)
+/// * ByPolygon There can be only one mapping coordinate for the whole polygon.
+/// * One mapping per polygon polygon x has this normal x
+/// * For each vertex of the polygon then set the normal to x
+/// * ByEdge There will be one mapping coordinate for each unique edge in the mesh. This is meant to be used with smoothing layer elements. (Mapping is referencing the edge id)
+/// * AllSame There can be only one mapping coordinate for the whole surface.
+class MeshGeometry : public Geometry {
+public:
+ enum class MapType {
+ none = 0, // No mapping type. Stored as "None".
+ vertex, // Maps per vertex. Stored as "ByVertice".
+ polygon_vertex, // Maps per polygon vertex. Stored as "ByPolygonVertex".
+ polygon, // Maps per polygon. Stored as "ByPolygon".
+ edge, // Maps per edge. Stored as "ByEdge".
+ all_the_same // Uaps to everything. Stored as "AllSame".
+ };
+
+ enum class ReferenceType {
+ direct = 0,
+ index = 1,
+ index_to_direct = 2
+ };
+
+ template <class T>
+ struct MappingData {
+ MapType map_type = MapType::none;
+ ReferenceType ref_type = ReferenceType::direct;
+ std::vector<T> data;
+ /// The meaning of the indices depends from the `MapType`.
+ /// If `ref_type` is `direct` this map is hollow.