FBX -> Scene, part 2. Code changes, file renames (#1704)
* First pass FBX -> Scene File conversion. This is the simple pass, minimizing code changes and focused on comments. Signed-off-by: stankowi <4838196+AMZN-stankowi@users.noreply.github.com> * Step 1 of part 2 of the FBX -> Scene rename Signed-off-by: stankowi <4838196+AMZN-stankowi@users.noreply.github.com> * Renaming FbxSceneBuilder folder to SceneBuilder Signed-off-by: stankowi <4838196+AMZN-stankowi@users.noreply.github.com> * Renamed files Signed-off-by: stankowi <4838196+AMZN-stankowi@users.noreply.github.com> * More FBX -> Scene Signed-off-by: stankowi <4838196+AMZN-stankowi@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include <assimp/mesh.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <AzCore/Casting/numeric_cast.h>
|
||||
#include <AzCore/std/numeric.h>
|
||||
#include <AzCore/std/smart_ptr/make_shared.h>
|
||||
#include <SceneAPI/SceneBuilder/SceneSystem.h>
|
||||
#include <SceneAPI/SceneBuilder/ImportContexts/AssImpImportContexts.h>
|
||||
#include <SceneAPI/SceneBuilder/Importers/Utilities/AssImpMeshImporterUtilities.h>
|
||||
#include <SceneAPI/SceneCore/Utilities/Reporting.h>
|
||||
#include <SceneAPI/SceneData/GraphData/BlendShapeData.h>
|
||||
#include <SceneAPI/SceneData/GraphData/BoneData.h>
|
||||
#include <SceneAPI/SceneData/GraphData/MeshData.h>
|
||||
|
||||
namespace AZ::SceneAPI::SceneBuilder
|
||||
{
|
||||
bool BuildSceneMeshFromAssImpMesh(const aiNode* currentNode, const aiScene* scene, const SceneSystem& sceneSystem, AZStd::vector<AZStd::shared_ptr<DataTypes::IGraphObject>>& meshes,
|
||||
const AZStd::function<AZStd::shared_ptr<SceneData::GraphData::MeshData>()>& makeMeshFunc)
|
||||
{
|
||||
AZStd::unordered_map<int, int> assImpMatIndexToLYIndex;
|
||||
int lyMeshIndex = 0;
|
||||
|
||||
if(!currentNode || !scene)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
auto newMesh = makeMeshFunc();
|
||||
|
||||
newMesh->SetUnitSizeInMeters(sceneSystem.GetUnitSizeInMeters());
|
||||
newMesh->SetOriginalUnitSizeInMeters(sceneSystem.GetOriginalUnitSizeInMeters());
|
||||
|
||||
// AssImp separates meshes that have multiple materials.
|
||||
// This code re-combines them to match previous FBX SDK behavior,
|
||||
// so they can be separated by engine code instead.
|
||||
int vertOffset = 0;
|
||||
for (int m = 0; m < currentNode->mNumMeshes; ++m)
|
||||
{
|
||||
const aiMesh* mesh = scene->mMeshes[currentNode->mMeshes[m]];
|
||||
|
||||
// Lumberyard materials are created in order based on mesh references in the scene
|
||||
if (assImpMatIndexToLYIndex.find(mesh->mMaterialIndex) == assImpMatIndexToLYIndex.end())
|
||||
{
|
||||
assImpMatIndexToLYIndex.insert(AZStd::pair<int, int>(mesh->mMaterialIndex, lyMeshIndex++));
|
||||
}
|
||||
|
||||
for (int vertIdx = 0; vertIdx < mesh->mNumVertices; ++vertIdx)
|
||||
{
|
||||
AZ::Vector3 vertex(mesh->mVertices[vertIdx].x, mesh->mVertices[vertIdx].y, mesh->mVertices[vertIdx].z);
|
||||
|
||||
sceneSystem.SwapVec3ForUpAxis(vertex);
|
||||
sceneSystem.ConvertUnit(vertex);
|
||||
newMesh->AddPosition(vertex);
|
||||
newMesh->SetVertexIndexToControlPointIndexMap(vertIdx + vertOffset, vertIdx + vertOffset);
|
||||
|
||||
if (mesh->HasNormals())
|
||||
{
|
||||
AZ::Vector3 normal(mesh->mNormals[vertIdx].x, mesh->mNormals[vertIdx].y, mesh->mNormals[vertIdx].z);
|
||||
sceneSystem.SwapVec3ForUpAxis(normal);
|
||||
normal.NormalizeSafe();
|
||||
newMesh->AddNormal(normal);
|
||||
}
|
||||
}
|
||||
|
||||
for (int faceIdx = 0; faceIdx < mesh->mNumFaces; ++faceIdx)
|
||||
{
|
||||
aiFace face = mesh->mFaces[faceIdx];
|
||||
AZ::SceneAPI::DataTypes::IMeshData::Face meshFace;
|
||||
if (face.mNumIndices != 3)
|
||||
{
|
||||
// AssImp should have triangulated everything, so if this happens then someone has
|
||||
// probably changed AssImp's import settings. The engine only supports triangles.
|
||||
AZ_Error(Utilities::ErrorWindow, false,
|
||||
"Mesh on node %s has a face with %d vertices, only 3 vertices are supported per face.",
|
||||
currentNode->mName.C_Str(),
|
||||
face.mNumIndices);
|
||||
continue;
|
||||
}
|
||||
for (int idx = 0; idx < face.mNumIndices; ++idx)
|
||||
{
|
||||
meshFace.vertexIndex[idx] = face.mIndices[idx] + vertOffset;
|
||||
}
|
||||
|
||||
newMesh->AddFace(meshFace, assImpMatIndexToLYIndex[mesh->mMaterialIndex]);
|
||||
}
|
||||
vertOffset += mesh->mNumVertices;
|
||||
|
||||
}
|
||||
meshes.push_back(newMesh);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
GetMeshDataFromParentResult GetMeshDataFromParent(AssImpSceneNodeAppendedContext& context)
|
||||
{
|
||||
const DataTypes::IGraphObject* const parentData =
|
||||
context.m_scene.GetGraph().GetNodeContent(context.m_currentGraphPosition).get();
|
||||
|
||||
if (!parentData)
|
||||
{
|
||||
AZ_Error(Utilities::ErrorWindow, false,
|
||||
"GetMeshDataFromParent failed because the parent was null, it should only be called with a valid parent node");
|
||||
return AZ::Failure(Events::ProcessingResult::Failure);
|
||||
}
|
||||
|
||||
if (!parentData->RTTI_IsTypeOf(SceneData::GraphData::MeshData::TYPEINFO_Uuid()))
|
||||
{
|
||||
// The parent node may contain bone information and not mesh information, skip it.
|
||||
if (parentData->RTTI_IsTypeOf(SceneData::GraphData::BoneData::TYPEINFO_Uuid()))
|
||||
{
|
||||
// Return the ignore processing result in the failure.
|
||||
return AZ::Failure(Events::ProcessingResult::Ignored);
|
||||
}
|
||||
AZ_Error(Utilities::ErrorWindow, false,
|
||||
"Tried to get mesh data from parent for non-mesh parent data");
|
||||
return AZ::Failure(Events::ProcessingResult::Failure);
|
||||
}
|
||||
|
||||
const SceneData::GraphData::MeshData* const parentMeshData =
|
||||
azrtti_cast<const SceneData::GraphData::MeshData* const>(parentData);
|
||||
return AZ::Success(parentMeshData);
|
||||
}
|
||||
|
||||
uint64_t GetVertexCountForAllMeshesOnNode(const aiNode& node, const aiScene& scene)
|
||||
{
|
||||
return AZStd::accumulate(node.mMeshes, node.mMeshes + node.mNumMeshes, uint64_t{ 0u },
|
||||
[&scene](auto runningTotal, unsigned int meshIndex)
|
||||
{
|
||||
return runningTotal + scene.mMeshes[meshIndex]->mNumVertices;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SceneAPI/SceneCore/Events/ProcessingResult.h>
|
||||
|
||||
struct aiNode;
|
||||
struct aiScene;
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
namespace SceneData
|
||||
{
|
||||
namespace GraphData
|
||||
{
|
||||
class MeshData;
|
||||
class BlendShapeData;
|
||||
}
|
||||
}
|
||||
|
||||
namespace SceneAPI
|
||||
{
|
||||
namespace DataTypes
|
||||
{
|
||||
class IGraphObject;
|
||||
}
|
||||
struct AssImpSceneNodeAppendedContext;
|
||||
class SceneSystem;
|
||||
|
||||
namespace SceneBuilder
|
||||
{
|
||||
bool BuildSceneMeshFromAssImpMesh(const aiNode* currentNode, const aiScene* scene, const SceneSystem& sceneSystem, AZStd::vector<AZStd::shared_ptr<DataTypes::IGraphObject>>& meshes,
|
||||
const AZStd::function<AZStd::shared_ptr<SceneData::GraphData::MeshData>()>& makeMeshFunc);
|
||||
|
||||
typedef AZ::Outcome<const SceneData::GraphData::MeshData* const, Events::ProcessingResult> GetMeshDataFromParentResult;
|
||||
GetMeshDataFromParentResult GetMeshDataFromParent(AssImpSceneNodeAppendedContext& context);
|
||||
|
||||
// If a node in the original scene file has a mesh with multiple materials on it, the associated AssImp
|
||||
// node will have multiple meshes on it, broken apart per material. This returns the total number
|
||||
// of vertices on all meshes on the given node.
|
||||
uint64_t GetVertexCountForAllMeshesOnNode(const aiNode& node, const aiScene& scene);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include <AzCore/Casting/numeric_cast.h>
|
||||
#include <AzCore/std/algorithm.h>
|
||||
#include <AzCore/std/string/conversions.h>
|
||||
#include <AzToolsFramework/Debug/TraceContext.h>
|
||||
#include <SceneAPI/SceneBuilder/Importers/Utilities/RenamedNodesMap.h>
|
||||
#include <SceneAPI/SceneCore/Utilities/Reporting.h>
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
namespace SceneAPI
|
||||
{
|
||||
namespace SceneBuilder
|
||||
{
|
||||
bool RenamedNodesMap::SanitizeNodeName(AZStd::string& name, const Containers::SceneGraph& graph,
|
||||
Containers::SceneGraph::NodeIndex parentNode, const char* defaultName)
|
||||
{
|
||||
AZ_TraceContext("Node name", name);
|
||||
const AZStd::string originalNodeName(name);
|
||||
|
||||
bool isNameUpdated = false;
|
||||
// Nodes can't have an empty name, except of the root, otherwise nodes can't be referenced.
|
||||
if (name.empty())
|
||||
{
|
||||
name = defaultName;
|
||||
isNameUpdated = true;
|
||||
}
|
||||
|
||||
// The scene graph uses an arbitrary character (by default dot) to separate the names of the parents
|
||||
// therefore that character can't be used in the name.
|
||||
AZStd::replace_if(name.begin(), name.end(),
|
||||
[&isNameUpdated](char c) -> bool
|
||||
{
|
||||
if (c == Containers::SceneGraph::GetNodeSeperationCharacter())
|
||||
{
|
||||
isNameUpdated = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}, '_');
|
||||
|
||||
// Nodes under a particular parent have to be unique. Multiple nodes can share the same name, but they
|
||||
// can't reference the same parent in that case. This is to make sure the node can be quickly found as
|
||||
// the full path will be unique. To fix any issues, an index is appended.
|
||||
size_t index = 1;
|
||||
const size_t offset = name.length();
|
||||
while (graph.Find(parentNode, name).IsValid())
|
||||
{
|
||||
// Remove the previously tried extension.
|
||||
name.erase(offset, name.length() - offset);
|
||||
|
||||
name += ('_');
|
||||
name += AZStd::to_string(aznumeric_cast<u64>(index));
|
||||
index++;
|
||||
isNameUpdated = true;
|
||||
}
|
||||
|
||||
if (isNameUpdated)
|
||||
{
|
||||
AZ_TraceContext("New node name", name);
|
||||
AZ_TracePrintf(Utilities::WarningWindow, "The name of the node '%s' was invalid or conflicting and was updated to '%s'.",
|
||||
originalNodeName.c_str(), name.c_str());
|
||||
}
|
||||
|
||||
return isNameUpdated;
|
||||
}
|
||||
|
||||
bool RenamedNodesMap::RegisterNode(const std::shared_ptr<SDKNode::NodeWrapper>& node, const Containers::SceneGraph& graph,
|
||||
Containers::SceneGraph::NodeIndex parentNode, const char* defaultName)
|
||||
{
|
||||
return node ? RegisterNode(*node, graph, parentNode, defaultName) : false;
|
||||
}
|
||||
|
||||
bool RenamedNodesMap::RegisterNode(const std::shared_ptr<const SDKNode::NodeWrapper>& node, const Containers::SceneGraph& graph,
|
||||
Containers::SceneGraph::NodeIndex parentNode, const char* defaultName)
|
||||
{
|
||||
return node ? RegisterNode(*node, graph, parentNode, defaultName) : false;
|
||||
}
|
||||
|
||||
bool RenamedNodesMap::RegisterNode(const SDKNode::NodeWrapper& node, const Containers::SceneGraph& graph,
|
||||
Containers::SceneGraph::NodeIndex parentNode, const char* defaultName)
|
||||
{
|
||||
AZStd::string name = node.GetName();
|
||||
if (SanitizeNodeName(name, graph, parentNode, defaultName))
|
||||
{
|
||||
AZ_TraceContext("New node name", name);
|
||||
|
||||
// Only register if the name is updated, otherwise the name in the source scene's node can be returned.
|
||||
auto entry = m_idToName.find(node.GetUniqueId());
|
||||
if (entry == m_idToName.end())
|
||||
{
|
||||
m_idToName.insert(AZStd::make_pair(node.GetUniqueId(), AZStd::move(name)));
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
AZ_TraceContext("Previous name", entry->second);
|
||||
if (entry->second == name)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
AZ_Assert(false, "Node has already been registered with a different name.");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
const char* RenamedNodesMap::GetNodeName(const std::shared_ptr<SDKNode::NodeWrapper>& node) const
|
||||
{
|
||||
return node ? GetNodeName(*node) : "<invalid>";
|
||||
}
|
||||
|
||||
const char* RenamedNodesMap::GetNodeName(const std::shared_ptr<const SDKNode::NodeWrapper>& node) const
|
||||
{
|
||||
return node ? GetNodeName(*node) : "<invalid>";
|
||||
}
|
||||
|
||||
const char* RenamedNodesMap::GetNodeName(const AZStd::shared_ptr<SDKNode::NodeWrapper>& node) const
|
||||
{
|
||||
return node ? GetNodeName(*node) : "<invalid>";
|
||||
}
|
||||
|
||||
const char* RenamedNodesMap::GetNodeName(const AZStd::shared_ptr<const SDKNode::NodeWrapper>& node) const
|
||||
{
|
||||
return node ? GetNodeName(*node) : "<invalid>";
|
||||
}
|
||||
|
||||
const char* RenamedNodesMap::GetNodeName(const SDKNode::NodeWrapper& node) const
|
||||
{
|
||||
auto entry = m_idToName.find(node.GetUniqueId());
|
||||
if (entry != m_idToName.end())
|
||||
{
|
||||
return entry->second.c_str();
|
||||
}
|
||||
else
|
||||
{
|
||||
return node.GetName();
|
||||
}
|
||||
}
|
||||
} // namespace SceneBuilder
|
||||
} // namespace SceneAPI
|
||||
} // namespace AZ
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <AzCore/std/containers/unordered_map.h>
|
||||
#include <AzCore/std/string/string.h>
|
||||
#include <SceneAPI/SceneCore/Containers/SceneGraph.h>
|
||||
#include <SceneAPI/SDKWrapper/NodeWrapper.h>
|
||||
|
||||
namespace AZ
|
||||
{
|
||||
namespace SceneAPI
|
||||
{
|
||||
namespace SceneBuilder
|
||||
{
|
||||
class RenamedNodesMap
|
||||
{
|
||||
public:
|
||||
//! Checks if the provided name is valid for the position in the graph and makes corrections if
|
||||
//! problems are found.
|
||||
//! @param name The name of the node in the scene graph.
|
||||
//! @param graph The scene graph the node will be added to.
|
||||
//! @param parentNode The node that will be the intended parent for the the node who's name is being checked.
|
||||
//! @param defaultName If the provided name is empty, the defaultName will be used.
|
||||
//! @return True if the name was updated otherwise false.
|
||||
static bool SanitizeNodeName(AZStd::string& name, const Containers::SceneGraph& graph,
|
||||
Containers::SceneGraph::NodeIndex parentNode, const char* defaultName = "unnamed");
|
||||
|
||||
//! Register the name for later reference. If the name needs to be sanitized, the sanitized name will be stored.
|
||||
//! @param node The node that's to be registered.
|
||||
//! @param graph The scene graph the node will be added to.
|
||||
//! @param parentNode The node that will be the intended parent for the the node who's name is being checked.
|
||||
//! @param defaultName If the provided name is empty, the defaultName will be used.
|
||||
//! @return True if the node was successfully registered.
|
||||
bool RegisterNode(const std::shared_ptr<SDKNode::NodeWrapper>& node, const Containers::SceneGraph& graph,
|
||||
Containers::SceneGraph::NodeIndex parentNode, const char* defaultName = "unnamed");
|
||||
bool RegisterNode(const std::shared_ptr<const SDKNode::NodeWrapper>& node, const Containers::SceneGraph& graph,
|
||||
Containers::SceneGraph::NodeIndex parentNode, const char* defaultName = "unnamed");
|
||||
bool RegisterNode(const SDKNode::NodeWrapper& node, const Containers::SceneGraph& graph,
|
||||
Containers::SceneGraph::NodeIndex parentNode, const char* defaultName = "unnamed");
|
||||
|
||||
//! Returns the name of the given node, which may be sanitized if this was needed.
|
||||
const char* GetNodeName(const std::shared_ptr<SDKNode::NodeWrapper>& node) const;
|
||||
const char* GetNodeName(const std::shared_ptr<const SDKNode::NodeWrapper>& node) const;
|
||||
const char* GetNodeName(const AZStd::shared_ptr<SDKNode::NodeWrapper>& node) const;
|
||||
const char* GetNodeName(const AZStd::shared_ptr<const SDKNode::NodeWrapper>& node) const;
|
||||
const char* GetNodeName(const SDKNode::NodeWrapper& node) const;
|
||||
private:
|
||||
|
||||
AZStd::unordered_map<u64, AZStd::string> m_idToName;
|
||||
};
|
||||
} // namespace SceneBuilder
|
||||
} // namespace SceneAPI
|
||||
} // namespace AZ
|
||||
Reference in New Issue
Block a user