diff --git a/COPYRIGHT.txt b/COPYRIGHT.txt
index b1323e04410..59c4dc40203 100644
--- a/COPYRIGHT.txt
+++ b/COPYRIGHT.txt
@@ -63,13 +63,6 @@ Copyright: 2011, Ole Kniemeyer, MAXON, www.maxon.net
2014-2022, Godot Engine contributors.
License: Expat and Zlib
-Files: ./modules/fbx/fbx_parser/
-Comment: Open Asset Import Library (FBX parser)
-Copyright: 2006-2020, assimp team
- 2007-2022, Juan Linietsky, Ariel Manzur.
- 2014-2022, Godot Engine contributors.
-License: BSD-3-clause
-
Files: ./platform/android/java/lib/aidl/com/android/*
./platform/android/java/lib/res/layout/status_bar_ongoing_event_progress_bar.xml
./platform/android/java/lib/src/com/google/android/*
diff --git a/doc/classes/EditorSceneFormatImporterFBX.xml b/doc/classes/EditorSceneFormatImporterFBX.xml
deleted file mode 100644
index 21aebd45076..00000000000
--- a/doc/classes/EditorSceneFormatImporterFBX.xml
+++ /dev/null
@@ -1,32 +0,0 @@
-
-
-
- FBX 3D asset importer.
-
-
- This is an FBX 3D asset importer with full support for most FBX features.
- If exporting a FBX scene from Autodesk Maya, use these FBX export settings:
- [codeblock]
- - Smoothing Groups
- - Smooth Mesh
- - Triangluate (for meshes with blend shapes)
- - Bake Animation
- - Resample All
- - Deformed Models
- - Skins
- - Blend Shapes
- - Curve Filters
- - Constant Key Reducer
- - Auto Tangents Only
- - *Do not check* Constraints (as it will break the file)
- - Can check Embed Media (embeds textures into the exported FBX file)
- - Note that when importing embedded media, the texture and mesh will be a single immutable file.
- - You will have to re-export then re-import the FBX if the texture has changed.
- - Units: Centimeters
- - Up Axis: Y
- - Binary format in FBX 2017
- [/codeblock]
-
-
-
-
diff --git a/modules/fbx/README.md b/modules/fbx/README.md
deleted file mode 100644
index 8eca4bd3c90..00000000000
--- a/modules/fbx/README.md
+++ /dev/null
@@ -1,196 +0,0 @@
-# Open Source FBX Specification for the Importer
-
-The goal of this document is to make everything in FBX clearly stated, any errors will be corrected over time this
-is a first draft.
-
-## fbx parser - originally from assimp
-
-- Folder: /modules/fbx/fbx_parser
-- Upstream: assimp
-- Original Version: git (308db73d0b3c2d1870cd3e465eaa283692a4cf23, 2019)
-- License: BSD-3-Clause
-
-This can never be updated from upstream, we have heavily modified the parser to provide memory safety and add some
-functionality. If anything we should give this parser back to assimp at some point as it has a lot of new features.
-
-# Updating assimp fbx parser
-
-Don't. It's not possible the code is rewritten in many areas to remove thirdparty deps and various bugs are fixed.
-
-Many days were put into rewriting the parser to use safe code and safe memory accessors.
-
-# File Headers
-
-FBX Binaries start with the header "Kaydara FBX Binary"
-
-FBX ASCII documents contain a larger header, sometimes with copyright information for a file.
-
-Detecting these is pretty simple.
-
-# What is an OP link?
-It's an object to property link. It lists the properties for that object in some cases. Source and destination based by
-ID.
-
-# What is a OO link?
-Its an object to object link, it contains the ID source and destination ID.
-
-# FBX Node connections
-
-Nodes in FBX are connected using OO links, This means Object to Object.
-
-FBX has a single other kind of link which is Object Property, this is used for Object to Property Links, this can be
- extra attributes, defaults, or even some simple settings.
-
-# Bones / Joints / Locators
-
-Bones in FBX are nodes, they initially have the Model:: Type, then have links to SubDeformer the sub deformer
-is part of the skin there is also an explicit Skin link, which then links to the geometry using OO links in the
-document.
-
-# Rotation Order in FBX compared to Godot
-
-**Godot uses the rotation order:** YXZ
-
-**FBX has dynamic rotation order to prevent gimbal lock with complex animations**
-
-```cpp
-enum RotOrder {
- RotOrder_EulerXYZ = 0
- RotOrder_EulerXZY,
- RotOrder_EulerYZX,
- RotOrder_EulerYXZ,
- RotOrder_EulerZXY,
- RotOrder_EulerZYX,
- RotOrder_SphericXYZ // nobody uses this - as far as we can tell
-};
-```
-
-
-# Pivot transforms
-
-### Pivot description:
-- Maya and 3DS max consider everything to be in node space (bones joints, skins, lights, cameras, etc)
-- Everything is a node, this means essentially nodes are auto or variants
-- They are local to the node in the tree.
-- They are used to calculate where a node is in space
-```c++
-// For a better reference you can check editor_scene_importer_fbx.h
-// references: GenFBXTransform / read the data in
-// references: ComputePivotTransform / run the calculation
-// This is the local pivot transform for the node, not the global transforms
-Transform ComputePivotTransform(
- Transform3D chain[TransformationComp_MAXIMUM],
- Transform3D &geometric_transform) {
- // Maya pivots
- Transform3D T = chain[TransformationComp_Translation];
- Transform3D Roff = chain[TransformationComp_RotationOffset];
- Transform3D Rp = chain[TransformationComp_RotationPivot];
- Transform3D Rpre = chain[TransformationComp_PreRotation];
- Transform3D R = chain[TransformationComp_Rotation];
- Transform3D Rpost = chain[TransformationComp_PostRotation];
- Transform3D Soff = chain[TransformationComp_ScalingOffset];
- Transform3D Sp = chain[TransformationComp_ScalingPivot];
- Transform3D S = chain[TransformationComp_Scaling];
-
- // 3DS Max Pivots
- Transform3D OT = chain[TransformationComp_GeometricTranslation];
- Transform3D OR = chain[TransformationComp_GeometricRotation];
- Transform3D 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
- TransformInheritance_MAX // end-of-enum sentinel
-};
-
-enum TransformInheritance {
- Transform_RrSs = 0,
- // Local scaling is offset by rotation of parent node
- Transform_RSrs = 1,
- // Parent * Local (normal mode)
- Transform_Rrs = 2,
- // Node transform scale is the only relevant component
- TransformInheritance_MAX // end-of-enum sentinel
-};
-```
-
-# Axis in FBX
-
-Godot has one format for the declared axis
-
-AXIS X, AXIS Y, -AXIS Z
-
-FBX supports any format you can think of. As it has to support Maya and 3DS Max.
-
-#### FBX File Header
-```json
-GlobalSettings: {
- Version: 1000
- Properties70: {
- P: "UpAxis", "int", "Integer", "",1
- P: "UpAxisSign", "int", "Integer", "",1
- P: "FrontAxis", "int", "Integer", "",2
- P: "FrontAxisSign", "int", "Integer", "",1
- P: "CoordAxis", "int", "Integer", "",0
- P: "CoordAxisSign", "int", "Integer", "",1
- P: "OriginalUpAxis", "int", "Integer", "",1
- P: "OriginalUpAxisSign", "int", "Integer", "",1
- P: "UnitScaleFactor", "double", "Number", "",1
- P: "OriginalUnitScaleFactor", "double", "Number", "",1
- P: "AmbientColor", "ColorRGB", "Color", "",0,0,0
- P: "DefaultCamera", "KString", "", "", "Producer Perspective"
- P: "TimeMode", "enum", "", "",6
- P: "TimeProtocol", "enum", "", "",2
- P: "SnapOnFrameMode", "enum", "", "",0
- P: "TimeSpanStart", "KTime", "Time", "",0
- P: "TimeSpanStop", "KTime", "Time", "",92372316000
- P: "CustomFrameRate", "double", "Number", "",-1
- P: "TimeMarker", "Compound", "", ""
- P: "CurrentTimeMarker", "int", "Integer", "",-1
- }
-}
-```
-
-#### 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.
diff --git a/modules/fbx/SCsub b/modules/fbx/SCsub
deleted file mode 100644
index 0311fddfee6..00000000000
--- a/modules/fbx/SCsub
+++ /dev/null
@@ -1,18 +0,0 @@
-#!/usr/bin/env python
-
-Import("env")
-Import("env_modules")
-
-env_fbx = env_modules.Clone()
-
-# Make includes relative to the folder path specified here so our includes are clean
-env_fbx.Prepend(CPPPATH=["#modules/fbx/"])
-
-if env["builtin_zlib"]:
- env_fbx.Prepend(CPPPATH=["#thirdparty/zlib/"])
-
-# Godot's own source files
-env_fbx.add_source_files(env.modules_sources, "tools/*.cpp")
-env_fbx.add_source_files(env.modules_sources, "data/*.cpp")
-env_fbx.add_source_files(env.modules_sources, "fbx_parser/*.cpp")
-env_fbx.add_source_files(env.modules_sources, "*.cpp")
diff --git a/modules/fbx/config.py b/modules/fbx/config.py
deleted file mode 100644
index 78929800b35..00000000000
--- a/modules/fbx/config.py
+++ /dev/null
@@ -1,16 +0,0 @@
-def can_build(env, platform):
- return env["tools"]
-
-
-def configure(env):
- pass
-
-
-def get_doc_classes():
- return [
- "EditorSceneImporterFBX",
- ]
-
-
-def get_doc_path():
- return "doc_classes"
diff --git a/modules/fbx/data/fbx_anim_container.h b/modules/fbx/data/fbx_anim_container.h
deleted file mode 100644
index 6559add8581..00000000000
--- a/modules/fbx/data/fbx_anim_container.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*************************************************************************/
-/* fbx_anim_container.h */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#ifndef FBX_ANIM_CONTAINER_H
-#define FBX_ANIM_CONTAINER_H
-
-#include "core/math/vector3.h"
-
-// Generic keyframes 99.99 percent of files will be vector3, except if quat interp is used, or visibility tracks
-// FBXTrack is used in a map in the implementation in fbx/editor_scene_importer_fbx.cpp
-// to avoid having to rewrite the entire logic I refactored this into the code instead.
-// once it works I can rewrite so we can add the fun misc features / small features
-struct FBXTrack {
- bool has_default = false;
- Vector3 default_value;
- std::map keyframes;
-};
-
-#endif //MODEL_ABSTRACTION_ANIM_CONTAINER_H
diff --git a/modules/fbx/data/fbx_bone.cpp b/modules/fbx/data/fbx_bone.cpp
deleted file mode 100644
index 72aba20fd40..00000000000
--- a/modules/fbx/data/fbx_bone.cpp
+++ /dev/null
@@ -1,56 +0,0 @@
-/*************************************************************************/
-/* fbx_bone.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#include "fbx_bone.h"
-
-#include "fbx_node.h"
-#include "import_state.h"
-
-Ref FBXSkinDeformer::get_link(const ImportState &state) const {
- print_verbose("bone name: " + bone->bone_name);
-
- // safe for when deformers must be polyfilled when skin has different count of binds to bones in the scene ;)
- if (!cluster) {
- return nullptr;
- }
-
- ERR_FAIL_COND_V_MSG(cluster->TargetNode() == nullptr, nullptr, "bone has invalid target node");
-
- Ref link_node;
- uint64_t id = cluster->TargetNode()->ID();
- if (state.fbx_target_map.has(id)) {
- link_node = state.fbx_target_map[id];
- } else {
- print_error("link node not found for " + itos(id));
- }
-
- // the node in space this is for, like if it's FOR a target.
- return link_node;
-}
diff --git a/modules/fbx/data/fbx_bone.h b/modules/fbx/data/fbx_bone.h
deleted file mode 100644
index 6c8f7f7cae6..00000000000
--- a/modules/fbx/data/fbx_bone.h
+++ /dev/null
@@ -1,90 +0,0 @@
-/*************************************************************************/
-/* fbx_bone.h */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#ifndef FBX_BONE_H
-#define FBX_BONE_H
-
-#include "fbx_node.h"
-#include "import_state.h"
-
-#include "fbx_parser/FBXDocument.h"
-
-struct PivotTransform;
-
-struct FBXBone : public RefCounted {
- uint64_t parent_bone_id = 0;
- uint64_t bone_id = 0;
-
- bool valid_parent = false; // if the parent bone id is set up.
- String bone_name = String(); // bone name
-
- bool is_root_bone() const {
- return !valid_parent;
- }
-
- // Godot specific data
- int godot_bone_id = -2; // godot internal bone id assigned after import
-
- // if a bone / armature is the root then FBX skeleton will contain the bone not any other skeleton.
- // this is to support joints by themselves in scenes
- bool valid_armature_id = false;
- uint64_t armature_id = 0;
-
- /* link node is the parent bone */
- mutable const FBXDocParser::Geometry *geometry = nullptr;
- mutable const FBXDocParser::ModelLimbNode *limb_node = nullptr;
-
- void set_node(Ref p_node) {
- node = p_node;
- }
-
- // Stores the pivot xform for this bone
-
- Ref node = nullptr;
- Ref parent_bone = nullptr;
- Ref fbx_skeleton = nullptr;
-};
-
-struct FBXSkinDeformer {
- FBXSkinDeformer(Ref p_bone, const FBXDocParser::Cluster *p_cluster) :
- cluster(p_cluster), bone(p_bone) {}
- ~FBXSkinDeformer() {}
- const FBXDocParser::Cluster *cluster;
- Ref bone;
-
- /* get associate model - the model can be invalid sometimes */
- Ref get_associate_model() const {
- return bone->parent_bone;
- }
-
- Ref get_link(const ImportState &state) const;
-};
-
-#endif // FBX_BONE_H
diff --git a/modules/fbx/data/fbx_material.cpp b/modules/fbx/data/fbx_material.cpp
deleted file mode 100644
index 36e20df3a98..00000000000
--- a/modules/fbx/data/fbx_material.cpp
+++ /dev/null
@@ -1,468 +0,0 @@
-/*************************************************************************/
-/* fbx_material.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#include "fbx_material.h"
-
-// FIXME: Shouldn't depend on core_bind.h! Use DirAccessRef like the rest of
-// the engine instead of core_bind::Directory.
-#include "core/core_bind.h"
-#include "scene/resources/material.h"
-#include "scene/resources/texture.h"
-#include "tools/validation_tools.h"
-
-String FBXMaterial::get_material_name() const {
- return material_name;
-}
-
-void FBXMaterial::set_imported_material(FBXDocParser::Material *p_material) {
- material = p_material;
-}
-
-void FBXMaterial::add_search_string(String p_filename, String p_current_directory, String search_directory, Vector &texture_search_paths) {
- if (search_directory.is_empty()) {
- texture_search_paths.push_back(p_current_directory.get_base_dir().plus_file(p_filename));
- } else {
- texture_search_paths.push_back(p_current_directory.get_base_dir().plus_file(search_directory + "/" + p_filename));
- texture_search_paths.push_back(p_current_directory.get_base_dir().plus_file("../" + search_directory + "/" + p_filename));
- }
-}
-
-String find_file(const String &p_base, const String &p_file_to_find) {
- core_bind::Directory dir;
- dir.open(p_base);
-
- dir.list_dir_begin();
- String n = dir.get_next();
- while (!n.is_empty()) {
- if (n == "." || n == "..") {
- n = dir.get_next();
- continue;
- }
- if (dir.current_is_dir()) {
- // Don't use `path_to` or the returned path will be wrong.
- const String f = find_file(p_base + "/" + n, p_file_to_find);
- if (!f.is_empty()) {
- return f;
- }
- } else if (n == p_file_to_find) {
- return p_base + "/" + n;
- }
- n = dir.get_next();
- }
- dir.list_dir_end();
-
- return String();
-}
-
-// fbx will not give us good path information and let's not regex them to fix them
-// no relative paths are in fbx generally they have a rel field but it's populated incorrectly by the SDK.
-String FBXMaterial::find_texture_path_by_filename(const String p_filename, const String p_current_directory) {
- core_bind::Directory dir;
- Vector paths;
- add_search_string(p_filename, p_current_directory, "", paths);
- add_search_string(p_filename, p_current_directory, "texture", paths);
- add_search_string(p_filename, p_current_directory, "textures", paths);
- add_search_string(p_filename, p_current_directory, "Textures", paths);
- add_search_string(p_filename, p_current_directory, "materials", paths);
- add_search_string(p_filename, p_current_directory, "mats", paths);
- add_search_string(p_filename, p_current_directory, "pictures", paths);
- add_search_string(p_filename, p_current_directory, "images", paths);
-
- for (int i = 0; i < paths.size(); i++) {
- if (dir.file_exists(paths[i])) {
- return paths[i];
- }
- }
-
- // We were not able to find the texture in the common locations,
- // try to find it into the project globally.
- // The common textures can be stored into one of those folders:
- // res://asset
- // res://texture
- // res://material
- // res://mat
- // res://image
- // res://picture
- //
- // Note the folders can also be called with custom names, like:
- // res://my_assets
- // since the keyword `asset` is into the directory name the textures will be
- // searched there too.
-
- dir.open("res://");
- dir.list_dir_begin();
- String n = dir.get_next();
- while (!n.is_empty()) {
- if (n == "." || n == "..") {
- n = dir.get_next();
- continue;
- }
- if (dir.current_is_dir()) {
- const String lower_n = n.to_lower();
- if (
- // Don't need to use plural.
- lower_n.find("asset") >= 0 ||
- lower_n.find("texture") >= 0 ||
- lower_n.find("material") >= 0 ||
- lower_n.find("mat") >= 0 ||
- lower_n.find("image") >= 0 ||
- lower_n.find("picture") >= 0) {
- // Don't use `path_to` or the returned path will be wrong.
- const String f = find_file(String("res://") + n, p_filename);
- if (!f.is_empty()) {
- return f;
- }
- }
- }
- n = dir.get_next();
- }
- dir.list_dir_end();
-
- return "";
-}
-
-template
-T extract_from_prop(FBXDocParser::PropertyPtr prop, const T &p_default, const std::string &p_name, const String &p_type) {
- ERR_FAIL_COND_V_MSG(prop == nullptr, p_default, "invalid property passed to extractor");
- const FBXDocParser::TypedProperty *val = dynamic_cast *>(prop);
-
- 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);
- // Make sure to not lost any eventual opacity.
- return val->Value();
-}
-
-Ref FBXMaterial::import_material(ImportState &state) {
- ERR_FAIL_COND_V(material == nullptr, nullptr);
-
- const String p_fbx_current_directory = state.path;
-
- Ref spatial_material;
- spatial_material.instantiate();
-
- // read the material file
- // is material two sided
- // read material name
- print_verbose("[material] material name: " + ImportUtils::FBXNodeToName(material->Name()));
-
- material_name = ImportUtils::FBXNodeToName(material->Name());
-
- for (const std::pair iter : material->Textures()) {
- const uint64_t texture_id = iter.second->ID();
- const std::string &fbx_mapping_name = iter.first;
- const FBXDocParser::Texture *fbx_texture_data = iter.second;
- const String absolute_texture_path = iter.second->FileName().c_str();
- const String texture_name = absolute_texture_path.get_file();
- const String file_extension = absolute_texture_path.get_extension().to_upper();
-
- const String debug_string = "texture id: " + itos(texture_id) + " texture name: " + String(iter.second->Name().c_str()) + " mapping name: " + String(fbx_mapping_name.c_str());
- // remember errors STILL need this string at the end for when you aren't in verbose debug mode :) they need context for when you're not verbose-ing.
- print_verbose(debug_string);
-
- const String file_extension_uppercase = file_extension.to_upper();
-
- if (fbx_transparency_flags.count(fbx_mapping_name) > 0) {
- // just enable it later let's make this fine-tuned.
- spatial_material->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA);
- }
-
- ERR_CONTINUE_MSG(file_extension.is_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()));
- // get the texture map type
- const StandardMaterial3D::TextureParam mapping_mode = fbx_texture_map.at(fbx_mapping_name);
- print_verbose("Set FBX mapping mode to " + get_texture_param_name(mapping_mode));
-
- Ref texture;
- print_verbose("texture mapping name: " + texture_name);
-
- if (state.cached_image_searches.has(texture_name)) {
- texture = state.cached_image_searches[texture_name];
- } else {
- String path = find_texture_path_by_filename(texture_name, p_fbx_current_directory);
- if (!path.is_empty()) {
- Ref image_texture = ResourceLoader::load(path);
-
- ERR_CONTINUE(image_texture.is_null());
-
- texture = image_texture;
- state.cached_image_searches.insert(texture_name, texture);
- print_verbose("Created texture from loaded image file.");
-
- } else if (fbx_texture_data != nullptr && fbx_texture_data->Media() != nullptr && fbx_texture_data->Media()->IsEmbedded()) {
- // This is an embedded texture. Extract it.
- Ref image;
- //image.instantiate(); // oooo double instance bug? why make Image::_png_blah call
-
- const String extension = texture_name.get_extension().to_upper();
- if (extension == "PNG") {
- // The stored file is a PNG.
- image = Image::_png_mem_loader_func(fbx_texture_data->Media()->Content(), fbx_texture_data->Media()->ContentLength());
- ERR_CONTINUE_MSG(image.is_valid() == false, "FBX Embedded PNG image load fail.");
-
- } else if (
- extension == "JPEG" ||
- extension == "JPG") {
- // The stored file is a JPEG.
- image = Image::_jpg_mem_loader_func(fbx_texture_data->Media()->Content(), fbx_texture_data->Media()->ContentLength());
- ERR_CONTINUE_MSG(image.is_valid() == false, "FBX Embedded JPEG image load fail.");
-
- } else if (extension == "TGA") {
- // The stored file is a TGA.
- image = Image::_tga_mem_loader_func(fbx_texture_data->Media()->Content(), fbx_texture_data->Media()->ContentLength());
- ERR_CONTINUE_MSG(image.is_valid() == false, "FBX Embedded TGA image load fail.");
-
- } else if (extension == "WEBP") {
- // The stored file is a WEBP.
- image = Image::_webp_mem_loader_func(fbx_texture_data->Media()->Content(), fbx_texture_data->Media()->ContentLength());
- ERR_CONTINUE_MSG(image.is_valid() == false, "FBX Embedded WEBP image load fail.");
-
- // } else if (extension == "DDS") {
- // // In this moment is not possible to extract a DDS from a buffer, TODO consider add it to godot. See `textureloader_dds.cpp::load().
- // // The stored file is a DDS.
- } else {
- ERR_CONTINUE_MSG(true, "The embedded image with extension: " + extension + " is not yet supported. Open an issue please.");
- }
-
- Ref image_texture;
- image_texture.instantiate();
- image_texture->create_from_image(image);
-
- texture = image_texture;
-
- // TODO: this is potentially making something with the same name have a match incorrectly USE FBX ID as Hash. #fuck it later.
- state.cached_image_searches[texture_name] = texture;
- 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.");
- }
- }
-
- spatial_material->set_texture(mapping_mode, texture);
- }
-
- if (spatial_material.is_valid()) {
- spatial_material->set_name(material_name);
- }
-
- /// ALL below is related to properties
- for (FBXDocParser::LazyPropertyMap::value_type iter : material->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.");
-
- const FBXDocParser::PropertyTable *tbl = material;
- FBXDocParser::PropertyPtr prop = tbl->Get(name);
-
- ERR_CONTINUE_MSG(prop == nullptr, "This file may be corrupted because is not possible to extract the material parameter: " + String(name.c_str()));
-
- if (spatial_material.is_null()) {
- // Done here so if no data no material is created.
- spatial_material.instantiate();
- }
-
- const FBXDocParser::TypedProperty *real_value = dynamic_cast *>(prop);
- const FBXDocParser::TypedProperty *vector_value = dynamic_cast *>(prop);
-
- if (!real_value && !vector_value) {
- //WARN_PRINT("unsupported datatype in property: " + String(name.c_str()));
- continue;
- }
-
- if (vector_value && !real_value) {
- if (vector_value->Value() == Vector3(0, 0, 0) && !real_value) {
- continue;
- }
- }
-
- switch (desc) {
- case PROPERTY_DESC_ALBEDO_COLOR: {
- if (vector_value) {
- const Vector3 &color = vector_value->Value();
- // Make sure to not lost any eventual opacity.
- if (color != Vector3(0, 0, 0)) {
- Color c = Color();
- c[0] = color[0];
- c[1] = color[1];
- c[2] = color[2];
- spatial_material->set_albedo(c);
- }
-
- } else if (real_value) {
- print_error("albedo is unsupported format?");
- }
- } break;
- case PROPERTY_DESC_TRANSPARENT: {
- if (real_value) {
- const real_t opacity = real_value->Value();
- if (opacity < (1.0 - CMP_EPSILON)) {
- Color c = spatial_material->get_albedo();
- c.a = opacity;
- spatial_material->set_albedo(c);
-
- spatial_material->set_transparency(BaseMaterial3D::TRANSPARENCY_ALPHA);
- spatial_material->set_depth_draw_mode(BaseMaterial3D::DEPTH_DRAW_OPAQUE_ONLY);
- }
- } else if (vector_value) {
- print_error("unsupported transparent desc type vector!");
- }
- } break;
- case PROPERTY_DESC_SPECULAR: {
- if (real_value) {
- print_verbose("specular real value: " + rtos(real_value->Value()));
- spatial_material->set_specular(MIN(1.0, real_value->Value()));
- }
-
- if (vector_value) {
- print_error("unsupported specular vector value: " + vector_value->Value());
- }
- } break;
-
- case PROPERTY_DESC_SPECULAR_COLOR: {
- if (vector_value) {
- print_error("unsupported specular color: " + vector_value->Value());
- }
- } break;
- case PROPERTY_DESC_SHINYNESS: {
- if (real_value) {
- print_error("unsupported shinyness:" + rtos(real_value->Value()));
- }
- } break;
- case PROPERTY_DESC_METALLIC: {
- if (real_value) {
- print_verbose("metallic real value: " + rtos(real_value->Value()));
- spatial_material->set_metallic(MIN(1.0f, real_value->Value()));
- } else {
- print_error("unsupported value type for metallic");
- }
- } break;
- case PROPERTY_DESC_ROUGHNESS: {
- if (real_value) {
- print_verbose("roughness real value: " + rtos(real_value->Value()));
- spatial_material->set_roughness(MIN(1.0f, real_value->Value()));
- } else {
- print_error("unsupported value type for roughness");
- }
- } break;
- case PROPERTY_DESC_COAT: {
- if (real_value) {
- print_verbose("clearcoat real value: " + rtos(real_value->Value()));
- spatial_material->set_clearcoat(MIN(1.0f, real_value->Value()));
- } else {
- print_error("unsupported value type for clearcoat");
- }
- } break;
- case PROPERTY_DESC_COAT_ROUGHNESS: {
- // meaning is that approx equal to zero is disabled not actually zero. ;)
- if (real_value && Math::is_zero_approx(real_value->Value())) {
- print_verbose("clearcoat real value: " + rtos(real_value->Value()));
- spatial_material->set_clearcoat_roughness(real_value->Value());
- } else {
- print_error("unsupported value type for clearcoat gloss");
- }
- } break;
- case PROPERTY_DESC_EMISSIVE: {
- if (real_value && Math::is_zero_approx(real_value->Value())) {
- print_verbose("Emissive real value: " + rtos(real_value->Value()));
- spatial_material->set_emission_energy(real_value->Value());
- } else if (vector_value && !vector_value->Value().is_equal_approx(Vector3(0, 0, 0))) {
- const Vector3 &color = vector_value->Value();
- Color c;
- c[0] = color[0];
- c[1] = color[1];
- c[2] = color[2];
- spatial_material->set_emission(c);
- }
- } break;
- case PROPERTY_DESC_EMISSIVE_COLOR: {
- if (vector_value && !vector_value->Value().is_equal_approx(Vector3(0, 0, 0))) {
- const Vector3 &color = vector_value->Value();
- Color c;
- c[0] = color[0];
- c[1] = color[1];
- c[2] = color[2];
- spatial_material->set_emission(c);
- } else {
- print_error("unsupported value type for emissive color");
- }
- } break;
- case PROPERTY_DESC_NOT_FOUND:
- case PROPERTY_DESC_IGNORE:
- break;
- default:
- break;
- }
- }
-
- return spatial_material;
-}
diff --git a/modules/fbx/data/fbx_material.h b/modules/fbx/data/fbx_material.h
deleted file mode 100644
index e20b43561b9..00000000000
--- a/modules/fbx/data/fbx_material.h
+++ /dev/null
@@ -1,285 +0,0 @@
-/*************************************************************************/
-/* fbx_material.h */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#ifndef FBX_MATERIAL_H
-#define FBX_MATERIAL_H
-
-#include "tools/import_utils.h"
-
-#include "core/object/ref_counted.h"
-#include "core/string/ustring.h"
-
-struct FBXMaterial : public RefCounted {
- String material_name = String();
- bool warning_non_pbr_material = false;
- FBXDocParser::Material *material = nullptr;
-
- /* Godot materials
- *** Texture Maps:
- * Albedo - color, texture
- * Metallic - specular, metallic, texture
- * Roughness - roughness, texture
- * Emission - color, texture
- * Normal Map - scale, texture
- * Ambient Occlusion - texture
- * Refraction - scale, texture
- *** Has Settings for:
- * UV1 - SCALE, OFFSET
- * UV2 - SCALE, OFFSET
- *** Flags for
- * Transparent
- * Cull Mode
- */
-
- enum class MapMode {
- AlbedoM = 0,
- MetallicM,
- SpecularM,
- EmissionM,
- RoughnessM,
- NormalM,
- AmbientOcclusionM,
- RefractionM,
- ReflectionM,
- };
-
- /* Returns the string representation of the TextureParam enum */
- static String get_texture_param_name(StandardMaterial3D::TextureParam param) {
- switch (param) {
- case StandardMaterial3D::TEXTURE_ALBEDO:
- return "TEXTURE_ALBEDO";
- case StandardMaterial3D::TEXTURE_METALLIC:
- return "TEXTURE_METALLIC";
- case StandardMaterial3D::TEXTURE_ROUGHNESS:
- return "TEXTURE_ROUGHNESS";
- case StandardMaterial3D::TEXTURE_EMISSION:
- return "TEXTURE_EMISSION";
- case StandardMaterial3D::TEXTURE_NORMAL:
- return "TEXTURE_NORMAL";
- case StandardMaterial3D::TEXTURE_RIM:
- return "TEXTURE_RIM";
- case StandardMaterial3D::TEXTURE_CLEARCOAT:
- return "TEXTURE_CLEARCOAT";
- case StandardMaterial3D::TEXTURE_FLOWMAP:
- return "TEXTURE_FLOWMAP";
- case StandardMaterial3D::TEXTURE_AMBIENT_OCCLUSION:
- return "TEXTURE_AMBIENT_OCCLUSION";
- // case StandardMaterial3D::TEXTURE_DEPTH: // TODO: work out how to make this function again!
- // return "TEXTURE_DEPTH";
- case StandardMaterial3D::TEXTURE_SUBSURFACE_SCATTERING:
- return "TEXTURE_SUBSURFACE_SCATTERING";
- // case StandardMaterial3D::TEXTURE_TRANSMISSION: // TODO: work out how to make this function again!
- // return "TEXTURE_TRANSMISSION";
- case StandardMaterial3D::TEXTURE_REFRACTION:
- return "TEXTURE_REFRACTION";
- case StandardMaterial3D::TEXTURE_DETAIL_MASK:
- return "TEXTURE_DETAIL_MASK";
- case StandardMaterial3D::TEXTURE_DETAIL_ALBEDO:
- return "TEXTURE_DETAIL_ALBEDO";
- case StandardMaterial3D::TEXTURE_DETAIL_NORMAL:
- return "TEXTURE_DETAIL_NORMAL";
- case StandardMaterial3D::TEXTURE_MAX:
- return "TEXTURE_MAX";
- default:
- return "broken horribly";
- }
- };
-
- // TODO make this static?
- const std::map fbx_transparency_flags = {
- /* Transparent */
- { "TransparentColor", true },
- { "Maya|opacity", true }
- };
-
- // TODO make this static?
- const std::map fbx_texture_map = {
- /* Diffuse */
- { "Maya|base", StandardMaterial3D::TextureParam::TEXTURE_ALBEDO },
- { "DiffuseColor", StandardMaterial3D::TextureParam::TEXTURE_ALBEDO },
- { "Maya|DiffuseTexture", StandardMaterial3D::TextureParam::TEXTURE_ALBEDO },
- { "Maya|baseColor", StandardMaterial3D::TextureParam::TEXTURE_ALBEDO },
- { "Maya|baseColor|file", StandardMaterial3D::TextureParam::TEXTURE_ALBEDO },
- { "3dsMax|Parameters|base_color_map", StandardMaterial3D::TextureParam::TEXTURE_ALBEDO },
- { "Maya|TEX_color_map|file", StandardMaterial3D::TextureParam::TEXTURE_ALBEDO },
- { "Maya|TEX_color_map", StandardMaterial3D::TextureParam::TEXTURE_ALBEDO },
- /* Emission */
- { "EmissiveColor", StandardMaterial3D::TextureParam::TEXTURE_EMISSION },
- { "EmissiveFactor", StandardMaterial3D::TextureParam::TEXTURE_EMISSION },
- { "Maya|emissionColor", StandardMaterial3D::TextureParam::TEXTURE_EMISSION },
- { "Maya|emissionColor|file", StandardMaterial3D::TextureParam::TEXTURE_EMISSION },
- { "3dsMax|Parameters|emission_map", StandardMaterial3D::TextureParam::TEXTURE_EMISSION },
- { "Maya|TEX_emissive_map", StandardMaterial3D::TextureParam::TEXTURE_EMISSION },
- { "Maya|TEX_emissive_map|file", StandardMaterial3D::TextureParam::TEXTURE_EMISSION },
- /* Metallic */
- { "Maya|metalness", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
- { "Maya|metalness|file", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
- { "3dsMax|Parameters|metalness_map", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
- { "Maya|TEX_metallic_map", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
- { "Maya|TEX_metallic_map|file", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
-
- /* Roughness */
- // Arnold Roughness Map
- { "Maya|specularRoughness", StandardMaterial3D::TextureParam::TEXTURE_ROUGHNESS },
-
- { "3dsMax|Parameters|roughness_map", StandardMaterial3D::TextureParam::TEXTURE_ROUGHNESS },
- { "Maya|TEX_roughness_map", StandardMaterial3D::TextureParam::TEXTURE_ROUGHNESS },
- { "Maya|TEX_roughness_map|file", StandardMaterial3D::TextureParam::TEXTURE_ROUGHNESS },
-
- /* Normal */
- { "NormalMap", StandardMaterial3D::TextureParam::TEXTURE_NORMAL },
- //{ "Bump", Material::TextureParam::TEXTURE_NORMAL },
- //{ "3dsMax|Parameters|bump_map", Material::TextureParam::TEXTURE_NORMAL },
- { "Maya|NormalTexture", StandardMaterial3D::TextureParam::TEXTURE_NORMAL },
- //{ "Maya|normalCamera", Material::TextureParam::TEXTURE_NORMAL },
- //{ "Maya|normalCamera|file", Material::TextureParam::TEXTURE_NORMAL },
- { "Maya|TEX_normal_map", StandardMaterial3D::TextureParam::TEXTURE_NORMAL },
- { "Maya|TEX_normal_map|file", StandardMaterial3D::TextureParam::TEXTURE_NORMAL },
- /* AO */
- { "Maya|TEX_ao_map", StandardMaterial3D::TextureParam::TEXTURE_AMBIENT_OCCLUSION },
- { "Maya|TEX_ao_map|file", StandardMaterial3D::TextureParam::TEXTURE_AMBIENT_OCCLUSION },
-
- // TODO: specular workflow conversion
- // { "SpecularColor", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
- // { "Maya|specularColor", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
- // { "Maya|SpecularTexture", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
- // { "Maya|SpecularTexture|file", StandardMaterial3D::TextureParam::TEXTURE_METALLIC },
- // { "ShininessExponent", SpatialMaterial::TextureParam::UNSUPPORTED },
- // { "ReflectionFactor", SpatialMaterial::TextureParam::UNSUPPORTED },
-
- //{ "TransparentColor",SpatialMaterial::TextureParam::TEXTURE_CHANNEL_ALPHA },
- //{ "TransparencyFactor",SpatialMaterial::TextureParam::TEXTURE_CHANNEL_ALPHA }
-
- // TODO: diffuse roughness
- //{ "Maya|diffuseRoughness", SpatialMaterial::TextureParam::UNSUPPORTED },
- //{ "Maya|diffuseRoughness|file", SpatialMaterial::TextureParam::UNSUPPORTED },
-
- };
-
- // TODO make this static?
- enum PropertyDesc {
- PROPERTY_DESC_NOT_FOUND,
- PROPERTY_DESC_ALBEDO_COLOR,
- PROPERTY_DESC_TRANSPARENT,
- PROPERTY_DESC_METALLIC,
- PROPERTY_DESC_ROUGHNESS,
- PROPERTY_DESC_SPECULAR,
- PROPERTY_DESC_SPECULAR_COLOR,
- PROPERTY_DESC_SHINYNESS,
- PROPERTY_DESC_COAT,
- PROPERTY_DESC_COAT_ROUGHNESS,
- PROPERTY_DESC_EMISSIVE,
- PROPERTY_DESC_EMISSIVE_COLOR,
- PROPERTY_DESC_IGNORE
- };
-
- const std::map fbx_properties_desc = {
- /* Albedo */
- { "DiffuseColor", PROPERTY_DESC_ALBEDO_COLOR },
- { "Maya|baseColor", PROPERTY_DESC_ALBEDO_COLOR },
-
- /* Specular */
- { "Maya|specular", PROPERTY_DESC_SPECULAR },
- { "Maya|specularColor", PROPERTY_DESC_SPECULAR_COLOR },
-
- /* Specular roughness - arnold roughness map */
- { "Maya|specularRoughness", PROPERTY_DESC_ROUGHNESS },
-
- /* Transparent */
- { "Opacity", PROPERTY_DESC_TRANSPARENT },
- { "TransparencyFactor", PROPERTY_DESC_TRANSPARENT },
- { "Maya|opacity", PROPERTY_DESC_TRANSPARENT },
-
- { "Maya|metalness", PROPERTY_DESC_METALLIC },
- { "Maya|metallic", PROPERTY_DESC_METALLIC },
-
- /* Roughness */
- { "Maya|roughness", PROPERTY_DESC_ROUGHNESS },
-
- /* Coat */
- //{ "Maya|coat", PROPERTY_DESC_COAT },
-
- /* Coat roughness */
- //{ "Maya|coatRoughness", PROPERTY_DESC_COAT_ROUGHNESS },
-
- /* Emissive */
- { "Maya|emission", PROPERTY_DESC_EMISSIVE },
- { "Maya|emissive", PROPERTY_DESC_EMISSIVE },
-
- /* Emissive color */
- { "EmissiveColor", PROPERTY_DESC_EMISSIVE_COLOR },
- { "Maya|emissionColor", PROPERTY_DESC_EMISSIVE_COLOR },
-
- /* Ignore */
- { "Shininess", PROPERTY_DESC_IGNORE },
- { "Reflectivity", PROPERTY_DESC_IGNORE },
- { "Maya|diffuseRoughness", PROPERTY_DESC_IGNORE },
- { "Maya", PROPERTY_DESC_IGNORE },
- { "Diffuse", PROPERTY_DESC_ALBEDO_COLOR },
- { "Maya|TypeId", PROPERTY_DESC_IGNORE },
- { "Ambient", PROPERTY_DESC_IGNORE },
- { "AmbientColor", PROPERTY_DESC_IGNORE },
- { "ShininessExponent", PROPERTY_DESC_IGNORE },
- { "Specular", PROPERTY_DESC_IGNORE },
- { "SpecularColor", PROPERTY_DESC_IGNORE },
- { "SpecularFactor", PROPERTY_DESC_IGNORE },
- //{ "BumpFactor", PROPERTY_DESC_IGNORE },
- { "Maya|exitToBackground", PROPERTY_DESC_IGNORE },
- { "Maya|indirectDiffuse", PROPERTY_DESC_IGNORE },
- { "Maya|indirectSpecular", PROPERTY_DESC_IGNORE },
- { "Maya|internalReflections", PROPERTY_DESC_IGNORE },
- { "DiffuseFactor", PROPERTY_DESC_IGNORE },
- { "AmbientFactor", PROPERTY_DESC_IGNORE },
- { "ReflectionColor", PROPERTY_DESC_IGNORE },
- { "Emissive", PROPERTY_DESC_IGNORE },
- { "Maya|coatColor", PROPERTY_DESC_IGNORE },
- { "Maya|coatNormal", PROPERTY_DESC_IGNORE },
- { "Maya|coatIOR", PROPERTY_DESC_IGNORE },
- };
-
- /* storing the texture properties like color */
- template
- struct TexturePropertyMapping : RefCounted {
- StandardMaterial3D::TextureParam map_mode = StandardMaterial3D::TextureParam::TEXTURE_ALBEDO;
- const T property = T();
- };
-
- static void add_search_string(String p_filename, String p_current_directory, String search_directory, Vector &texture_search_paths);
-
- static String find_texture_path_by_filename(const String p_filename, const String p_current_directory);
-
- String get_material_name() const;
-
- void set_imported_material(FBXDocParser::Material *p_material);
-
- Ref import_material(ImportState &state);
-};
-
-#endif // FBX_MATERIAL_H
diff --git a/modules/fbx/data/fbx_mesh_data.cpp b/modules/fbx/data/fbx_mesh_data.cpp
deleted file mode 100644
index 1d598517787..00000000000
--- a/modules/fbx/data/fbx_mesh_data.cpp
+++ /dev/null
@@ -1,1446 +0,0 @@
-/*************************************************************************/
-/* fbx_mesh_data.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#include "fbx_mesh_data.h"
-
-#include "core/templates/local_vector.h"
-#include "scene/resources/importer_mesh.h"
-#include "scene/resources/mesh.h"
-#include "scene/resources/surface_tool.h"
-
-#include "thirdparty/misc/polypartition.h"
-
-template
-T collect_first(const Vector> *p_data, T p_fall_back) {
- if (p_data->is_empty()) {
- return p_fall_back;
- }
-
- return (*p_data)[0].data;
-}
-
-template
-HashMap collect_all(const Vector> *p_data, HashMap p_fall_back) {
- if (p_data->is_empty()) {
- return p_fall_back;
- }
-
- HashMap collection;
- for (int i = 0; i < p_data->size(); i += 1) {
- const VertexData &vd = (*p_data)[i];
- collection[vd.polygon_index] = vd.data;
- }
- return collection;
-}
-
-template
-T collect_average(const Vector> *p_data, T p_fall_back) {
- if (p_data->is_empty()) {
- return p_fall_back;
- }
-
- T combined = (*p_data)[0].data; // Make sure the data is always correctly initialized.
- print_verbose("size of data: " + itos(p_data->size()));
- for (int i = 1; i < p_data->size(); i += 1) {
- combined += (*p_data)[i].data;
- }
- combined = combined / real_t(p_data->size());
-
- return combined.normalized();
-}
-
-HashMap collect_normal(const Vector> *p_data, HashMap p_fall_back) {
- if (p_data->is_empty()) {
- return p_fall_back;
- }
-
- HashMap collection;
- for (int i = 0; i < p_data->size(); i += 1) {
- const VertexData &vd = (*p_data)[i];
- collection[vd.polygon_index] = vd.data;
- }
- return collection;
-}
-
-HashMap collect_uv(const Vector> *p_data, HashMap p_fall_back) {
- if (p_data->is_empty()) {
- return p_fall_back;
- }
-
- HashMap collection;
- for (int i = 0; i < p_data->size(); i += 1) {
- const VertexData &vd = (*p_data)[i];
- collection[vd.polygon_index] = vd.data;
- }
- return collection;
-}
-
-ImporterMeshInstance3D *FBXMeshData::create_fbx_mesh(const ImportState &state, const FBXDocParser::MeshGeometry *p_mesh_geometry, const FBXDocParser::Model *model, bool use_compression) {
- mesh_geometry = p_mesh_geometry;
- // todo: make this just use a uint64_t FBX ID this is a copy of our original materials unfortunately.
- const std::vector &material_lookup = model->GetMaterials();
-
- // TODO: perf hotspot on large files
- // this can be a very large copy
- std::vector polygon_indices = mesh_geometry->get_polygon_indices();
- std::vector vertices = mesh_geometry->get_vertices();
-
- // Phase 1. Parse all FBX data.
- HashMap normals;
- HashMap> normals_raw = extract_per_vertex_data(
- vertices.size(),
- mesh_geometry->get_edge_map(),
- polygon_indices,
- mesh_geometry->get_normals(),
- &collect_all,
- HashMap());
-
- HashMap uvs_0;
- HashMap> uvs_0_raw = extract_per_vertex_data(
- vertices.size(),
- mesh_geometry->get_edge_map(),
- polygon_indices,
- mesh_geometry->get_uv_0(),
- &collect_all,
- HashMap());
-
- HashMap uvs_1;
- HashMap> uvs_1_raw = extract_per_vertex_data(
- vertices.size(),
- mesh_geometry->get_edge_map(),
- polygon_indices,
- mesh_geometry->get_uv_1(),
- &collect_all,
- HashMap());
-
- HashMap colors;
- HashMap> colors_raw = extract_per_vertex_data(
- vertices.size(),
- mesh_geometry->get_edge_map(),
- polygon_indices,
- mesh_geometry->get_colors(),
- &collect_all,
- HashMap());
-
- // TODO what about tangents?
- // TODO what about bi-nomials?
- // TODO there is other?
-
- HashMap polygon_surfaces = extract_per_polygon(
- vertices.size(),
- polygon_indices,
- mesh_geometry->get_material_allocation_id(),
- -1);
-
- HashMap morphs;
- extract_morphs(mesh_geometry, morphs);
-
- // TODO please add skinning.
- //mesh_id = mesh_geometry->ID();
-
- sanitize_vertex_weights(state);
-
- // Reorganize polygon vertices to correctly take into account strange
- // UVs.
- reorganize_vertices(
- polygon_indices,
- vertices,
- normals,
- uvs_0,
- uvs_1,
- colors,
- morphs,
- normals_raw,
- colors_raw,
- uvs_0_raw,
- uvs_1_raw);
-
- const int color_count = colors.size();
- print_verbose("Vertex color count: " + itos(color_count));
-
- // Make sure that from this moment on the mesh_geometry is no used anymore.
- // This is a safety step, because the mesh_geometry data are no more valid
- // at this point.
-
- const int vertex_count = vertices.size();
-
- print_verbose("Vertex count: " + itos(vertex_count));
-
- // The map key is the material allocator id that is also used as surface id.
- HashMap surfaces;
-
- // Phase 2. For each material create a surface tool (So a different mesh).
- {
- if (polygon_surfaces.is_empty()) {
- // No material, just use the default one with index -1.
- // Set -1 to all polygons.
- const int polygon_count = count_polygons(polygon_indices);
- for (int p = 0; p < polygon_count; p += 1) {
- polygon_surfaces[p] = -1;
- }
- }
-
- // Create the surface now.
- for (const int *polygon_id = polygon_surfaces.next(nullptr); polygon_id != nullptr; polygon_id = polygon_surfaces.next(polygon_id)) {
- const int surface_id = polygon_surfaces[*polygon_id];
- if (surfaces.has(surface_id) == false) {
- SurfaceData sd;
- sd.surface_tool.instantiate();
- sd.surface_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
-
- if (surface_id < 0) {
- // nothing to do
- } else if (surface_id < (int)material_lookup.size()) {
- const FBXDocParser::Material *mat_mapping = material_lookup.at(surface_id);
- const uint64_t mapping_id = mat_mapping->ID();
- if (state.cached_materials.has(mapping_id)) {
- sd.material = state.cached_materials[mapping_id];
- }
- } else {
- WARN_PRINT("out of bounds surface detected, FBX file has corrupt material data");
- }
-
- surfaces.set(surface_id, sd);
- }
- }
- }
-
- // Phase 3. Map the vertices relative to each surface, in this way we can
- // just insert the vertices that we need per each surface.
- {
- PolygonId polygon_index = -1;
- SurfaceId surface_id = -1;
- SurfaceData *surface_data = nullptr;
-
- for (size_t polygon_vertex = 0; polygon_vertex < polygon_indices.size(); polygon_vertex += 1) {
- if (is_start_of_polygon(polygon_indices, polygon_vertex)) {
- polygon_index += 1;
- ERR_FAIL_COND_V_MSG(polygon_surfaces.has(polygon_index) == false, nullptr, "The FBX file is corrupted, This surface_index is not expected.");
- surface_id = polygon_surfaces[polygon_index];
- surface_data = surfaces.getptr(surface_id);
- CRASH_COND(surface_data == nullptr); // Can't be null.
- }
-
- const int vertex = get_vertex_from_polygon_vertex(polygon_indices, polygon_vertex);
-
- // The vertex position in the surface
- // Uses a lookup table for speed with large scenes
- bool has_polygon_vertex_index = surface_data->lookup_table.has(vertex);
- int surface_polygon_vertex_index = -1;
-
- if (has_polygon_vertex_index) {
- surface_polygon_vertex_index = surface_data->lookup_table[vertex];
- } else {
- surface_polygon_vertex_index = surface_data->vertices_map.size();
- surface_data->lookup_table[vertex] = surface_polygon_vertex_index;
- surface_data->vertices_map.push_back(vertex);
- }
-
- surface_data->surface_polygon_vertex[polygon_index].push_back(surface_polygon_vertex_index);
- }
- }
-
- //print_verbose("[debug UV 1] UV1: " + itos(uvs_0.size()));
- //print_verbose("[debug UV 2] UV2: " + itos(uvs_1.size()));
-
- // Phase 4. Per each surface just insert the vertices and add the indices.
- for (const SurfaceId *surface_id = surfaces.next(nullptr); surface_id != nullptr; surface_id = surfaces.next(surface_id)) {
- SurfaceData *surface = surfaces.getptr(*surface_id);
-
- // Just add the vertices data.
- for (unsigned int i = 0; i < surface->vertices_map.size(); i += 1) {
- const Vertex vertex = surface->vertices_map[i];
-
- // This must be done before add_vertex because the surface tool is
- // expecting this before the st->add_vertex() call
- add_vertex(state,
- surface->surface_tool,
- state.scale,
- vertex,
- vertices,
- normals,
- uvs_0,
- uvs_1,
- colors);
- }
-
- // Triangulate the various polygons and add the indices.
- for (const PolygonId *polygon_id = surface->surface_polygon_vertex.next(nullptr); polygon_id != nullptr; polygon_id = surface->surface_polygon_vertex.next(polygon_id)) {
- const Vector *indices = surface->surface_polygon_vertex.getptr(*polygon_id);
- triangulate_polygon(
- surface,
- *indices,
- vertices);
- }
- }
-
- // Phase 5. Compose the morphs if any.
- for (const SurfaceId *surface_id = surfaces.next(nullptr); surface_id != nullptr; surface_id = surfaces.next(surface_id)) {
- SurfaceData *surface = surfaces.getptr(*surface_id);
-
- for (const String *morph_name = morphs.next(nullptr); morph_name != nullptr; morph_name = morphs.next(morph_name)) {
- MorphVertexData *morph_data = morphs.getptr(*morph_name);
-
- // As said by the docs, this is not supposed to be different than
- // vertex_count.
- CRASH_COND(morph_data->vertices.size() != vertex_count);
- CRASH_COND(morph_data->normals.size() != vertex_count);
-
- Vector3 *vertices_ptr = morph_data->vertices.ptrw();
- Vector3 *normals_ptr = morph_data->normals.ptrw();
-
- Ref morph_st;
- morph_st.instantiate();
- morph_st->begin(Mesh::PRIMITIVE_TRIANGLES);
-
- for (unsigned int vi = 0; vi < surface->vertices_map.size(); vi += 1) {
- const Vertex &vertex = surface->vertices_map[vi];
- add_vertex(
- state,
- morph_st,
- state.scale,
- vertex,
- vertices,
- normals,
- uvs_0,
- uvs_1,
- colors,
- vertices_ptr[vertex],
- normals_ptr[vertex]);
- }
-
- if (state.is_blender_fbx) {
- morph_st->generate_normals();
- }
- morph_st->generate_tangents();
- surface->morphs.push_back(morph_st->commit_to_arrays());
- }
- }
-
- // Phase 6. Compose the mesh and return it.
- Ref mesh;
- mesh.instantiate();
-
- // Add blend shape info.
- for (const String *morph_name = morphs.next(nullptr); morph_name != nullptr; morph_name = morphs.next(morph_name)) {
- mesh->add_blend_shape(*morph_name);
- }
-
- // TODO always normalized, Why?
- mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_NORMALIZED);
-
- // Add surfaces.
- for (const SurfaceId *surface_id = surfaces.next(nullptr); surface_id != nullptr; surface_id = surfaces.next(surface_id)) {
- SurfaceData *surface = surfaces.getptr(*surface_id);
-
- if (state.is_blender_fbx) {
- surface->surface_tool->generate_normals();
- }
- // you can't generate them without a valid uv map.
- if (uvs_0_raw.size() > 0) {
- surface->surface_tool->generate_tangents();
- }
-
- Array mesh_array = surface->surface_tool->commit_to_arrays();
- Array blend_shapes = surface->morphs;
-
- // Enforce blend shape mask array format
- for (int i = 0; i < blend_shapes.size(); i++) {
- Array bsdata = blend_shapes[i];
-
- for (int j = 0; j < Mesh::ARRAY_MAX; j++) {
- if (!(Mesh::ARRAY_FORMAT_BLEND_SHAPE_MASK & (1 << j))) {
- bsdata[j] = Variant();
- }
- }
- }
-
- if (surface->material.is_valid()) {
- mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, mesh_array, blend_shapes, Dictionary(), surface->material, surface->material->get_name());
- } else {
- mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, mesh_array, blend_shapes);
- }
- }
-
- ImporterMeshInstance3D *godot_mesh = memnew(ImporterMeshInstance3D);
- godot_mesh->set_mesh(mesh);
- const String name = ImportUtils::FBXNodeToName(model->Name());
- godot_mesh->set_name(name); // hurry up compiling >.<
- mesh->set_name("mesh3d-" + name);
- return godot_mesh;
-}
-
-void FBXMeshData::sanitize_vertex_weights(const ImportState &state) {
- const int max_vertex_influence_count = RS::ARRAY_WEIGHTS_SIZE;
- Map skeleton_to_skin_bind_id;
- // TODO: error's need added
- const FBXDocParser::Skin *fbx_skin = mesh_geometry->DeformerSkin();
-
- if (fbx_skin == nullptr || fbx_skin->Clusters().size() == 0) {
- return; // do nothing
- }
-
- //
- // Precalculate the skin cluster mapping
- //
-
- int bind_id = 0;
- for (const FBXDocParser::Cluster *cluster : fbx_skin->Clusters()) {
- ERR_CONTINUE_MSG(!state.fbx_bone_map.has(cluster->TargetNode()->ID()), "Missing bone map for cluster target node with id " + uitos(cluster->TargetNode()->ID()) + ".");
- Ref bone = state.fbx_bone_map[cluster->TargetNode()->ID()];
- skeleton_to_skin_bind_id.insert(bone->godot_bone_id, bind_id);
- bind_id++;
- }
-
- for (const Vertex *v = vertex_weights.next(nullptr); v != nullptr; v = vertex_weights.next(v)) {
- VertexWeightMapping *vm = vertex_weights.getptr(*v);
- ERR_CONTINUE(vm->bones.size() != vm->weights.size()); // No message, already checked.
- ERR_CONTINUE(vm->bones_ref.size() != vm->weights.size()); // No message, already checked.
-
- const int initial_size = vm->weights.size();
- {
- // Init bone id
- int *bones_ptr = vm->bones.ptrw();
- Ref *bones_ref_ptr = vm->bones_ref.ptrw();
-
- for (int i = 0; i < vm->weights.size(); i += 1) {
- // At this point this is not possible because the skeleton is already initialized.
- CRASH_COND(bones_ref_ptr[i]->godot_bone_id == -2);
- bones_ptr[i] = skeleton_to_skin_bind_id[bones_ref_ptr[i]->godot_bone_id];
- }
-
- // From this point on the data is no more valid.
- vm->bones_ref.clear();
- }
-
- {
- // Sort
- float *weights_ptr = vm->weights.ptrw();
- int *bones_ptr = vm->bones.ptrw();
- for (int i = 0; i < vm->weights.size(); i += 1) {
- for (int x = i + 1; x < vm->weights.size(); x += 1) {
- if (weights_ptr[i] < weights_ptr[x]) {
- SWAP(weights_ptr[i], weights_ptr[x]);
- SWAP(bones_ptr[i], bones_ptr[x]);
- }
- }
- }
- }
-
- {
- // Resize
- vm->weights.resize(max_vertex_influence_count);
- vm->bones.resize(max_vertex_influence_count);
- float *weights_ptr = vm->weights.ptrw();
- int *bones_ptr = vm->bones.ptrw();
- for (int i = initial_size; i < max_vertex_influence_count; i += 1) {
- weights_ptr[i] = 0.0;
- bones_ptr[i] = 0;
- }
-
- // Normalize
- real_t sum = 0.0;
- for (int i = 0; i < max_vertex_influence_count; i += 1) {
- sum += weights_ptr[i];
- }
- if (sum > 0.0) {
- for (int i = 0; i < vm->weights.size(); i += 1) {
- weights_ptr[i] = weights_ptr[i] / sum;
- }
- }
- }
- }
-}
-
-void FBXMeshData::reorganize_vertices(
- // TODO: perf hotspot on insane files
- std::vector &r_polygon_indices,
- std::vector &r_vertices,
- HashMap &r_normals,
- HashMap &r_uv_1,
- HashMap &r_uv_2,
- HashMap &r_color,
- HashMap &r_morphs,
- HashMap> &r_normals_raw,
- HashMap> &r_colors_raw,
- HashMap> &r_uv_1_raw,
- HashMap> &r_uv_2_raw) {
- // Key: OldVertex; Value: [New vertices];
- HashMap> duplicated_vertices;
-
- PolygonId polygon_index = -1;
- for (int pv = 0; pv < (int)r_polygon_indices.size(); pv += 1) {
- if (is_start_of_polygon(r_polygon_indices, pv)) {
- polygon_index += 1;
- }
- const Vertex index = get_vertex_from_polygon_vertex(r_polygon_indices, pv);
-
- bool need_duplication = false;
- Vector2 this_vert_poly_uv1 = Vector2();
- Vector2 this_vert_poly_uv2 = Vector2();
- Vector3 this_vert_poly_normal = Vector3();
- Color this_vert_poly_color = Color();
-
- // Take the normal and see if we need to duplicate this polygon.
- if (r_normals_raw.has(index)) {
- const HashMap *nrml_arr = r_normals_raw.getptr(index);
-
- if (nrml_arr->has(polygon_index)) {
- this_vert_poly_normal = nrml_arr->get(polygon_index);
- } else if (nrml_arr->has(-1)) {
- this_vert_poly_normal = nrml_arr->get(-1);
- } else {
- print_error("invalid normal detected: " + itos(index) + " polygon index: " + itos(polygon_index));
- for (const PolygonId *pid = nrml_arr->next(nullptr); pid != nullptr; pid = nrml_arr->next(pid)) {
- print_verbose("debug contents key: " + itos(*pid));
-
- if (nrml_arr->has(*pid)) {
- print_verbose("contents valid: " + nrml_arr->get(*pid));
- }
- }
- }
-
- // Now, check if we need to duplicate it.
- for (const PolygonId *pid = nrml_arr->next(nullptr); pid != nullptr; pid = nrml_arr->next(pid)) {
- if (*pid == polygon_index) {
- continue;
- }
-
- const Vector3 vert_poly_normal = *nrml_arr->getptr(*pid);
- if (!vert_poly_normal.is_equal_approx(this_vert_poly_normal)) {
- // Yes this polygon need duplication.
- need_duplication = true;
- break;
- }
- }
- }
-
- // TODO: make me vertex color
- // Take the normal and see if we need to duplicate this polygon.
- if (r_colors_raw.has(index)) {
- const HashMap *color_arr = r_colors_raw.getptr(index);
-
- if (color_arr->has(polygon_index)) {
- this_vert_poly_color = color_arr->get(polygon_index);
- } else if (color_arr->has(-1)) {
- this_vert_poly_color = color_arr->get(-1);
- } else {
- print_error("invalid color detected: " + itos(index) + " polygon index: " + itos(polygon_index));
- for (const PolygonId *pid = color_arr->next(nullptr); pid != nullptr; pid = color_arr->next(pid)) {
- print_verbose("debug contents key: " + itos(*pid));
-
- if (color_arr->has(*pid)) {
- print_verbose("contents valid: " + color_arr->get(*pid));
- }
- }
- }
-
- // Now, check if we need to duplicate it.
- for (const PolygonId *pid = color_arr->next(nullptr); pid != nullptr; pid = color_arr->next(pid)) {
- if (*pid == polygon_index) {
- continue;
- }
-
- const Color vert_poly_color = *color_arr->getptr(*pid);
- if (!this_vert_poly_color.is_equal_approx(vert_poly_color)) {
- // Yes this polygon need duplication.
- need_duplication = true;
- break;
- }
- }
- }
-
- // Take the UV1 and UV2 and see if we need to duplicate this polygon.
- {
- HashMap> *uv_raw = &r_uv_1_raw;
- Vector2 *this_vert_poly_uv = &this_vert_poly_uv1;
- for (int kk = 0; kk < 2; kk++) {
- if (uv_raw->has(index)) {
- const HashMap *uvs = uv_raw->getptr(index);
-
- if (uvs->has(polygon_index)) {
- // This Polygon has its own uv.
- (*this_vert_poly_uv) = *uvs->getptr(polygon_index);
-
- // Check if we need to duplicate it.
- for (const PolygonId *pid = uvs->next(nullptr); pid != nullptr; pid = uvs->next(pid)) {
- if (*pid == polygon_index) {
- continue;
- }
- const Vector2 vert_poly_uv = *uvs->getptr(*pid);
- if (!vert_poly_uv.is_equal_approx(*this_vert_poly_uv)) {
- // Yes this polygon need duplication.
- need_duplication = true;
- break;
- }
- }
- } else if (uvs->has(-1)) {
- // It has the default UV.
- (*this_vert_poly_uv) = *uvs->getptr(-1);
- } else if (uvs->size() > 0) {
- // No uv, this is strange, just take the first and duplicate.
- (*this_vert_poly_uv) = *uvs->getptr(*uvs->next(nullptr));
- WARN_PRINT("No UVs for this polygon, while there is no default and some other polygons have it. This FBX file may be corrupted.");
- }
- }
- uv_raw = &r_uv_2_raw;
- this_vert_poly_uv = &this_vert_poly_uv2;
- }
- }
-
- // If we want to duplicate it, Let's see if we already duplicated this
- // vertex.
- if (need_duplication) {
- if (duplicated_vertices.has(index)) {
- Vertex similar_vertex = -1;
- // Let's see if one of the new vertices has the same data of this.
- const Vector *new_vertices = duplicated_vertices.getptr(index);
- for (int j = 0; j < new_vertices->size(); j += 1) {
- const Vertex new_vertex = (*new_vertices)[j];
- bool same_uv1 = false;
- bool same_uv2 = false;
- bool same_normal = false;
- bool same_color = false;
-
- if (r_uv_1.has(new_vertex)) {
- if ((this_vert_poly_uv1 - (*r_uv_1.getptr(new_vertex))).length_squared() <= CMP_EPSILON) {
- same_uv1 = true;
- }
- }
-
- if (r_uv_2.has(new_vertex)) {
- if ((this_vert_poly_uv2 - (*r_uv_2.getptr(new_vertex))).length_squared() <= CMP_EPSILON) {
- same_uv2 = true;
- }
- }
-
- if (r_color.has(new_vertex)) {
- if (this_vert_poly_color.is_equal_approx((*r_color.getptr(new_vertex)))) {
- same_color = true;
- }
- }
-
- if (r_normals.has(new_vertex)) {
- if ((this_vert_poly_normal - (*r_normals.getptr(new_vertex))).length_squared() <= CMP_EPSILON) {
- same_uv2 = true;
- }
- }
-
- if (same_uv1 && same_uv2 && same_normal && same_color) {
- similar_vertex = new_vertex;
- break;
- }
- }
-
- if (similar_vertex != -1) {
- // Update polygon.
- if (is_end_of_polygon(r_polygon_indices, pv)) {
- r_polygon_indices[pv] = ~similar_vertex;
- } else {
- r_polygon_indices[pv] = similar_vertex;
- }
- need_duplication = false;
- }
- }
- }
-
- if (need_duplication) {
- const Vertex old_index = index;
- const Vertex new_index = r_vertices.size();
-
- // Polygon index.
- if (is_end_of_polygon(r_polygon_indices, pv)) {
- r_polygon_indices[pv] = ~new_index;
- } else {
- r_polygon_indices[pv] = new_index;
- }
-
- // Vertex position.
- r_vertices.push_back(r_vertices[old_index]);
-
- // Normals
- if (r_normals_raw.has(old_index)) {
- r_normals.set(new_index, this_vert_poly_normal);
- r_normals_raw.getptr(old_index)->erase(polygon_index);
- r_normals_raw[new_index][polygon_index] = this_vert_poly_normal;
- }
-
- // Vertex Color
- if (r_colors_raw.has(old_index)) {
- r_color.set(new_index, this_vert_poly_color);
- r_colors_raw.getptr(old_index)->erase(polygon_index);
- r_colors_raw[new_index][polygon_index] = this_vert_poly_color;
- }
-
- // UV 0
- if (r_uv_1_raw.has(old_index)) {
- r_uv_1.set(new_index, this_vert_poly_uv1);
- r_uv_1_raw.getptr(old_index)->erase(polygon_index);
- r_uv_1_raw[new_index][polygon_index] = this_vert_poly_uv1;
- }
-
- // UV 1
- if (r_uv_2_raw.has(old_index)) {
- r_uv_2.set(new_index, this_vert_poly_uv2);
- r_uv_2_raw.getptr(old_index)->erase(polygon_index);
- r_uv_2_raw[new_index][polygon_index] = this_vert_poly_uv2;
- }
-
- // Morphs
- for (const String *mname = r_morphs.next(nullptr); mname != nullptr; mname = r_morphs.next(mname)) {
- MorphVertexData *d = r_morphs.getptr(*mname);
- // This can't never happen.
- CRASH_COND(d == nullptr);
- if (d->vertices.size() > old_index) {
- d->vertices.push_back(d->vertices[old_index]);
- }
- if (d->normals.size() > old_index) {
- d->normals.push_back(d->normals[old_index]);
- }
- }
-
- if (vertex_weights.has(old_index)) {
- vertex_weights.set(new_index, vertex_weights[old_index]);
- }
-
- duplicated_vertices[old_index].push_back(new_index);
- } else {
- if (r_normals_raw.has(index) &&
- r_normals.has(index) == false) {
- r_normals.set(index, this_vert_poly_normal);
- }
-
- if (r_colors_raw.has(index) && r_color.has(index) == false) {
- r_color.set(index, this_vert_poly_color);
- }
-
- if (r_uv_1_raw.has(index) &&
- r_uv_1.has(index) == false) {
- r_uv_1.set(index, this_vert_poly_uv1);
- }
-
- if (r_uv_2_raw.has(index) &&
- r_uv_2.has(index) == false) {
- r_uv_2.set(index, this_vert_poly_uv2);
- }
- }
- }
-}
-
-void FBXMeshData::add_vertex(
- const ImportState &state,
- Ref p_surface_tool,
- real_t p_scale,
- Vertex p_vertex,
- const std::vector &p_vertices_position,
- const HashMap &p_normals,
- const HashMap &p_uvs_0,
- const HashMap &p_uvs_1,
- const HashMap &p_colors,
- const Vector3 &p_morph_value,
- const Vector3 &p_morph_normal) {
- ERR_FAIL_INDEX_MSG(p_vertex, (Vertex)p_vertices_position.size(), "FBX file is corrupted, the position of the vertex can't be retrieved.");
-
- if (p_normals.has(p_vertex) && !state.is_blender_fbx) {
- p_surface_tool->set_normal(p_normals[p_vertex] + p_morph_normal);
- }
-
- if (p_uvs_0.has(p_vertex)) {
- //print_verbose("uv1: [" + itos(p_vertex) + "] " + p_uvs_0[p_vertex]);
- // Inverts Y UV.
- p_surface_tool->set_uv(Vector2(p_uvs_0[p_vertex].x, 1 - p_uvs_0[p_vertex].y));
- }
-
- if (p_uvs_1.has(p_vertex)) {
- //print_verbose("uv2: [" + itos(p_vertex) + "] " + p_uvs_1[p_vertex]);
- // Inverts Y UV.
- p_surface_tool->set_uv2(Vector2(p_uvs_1[p_vertex].x, 1 - p_uvs_1[p_vertex].y));
- }
-
- if (p_colors.has(p_vertex)) {
- p_surface_tool->set_color(p_colors[p_vertex]);
- }
-
- // TODO what about binormals?
- // TODO there is other?
-
- if (vertex_weights.has(p_vertex)) {
- // Let's extract the weight info.
- const VertexWeightMapping *vm = vertex_weights.getptr(p_vertex);
- const Vector &bones = vm->bones;
-
- // the bug is that the bone idx is wrong because it is not ref'ing the skin.
-
- if (bones.size() > RS::ARRAY_WEIGHTS_SIZE) {
- print_error("[weight overflow detected]");
- }
-
- p_surface_tool->set_weights(vm->weights);
- // 0 1 2 3 4 5 6 7 < local skeleton / skin for mesh
- // 0 1 2 3 4 5 6 7 8 9 10 < actual skeleton with all joints
- p_surface_tool->set_bones(bones);
- }
-
- // The surface tool want the vertex position as last thing.
- p_surface_tool->add_vertex((p_vertices_position[p_vertex] + p_morph_value) * p_scale);
-}
-
-void FBXMeshData::triangulate_polygon(SurfaceData *surface, const Vector &p_polygon_vertex, const std::vector &p_vertices) const {
- Ref st(surface->surface_tool);
- const int polygon_vertex_count = p_polygon_vertex.size();
- //const Vector& p_surface_vertex_map
- if (polygon_vertex_count == 1) {
- // point to triangle
- st->add_index(p_polygon_vertex[0]);
- st->add_index(p_polygon_vertex[0]);
- st->add_index(p_polygon_vertex[0]);
- return;
- } else if (polygon_vertex_count == 2) {
- // line to triangle
- st->add_index(p_polygon_vertex[1]);
- st->add_index(p_polygon_vertex[1]);
- st->add_index(p_polygon_vertex[0]);
- return;
- } else if (polygon_vertex_count == 3) {
- // triangle to triangle
- st->add_index(p_polygon_vertex[0]);
- st->add_index(p_polygon_vertex[2]);
- st->add_index(p_polygon_vertex[1]);
- return;
- } else if (polygon_vertex_count == 4) {
- // quad to triangle - this code is awesome for import times
- // it prevents triangles being generated slowly
- st->add_index(p_polygon_vertex[0]);
- st->add_index(p_polygon_vertex[2]);
- st->add_index(p_polygon_vertex[1]);
- st->add_index(p_polygon_vertex[2]);
- st->add_index(p_polygon_vertex[0]);
- st->add_index(p_polygon_vertex[3]);
- return;
- } else {
- // non triangulated - we must run the triangulation algorithm
- bool is_simple_convex = false;
- // this code is 'slow' but required it triangulates all the unsupported geometry.
- // Doesn't allow for bigger polygons because those are unlikely be convex
- if (polygon_vertex_count <= 6) {
- // Start from true, check if it's false.
- is_simple_convex = true;
- Vector3 first_vec;
- for (int i = 0; i < polygon_vertex_count; i += 1) {
- const Vector3 p1 = p_vertices[surface->vertices_map[p_polygon_vertex[i]]];
- const Vector3 p2 = p_vertices[surface->vertices_map[p_polygon_vertex[(i + 1) % polygon_vertex_count]]];
- const Vector3 p3 = p_vertices[surface->vertices_map[p_polygon_vertex[(i + 2) % polygon_vertex_count]]];
-
- const Vector3 edge1 = p1 - p2;
- const Vector3 edge2 = p3 - p2;
-
- const Vector3 res = edge1.normalized().cross(edge2.normalized()).normalized();
- if (i == 0) {
- first_vec = res;
- } else {
- if (first_vec.dot(res) < 0.0) {
- // Ok we found an angle that is not the same dir of the
- // others.
- is_simple_convex = false;
- break;
- }
- }
- }
- }
-
- if (is_simple_convex) {
- // This is a convex polygon, so just triangulate it.
- for (int i = 0; i < (polygon_vertex_count - 2); i += 1) {
- st->add_index(p_polygon_vertex[2 + i]);
- st->add_index(p_polygon_vertex[1 + i]);
- st->add_index(p_polygon_vertex[0]);
- }
- return;
- }
- }
-
- {
- // This is a concave polygon.
-
- std::vector poly_vertices(polygon_vertex_count);
- for (int i = 0; i < polygon_vertex_count; i += 1) {
- poly_vertices[i] = p_vertices[surface->vertices_map[p_polygon_vertex[i]]];
- }
-
- const Vector3 poly_norm = get_poly_normal(poly_vertices);
- if (poly_norm.length_squared() <= CMP_EPSILON) {
- ERR_FAIL_COND_MSG(poly_norm.length_squared() <= CMP_EPSILON, "The normal of this poly was not computed. Is this FBX file corrupted.");
- }
-
- // Select the plan coordinate.
- int axis_1_coord = 0;
- int axis_2_coord = 1;
- {
- real_t inv = poly_norm.z;
-
- const real_t axis_x = ABS(poly_norm.x);
- const real_t axis_y = ABS(poly_norm.y);
- const real_t axis_z = ABS(poly_norm.z);
-
- if (axis_x > axis_y) {
- if (axis_x > axis_z) {
- // For the most part the normal point toward X.
- axis_1_coord = 1;
- axis_2_coord = 2;
- inv = poly_norm.x;
- }
- } else if (axis_y > axis_z) {
- // For the most part the normal point toward Y.
- axis_1_coord = 2;
- axis_2_coord = 0;
- inv = poly_norm.y;
- }
-
- // Swap projection axes to take the negated projection vector into account
- if (inv < 0.0f) {
- SWAP(axis_1_coord, axis_2_coord);
- }
- }
-
- TPPLPoly tppl_poly;
- tppl_poly.Init(polygon_vertex_count);
- std::vector projected_vertices(polygon_vertex_count);
- for (int i = 0; i < polygon_vertex_count; i += 1) {
- const Vector2 pv(poly_vertices[i][axis_1_coord], poly_vertices[i][axis_2_coord]);
- projected_vertices[i] = pv;
- tppl_poly.GetPoint(i) = pv;
- }
- tppl_poly.SetOrientation(TPPL_ORIENTATION_CCW);
-
- List out_poly;
-
- TPPLPartition tppl_partition;
- if (tppl_partition.Triangulate_OPT(&tppl_poly, &out_poly) == 0) { // Good result.
- if (tppl_partition.Triangulate_EC(&tppl_poly, &out_poly) == 0) { // Medium result.
- if (tppl_partition.Triangulate_MONO(&tppl_poly, &out_poly) == 0) { // Really poor result.
- ERR_FAIL_MSG("The triangulation of this polygon failed, please try to triangulate your mesh or check if it has broken polygons.");
- }
- }
- }
-
- std::vector tris(out_poly.size());
- for (List::Element *I = out_poly.front(); I; I = I->next()) {
- TPPLPoly &tp = I->get();
-
- ERR_FAIL_COND_MSG(tp.GetNumPoints() != 3, "The triangulator returned more points, how this is possible?");
- // Find Index
- for (int i = 2; i >= 0; i -= 1) {
- const Vector2 vertex = tp.GetPoint(i);
- bool done = false;
- // Find Index
- for (int y = 0; y < polygon_vertex_count; y += 1) {
- if ((projected_vertices[y] - vertex).length_squared() <= CMP_EPSILON) {
- // This seems the right vertex
- st->add_index(p_polygon_vertex[y]);
- done = true;
- break;
- }
- }
- ERR_FAIL_COND(done == false);
- }
- }
- }
-}
-
-void FBXMeshData::gen_weight_info(Ref st, Vertex vertex_id) const {
- if (vertex_weights.is_empty()) {
- return;
- }
-
- if (vertex_weights.has(vertex_id)) {
- // Let's extract the weight info.
- const VertexWeightMapping *vm = vertex_weights.getptr(vertex_id);
- st->set_weights(vm->weights);
- st->set_bones(vm->bones);
- }
-}
-
-int FBXMeshData::get_vertex_from_polygon_vertex(const std::vector &p_polygon_indices, int p_index) const {
- if (p_index < 0 || p_index >= (int)p_polygon_indices.size()) {
- return -1;
- }
-
- const int vertex = p_polygon_indices[p_index];
- if (vertex >= 0) {
- return vertex;
- } else {
- // Negative numbers are the end of the face, reversing the bits is
- // possible to obtain the positive correct vertex number.
- return ~vertex;
- }
-}
-
-bool FBXMeshData::is_end_of_polygon(const std::vector &p_polygon_indices, int p_index) const {
- if (p_index < 0 || p_index >= (int)p_polygon_indices.size()) {
- return false;
- }
-
- const int vertex = p_polygon_indices[p_index];
-
- // If the index is negative this is the end of the Polygon.
- return vertex < 0;
-}
-
-bool FBXMeshData::is_start_of_polygon(const std::vector &p_polygon_indices, int p_index) const {
- if (p_index < 0 || p_index >= (int)p_polygon_indices.size()) {
- return false;
- }
-
- if (p_index == 0) {
- return true;
- }
-
- // If the previous indices is negative this is the begin of a new Polygon.
- return p_polygon_indices[p_index - 1] < 0;
-}
-
-int FBXMeshData::count_polygons(const std::vector &p_polygon_indices) const {
- // The negative numbers define the end of the polygon. Counting the amount of
- // negatives the numbers of polygons are obtained.
- int count = 0;
- for (size_t i = 0; i < p_polygon_indices.size(); i += 1) {
- if (p_polygon_indices[i] < 0) {
- count += 1;
- }
- }
- return count;
-}
-
-template
-HashMap FBXMeshData::extract_per_vertex_data(
- int p_vertex_count,
- const std::vector &p_edge_map,
- const std::vector &p_mesh_indices,
- const FBXDocParser::MeshGeometry::MappingData &p_mapping_data,
- R (*collector_function)(const Vector> *p_vertex_data, R p_fall_back),
- R p_fall_back) const {
- /* When index_to_direct is set
- * index size is 184 ( contains index for the data array [values 0, 96] )
- * data size is 96 (contains uv coordinates)
- * this means index is simple data reduction basically
- */
- ////
- if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct && p_mapping_data.index.size() == 0) {
- print_verbose("debug count: index size: " + itos(p_mapping_data.index.size()) + ", data size: " + itos(p_mapping_data.data.size()));
- print_verbose("vertex indices count: " + itos(p_mesh_indices.size()));
- print_verbose("Edge map size: " + itos(p_edge_map.size()));
- }
-
- ERR_FAIL_COND_V_MSG(p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct && p_mapping_data.index.size() == 0, (HashMap()), "FBX importer needs to map correctly to this field, please specify the override index name to fix this problem!");
- ERR_FAIL_COND_V_MSG(p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index && p_mapping_data.index.size() == 0, (HashMap()), "The FBX seems corrupted");
-
- // Aggregate vertex data.
- HashMap>> aggregate_vertex_data;
-
- switch (p_mapping_data.map_type) {
- case FBXDocParser::MeshGeometry::MapType::none: {
- // No data nothing to do.
- return (HashMap());
- }
- case FBXDocParser::MeshGeometry::MapType::vertex: {
- ERR_FAIL_COND_V_MSG(p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct, (HashMap()), "We will support in future");
-
- if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
- // The data is mapped per vertex directly.
- ERR_FAIL_COND_V_MSG((int)p_mapping_data.data.size() != p_vertex_count, (HashMap()), "FBX file corrupted: #ERR01");
- for (size_t vertex_index = 0; vertex_index < p_mapping_data.data.size(); vertex_index += 1) {
- aggregate_vertex_data[vertex_index].push_back({ -1, p_mapping_data.data[vertex_index] });
- }
- } else {
- // The data is mapped per vertex using a reference.
- // The indices array, contains a *reference_id for each vertex.
- // * Note that the reference_id is the id of data into the data array.
- //
- // https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
- ERR_FAIL_COND_V_MSG((int)p_mapping_data.index.size() != p_vertex_count, (HashMap()), "FBX file corrupted: #ERR02");
- for (size_t vertex_index = 0; vertex_index < p_mapping_data.index.size(); vertex_index += 1) {
- ERR_FAIL_INDEX_V_MSG(p_mapping_data.index[vertex_index], (int)p_mapping_data.data.size(), (HashMap()), "FBX file seems corrupted: #ERR03.");
- aggregate_vertex_data[vertex_index].push_back({ -1, p_mapping_data.data[p_mapping_data.index[vertex_index]] });
- }
- }
- } break;
- case FBXDocParser::MeshGeometry::MapType::polygon_vertex: {
- if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct) {
- // The data is mapped using each index from the indexes array then direct to the data (data reduction algorithm)
- ERR_FAIL_COND_V_MSG((int)p_mesh_indices.size() != (int)p_mapping_data.index.size(), (HashMap()), "FBX file seems corrupted: #ERR04");
- int polygon_id = -1;
- for (size_t polygon_vertex_index = 0; polygon_vertex_index < p_mapping_data.index.size(); polygon_vertex_index += 1) {
- if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
- polygon_id += 1;
- }
- const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
- ERR_FAIL_COND_V_MSG(vertex_index < 0, (HashMap()), "FBX file corrupted: #ERR05");
- ERR_FAIL_COND_V_MSG(vertex_index >= p_vertex_count, (HashMap()), "FBX file corrupted: #ERR06");
- const int index_to_direct = get_vertex_from_polygon_vertex(p_mapping_data.index, polygon_vertex_index);
- T value = p_mapping_data.data[index_to_direct];
- aggregate_vertex_data[vertex_index].push_back({ polygon_id, value });
- }
- } else if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
- // The data are mapped per polygon vertex directly.
- ERR_FAIL_COND_V_MSG((int)p_mesh_indices.size() != (int)p_mapping_data.data.size(), (HashMap()), "FBX file seems corrupted: #ERR04");
- int polygon_id = -1;
- for (size_t polygon_vertex_index = 0; polygon_vertex_index < p_mapping_data.data.size(); polygon_vertex_index += 1) {
- if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
- polygon_id += 1;
- }
- const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
- ERR_FAIL_COND_V_MSG(vertex_index < 0, (HashMap()), "FBX file corrupted: #ERR05");
- ERR_FAIL_COND_V_MSG(vertex_index >= p_vertex_count, (HashMap()), "FBX file corrupted: #ERR06");
-
- aggregate_vertex_data[vertex_index].push_back({ polygon_id, p_mapping_data.data[polygon_vertex_index] });
- }
- } else {
- // The data is mapped per polygon_vertex using a reference.
- // The indices array, contains a *reference_id for each polygon_vertex.
- // * Note that the reference_id is the id of data into the data array.
- //
- // https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
- ERR_FAIL_COND_V_MSG(p_mesh_indices.size() != p_mapping_data.index.size(), (HashMap()), "FBX file corrupted: #ERR7");
- int polygon_id = -1;
- for (size_t polygon_vertex_index = 0; polygon_vertex_index < p_mapping_data.index.size(); polygon_vertex_index += 1) {
- if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
- polygon_id += 1;
- }
- const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
- ERR_FAIL_COND_V_MSG(vertex_index < 0, (HashMap()), "FBX file corrupted: #ERR8");
- ERR_FAIL_COND_V_MSG(vertex_index >= p_vertex_count, (HashMap()), "FBX file seems corrupted: #ERR9.");
- ERR_FAIL_COND_V_MSG(p_mapping_data.index[polygon_vertex_index] < 0, (HashMap()), "FBX file seems corrupted: #ERR10.");
- ERR_FAIL_COND_V_MSG(p_mapping_data.index[polygon_vertex_index] >= (int)p_mapping_data.data.size(), (HashMap()), "FBX file seems corrupted: #ERR11.");
- aggregate_vertex_data[vertex_index].push_back({ polygon_id, p_mapping_data.data[p_mapping_data.index[polygon_vertex_index]] });
- }
- }
- } break;
- case FBXDocParser::MeshGeometry::MapType::polygon: {
- if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
- // The data are mapped per polygon directly.
- const int polygon_count = count_polygons(p_mesh_indices);
- ERR_FAIL_COND_V_MSG(polygon_count != (int)p_mapping_data.data.size(), (HashMap()), "FBX file seems corrupted: #ERR12");
-
- // Advance each polygon vertex, each new polygon advance the polygon index.
- int polygon_index = -1;
- for (size_t polygon_vertex_index = 0;
- polygon_vertex_index < p_mesh_indices.size();
- polygon_vertex_index += 1) {
- if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
- polygon_index += 1;
- ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_mapping_data.data.size(), (HashMap()), "FBX file seems corrupted: #ERR13");
- }
-
- const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
- ERR_FAIL_INDEX_V_MSG(vertex_index, p_vertex_count, (HashMap()), "FBX file corrupted: #ERR14");
-
- aggregate_vertex_data[vertex_index].push_back({ polygon_index, p_mapping_data.data[polygon_index] });
- }
- ERR_FAIL_COND_V_MSG((polygon_index + 1) != polygon_count, (HashMap()), "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.
- //
- // https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
- const int polygon_count = count_polygons(p_mesh_indices);
- ERR_FAIL_COND_V_MSG(polygon_count != (int)p_mapping_data.index.size(), (HashMap()), "FBX file seems corrupted: #ERR17");
-
- // Advance each polygon vertex, each new polygon advance the polygon index.
- int polygon_index = -1;
- for (size_t polygon_vertex_index = 0;
- polygon_vertex_index < p_mesh_indices.size();
- polygon_vertex_index += 1) {
- if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
- polygon_index += 1;
- ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_mapping_data.index.size(), (HashMap()), "FBX file seems corrupted: #ERR18");
- ERR_FAIL_INDEX_V_MSG(p_mapping_data.index[polygon_index], (int)p_mapping_data.data.size(), (HashMap()), "FBX file seems corrupted: #ERR19");
- }
-
- const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
- ERR_FAIL_INDEX_V_MSG(vertex_index, p_vertex_count, (HashMap()), "FBX file corrupted: #ERR20");
-
- aggregate_vertex_data[vertex_index].push_back({ polygon_index, p_mapping_data.data[p_mapping_data.index[polygon_index]] });
- }
- ERR_FAIL_COND_V_MSG((polygon_index + 1) != polygon_count, (HashMap()), "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) {
- // The data are mapped per edge directly.
- ERR_FAIL_COND_V_MSG(p_edge_map.size() != p_mapping_data.data.size(), (HashMap()), "FBX file seems corrupted: #ERR23");
- for (size_t edge_index = 0; edge_index < p_mapping_data.data.size(); edge_index += 1) {
- const FBXDocParser::MeshGeometry::Edge edge = FBXDocParser::MeshGeometry::get_edge(p_edge_map, edge_index);
- ERR_FAIL_INDEX_V_MSG(edge.vertex_0, p_vertex_count, (HashMap()), "FBX file corrupted: #ERR24");
- ERR_FAIL_INDEX_V_MSG(edge.vertex_1, p_vertex_count, (HashMap()), "FBX file corrupted: #ERR25");
- ERR_FAIL_INDEX_V_MSG(edge.vertex_0, (int)p_mapping_data.data.size(), (HashMap()), "FBX file corrupted: #ERR26");
- ERR_FAIL_INDEX_V_MSG(edge.vertex_1, (int)p_mapping_data.data.size(), (HashMap()), "FBX file corrupted: #ERR27");
- aggregate_vertex_data[edge.vertex_0].push_back({ -1, p_mapping_data.data[edge_index] });
- aggregate_vertex_data[edge.vertex_1].push_back({ -1, p_mapping_data.data[edge_index] });
- }
- } else {
- // The data is mapped per edge 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.
- //
- // https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
- ERR_FAIL_COND_V_MSG(p_edge_map.size() != p_mapping_data.index.size(), (HashMap()), "FBX file seems corrupted: #ERR28");
- for (size_t edge_index = 0; edge_index < p_mapping_data.data.size(); edge_index += 1) {
- const FBXDocParser::MeshGeometry::Edge edge = FBXDocParser::MeshGeometry::get_edge(p_edge_map, edge_index);
- ERR_FAIL_INDEX_V_MSG(edge.vertex_0, p_vertex_count, (HashMap()), "FBX file corrupted: #ERR29");
- ERR_FAIL_INDEX_V_MSG(edge.vertex_1, p_vertex_count, (HashMap()), "FBX file corrupted: #ERR30");
- ERR_FAIL_INDEX_V_MSG(edge.vertex_0, (int)p_mapping_data.index.size(), (HashMap()), "FBX file corrupted: #ERR31");
- ERR_FAIL_INDEX_V_MSG(edge.vertex_1, (int)p_mapping_data.index.size(), (HashMap()), "FBX file corrupted: #ERR32");
- ERR_FAIL_INDEX_V_MSG(p_mapping_data.index[edge.vertex_0], (int)p_mapping_data.data.size(), (HashMap()), "FBX file corrupted: #ERR33");
- ERR_FAIL_INDEX_V_MSG(p_mapping_data.index[edge.vertex_1], (int)p_mapping_data.data.size(), (HashMap()), "FBX file corrupted: #ERR34");
- aggregate_vertex_data[edge.vertex_0].push_back({ -1, p_mapping_data.data[p_mapping_data.index[edge_index]] });
- aggregate_vertex_data[edge.vertex_1].push_back({ -1, p_mapping_data.data[p_mapping_data.index[edge_index]] });
- }
- }
- } break;
- case FBXDocParser::MeshGeometry::MapType::all_the_same: {
- // No matter the mode, no matter the data size; The first always win
- // and is set to all the vertices.
- ERR_FAIL_COND_V_MSG(p_mapping_data.data.size() <= 0, (HashMap()), "FBX file seems corrupted: #ERR35");
- if (p_mapping_data.data.size() > 0) {
- for (int vertex_index = 0; vertex_index < p_vertex_count; vertex_index += 1) {
- aggregate_vertex_data[vertex_index].push_back({ -1, p_mapping_data.data[0] });
- }
- }
- } break;
- }
-
- if (aggregate_vertex_data.size() == 0) {
- return (HashMap());
- }
-
- // A map is used because turns out that the some FBX file are not well organized
- // with vertices well compacted. Using a map allows avoid those issues.
- HashMap result;
-
- // Aggregate the collected data.
- for (const Vertex *index = aggregate_vertex_data.next(nullptr); index != nullptr; index = aggregate_vertex_data.next(index)) {
- Vector> *aggregated_vertex = aggregate_vertex_data.getptr(*index);
- // This can't be null because we are just iterating.
- CRASH_COND(aggregated_vertex == nullptr);
-
- ERR_FAIL_INDEX_V_MSG(0, aggregated_vertex->size(), (HashMap()), "The FBX file is corrupted, No valid data for this vertex index.");
- result[*index] = collector_function(aggregated_vertex, p_fall_back);
- }
-
- // Sanitize the data now, if the file is broken we can try import it anyway.
- bool problem_found = false;
- for (size_t i = 0; i < p_mesh_indices.size(); i += 1) {
- const Vertex vertex = get_vertex_from_polygon_vertex(p_mesh_indices, i);
- if (result.has(vertex) == false) {
- result[vertex] = p_fall_back;
- problem_found = true;
- }
- }
- if (problem_found) {
- WARN_PRINT("Some data is missing, this FBX file may be corrupted: #WARN0.");
- }
-
- return result;
-}
-
-template
-HashMap FBXMeshData::extract_per_polygon(
- int p_vertex_count,
- const std::vector &p_polygon_indices,
- const FBXDocParser::MeshGeometry::MappingData &p_fbx_data,
- T p_fallback_value) const {
- ERR_FAIL_COND_V_MSG(p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct && p_fbx_data.data.size() == 0, (HashMap()), "invalid index to direct array");
- ERR_FAIL_COND_V_MSG(p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index && p_fbx_data.index.size() == 0, (HashMap()), "The FBX seems corrupted");
-
- const int polygon_count = count_polygons(p_polygon_indices);
-
- // Aggregate vertex data.
- HashMap> aggregate_polygon_data;
-
- switch (p_fbx_data.map_type) {
- case FBXDocParser::MeshGeometry::MapType::none: {
- // No data nothing to do.
- return (HashMap());
- }
- case FBXDocParser::MeshGeometry::MapType::vertex: {
- ERR_FAIL_V_MSG((HashMap()), "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()), "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()), "FBX file is corrupted: #ERR62");
- aggregate_polygon_data[polygon_index].push_back(p_fbx_data.data[polygon_index]);
- } else {
- ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_fbx_data.index.size(), (HashMap()), "FBX file is corrupted: #ERR62");
-
- const int index_to_direct = get_vertex_from_polygon_vertex(p_fbx_data.index, polygon_index);
- T value = p_fbx_data.data[index_to_direct];
- aggregate_polygon_data[polygon_index].push_back(value);
- }
- }
- } else if (p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
- // The data are mapped per polygon directly.
- ERR_FAIL_COND_V_MSG(polygon_count != (int)p_fbx_data.data.size(), (HashMap()), "FBX file is corrupted: #ERR51");
-
- // Advance each polygon vertex, each new polygon advance the polygon index.
- for (int polygon_index = 0;
- polygon_index < polygon_count;
- polygon_index += 1) {
- ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_fbx_data.data.size(), (HashMap()), "FBX file is corrupted: #ERR52");
- aggregate_polygon_data[polygon_index].push_back(p_fbx_data.data[polygon_index]);
- }
- } 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.
- //
- // https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
- ERR_FAIL_COND_V_MSG(polygon_count != (int)p_fbx_data.index.size(), (HashMap()), "FBX file seems corrupted: #ERR52");
-
- // Advance each polygon vertex, each new polygon advance the polygon index.
- for (int polygon_index = 0;
- polygon_index < polygon_count;
- polygon_index += 1) {
- ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_fbx_data.index.size(), (HashMap()), "FBX file is corrupted: #ERR53");
- ERR_FAIL_INDEX_V_MSG(p_fbx_data.index[polygon_index], (int)p_fbx_data.data.size(), (HashMap()), "FBX file is corrupted: #ERR54");
- aggregate_polygon_data[polygon_index].push_back(p_fbx_data.data[p_fbx_data.index[polygon_index]]);
- }
- }
- } break;
- case FBXDocParser::MeshGeometry::MapType::edge: {
- ERR_FAIL_V_MSG((HashMap()), "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
- // and is set to all the vertices.
- ERR_FAIL_COND_V_MSG(p_fbx_data.data.size() <= 0, (HashMap()), "FBX file seems corrupted: #ERR55");
- if (p_fbx_data.data.size() > 0) {
- for (int polygon_index = 0; polygon_index < polygon_count; polygon_index += 1) {
- aggregate_polygon_data[polygon_index].push_back(p_fbx_data.data[0]);
- }
- }
- } break;
- }
-
- if (aggregate_polygon_data.size() == 0) {
- return (HashMap());
- }
-
- // A map is used because turns out that the some FBX file are not well organized
- // with vertices well compacted. Using a map allows avoid those issues.
- HashMap polygons;
-
- // Take the first value for each vertex.
- for (const Vertex *index = aggregate_polygon_data.next(nullptr); index != nullptr; index = aggregate_polygon_data.next(index)) {
- Vector *aggregated_polygon = aggregate_polygon_data.getptr(*index);
- // This can't be null because we are just iterating.
- CRASH_COND(aggregated_polygon == nullptr);
-
- ERR_FAIL_INDEX_V_MSG(0, (int)aggregated_polygon->size(), (HashMap()), "The FBX file is corrupted, No valid data for this polygon index.");
-
- // Validate the final value.
- polygons[*index] = (*aggregated_polygon)[0];
- }
-
- // Sanitize the data now, if the file is broken we can try import it anyway.
- bool problem_found = false;
- for (int polygon_i = 0; polygon_i < polygon_count; polygon_i += 1) {
- if (polygons.has(polygon_i) == false) {
- polygons[polygon_i] = p_fallback_value;
- problem_found = true;
- }
- }
- if (problem_found) {
- WARN_PRINT("Some data is missing, this FBX file may be corrupted: #WARN1.");
- }
-
- return polygons;
-}
-
-void FBXMeshData::extract_morphs(const FBXDocParser::MeshGeometry *mesh_geometry, HashMap &r_data) {
- r_data.clear();
-
- const int vertex_count = mesh_geometry->get_vertices().size();
-
- for (const FBXDocParser::BlendShape *blend_shape : mesh_geometry->get_blend_shapes()) {
- for (const FBXDocParser::BlendShapeChannel *blend_shape_channel : blend_shape->BlendShapeChannels()) {
- const std::vector &shape_geometries = blend_shape_channel->GetShapeGeometries();
- for (const FBXDocParser::ShapeGeometry *shape_geometry : shape_geometries) {
- String morph_name = ImportUtils::FBXAnimMeshName(shape_geometry->Name()).c_str();
- if (morph_name.is_empty()) {
- morph_name = "morph";
- }
-
- // TODO we have only these??
- const std::vector &morphs_vertex_indices = shape_geometry->GetIndices();
- const std::vector &morphs_vertices = shape_geometry->GetVertices();
- const std::vector &morphs_normals = shape_geometry->GetNormals();
-
- ERR_FAIL_COND_MSG((int)morphs_vertex_indices.size() > vertex_count, "The FBX file is corrupted: #ERR103");
- ERR_FAIL_COND_MSG(morphs_vertex_indices.size() != morphs_vertices.size(), "The FBX file is corrupted: #ERR104");
- ERR_FAIL_COND_MSG((int)morphs_vertices.size() > vertex_count, "The FBX file is corrupted: #ERR105");
- ERR_FAIL_COND_MSG(morphs_normals.size() != 0 && morphs_normals.size() != morphs_vertices.size(), "The FBX file is corrupted: #ERR106");
-
- if (r_data.has(morph_name) == false) {
- // This morph doesn't exist yet.
- // Create it.
- MorphVertexData md;
- md.vertices.resize(vertex_count);
- md.normals.resize(vertex_count);
- r_data.set(morph_name, md);
- }
-
- MorphVertexData *data = r_data.getptr(morph_name);
- Vector3 *data_vertices_ptr = data->vertices.ptrw();
- Vector3 *data_normals_ptr = data->normals.ptrw();
-
- for (int i = 0; i < (int)morphs_vertex_indices.size(); i += 1) {
- const Vertex vertex = morphs_vertex_indices[i];
-
- ERR_FAIL_INDEX_MSG(vertex, vertex_count, "The blend shapes of this FBX file are corrupted. It has a not valid vertex.");
-
- data_vertices_ptr[vertex] = morphs_vertices[i];
-
- if (morphs_normals.size() != 0) {
- data_normals_ptr[vertex] = morphs_normals[i];
- }
- }
- }
- }
- }
-}
diff --git a/modules/fbx/data/fbx_mesh_data.h b/modules/fbx/data/fbx_mesh_data.h
deleted file mode 100644
index 358d0c2cb6e..00000000000
--- a/modules/fbx/data/fbx_mesh_data.h
+++ /dev/null
@@ -1,200 +0,0 @@
-/*************************************************************************/
-/* fbx_mesh_data.h */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#ifndef FBX_MESH_DATA_H
-#define FBX_MESH_DATA_H
-
-#include "core/templates/hash_map.h"
-#include "core/templates/local_vector.h"
-#include "core/templates/ordered_hash_map.h"
-#include "editor/import/resource_importer_scene.h"
-#include "scene/3d/importer_mesh_instance_3d.h"
-#include "scene/3d/mesh_instance_3d.h"
-#include "scene/resources/surface_tool.h"
-
-#include "fbx_bone.h"
-#include "fbx_parser/FBXMeshGeometry.h"
-#include "import_state.h"
-#include "tools/import_utils.h"
-
-struct FBXNode;
-struct FBXMeshData;
-struct FBXBone;
-struct ImportState;
-
-typedef int Vertex;
-typedef int SurfaceId;
-typedef int PolygonId;
-typedef int DataIndex;
-
-struct SurfaceData {
- Ref surface_tool;
- OrderedHashMap lookup_table; // proposed fix is to replace lookup_table[vertex_id] to give the position of the vertices_map[int] index.
- LocalVector vertices_map; // this must be ordered the same as insertion <-- slow to do find() operation.
- Ref material;
- HashMap> surface_polygon_vertex;
- Array morphs;
-};
-
-struct VertexWeightMapping {
- Vector weights;
- Vector bones;
- // This extra vector is used because the bone id is computed in a second step.
- // TODO Get rid of this extra step is a good idea.
- Vector[> bones_ref;
-};
-
-template
-struct VertexData {
- int polygon_index;
- T data;
-};
-
-// Caches mesh information and instantiates meshes for you using helper functions.
-struct FBXMeshData : RefCounted {
- struct MorphVertexData {
- // TODO we have only these??
- /// Each element is a vertex. Not supposed to be void.
- Vector vertices;
- /// Each element is a vertex. Not supposed to be void.
- Vector normals;
- };
-
- // FIXME: remove this is a hack for testing only
- mutable const FBXDocParser::MeshGeometry *mesh_geometry = nullptr;
-
- Ref mesh_node = nullptr;
- /// vertex id, Weight Info
- /// later: perf we can use array here
- HashMap vertex_weights;
-
- // translate fbx mesh data from document context to FBX Mesh Geometry Context
- bool valid_weight_indexes = false;
-
- ImporterMeshInstance3D *create_fbx_mesh(const ImportState &state, const FBXDocParser::MeshGeometry *p_mesh_geometry, const FBXDocParser::Model *model, bool use_compression);
-
- void gen_weight_info(Ref st, int vertex_id) const;
-
- /* mesh maximum weight count */
- bool valid_weight_count = false;
- int max_weight_count = 0;
- uint64_t armature_id = 0;
- bool valid_armature_id = false;
- ImporterMeshInstance3D *godot_mesh_instance = nullptr;
-
-private:
- void sanitize_vertex_weights(const ImportState &state);
-
- /// Make sure to reorganize the vertices so that the correct UV is taken.
- /// This step is needed because differently from the normal, that can be
- /// combined, the UV may need its own triangle because sometimes they have
- /// really different UV for the same vertex but different polygon.
- /// This function make sure to add another vertex for those UVS.
- void reorganize_vertices(
- std::vector &r_polygon_indices,
- std::vector &r_vertices,
- HashMap &r_normals,
- HashMap &r_uv_1,
- HashMap &r_uv_2,
- HashMap &r_color,
- HashMap &r_morphs,
- HashMap> &r_normals_raw,
- HashMap> &r_colors_raw,
- HashMap> &r_uv_1_raw,
- HashMap> &r_uv_2_raw);
-
- void add_vertex(
- const ImportState &state,
- Ref p_surface_tool,
- real_t p_scale,
- int p_vertex,
- const std::vector &p_vertices_position,
- const HashMap &p_normals,
- const HashMap &p_uvs_0,
- const HashMap &p_uvs_1,
- const HashMap &p_colors,
- const Vector3 &p_morph_value = Vector3(),
- const Vector3 &p_morph_normal = Vector3());
-
- void triangulate_polygon(SurfaceData *surface, const Vector &p_polygon_vertex, const std::vector &p_vertices) const;
-
- /// This function is responsible to convert the FBX polygon vertex to
- /// vertex index.
- /// The polygon vertices are stored in an array with some negative
- /// values. The negative values define the last face index.
- /// For example the following `face_array` contains two faces, the former
- /// with 3 vertices and the latter with a line:
- /// [0,2,-2,3,-5]
- /// Parsed as:
- /// [0, 2, 1, 3, 4]
- /// The negative values are computed using this formula: `(-value) - 1`
- ///
- /// Returns the vertex index from the polygon vertex.
- /// Returns -1 if `p_index` is invalid.
- int get_vertex_from_polygon_vertex(const std::vector &p_face_indices, int p_index) const;
-
- /// Returns true if this polygon_vertex_index is the end of a new polygon.
- bool is_end_of_polygon(const std::vector &p_face_indices, int p_index) const;
-
- /// Returns true if this polygon_vertex_index is the begin of a new polygon.
- bool is_start_of_polygon(const std::vector &p_face_indices, int p_index) const;
-
- /// Returns the number of polygons.
- int count_polygons(const std::vector &p_face_indices) const;
-
- /// Used to extract data from the `MappingData` aligned with vertex.
- /// Useful to extract normal/uvs/colors/tangents/etc...
- /// If the function fails somehow, it returns an hollow vector and print an error.
- template
- HashMap extract_per_vertex_data(
- int p_vertex_count,
- const std::vector &p_edges,
- const std::vector &p_mesh_indices,
- const FBXDocParser::MeshGeometry::MappingData &p_mapping_data,
- R (*collector_function)(const Vector> *p_vertex_data, R p_fall_back),
- R p_fall_back) const;
-
- /// Used to extract data from the `MappingData` organized per polygon.
- /// Useful to extract the material
- /// If the function fails somehow, it returns an hollow vector and print an error.
- template
- HashMap extract_per_polygon(
- int p_vertex_count,
- const std::vector &p_face_indices,
- const FBXDocParser::MeshGeometry::MappingData &p_fbx_data,
- T p_fallback_value) const;
-
- /// Extracts the morph data and organizes it per vertices.
- /// The returned `MorphVertexData` arrays are never something different
- /// then the `vertex_count`.
- void extract_morphs(const FBXDocParser::MeshGeometry *mesh_geometry, HashMap &r_data);
-};
-
-#endif // FBX_MESH_DATA_H
diff --git a/modules/fbx/data/fbx_node.h b/modules/fbx/data/fbx_node.h
deleted file mode 100644
index 7a4139dcdf5..00000000000
--- a/modules/fbx/data/fbx_node.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/*************************************************************************/
-/* fbx_node.h */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#ifndef FBX_NODE_H
-#define FBX_NODE_H
-
-#include "fbx_skeleton.h"
-#include "model_abstraction.h"
-#include "pivot_transform.h"
-
-#include "fbx_parser/FBXDocument.h"
-
-class Node3D;
-struct PivotTransform;
-
-struct FBXNode : RefCounted, ModelAbstraction {
- uint64_t current_node_id = 0;
- String node_name = String();
- Node3D *godot_node = nullptr;
-
- // used to parent the skeleton once the tree is built.
- Ref skeleton_node = Ref();
-
- void set_parent(Ref p_parent) {
- fbx_parent = p_parent;
- }
-
- void set_pivot_transform(Ref p_pivot_transform) {
- pivot_transform = p_pivot_transform;
- }
-
- Ref pivot_transform = Ref(); // local and global xform data
- Ref fbx_parent = Ref(); // parent node
-};
-
-#endif // FBX_NODE_H
diff --git a/modules/fbx/data/fbx_skeleton.cpp b/modules/fbx/data/fbx_skeleton.cpp
deleted file mode 100644
index 0225df16af1..00000000000
--- a/modules/fbx/data/fbx_skeleton.cpp
+++ /dev/null
@@ -1,130 +0,0 @@
-/*************************************************************************/
-/* fbx_skeleton.cpp */
-/*************************************************************************/
-/* This file is part of: */
-/* GODOT ENGINE */
-/* https://godotengine.org */
-/*************************************************************************/
-/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
-/* */
-/* Permission is hereby granted, free of charge, to any person obtaining */
-/* a copy of this software and associated documentation files (the */
-/* "Software"), to deal in the Software without restriction, including */
-/* without limitation the rights to use, copy, modify, merge, publish, */
-/* distribute, sublicense, and/or sell copies of the Software, and to */
-/* permit persons to whom the Software is furnished to do so, subject to */
-/* the following conditions: */
-/* */
-/* The above copyright notice and this permission notice shall be */
-/* included in all copies or substantial portions of the Software. */
-/* */
-/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
-/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
-/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
-/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
-/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
-/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
-/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
-/*************************************************************************/
-
-#include "fbx_skeleton.h"
-
-#include "import_state.h"
-
-#include "tools/import_utils.h"
-
-void FBXSkeleton::init_skeleton(const ImportState &state) {
- int skeleton_bone_count = skeleton_bones.size();
-
- if (skeleton == nullptr && skeleton_bone_count > 0) {
- skeleton = memnew(Skeleton3D);
-
- if (fbx_node.is_valid()) {
- // cache skeleton attachment for later during node creation
- // can't be done until after node hierarchy is built
- if (fbx_node->godot_node != state.root) {
- fbx_node->skeleton_node = Ref(this);
- print_verbose("cached armature skeleton attachment for node " + fbx_node->node_name);
- } else {
- // root node must never be a skeleton to prevent cyclic skeletons from being allowed (skeleton in a skeleton)
- fbx_node->godot_node->add_child(skeleton);
- skeleton->set_owner(state.root_owner);
- skeleton->set_name("Skeleton3D");
- print_verbose("created armature skeleton for root");
- }
- } else {
- memfree(skeleton);
- skeleton = nullptr;
- print_error("[doc] skeleton has no valid node to parent nodes to - erasing");
- skeleton_bones.clear();
- return;
- }
- }
-
- // Make the bone name uniques.
- for (int x = 0; x < skeleton_bone_count; x++) {
- Ref bone = skeleton_bones[x];
- if (bone.is_valid()) {
- // Make sure the bone name is unique.
- const String bone_name = bone->bone_name;
- int same_name_count = 0;
- for (int y = x + 1; y < skeleton_bone_count; y++) {
- Ref other_bone = skeleton_bones[y];
- if (other_bone.is_valid()) {
- if (other_bone->bone_name == bone_name) {
- same_name_count += 1;
- other_bone->bone_name += "_" + itos(same_name_count);
- }
- }
- }
- }
- }
-
- Map> bone_map;
- // implement fbx cluster skin logic here this is where it goes
- int bone_count = 0;
- for (int x = 0; x < skeleton_bone_count; x++) {
- Ref]