/* Open Asset Import Library (assimp) ---------------------------------------------------------------------- Copyright (c) 2006-2022, assimp team All rights reserved. Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the assimp team, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of the assimp team. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---------------------------------------------------------------------- */ /** @file FBXConverter.cpp * @brief Implementation of the FBX DOM -> aiScene converter */ #ifndef ASSIMP_BUILD_NO_FBX_IMPORTER #include "FBXConverter.h" #include "FBXDocument.h" #include "FBXImporter.h" #include "FBXMeshGeometry.h" #include "FBXParser.h" #include "FBXProperties.h" #include "FBXUtil.h" #include #include #include #include #include #include #include #include #include #include #include #include namespace Assimp { namespace FBX { using namespace Util; #define MAGIC_NODE_TAG "_$AssimpFbx$" #define CONVERT_FBX_TIME(time) static_cast(time) / 46186158000LL FBXConverter::FBXConverter(aiScene *out, const Document &doc, bool removeEmptyBones) : defaultMaterialIndex(), mMeshes(), lights(), cameras(), textures(), materials_converted(), textures_converted(), meshes_converted(), node_anim_chain_bits(), mNodeNames(), anim_fps(), mSceneOut(out), doc(doc), mRemoveEmptyBones(removeEmptyBones) { // animations need to be converted first since this will // populate the node_anim_chain_bits map, which is needed // to determine which nodes need to be generated. ConvertAnimations(); // Embedded textures in FBX could be connected to nothing but to itself, // for instance Texture -> Video connection only but not to the main graph, // The idea here is to traverse all objects to find these Textures and convert them, // so later during material conversion it will find converted texture in the textures_converted array. if (doc.Settings().readTextures) { ConvertOrphanedEmbeddedTextures(); } ConvertRootNode(); if (doc.Settings().readAllMaterials) { // unfortunately this means we have to evaluate all objects for (const ObjectMap::value_type &v : doc.Objects()) { const Object *ob = v.second->Get(); if (!ob) { continue; } const Material *mat = dynamic_cast(ob); if (mat) { if (materials_converted.find(mat) == materials_converted.end()) { ConvertMaterial(*mat, nullptr); } } } } ConvertGlobalSettings(); TransferDataToScene(); // if we didn't read any meshes set the AI_SCENE_FLAGS_INCOMPLETE // to make sure the scene passes assimp's validation. FBX files // need not contain geometry (i.e. camera animations, raw armatures). if (out->mNumMeshes == 0) { out->mFlags |= AI_SCENE_FLAGS_INCOMPLETE; } } FBXConverter::~FBXConverter() { std::for_each(mMeshes.begin(), mMeshes.end(), Util::delete_fun()); std::for_each(materials.begin(), materials.end(), Util::delete_fun()); std::for_each(animations.begin(), animations.end(), Util::delete_fun()); std::for_each(lights.begin(), lights.end(), Util::delete_fun()); std::for_each(cameras.begin(), cameras.end(), Util::delete_fun()); std::for_each(textures.begin(), textures.end(), Util::delete_fun()); } void FBXConverter::ConvertRootNode() { mSceneOut->mRootNode = new aiNode(); std::string unique_name; GetUniqueName("RootNode", unique_name); mSceneOut->mRootNode->mName.Set(unique_name); // root has ID 0 ConvertNodes(0L, mSceneOut->mRootNode, mSceneOut->mRootNode); } static std::string getAncestorBaseName(const aiNode *node) { const char *nodeName = nullptr; size_t length = 0; while (node && (!nodeName || length == 0)) { nodeName = node->mName.C_Str(); length = node->mName.length; node = node->mParent; } if (!nodeName || length == 0) { return {}; } // could be std::string_view if c++17 available return std::string(nodeName, length); } // Make unique name std::string FBXConverter::MakeUniqueNodeName(const Model *const model, const aiNode &parent) { std::string original_name = FixNodeName(model->Name()); if (original_name.empty()) { original_name = getAncestorBaseName(&parent); } std::string unique_name; GetUniqueName(original_name, unique_name); return unique_name; } /// This struct manages nodes which may or may not end up in the node hierarchy. /// When a node becomes a child of another node, that node becomes its owner and mOwnership should be released. struct FBXConverter::PotentialNode { PotentialNode() : mOwnership(new aiNode), mNode(mOwnership.get()) {} PotentialNode(const std::string& name) : mOwnership(new aiNode(name)), mNode(mOwnership.get()) {} aiNode* operator->() { return mNode; } std::unique_ptr mOwnership; aiNode* mNode; }; /// todo: pre-build node hierarchy /// todo: get bone from stack /// todo: make map of aiBone* to aiNode* /// then update convert clusters to the new format void FBXConverter::ConvertNodes(uint64_t id, aiNode *parent, aiNode *root_node) { const std::vector &conns = doc.GetConnectionsByDestinationSequenced(id, "Model"); std::vector nodes; nodes.reserve(conns.size()); std::vector nodes_chain; std::vector post_nodes_chain; for (const Connection *con : conns) { // ignore object-property links if (con->PropertyName().length()) { // really important we document why this is ignored. FBXImporter::LogInfo("ignoring property link - no docs on why this is ignored"); continue; //? } // convert connection source object into Object base class const Object *const object = con->SourceObject(); if (nullptr == object) { FBXImporter::LogError("failed to convert source object for Model link"); continue; } // FBX Model::Cube, Model::Bone001, etc elements // This detects if we can cast the object into this model structure. const Model *const model = dynamic_cast(object); if (nullptr != model) { nodes_chain.clear(); post_nodes_chain.clear(); aiMatrix4x4 new_abs_transform = parent->mTransformation; std::string node_name = FixNodeName(model->Name()); // even though there is only a single input node, the design of // assimp (or rather: the complicated transformation chain that // is employed by fbx) means that we may need multiple aiNode's // to represent a fbx node's transformation. // generate node transforms - this includes pivot data // if need_additional_node is true then you t const bool need_additional_node = GenerateTransformationNodeChain(*model, node_name, nodes_chain, post_nodes_chain); // assert that for the current node we must have at least a single transform ai_assert(nodes_chain.size()); if (need_additional_node) { nodes_chain.emplace_back(node_name); } //setup metadata on newest node SetupNodeMetadata(*model, *nodes_chain.back().mNode); // link all nodes in a row aiNode *last_parent = parent; for (PotentialNode& child : nodes_chain) { ai_assert(child.mNode); if (last_parent != parent) { last_parent->mNumChildren = 1; last_parent->mChildren = new aiNode *[1]; last_parent->mChildren[0] = child.mOwnership.release(); } child->mParent = last_parent; last_parent = child.mNode; } // attach geometry ConvertModel(*model, nodes_chain.back().mNode, root_node, new_abs_transform); // check if there will be any child nodes const std::vector &child_conns = doc.GetConnectionsByDestinationSequenced(model->ID(), "Model"); // if so, link the geometric transform inverse nodes // before we attach any child nodes if (child_conns.size()) { for (PotentialNode& postnode : post_nodes_chain) { ai_assert(postnode.mNode); if (last_parent != parent) { last_parent->mNumChildren = 1; last_parent->mChildren = new aiNode *[1]; last_parent->mChildren[0] = postnode.mOwnership.release(); } postnode->mParent = last_parent; last_parent = postnode.mNode; } } else { // free the nodes we allocated as we don't need them post_nodes_chain.clear(); } // recursion call - child nodes ConvertNodes(model->ID(), last_parent, root_node); if (doc.Settings().readLights) { ConvertLights(*model, node_name); } if (doc.Settings().readCameras) { ConvertCameras(*model, node_name); } nodes.push_back(std::move(nodes_chain.front())); nodes_chain.clear(); } } if (nodes.empty()) { parent->mNumChildren = 0; parent->mChildren = nullptr; } parent->mChildren = new aiNode *[nodes.size()](); parent->mNumChildren = static_cast(nodes.size()); for (unsigned int i = 0; i < nodes.size(); ++i) { parent->mChildren[i] = nodes[i].mOwnership.release(); } } void FBXConverter::ConvertLights(const Model &model, const std::string &orig_name) { const std::vector &node_attrs = model.GetAttributes(); for (const NodeAttribute *attr : node_attrs) { const Light *const light = dynamic_cast(attr); if (light) { ConvertLight(*light, orig_name); } } } void FBXConverter::ConvertCameras(const Model &model, const std::string &orig_name) { const std::vector &node_attrs = model.GetAttributes(); for (const NodeAttribute *attr : node_attrs) { const Camera *const cam = dynamic_cast(attr); if (cam) { ConvertCamera(*cam, orig_name); } } } void FBXConverter::ConvertLight(const Light &light, const std::string &orig_name) { lights.push_back(new aiLight()); aiLight *const out_light = lights.back(); out_light->mName.Set(orig_name); const float intensity = light.Intensity() / 100.0f; const aiVector3D &col = light.Color(); out_light->mColorDiffuse = aiColor3D(col.x, col.y, col.z); out_light->mColorDiffuse.r *= intensity; out_light->mColorDiffuse.g *= intensity; out_light->mColorDiffuse.b *= intensity; out_light->mColorSpecular = out_light->mColorDiffuse; //lights are defined along negative y direction out_light->mPosition = aiVector3D(0.0f); out_light->mDirection = aiVector3D(0.0f, -1.0f, 0.0f); out_light->mUp = aiVector3D(0.0f, 0.0f, -1.0f); switch (light.LightType()) { case Light::Type_Point: out_light->mType = aiLightSource_POINT; break; case Light::Type_Directional: out_light->mType = aiLightSource_DIRECTIONAL; break; case Light::Type_Spot: out_light->mType = aiLightSource_SPOT; out_light->mAngleOuterCone = AI_DEG_TO_RAD(light.OuterAngle()); out_light->mAngleInnerCone = AI_DEG_TO_RAD(light.InnerAngle()); break; case Light::Type_Area: FBXImporter::LogWarn("cannot represent area light, set to UNDEFINED"); out_light->mType = aiLightSource_UNDEFINED; break; case Light::Type_Volume: FBXImporter::LogWarn("cannot represent volume light, set to UNDEFINED"); out_light->mType = aiLightSource_UNDEFINED; break; default: ai_assert(false); } float decay = light.DecayStart(); switch (light.DecayType()) { case Light::Decay_None: out_light->mAttenuationConstant = decay; out_light->mAttenuationLinear = 0.0f; out_light->mAttenuationQuadratic = 0.0f; break; case Light::Decay_Linear: out_light->mAttenuationConstant = 0.0f; out_light->mAttenuationLinear = 2.0f / decay; out_light->mAttenuationQuadratic = 0.0f; break; case Light::Decay_Quadratic: out_light->mAttenuationConstant = 0.0f; out_light->mAttenuationLinear = 0.0f; out_light->mAttenuationQuadratic = 2.0f / (decay * decay); break; case Light::Decay_Cubic: FBXImporter::LogWarn("cannot represent cubic attenuation, set to Quadratic"); out_light->mAttenuationQuadratic = 1.0f; break; default: ai_assert(false); break; } } void FBXConverter::ConvertCamera(const Camera &cam, const std::string &orig_name) { cameras.push_back(new aiCamera()); aiCamera *const out_camera = cameras.back(); out_camera->mName.Set(orig_name); out_camera->mAspect = cam.AspectWidth() / cam.AspectHeight(); // NOTE: Camera mPosition, mLookAt and mUp must be set to default here. // All transformations to the camera will be handled by its node in the scenegraph. out_camera->mPosition = aiVector3D(0.0f); out_camera->mLookAt = aiVector3D(1.0f, 0.0f, 0.0f); out_camera->mUp = aiVector3D(0.0f, 1.0f, 0.0f); // NOTE: Some software (maya) does not put FieldOfView in FBX, so we compute // mHorizontalFOV from FocalLength and FilmWidth with unit conversion. // TODO: This is not a complete solution for how FBX cameras can be stored. // TODO: Incorporate non-square pixel aspect ratio. // TODO: FBX aperture mode might be storing vertical FOV in need of conversion with aspect ratio. float fov_deg = cam.FieldOfView(); // If FOV not specified in file, compute using FilmWidth and FocalLength. if (fov_deg == kFovUnknown) { float film_width_inches = cam.FilmWidth(); float focal_length_mm = cam.FocalLength(); ASSIMP_LOG_VERBOSE_DEBUG("FBX FOV unspecified. Computing from FilmWidth (", film_width_inches, "inches) and FocalLength (", focal_length_mm, "mm)."); double half_fov_rad = std::atan2(film_width_inches * 25.4 * 0.5, focal_length_mm); out_camera->mHorizontalFOV = static_cast(half_fov_rad); } else { // FBX fov is full-view degrees. We want half-view radians. out_camera->mHorizontalFOV = AI_DEG_TO_RAD(fov_deg) * 0.5f; } out_camera->mClipPlaneNear = cam.NearPlane(); out_camera->mClipPlaneFar = cam.FarPlane(); } void FBXConverter::GetUniqueName(const std::string &name, std::string &uniqueName) { uniqueName = name; auto it_pair = mNodeNames.insert({ name, 0 }); // duplicate node name instance count unsigned int &i = it_pair.first->second; while (!it_pair.second) { ++i; std::ostringstream ext; ext << name << std::setfill('0') << std::setw(3) << i; uniqueName = ext.str(); it_pair = mNodeNames.insert({ uniqueName, 0 }); } } const char *FBXConverter::NameTransformationComp(TransformationComp comp) { switch (comp) { case TransformationComp_Translation: return "Translation"; case TransformationComp_RotationOffset: return "RotationOffset"; case TransformationComp_RotationPivot: return "RotationPivot"; case TransformationComp_PreRotation: return "PreRotation"; case TransformationComp_Rotation: return "Rotation"; case TransformationComp_PostRotation: return "PostRotation"; case TransformationComp_RotationPivotInverse: return "RotationPivotInverse"; case TransformationComp_ScalingOffset: return "ScalingOffset"; case TransformationComp_ScalingPivot: return "ScalingPivot"; case TransformationComp_Scaling: return "Scaling"; case TransformationComp_ScalingPivotInverse: return "ScalingPivotInverse"; case TransformationComp_GeometricScaling: return "GeometricScaling"; case TransformationComp_GeometricRotation: return "GeometricRotation"; case TransformationComp_GeometricTranslation: return "GeometricTranslation"; case TransformationComp_GeometricScalingInverse: return "GeometricScalingInverse"; case TransformationComp_GeometricRotationInverse: return "GeometricRotationInverse"; case TransformationComp_GeometricTranslationInverse: return "GeometricTranslationInverse"; case TransformationComp_MAXIMUM: // this is to silence compiler warnings default: break; } ai_assert(false); return nullptr; } const char *FBXConverter::NameTransformationCompProperty(TransformationComp comp) { switch (comp) { case TransformationComp_Translation: return "Lcl Translation"; case TransformationComp_RotationOffset: return "RotationOffset"; case TransformationComp_RotationPivot: return "RotationPivot"; case TransformationComp_PreRotation: return "PreRotation"; case TransformationComp_Rotation: return "Lcl Rotation"; case TransformationComp_PostRotation: return "PostRotation"; case TransformationComp_RotationPivotInverse: return "RotationPivotInverse"; case TransformationComp_ScalingOffset: return "ScalingOffset"; case TransformationComp_ScalingPivot: return "ScalingPivot"; case TransformationComp_Scaling: return "Lcl Scaling"; case TransformationComp_ScalingPivotInverse: return "ScalingPivotInverse"; case TransformationComp_GeometricScaling: return "GeometricScaling"; case TransformationComp_GeometricRotation: return "GeometricRotation"; case TransformationComp_GeometricTranslation: return "GeometricTranslation"; case TransformationComp_GeometricScalingInverse: return "GeometricScalingInverse"; case TransformationComp_GeometricRotationInverse: return "GeometricRotationInverse"; case TransformationComp_GeometricTranslationInverse: return "GeometricTranslationInverse"; case TransformationComp_MAXIMUM: // this is to silence compiler warnings break; } ai_assert(false); return nullptr; } aiVector3D FBXConverter::TransformationCompDefaultValue(TransformationComp comp) { // XXX a neat way to solve the never-ending special cases for scaling // would be to do everything in log space! return comp == TransformationComp_Scaling ? aiVector3D(1.f, 1.f, 1.f) : aiVector3D(); } void FBXConverter::GetRotationMatrix(Model::RotOrder mode, const aiVector3D &rotation, aiMatrix4x4 &out) { if (mode == Model::RotOrder_SphericXYZ) { FBXImporter::LogError("Unsupported RotationMode: SphericXYZ"); out = aiMatrix4x4(); return; } const float angle_epsilon = Math::getEpsilon(); out = aiMatrix4x4(); bool is_id[3] = { true, true, true }; aiMatrix4x4 temp[3]; const auto rot = AI_DEG_TO_RAD(rotation); if (std::fabs(rot.z) > angle_epsilon) { aiMatrix4x4::RotationZ(rot.z, temp[2]); is_id[2] = false; } if (std::fabs(rot.y) > angle_epsilon) { aiMatrix4x4::RotationY(rot.y, temp[1]); is_id[1] = false; } if (std::fabs(rot.x) > angle_epsilon) { aiMatrix4x4::RotationX(rot.x, temp[0]); is_id[0] = false; } int order[3] = { -1, -1, -1 }; // note: rotation order is inverted since we're left multiplying as is usual in assimp switch (mode) { case Model::RotOrder_EulerXYZ: order[0] = 2; order[1] = 1; order[2] = 0; break; case Model::RotOrder_EulerXZY: order[0] = 1; order[1] = 2; order[2] = 0; break; case Model::RotOrder_EulerYZX: order[0] = 0; order[1] = 2; order[2] = 1; break; case Model::RotOrder_EulerYXZ: order[0] = 2; order[1] = 0; order[2] = 1; break; case Model::RotOrder_EulerZXY: order[0] = 1; order[1] = 0; order[2] = 2; break; case Model::RotOrder_EulerZYX: order[0] = 0; order[1] = 1; order[2] = 2; break; default: ai_assert(false); break; } ai_assert(order[0] >= 0); ai_assert(order[0] <= 2); ai_assert(order[1] >= 0); ai_assert(order[1] <= 2); ai_assert(order[2] >= 0); ai_assert(order[2] <= 2); if (!is_id[order[0]]) { out = temp[order[0]]; } if (!is_id[order[1]]) { out = out * temp[order[1]]; } if (!is_id[order[2]]) { out = out * temp[order[2]]; } } bool FBXConverter::NeedsComplexTransformationChain(const Model &model) { const PropertyTable &props = model.Props(); const auto zero_epsilon = Math::getEpsilon(); const aiVector3D all_ones(1.0f, 1.0f, 1.0f); for (size_t i = 0; i < TransformationComp_MAXIMUM; ++i) { const TransformationComp comp = static_cast(i); if (comp == TransformationComp_Rotation || comp == TransformationComp_Scaling || comp == TransformationComp_Translation || comp == TransformationComp_PreRotation || comp == TransformationComp_PostRotation) { continue; } bool scale_compare = (comp == TransformationComp_GeometricScaling || comp == TransformationComp_Scaling); bool ok = true; const aiVector3D &v = PropertyGet(props, NameTransformationCompProperty(comp), ok); if (ok && scale_compare) { if ((v - all_ones).SquareLength() > zero_epsilon) { return true; } } else if (ok) { if (v.SquareLength() > zero_epsilon) { return true; } } } return false; } std::string FBXConverter::NameTransformationChainNode(const std::string &name, TransformationComp comp) { return name + std::string(MAGIC_NODE_TAG) + "_" + NameTransformationComp(comp); } bool FBXConverter::GenerateTransformationNodeChain(const Model &model, const std::string &name, std::vector &output_nodes, std::vector &post_output_nodes) { const PropertyTable &props = model.Props(); const Model::RotOrder rot = model.RotationOrder(); bool ok; aiMatrix4x4 chain[TransformationComp_MAXIMUM]; ai_assert(TransformationComp_MAXIMUM < 32); std::uint32_t chainBits = 0; // A node won't need a node chain if it only has these. const std::uint32_t chainMaskSimple = (1 << TransformationComp_Translation) + (1 << TransformationComp_Scaling) + (1 << TransformationComp_Rotation); // A node will need a node chain if it has any of these. const std::uint32_t chainMaskComplex = ((1 << (TransformationComp_MAXIMUM)) - 1) - chainMaskSimple; std::fill_n(chain, static_cast(TransformationComp_MAXIMUM), aiMatrix4x4()); // generate transformation matrices for all the different transformation components const float zero_epsilon = Math::getEpsilon(); const aiVector3D all_ones(1.0f, 1.0f, 1.0f); const aiVector3D &PreRotation = PropertyGet(props, "PreRotation", ok); if (ok && PreRotation.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_PreRotation); GetRotationMatrix(Model::RotOrder::RotOrder_EulerXYZ, PreRotation, chain[TransformationComp_PreRotation]); } const aiVector3D &PostRotation = PropertyGet(props, "PostRotation", ok); if (ok && PostRotation.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_PostRotation); GetRotationMatrix(Model::RotOrder::RotOrder_EulerXYZ, PostRotation, chain[TransformationComp_PostRotation]); } const aiVector3D &RotationPivot = PropertyGet(props, "RotationPivot", ok); if (ok && RotationPivot.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_RotationPivot) | (1 << TransformationComp_RotationPivotInverse); aiMatrix4x4::Translation(RotationPivot, chain[TransformationComp_RotationPivot]); aiMatrix4x4::Translation(-RotationPivot, chain[TransformationComp_RotationPivotInverse]); } const aiVector3D &RotationOffset = PropertyGet(props, "RotationOffset", ok); if (ok && RotationOffset.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_RotationOffset); aiMatrix4x4::Translation(RotationOffset, chain[TransformationComp_RotationOffset]); } const aiVector3D &ScalingOffset = PropertyGet(props, "ScalingOffset", ok); if (ok && ScalingOffset.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_ScalingOffset); aiMatrix4x4::Translation(ScalingOffset, chain[TransformationComp_ScalingOffset]); } const aiVector3D &ScalingPivot = PropertyGet(props, "ScalingPivot", ok); if (ok && ScalingPivot.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_ScalingPivot) | (1 << TransformationComp_ScalingPivotInverse); aiMatrix4x4::Translation(ScalingPivot, chain[TransformationComp_ScalingPivot]); aiMatrix4x4::Translation(-ScalingPivot, chain[TransformationComp_ScalingPivotInverse]); } const aiVector3D &Translation = PropertyGet(props, "Lcl Translation", ok); if (ok && Translation.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_Translation); aiMatrix4x4::Translation(Translation, chain[TransformationComp_Translation]); } const aiVector3D &Scaling = PropertyGet(props, "Lcl Scaling", ok); if (ok && (Scaling - all_ones).SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_Scaling); aiMatrix4x4::Scaling(Scaling, chain[TransformationComp_Scaling]); } const aiVector3D &Rotation = PropertyGet(props, "Lcl Rotation", ok); if (ok && Rotation.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_Rotation); GetRotationMatrix(rot, Rotation, chain[TransformationComp_Rotation]); } const aiVector3D &GeometricScaling = PropertyGet(props, "GeometricScaling", ok); if (ok && (GeometricScaling - all_ones).SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_GeometricScaling); aiMatrix4x4::Scaling(GeometricScaling, chain[TransformationComp_GeometricScaling]); aiVector3D GeometricScalingInverse = GeometricScaling; bool canscale = true; for (unsigned int i = 0; i < 3; ++i) { if (std::fabs(GeometricScalingInverse[i]) > zero_epsilon) { GeometricScalingInverse[i] = 1.0f / GeometricScaling[i]; } else { FBXImporter::LogError("cannot invert geometric scaling matrix with a 0.0 scale component"); canscale = false; break; } } if (canscale) { chainBits = chainBits | (1 << TransformationComp_GeometricScalingInverse); aiMatrix4x4::Scaling(GeometricScalingInverse, chain[TransformationComp_GeometricScalingInverse]); } } const aiVector3D &GeometricRotation = PropertyGet(props, "GeometricRotation", ok); if (ok && GeometricRotation.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_GeometricRotation) | (1 << TransformationComp_GeometricRotationInverse); GetRotationMatrix(rot, GeometricRotation, chain[TransformationComp_GeometricRotation]); GetRotationMatrix(rot, GeometricRotation, chain[TransformationComp_GeometricRotationInverse]); chain[TransformationComp_GeometricRotationInverse].Inverse(); } const aiVector3D &GeometricTranslation = PropertyGet(props, "GeometricTranslation", ok); if (ok && GeometricTranslation.SquareLength() > zero_epsilon) { chainBits = chainBits | (1 << TransformationComp_GeometricTranslation) | (1 << TransformationComp_GeometricTranslationInverse); aiMatrix4x4::Translation(GeometricTranslation, chain[TransformationComp_GeometricTranslation]); aiMatrix4x4::Translation(-GeometricTranslation, chain[TransformationComp_GeometricTranslationInverse]); } // now, if we have more than just Translation, Scaling and Rotation, // we need to generate a full node chain to accommodate for assimp's // lack to express pivots and offsets. if ((chainBits & chainMaskComplex) && doc.Settings().preservePivots) { FBXImporter::LogInfo("generating full transformation chain for node: ", name); // query the anim_chain_bits dictionary to find out which chain elements // have associated node animation channels. These can not be dropped // even if they have identity transform in bind pose. NodeAnimBitMap::const_iterator it = node_anim_chain_bits.find(name); const unsigned int anim_chain_bitmask = (it == node_anim_chain_bits.end() ? 0 : (*it).second); unsigned int bit = 0x1; for (size_t i = 0; i < TransformationComp_MAXIMUM; ++i, bit <<= 1) { const TransformationComp comp = static_cast(i); if ((chainBits & bit) == 0 && (anim_chain_bitmask & bit) == 0) { continue; } if (comp == TransformationComp_PostRotation) { chain[i] = chain[i].Inverse(); } PotentialNode nd; nd->mName.Set(NameTransformationChainNode(name, comp)); nd->mTransformation = chain[i]; // geometric inverses go in a post-node chain if (comp == TransformationComp_GeometricScalingInverse || comp == TransformationComp_GeometricRotationInverse || comp == TransformationComp_GeometricTranslationInverse) { post_output_nodes.emplace_back(std::move(nd)); } else { output_nodes.emplace_back(std::move(nd)); } } ai_assert(output_nodes.size()); return true; } // else, we can just multiply the matrices together PotentialNode nd; // name passed to the method is already unique nd->mName.Set(name); // for (const auto &transform : chain) { // skip inverse chain for no preservePivots for (unsigned int i = TransformationComp_Translation; i < TransformationComp_MAXIMUM; i++) { nd->mTransformation = nd->mTransformation * chain[i]; } output_nodes.push_back(std::move(nd)); return false; } void FBXConverter::SetupNodeMetadata(const Model &model, aiNode &nd) { const PropertyTable &props = model.Props(); DirectPropertyMap unparsedProperties = props.GetUnparsedProperties(); // create metadata on node const std::size_t numStaticMetaData = 2; aiMetadata *data = aiMetadata::Alloc(static_cast(unparsedProperties.size() + numStaticMetaData)); nd.mMetaData = data; int index = 0; // find user defined properties (3ds Max) data->Set(index++, "UserProperties", aiString(PropertyGet(props, "UDP3DSMAX", ""))); // preserve the info that a node was marked as Null node in the original file. data->Set(index++, "IsNull", model.IsNull() ? true : false); // add unparsed properties to the node's metadata for (const DirectPropertyMap::value_type &prop : unparsedProperties) { // Interpret the property as a concrete type if (const TypedProperty *interpretedBool = prop.second->As>()) { data->Set(index++, prop.first, interpretedBool->Value()); } else if (const TypedProperty *interpretedInt = prop.second->As>()) { data->Set(index++, prop.first, interpretedInt->Value()); } else if (const TypedProperty *interpretedUInt = prop.second->As>()) { data->Set(index++, prop.first, interpretedUInt->Value()); } else if (const TypedProperty *interpretedUint64 = prop.second->As>()) { data->Set(index++, prop.first, interpretedUint64->Value()); } else if (const TypedProperty *interpretedint64 = prop.second->As>()) { data->Set(index++, prop.first, interpretedint64->Value()); } else if (const TypedProperty *interpretedFloat = prop.second->As>()) { data->Set(index++, prop.first, interpretedFloat->Value()); } else if (const TypedProperty *interpretedString = prop.second->As>()) { data->Set(index++, prop.first, aiString(interpretedString->Value())); } else if (const TypedProperty *interpretedVec3 = prop.second->As>()) { data->Set(index++, prop.first, interpretedVec3->Value()); } else { ai_assert(false); } } } void FBXConverter::ConvertModel(const Model &model, aiNode *parent, aiNode *root_node, const aiMatrix4x4 &absolute_transform) { const std::vector &geos = model.GetGeometry(); std::vector meshes; meshes.reserve(geos.size()); for (const Geometry *geo : geos) { const MeshGeometry *const mesh = dynamic_cast(geo); const LineGeometry *const line = dynamic_cast(geo); if (mesh) { const std::vector &indices = ConvertMesh(*mesh, model, parent, root_node, absolute_transform); std::copy(indices.begin(), indices.end(), std::back_inserter(meshes)); } else if (line) { const std::vector &indices = ConvertLine(*line, root_node); std::copy(indices.begin(), indices.end(), std::back_inserter(meshes)); } else if (geo) { FBXImporter::LogWarn("ignoring unrecognized geometry: ", geo->Name()); } else { FBXImporter::LogWarn("skipping null geometry"); } } if (meshes.size()) { parent->mMeshes = new unsigned int[meshes.size()](); parent->mNumMeshes = static_cast(meshes.size()); std::swap_ranges(meshes.begin(), meshes.end(), parent->mMeshes); } } std::vector FBXConverter::ConvertMesh(const MeshGeometry &mesh, const Model &model, aiNode *parent, aiNode *root_node, const aiMatrix4x4 &absolute_transform) { std::vector temp; MeshMap::const_iterator it = meshes_converted.find(&mesh); if (it != meshes_converted.end()) { std::copy((*it).second.begin(), (*it).second.end(), std::back_inserter(temp)); return temp; } const std::vector &vertices = mesh.GetVertices(); const std::vector &faces = mesh.GetFaceIndexCounts(); if (vertices.empty() || faces.empty()) { FBXImporter::LogWarn("ignoring empty geometry: ", mesh.Name()); return temp; } // one material per mesh maps easily to aiMesh. Multiple material // meshes need to be split. const MatIndexArray &mindices = mesh.GetMaterialIndices(); if (doc.Settings().readMaterials && !mindices.empty()) { const MatIndexArray::value_type base = mindices[0]; for (MatIndexArray::value_type index : mindices) { if (index != base) { return ConvertMeshMultiMaterial(mesh, model, absolute_transform, parent, root_node); } } } // faster code-path, just copy the data temp.push_back(ConvertMeshSingleMaterial(mesh, model, absolute_transform, parent, root_node)); return temp; } std::vector FBXConverter::ConvertLine(const LineGeometry &line, aiNode *root_node) { std::vector temp; const std::vector &vertices = line.GetVertices(); const std::vector &indices = line.GetIndices(); if (vertices.empty() || indices.empty()) { FBXImporter::LogWarn("ignoring empty line: ", line.Name()); return temp; } aiMesh *const out_mesh = SetupEmptyMesh(line, root_node); out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE; // copy vertices out_mesh->mNumVertices = static_cast(vertices.size()); out_mesh->mVertices = new aiVector3D[out_mesh->mNumVertices]; std::copy(vertices.begin(), vertices.end(), out_mesh->mVertices); //Number of line segments (faces) is "Number of Points - Number of Endpoints" //N.B.: Endpoints in FbxLine are denoted by negative indices. //If such an Index is encountered, add 1 and multiply by -1 to get the real index. unsigned int epcount = 0; for (unsigned i = 0; i < indices.size(); i++) { if (indices[i] < 0) { epcount++; } } unsigned int pcount = static_cast(indices.size()); unsigned int scount = out_mesh->mNumFaces = pcount - epcount; aiFace *fac = out_mesh->mFaces = new aiFace[scount](); for (unsigned int i = 0; i < pcount; ++i) { if (indices[i] < 0) continue; aiFace &f = *fac++; f.mNumIndices = 2; //2 == aiPrimitiveType_LINE f.mIndices = new unsigned int[2]; f.mIndices[0] = indices[i]; int segid = indices[(i + 1 == pcount ? 0 : i + 1)]; //If we have reached he last point, wrap around f.mIndices[1] = (segid < 0 ? (segid + 1) * -1 : segid); //Convert EndPoint Index to normal Index } temp.push_back(static_cast(mMeshes.size() - 1)); return temp; } aiMesh *FBXConverter::SetupEmptyMesh(const Geometry &mesh, aiNode *parent) { aiMesh *const out_mesh = new aiMesh(); mMeshes.push_back(out_mesh); meshes_converted[&mesh].push_back(static_cast(mMeshes.size() - 1)); // set name std::string name = mesh.Name(); if (name.substr(0, 10) == "Geometry::") { name = name.substr(10); } if (name.length()) { out_mesh->mName.Set(name); } else { out_mesh->mName = parent->mName; } return out_mesh; } static aiSkeleton *createAiSkeleton(SkeletonBoneContainer &sbc) { if (sbc.MeshArray.empty() || sbc.SkeletonBoneToMeshLookup.empty()) { return nullptr; } aiSkeleton *skeleton = new aiSkeleton; for (auto *mesh : sbc.MeshArray) { auto it = sbc.SkeletonBoneToMeshLookup.find(mesh); if (it == sbc.SkeletonBoneToMeshLookup.end()) { continue; } SkeletonBoneArray *ba = it->second; if (ba == nullptr) { continue; } skeleton->mNumBones = static_cast(ba->size()); skeleton->mBones = new aiSkeletonBone*[skeleton->mNumBones]; size_t index = 0; for (auto bone : (* ba)) { skeleton->mBones[index] = bone; ++index; } } return skeleton; } unsigned int FBXConverter::ConvertMeshSingleMaterial(const MeshGeometry &mesh, const Model &model, const aiMatrix4x4 &absolute_transform, aiNode *parent, aiNode *) { const MatIndexArray &mindices = mesh.GetMaterialIndices(); aiMesh *const out_mesh = SetupEmptyMesh(mesh, parent); const std::vector &vertices = mesh.GetVertices(); const std::vector &faces = mesh.GetFaceIndexCounts(); // copy vertices out_mesh->mNumVertices = static_cast(vertices.size()); out_mesh->mVertices = new aiVector3D[vertices.size()]; std::copy(vertices.begin(), vertices.end(), out_mesh->mVertices); // generate dummy faces out_mesh->mNumFaces = static_cast(faces.size()); aiFace *fac = out_mesh->mFaces = new aiFace[faces.size()](); unsigned int cursor = 0; for (unsigned int pcount : faces) { aiFace &f = *fac++; f.mNumIndices = pcount; f.mIndices = new unsigned int[pcount]; switch (pcount) { case 1: out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT; break; case 2: out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE; break; case 3: out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE; break; default: out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON; break; } for (unsigned int i = 0; i < pcount; ++i) { f.mIndices[i] = cursor++; } } // copy normals const std::vector &normals = mesh.GetNormals(); if (normals.size()) { ai_assert(normals.size() == vertices.size()); out_mesh->mNormals = new aiVector3D[vertices.size()]; std::copy(normals.begin(), normals.end(), out_mesh->mNormals); } // copy tangents - assimp requires both tangents and bitangents (binormals) // to be present, or neither of them. Compute binormals from normals // and tangents if needed. const std::vector &tangents = mesh.GetTangents(); const std::vector *binormals = &mesh.GetBinormals(); if (tangents.size()) { std::vector tempBinormals; if (!binormals->size()) { if (normals.size()) { tempBinormals.resize(normals.size()); for (unsigned int i = 0; i < tangents.size(); ++i) { tempBinormals[i] = normals[i] ^ tangents[i]; } binormals = &tempBinormals; } else { binormals = nullptr; } } if (binormals) { ai_assert(tangents.size() == vertices.size()); ai_assert(binormals->size() == vertices.size()); out_mesh->mTangents = new aiVector3D[vertices.size()]; std::copy(tangents.begin(), tangents.end(), out_mesh->mTangents); out_mesh->mBitangents = new aiVector3D[vertices.size()]; std::copy(binormals->begin(), binormals->end(), out_mesh->mBitangents); } } // copy texture coords for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i) { const std::vector &uvs = mesh.GetTextureCoords(i); if (uvs.empty()) { break; } aiVector3D *out_uv = out_mesh->mTextureCoords[i] = new aiVector3D[vertices.size()]; for (const aiVector2D &v : uvs) { *out_uv++ = aiVector3D(v.x, v.y, 0.0f); } out_mesh->SetTextureCoordsName(i, aiString(mesh.GetTextureCoordChannelName(i))); out_mesh->mNumUVComponents[i] = 2; } // copy vertex colors for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i) { const std::vector &colors = mesh.GetVertexColors(i); if (colors.empty()) { break; } out_mesh->mColors[i] = new aiColor4D[vertices.size()]; std::copy(colors.begin(), colors.end(), out_mesh->mColors[i]); } if (!doc.Settings().readMaterials || mindices.empty()) { FBXImporter::LogError("no material assigned to mesh, setting default material"); out_mesh->mMaterialIndex = GetDefaultMaterial(); } else { ConvertMaterialForMesh(out_mesh, model, mesh, mindices[0]); } if (doc.Settings().readWeights && mesh.DeformerSkin() != nullptr && !doc.Settings().useSkeleton) { ConvertWeights(out_mesh, mesh, absolute_transform, parent, NO_MATERIAL_SEPARATION, nullptr); } else if (doc.Settings().readWeights && mesh.DeformerSkin() != nullptr && doc.Settings().useSkeleton) { SkeletonBoneContainer sbc; ConvertWeightsToSkeleton(out_mesh, mesh, absolute_transform, parent, NO_MATERIAL_SEPARATION, nullptr, sbc); aiSkeleton *skeleton = createAiSkeleton(sbc); if (skeleton != nullptr) { mSkeletons.emplace_back(skeleton); } } std::vector animMeshes; for (const BlendShape *blendShape : mesh.GetBlendShapes()) { for (const BlendShapeChannel *blendShapeChannel : blendShape->BlendShapeChannels()) { const auto& shapeGeometries = blendShapeChannel->GetShapeGeometries(); for (const ShapeGeometry *shapeGeometry : shapeGeometries) { aiAnimMesh *animMesh = aiCreateAnimMesh(out_mesh); const auto &curVertices = shapeGeometry->GetVertices(); const auto &curNormals = shapeGeometry->GetNormals(); const auto &curIndices = shapeGeometry->GetIndices(); //losing channel name if using shapeGeometry->Name() // if blendShapeChannel Name is empty or don't have a ".", add geoMetryName; auto aniName = FixAnimMeshName(blendShapeChannel->Name()); auto geoMetryName = FixAnimMeshName(shapeGeometry->Name()); if (aniName.empty()) { aniName = geoMetryName; } else if (aniName.find('.') == aniName.npos) { aniName += "." + geoMetryName; } animMesh->mName.Set(aniName); for (size_t j = 0; j < curIndices.size(); j++) { const unsigned int curIndex = curIndices.at(j); aiVector3D vertex = curVertices.at(j); aiVector3D normal = curNormals.at(j); unsigned int count = 0; const unsigned int *outIndices = mesh.ToOutputVertexIndex(curIndex, count); for (unsigned int k = 0; k < count; k++) { unsigned int index = outIndices[k]; animMesh->mVertices[index] += vertex; if (animMesh->mNormals != nullptr) { animMesh->mNormals[index] += normal; animMesh->mNormals[index].NormalizeSafe(); } } } animMesh->mWeight = shapeGeometries.size() > 1 ? blendShapeChannel->DeformPercent() / 100.0f : 1.0f; animMeshes.push_back(animMesh); } } } const size_t numAnimMeshes = animMeshes.size(); if (numAnimMeshes > 0) { out_mesh->mNumAnimMeshes = static_cast(numAnimMeshes); out_mesh->mAnimMeshes = new aiAnimMesh *[numAnimMeshes]; for (size_t i = 0; i < numAnimMeshes; i++) { out_mesh->mAnimMeshes[i] = animMeshes.at(i); } } return static_cast(mMeshes.size() - 1); } std::vector FBXConverter::ConvertMeshMultiMaterial(const MeshGeometry &mesh, const Model &model, const aiMatrix4x4 &absolute_transform, aiNode *parent, aiNode *root_node) { const MatIndexArray &mindices = mesh.GetMaterialIndices(); ai_assert(mindices.size()); std::set had; std::vector indices; for (MatIndexArray::value_type index : mindices) { if (had.find(index) == had.end()) { indices.push_back(ConvertMeshMultiMaterial(mesh, model, absolute_transform, index, parent, root_node)); had.insert(index); } } return indices; } unsigned int FBXConverter::ConvertMeshMultiMaterial(const MeshGeometry &mesh, const Model &model, const aiMatrix4x4 &absolute_transform, MatIndexArray::value_type index, aiNode *parent, aiNode *) { aiMesh *const out_mesh = SetupEmptyMesh(mesh, parent); const MatIndexArray &mindices = mesh.GetMaterialIndices(); const std::vector &vertices = mesh.GetVertices(); const std::vector &faces = mesh.GetFaceIndexCounts(); const bool process_weights = doc.Settings().readWeights && mesh.DeformerSkin() != nullptr; unsigned int count_faces = 0; unsigned int count_vertices = 0; // count faces std::vector::const_iterator itf = faces.begin(); for (MatIndexArray::const_iterator it = mindices.begin(), end = mindices.end(); it != end; ++it, ++itf) { if ((*it) != index) { continue; } ++count_faces; count_vertices += *itf; } ai_assert(count_faces); ai_assert(count_vertices); // mapping from output indices to DOM indexing, needed to resolve weights or blendshapes std::vector reverseMapping; std::map translateIndexMap; if (process_weights || mesh.GetBlendShapes().size() > 0) { reverseMapping.resize(count_vertices); } // allocate output data arrays, but don't fill them yet out_mesh->mNumVertices = count_vertices; out_mesh->mVertices = new aiVector3D[count_vertices]; out_mesh->mNumFaces = count_faces; aiFace *fac = out_mesh->mFaces = new aiFace[count_faces](); // allocate normals const std::vector &normals = mesh.GetNormals(); if (normals.size()) { ai_assert(normals.size() == vertices.size()); out_mesh->mNormals = new aiVector3D[count_vertices]; } // allocate tangents, binormals. const std::vector &tangents = mesh.GetTangents(); const std::vector *binormals = &mesh.GetBinormals(); std::vector tempBinormals; if (tangents.size()) { if (!binormals->size()) { if (normals.size()) { // XXX this computes the binormals for the entire mesh, not only // the part for which we need them. tempBinormals.resize(normals.size()); for (unsigned int i = 0; i < tangents.size(); ++i) { tempBinormals[i] = normals[i] ^ tangents[i]; } binormals = &tempBinormals; } else { binormals = nullptr; } } if (binormals) { ai_assert(tangents.size() == vertices.size()); ai_assert(binormals->size() == vertices.size()); out_mesh->mTangents = new aiVector3D[count_vertices]; out_mesh->mBitangents = new aiVector3D[count_vertices]; } } // allocate texture coords unsigned int num_uvs = 0; for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; ++i, ++num_uvs) { const std::vector &uvs = mesh.GetTextureCoords(i); if (uvs.empty()) { break; } out_mesh->mTextureCoords[i] = new aiVector3D[count_vertices]; out_mesh->mNumUVComponents[i] = 2; } // allocate vertex colors unsigned int num_vcs = 0; for (unsigned int i = 0; i < AI_MAX_NUMBER_OF_COLOR_SETS; ++i, ++num_vcs) { const std::vector &colors = mesh.GetVertexColors(i); if (colors.empty()) { break; } out_mesh->mColors[i] = new aiColor4D[count_vertices]; } unsigned int cursor = 0, in_cursor = 0; itf = faces.begin(); for (MatIndexArray::const_iterator it = mindices.begin(), end = mindices.end(); it != end; ++it, ++itf) { const unsigned int pcount = *itf; if ((*it) != index) { in_cursor += pcount; continue; } aiFace &f = *fac++; f.mNumIndices = pcount; f.mIndices = new unsigned int[pcount]; switch (pcount) { case 1: out_mesh->mPrimitiveTypes |= aiPrimitiveType_POINT; break; case 2: out_mesh->mPrimitiveTypes |= aiPrimitiveType_LINE; break; case 3: out_mesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE; break; default: out_mesh->mPrimitiveTypes |= aiPrimitiveType_POLYGON; break; } for (unsigned int i = 0; i < pcount; ++i, ++cursor, ++in_cursor) { f.mIndices[i] = cursor; if (reverseMapping.size()) { reverseMapping[cursor] = in_cursor; translateIndexMap[in_cursor] = cursor; } out_mesh->mVertices[cursor] = vertices[in_cursor]; if (out_mesh->mNormals) { out_mesh->mNormals[cursor] = normals[in_cursor]; } if (out_mesh->mTangents) { out_mesh->mTangents[cursor] = tangents[in_cursor]; out_mesh->mBitangents[cursor] = (*binormals)[in_cursor]; } for (unsigned int j = 0; j < num_uvs; ++j) { const std::vector &uvs = mesh.GetTextureCoords(j); out_mesh->mTextureCoords[j][cursor] = aiVector3D(uvs[in_cursor].x, uvs[in_cursor].y, 0.0f); } for (unsigned int j = 0; j < num_vcs; ++j) { const std::vector &cols = mesh.GetVertexColors(j); out_mesh->mColors[j][cursor] = cols[in_cursor]; } } } ConvertMaterialForMesh(out_mesh, model, mesh, index); if (process_weights) { ConvertWeights(out_mesh, mesh, absolute_transform, parent, index, &reverseMapping); } std::vector animMeshes; for (const BlendShape *blendShape : mesh.GetBlendShapes()) { for (const BlendShapeChannel *blendShapeChannel : blendShape->BlendShapeChannels()) { const auto& shapeGeometries = blendShapeChannel->GetShapeGeometries(); for (const ShapeGeometry *shapeGeometry : shapeGeometries) { aiAnimMesh *animMesh = aiCreateAnimMesh(out_mesh); const auto& curVertices = shapeGeometry->GetVertices(); const auto& curNormals = shapeGeometry->GetNormals(); const auto& curIndices = shapeGeometry->GetIndices(); animMesh->mName.Set(FixAnimMeshName(shapeGeometry->Name())); for (size_t j = 0; j < curIndices.size(); j++) { unsigned int curIndex = curIndices.at(j); aiVector3D vertex = curVertices.at(j); aiVector3D normal = curNormals.at(j); unsigned int count = 0; const unsigned int *outIndices = mesh.ToOutputVertexIndex(curIndex, count); for (unsigned int k = 0; k < count; k++) { unsigned int outIndex = outIndices[k]; if (translateIndexMap.find(outIndex) == translateIndexMap.end()) continue; unsigned int transIndex = translateIndexMap[outIndex]; animMesh->mVertices[transIndex] += vertex; if (animMesh->mNormals != nullptr) { animMesh->mNormals[transIndex] += normal; animMesh->mNormals[transIndex].NormalizeSafe(); } } } animMesh->mWeight = shapeGeometries.size() > 1 ? blendShapeChannel->DeformPercent() / 100.0f : 1.0f; animMeshes.push_back(animMesh); } } } const size_t numAnimMeshes = animMeshes.size(); if (numAnimMeshes > 0) { out_mesh->mNumAnimMeshes = static_cast(numAnimMeshes); out_mesh->mAnimMeshes = new aiAnimMesh *[numAnimMeshes]; for (size_t i = 0; i < numAnimMeshes; i++) { out_mesh->mAnimMeshes[i] = animMeshes.at(i); } } return static_cast(mMeshes.size() - 1); } static void copyBoneToSkeletonBone(aiMesh *mesh, aiBone *bone, aiSkeletonBone *skeletonBone ) { skeletonBone->mNumnWeights = bone->mNumWeights; skeletonBone->mWeights = bone->mWeights; skeletonBone->mOffsetMatrix = bone->mOffsetMatrix; skeletonBone->mMeshId = mesh; #ifndef ASSIMP_BUILD_NO_ARMATUREPOPULATE_PROCESS skeletonBone->mNode = bone->mNode; #endif skeletonBone->mParent = -1; } void FBXConverter::ConvertWeightsToSkeleton(aiMesh *out, const MeshGeometry &geo, const aiMatrix4x4 &absolute_transform, aiNode *parent, unsigned int materialIndex, std::vector *outputVertStartIndices, SkeletonBoneContainer &skeletonContainer) { if (skeletonContainer.SkeletonBoneToMeshLookup.find(out) != skeletonContainer.SkeletonBoneToMeshLookup.end()) { return; } ConvertWeights(out, geo, absolute_transform, parent, materialIndex, outputVertStartIndices); skeletonContainer.MeshArray.emplace_back(out); SkeletonBoneArray *ba = new SkeletonBoneArray; for (size_t i = 0; i < out->mNumBones; ++i) { aiBone *bone = out->mBones[i]; if (bone == nullptr) { continue; } aiSkeletonBone *skeletonBone = new aiSkeletonBone; copyBoneToSkeletonBone(out, bone, skeletonBone); ba->emplace_back(skeletonBone); } skeletonContainer.SkeletonBoneToMeshLookup[out] = ba; } void FBXConverter::ConvertWeights(aiMesh *out, const MeshGeometry &geo, const aiMatrix4x4 &absolute_transform, aiNode *parent, unsigned int materialIndex, std::vector *outputVertStartIndices) { ai_assert(geo.DeformerSkin()); std::vector out_indices, index_out_indices, count_out_indices; const Skin &sk = *geo.DeformerSkin(); std::vector bones; const bool no_mat_check = materialIndex == NO_MATERIAL_SEPARATION; ai_assert(no_mat_check || outputVertStartIndices); try { // iterate over the sub deformers for (const Cluster *cluster : sk.Clusters()) { ai_assert(cluster); const WeightIndexArray &indices = cluster->GetIndices(); const MatIndexArray &mats = geo.GetMaterialIndices(); const size_t no_index_sentinel = std::numeric_limits::max(); count_out_indices.clear(); index_out_indices.clear(); out_indices.clear(); // now check if *any* of these weights is contained in the output mesh, // taking notes so we don't need to do it twice. for (WeightIndexArray::value_type index : indices) { unsigned int count = 0; const unsigned int *const out_idx = geo.ToOutputVertexIndex(index, count); // ToOutputVertexIndex only returns nullptr if index is out of bounds // which should never happen ai_assert(out_idx != nullptr); index_out_indices.push_back(no_index_sentinel); count_out_indices.push_back(0); for (unsigned int i = 0; i < count; ++i) { if (no_mat_check || static_cast(mats[geo.FaceForVertexIndex(out_idx[i])]) == materialIndex) { if (index_out_indices.back() == no_index_sentinel) { index_out_indices.back() = out_indices.size(); } if (no_mat_check) { out_indices.push_back(out_idx[i]); } else { // this extra lookup is in O(logn), so the entire algorithm becomes O(nlogn) const std::vector::iterator it = std::lower_bound( outputVertStartIndices->begin(), outputVertStartIndices->end(), out_idx[i]); out_indices.push_back(std::distance(outputVertStartIndices->begin(), it)); } ++count_out_indices.back(); } } } // if we found at least one, generate the output bones // XXX this could be heavily simplified by collecting the bone // data in a single step. ConvertCluster(bones, cluster, out_indices, index_out_indices, count_out_indices, absolute_transform, parent); } bone_map.clear(); } catch (std::exception &) { std::for_each(bones.begin(), bones.end(), Util::delete_fun()); throw; } if (bones.empty()) { out->mBones = nullptr; out->mNumBones = 0; return; } out->mBones = new aiBone *[bones.size()](); out->mNumBones = static_cast(bones.size()); std::swap_ranges(bones.begin(), bones.end(), out->mBones); } void FBXConverter::ConvertCluster(std::vector &local_mesh_bones, const Cluster *cluster, std::vector &out_indices, std::vector &index_out_indices, std::vector &count_out_indices, const aiMatrix4x4 & /* absolute_transform*/, aiNode *) { ai_assert(cluster != nullptr); // make sure cluster valid std::string deformer_name = cluster->TargetNode()->Name(); aiString bone_name = aiString(FixNodeName(deformer_name)); aiBone *bone = nullptr; if (bone_map.count(deformer_name)) { ASSIMP_LOG_VERBOSE_DEBUG("retrieved bone from lookup ", bone_name.C_Str(), ". Deformer:", deformer_name); bone = bone_map[deformer_name]; } else { ASSIMP_LOG_VERBOSE_DEBUG("created new bone ", bone_name.C_Str(), ". Deformer: ", deformer_name); bone = new aiBone(); bone->mName = bone_name; bone->mOffsetMatrix = cluster->Transform(); // store local transform link for post processing /* bone->mOffsetMatrix = cluster->TransformLink(); bone->mOffsetMatrix.Inverse(); aiMatrix4x4 matrix = (aiMatrix4x4)absolute_transform; bone->mOffsetMatrix = bone->mOffsetMatrix * matrix; // * mesh_offset */ // // Now calculate the aiVertexWeights // aiVertexWeight *cursor = nullptr; bone->mNumWeights = static_cast(out_indices.size()); cursor = bone->mWeights = new aiVertexWeight[out_indices.size()]; const size_t no_index_sentinel = std::numeric_limits::max(); const WeightArray &weights = cluster->GetWeights(); const size_t c = index_out_indices.size(); for (size_t i = 0; i < c; ++i) { const size_t index_index = index_out_indices[i]; if (index_index == no_index_sentinel) { continue; } const size_t cc = count_out_indices[i]; for (size_t j = 0; j < cc; ++j) { // cursor runs from first element relative to the start // or relative to the start of the next indexes. aiVertexWeight &out_weight = *cursor++; out_weight.mVertexId = static_cast(out_indices[index_index + j]); out_weight.mWeight = weights[i]; } } bone_map.insert(std::pair(deformer_name, bone)); } ASSIMP_LOG_DEBUG("bone research: Indices size: ", out_indices.size()); // lookup must be populated in case something goes wrong // this also allocates bones to mesh instance outside local_mesh_bones.push_back(bone); } void FBXConverter::ConvertMaterialForMesh(aiMesh *out, const Model &model, const MeshGeometry &geo, MatIndexArray::value_type materialIndex) { // locate source materials for this mesh const std::vector &mats = model.GetMaterials(); if (static_cast(materialIndex) >= mats.size() || materialIndex < 0) { FBXImporter::LogError("material index out of bounds, setting default material"); out->mMaterialIndex = GetDefaultMaterial(); return; } const Material *const mat = mats[materialIndex]; MaterialMap::const_iterator it = materials_converted.find(mat); if (it != materials_converted.end()) { out->mMaterialIndex = (*it).second; return; } out->mMaterialIndex = ConvertMaterial(*mat, &geo); materials_converted[mat] = out->mMaterialIndex; } unsigned int FBXConverter::GetDefaultMaterial() { if (defaultMaterialIndex) { return defaultMaterialIndex - 1; } aiMaterial *out_mat = new aiMaterial(); materials.push_back(out_mat); const aiColor3D diffuse = aiColor3D(0.8f, 0.8f, 0.8f); out_mat->AddProperty(&diffuse, 1, AI_MATKEY_COLOR_DIFFUSE); aiString s; s.Set(AI_DEFAULT_MATERIAL_NAME); out_mat->AddProperty(&s, AI_MATKEY_NAME); defaultMaterialIndex = static_cast(materials.size()); return defaultMaterialIndex - 1; } unsigned int FBXConverter::ConvertMaterial(const Material &material, const MeshGeometry *const mesh) { const PropertyTable &props = material.Props(); // generate empty output material aiMaterial *out_mat = new aiMaterial(); materials_converted[&material] = static_cast(materials.size()); materials.push_back(out_mat); aiString str; // strip Material:: prefix std::string name = material.Name(); if (name.substr(0, 10) == "Material::") { name = name.substr(10); } // set material name if not empty - this could happen // and there should be no key for it in this case. if (name.length()) { str.Set(name); out_mat->AddProperty(&str, AI_MATKEY_NAME); } // Set the shading mode as best we can: The FBX specification only mentions Lambert and Phong, and only Phong is mentioned in Assimp's aiShadingMode enum. if (material.GetShadingModel() == "phong") { aiShadingMode shadingMode = aiShadingMode_Phong; out_mat->AddProperty(&shadingMode, 1, AI_MATKEY_SHADING_MODEL); } // shading stuff and colors SetShadingPropertiesCommon(out_mat, props); SetShadingPropertiesRaw(out_mat, props, material.Textures(), mesh); // texture assignments SetTextureProperties(out_mat, material.Textures(), mesh); SetTextureProperties(out_mat, material.LayeredTextures(), mesh); return static_cast(materials.size() - 1); } unsigned int FBXConverter::ConvertVideo(const Video &video) { // generate empty output texture aiTexture *out_tex = new aiTexture(); textures.push_back(out_tex); // assuming the texture is compressed out_tex->mWidth = static_cast(video.ContentLength()); // total data size out_tex->mHeight = 0; // fixed to 0 // steal the data from the Video to avoid an additional copy out_tex->pcData = reinterpret_cast(const_cast