ModelAssetBuilderComponent.cpp 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Model/ModelAssetBuilderComponent.h>
  9. #include <Model/MaterialAssetBuilderComponent.h>
  10. #include <Model/MorphTargetExporter.h>
  11. #include <Atom/RPI.Edit/Common/AssetUtils.h>
  12. #include <AzCore/Component/ComponentApplicationBus.h>
  13. #include <AzCore/Math/Aabb.h>
  14. #include <AzCore/Math/Transform.h>
  15. #include <AzCore/Serialization/SerializeContext.h>
  16. #include <AzCore/Serialization/Utils.h>
  17. #include <AzCore/std/smart_ptr/make_shared.h>
  18. #include <Atom/RPI.Reflect/Buffer/BufferAssetCreator.h>
  19. #include <Atom/RPI.Reflect/Material/MaterialAsset.h>
  20. #include <Atom/RPI.Reflect/Model/ModelAssetCreator.h>
  21. #include <Atom/RPI.Reflect/Model/ModelLodAssetCreator.h>
  22. #include <Atom/RPI.Reflect/Model/MorphTargetDelta.h>
  23. #include <Atom/RPI.Reflect/Model/SkinJointIdPadding.h>
  24. #include <Atom/RPI.Reflect/Model/SkinMetaAssetCreator.h>
  25. #include <Atom/RPI.Reflect/Model/ModelAssetHelpers.h>
  26. #include <SceneAPI/SceneCore/Containers/Scene.h>
  27. #include <SceneAPI/SceneCore/Containers/Views/PairIterator.h>
  28. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphChildIterator.h>
  29. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphDownwardsIterator.h>
  30. #include <SceneAPI/SceneCore/Containers/Views/SceneGraphUpwardsIterator.h>
  31. #include <SceneAPI/SceneCore/DataTypes/GraphData/IBoneData.h>
  32. #include <SceneAPI/SceneCore/DataTypes/GraphData/IBlendShapeData.h>
  33. #include <SceneAPI/SceneCore/DataTypes/Rules/ICoordinateSystemRule.h>
  34. #include <SceneAPI/SceneCore/DataTypes/Rules/ILodRule.h>
  35. #include <SceneAPI/SceneCore/DataTypes/Rules/ISkinRule.h>
  36. #include <SceneAPI/SceneCore/DataTypes/Rules/IClothRule.h>
  37. #include <SceneAPI/SceneCore/DataTypes/Rules/ITagRule.h>
  38. #include <SceneAPI/SceneCore/Events/ExportEventContext.h>
  39. #include <SceneAPI/SceneCore/Utilities/SceneGraphSelector.h>
  40. #include <SceneAPI/SceneCore/Utilities/Reporting.h>
  41. #include <SceneAPI/SceneData/Groups/MeshGroup.h>
  42. #include <SceneAPI/SceneData/Rules/StaticMeshAdvancedRule.h>
  43. #include <SceneAPI/SceneCore/Containers/Utilities/SceneUtilities.h>
  44. #include <SceneAPI/SceneCore/Containers/Utilities/Filters.h>
  45. static constexpr AZStd::string_view MismatchedVertexLayoutsAreErrorsKey{ "/O3DE/SceneAPI/ModelBuilder/MismatchedVertexLayoutsAreErrors" };
  46. /**
  47. * DEBUG DEFINES!
  48. * These are useful for debugging bad behavior from the builder.
  49. * By default this builder wants to merge meshes as much as possible
  50. * to cut down on the number of buffers it has to create. This is generally
  51. * helpful for rendering but can make debugging difficult.
  52. *
  53. * If you experience artifacts from models built by this builder try
  54. * commenting these out to disable certain merging features. This will
  55. * produce a large volume of buffers for large models but it should be a lot
  56. * easier to step through.
  57. */
  58. #define AZ_RPI_MESHES_SHARE_COMMON_BUFFERS
  59. namespace AZ
  60. {
  61. class Aabb;
  62. namespace RPI
  63. {
  64. static const uint64_t s_invalidMaterialUid = 0;
  65. static bool MismatchedVertexLayoutsAreErrors()
  66. {
  67. bool mismatchedVertexStreamsAreErrors = false;
  68. if (auto settingsRegistry = AZ::SettingsRegistry::Get(); settingsRegistry != nullptr)
  69. {
  70. settingsRegistry->Get(mismatchedVertexStreamsAreErrors, MismatchedVertexLayoutsAreErrorsKey);
  71. }
  72. return mismatchedVertexStreamsAreErrors;
  73. }
  74. void ModelAssetBuilderComponent::Reflect(ReflectContext* context)
  75. {
  76. if (auto* serialize = azrtti_cast<SerializeContext*>(context))
  77. {
  78. serialize->Class<ModelAssetBuilderComponent, SceneAPI::SceneCore::ExportingComponent>()
  79. ->Version(38); // Pad Skinning mesh buffers to respect appropriate alignment
  80. }
  81. }
  82. ModelAssetBuilderComponent::ModelAssetBuilderComponent()
  83. {
  84. BindToCall(&ModelAssetBuilderComponent::BuildModel);
  85. }
  86. //Supports a case-insensitive check for "lodN" or "lod_N" or "lod-N" or "lod:N" or "lod|N" or "lod#N" or "lod N" at the end of the name for the current node or an ancestor node.
  87. //Returns -1 if no valid naming convention is found.
  88. int GetLodIndexByNamingConvention(const char* name, size_t len)
  89. {
  90. //look for "lodN"
  91. if (len >= 4)
  92. {
  93. const char* subStr = &name[len - 4];
  94. if (azstrnicmp(subStr, "lod", 3) == 0)
  95. {
  96. const char lastLetter = name[len - 1];
  97. if (AZStd::is_digit(lastLetter))
  98. {
  99. return static_cast<int>(lastLetter) - '0';
  100. }
  101. }
  102. }
  103. //look for "lod_N"
  104. if (len >= 5)
  105. {
  106. const char* subStr = &name[len - 5];
  107. if (azstrnicmp(subStr, "lod", 3) == 0)
  108. {
  109. if (strchr("_-:|# ", name[len - 2]))
  110. {
  111. const char lastLetter = name[len - 1];
  112. if (AZStd::is_digit(lastLetter))
  113. {
  114. return static_cast<int>(lastLetter) - '0';
  115. }
  116. }
  117. }
  118. }
  119. return -1;
  120. }
  121. SceneAPI::Events::ProcessingResult ModelAssetBuilderComponent::BuildModel(ModelAssetBuilderContext& context)
  122. {
  123. {
  124. auto assetIdOutcome = RPI::AssetUtils::MakeAssetId(s_defaultVertexBufferPoolSourcePath, 0);
  125. if (!assetIdOutcome.IsSuccess())
  126. {
  127. return SceneAPI::Events::ProcessingResult::Failure;
  128. }
  129. m_systemInputAssemblyBufferPoolId = assetIdOutcome.GetValue();
  130. }
  131. m_modelName = context.m_group.GetName();
  132. const auto& scene = context.m_scene;
  133. const auto& sceneGraph = scene.GetGraph();
  134. m_sourceUuid = scene.GetSourceGuid();
  135. auto names = sceneGraph.GetNameStorage();
  136. auto content = sceneGraph.GetContentStorage();
  137. // Create a downwards, breadth-first view into the scene
  138. auto pairView = AZ::SceneAPI::Containers::Views::MakePairView(names, content);
  139. auto view = AZ::SceneAPI::Containers::Views::MakeSceneGraphDownwardsView<
  140. AZ::SceneAPI::Containers::Views::BreadthFirst>(
  141. sceneGraph, sceneGraph.GetRoot(), pairView.cbegin(), true);
  142. AZStd::vector<SourceMeshContentList> sourceMeshContentListsByLod;
  143. AZStd::shared_ptr<const SceneAPI::DataTypes::ILodRule> lodRule = context.m_group.GetRuleContainerConst().FindFirstByType<SceneAPI::DataTypes::ILodRule>();
  144. AZStd::vector<AZStd::vector<AZStd::string>> selectedMeshPathsByLod;
  145. // The Atom Model builder uses the optimized versions of meshes that are
  146. // placed in the SceneGraph during its generation phase. Users select
  147. // meshes based on their original name, and the mesh optimizer adds the
  148. // suffix "_optimized" to these mesh nodes in the scene graph. To target
  149. // these nodes, first filter for the non-optimized mesh nodes, then remap
  150. // from the non-optimized one to the optimized one. This callable is used
  151. // to filter for mesh nodes that are not the optimized ones.
  152. const auto isNonOptimizedMesh = [](const SceneAPI::Containers::SceneGraph& graph, SceneAPI::Containers::SceneGraph::NodeIndex& index)
  153. {
  154. return SceneAPI::Utilities::SceneGraphSelector::IsMesh(graph, index) &&
  155. !AZStd::string_view{graph.GetNodeName(index).GetName(), graph.GetNodeName(index).GetNameLength()}.ends_with(SceneAPI::Utilities::OptimizedMeshSuffix);
  156. };
  157. if (lodRule)
  158. {
  159. selectedMeshPathsByLod.resize(lodRule->GetLodCount());
  160. for (size_t lod = 0; lod < lodRule->GetLodCount(); ++lod)
  161. {
  162. selectedMeshPathsByLod[lod] = SceneAPI::Utilities::SceneGraphSelector::GenerateTargetNodes(
  163. sceneGraph, lodRule->GetSceneNodeSelectionList(lod), isNonOptimizedMesh,
  164. SceneAPI::Utilities::SceneGraphSelector::RemapToOptimizedMesh);
  165. }
  166. }
  167. // Gather the list of nodes in the graph that are selected as part of this
  168. // MeshGroup defined in context.m_group, then remap to the optimized mesh
  169. // nodes, if they exist.
  170. AZStd::vector<AZStd::string> selectedMeshPaths = SceneAPI::Utilities::SceneGraphSelector::GenerateTargetNodes(
  171. sceneGraph, context.m_group.GetSceneNodeSelectionList(), isNonOptimizedMesh,
  172. SceneAPI::Utilities::SceneGraphSelector::RemapToOptimizedMesh);
  173. // Iterate over the downwards, breadth-first view into the scene.
  174. // First we have to split the source mesh data up by lod.
  175. for (const auto& viewIt : view)
  176. {
  177. if (viewIt.second != nullptr &&
  178. azrtti_istypeof<MeshData>(viewIt.second.get()))
  179. {
  180. const AZStd::string meshPath(viewIt.first.GetPath(), viewIt.first.GetPathLength());
  181. const AZStd::string meshName(viewIt.first.GetName(), viewIt.first.GetNameLength());
  182. uint32_t lodIndex = 0; // Default to the 0th LOD if nothing is found
  183. if (lodRule)
  184. {
  185. // The LodRule contains the objects for Lod1 through LodN. Objects at Lod0 are not include in the LodRule
  186. for (size_t lod = 0; lod < selectedMeshPathsByLod.size(); ++lod)
  187. {
  188. AZStd::vector<AZStd::string>& paths = selectedMeshPathsByLod[lod];
  189. const auto it = AZStd::find(paths.begin(), paths.end(), meshPath);
  190. if (it != paths.end())
  191. {
  192. lodIndex = aznumeric_cast<uint32_t>(lod + 1);
  193. break;
  194. }
  195. }
  196. if (lodIndex == 0)
  197. {
  198. // Object was not found in the LodRule, but we still need to see if it was in the selection list
  199. const auto selectedMeshPathsIt = AZStd::find(selectedMeshPaths.begin(), selectedMeshPaths.end(), meshPath);
  200. if(selectedMeshPathsIt == selectedMeshPaths.end())
  201. {
  202. continue;
  203. }
  204. }
  205. }
  206. else
  207. {
  208. // Skip the mesh if it's not in the MeshGroup's selected mesh list
  209. const auto selectedMeshPathsIt = AZStd::find(selectedMeshPaths.begin(), selectedMeshPaths.end(), meshPath);
  210. if(selectedMeshPathsIt == selectedMeshPaths.end())
  211. {
  212. continue;
  213. }
  214. AZ_TracePrintf(AZ::SceneAPI::Utilities::LogWindow, "Using mesh '%s'", meshPath.c_str());
  215. // Select the Lod that this mesh is part of
  216. {
  217. int lodIndexFromName = GetLodIndexByNamingConvention(meshName.c_str(), meshName.size());
  218. if (lodIndexFromName >= 0)
  219. {
  220. lodIndex = aznumeric_cast<uint32_t>(lodIndexFromName);
  221. }
  222. else
  223. {
  224. // If the mesh node's name doesn't have the LOD identifier in it lets walk the parent hierarchy
  225. // The first parent node that has the LOD identifier is the LOD this mesh will be a part of
  226. SceneAPI::Containers::SceneGraph::NodeIndex meshNodeIndex = sceneGraph.Find(meshPath);
  227. SceneAPI::Containers::SceneGraph::NodeIndex parentNodeIndex = sceneGraph.GetNodeParent(meshNodeIndex);
  228. while (parentNodeIndex != sceneGraph.GetRoot())
  229. {
  230. const SceneAPI::Containers::SceneGraph::Name& parentNodeName = sceneGraph.GetNodeName(parentNodeIndex);
  231. lodIndexFromName = GetLodIndexByNamingConvention(parentNodeName.GetName(), parentNodeName.GetNameLength());
  232. if (lodIndexFromName >= 0)
  233. {
  234. lodIndex = aznumeric_cast<uint32_t>(lodIndexFromName);
  235. break;
  236. }
  237. parentNodeIndex = sceneGraph.GetNodeParent(parentNodeIndex);
  238. }
  239. }
  240. }
  241. }
  242. // Find which LodAssetBuilder we need to add this mesh to
  243. // If the lod is new we need to create and begin a new builder
  244. if (lodIndex + 1 >= sourceMeshContentListsByLod.size())
  245. {
  246. sourceMeshContentListsByLod.resize(lodIndex + 1);
  247. }
  248. SourceMeshContentList& sourceMeshContentList = sourceMeshContentListsByLod[lodIndex];
  249. // Gather mesh content
  250. SourceMeshContent sourceMesh;
  251. const auto node = sceneGraph.Find(meshPath);
  252. sourceMesh.m_worldTransform = AZ::SceneAPI::Utilities::DetermineWorldTransform(scene, node, context.m_group.GetRuleContainerConst());
  253. SceneAPI::Containers::SceneGraph::NodeIndex originalUnoptimizedMeshIndex =
  254. SceneAPI::Utilities::SceneGraphSelector::RemapToOriginalUnoptimizedMesh(sceneGraph, node);
  255. // Although the nodes used to gather mesh content are the optimized ones (when found), to make
  256. // this process transparent for the end-asset generated, the name assigned to the source mesh
  257. // content will not include the "_optimized" prefix or the group name.
  258. sourceMesh.m_name = sceneGraph.GetNodeName(originalUnoptimizedMeshIndex).GetName();
  259. // Add the MeshData to the source mesh
  260. AddToMeshContent(viewIt.second, sourceMesh);
  261. // Iterate over the immediate children of the mesh node, looking for additional data like uvs, tangents, etc.
  262. auto sibling = sceneGraph.GetNodeChild(node);
  263. bool traversing = true;
  264. while (traversing)
  265. {
  266. if (sibling.IsValid())
  267. {
  268. auto siblingContent = sceneGraph.GetNodeContent(sibling);
  269. // If a sibling is MeshData, that indicates a separate mesh node
  270. // that should not add to or overwrite the MeshData for the current node
  271. if (!azrtti_istypeof<MeshData>(siblingContent.get()))
  272. {
  273. AddToMeshContent(siblingContent, sourceMesh);
  274. }
  275. sibling = sceneGraph.GetNodeSibling(sibling);
  276. }
  277. else
  278. {
  279. traversing = false;
  280. }
  281. }
  282. sourceMesh.m_isMorphed = GetIsMorphed(sceneGraph, node);
  283. // Get the cloth data (only for full mesh LOD 0).
  284. sourceMesh.m_meshClothData = (lodIndex == 0)
  285. ? SceneAPI::DataTypes::IClothRule::FindClothData(
  286. sceneGraph,
  287. originalUnoptimizedMeshIndex,
  288. sourceMesh.m_meshData->GetVertexCount(),
  289. context.m_group.GetRuleContainerConst())
  290. : AZStd::vector<AZ::Color>{};
  291. // We've traversed this node and all its children that hold
  292. // relevant data We can move it into the list of content for this lod
  293. sourceMeshContentList.emplace_back(AZStd::move(sourceMesh));
  294. }
  295. }
  296. // Then in each Lod we need to group all faces by material id.
  297. // All sub meshes with the same material id get merged
  298. AZStd::vector<Data::Asset<ModelLodAsset>> lodAssets;
  299. lodAssets.resize(sourceMeshContentListsByLod.size());
  300. // Joint name to joint index map used for the skinning influences.
  301. AZStd::unordered_map<AZStd::string, uint16_t> jointNameToIndexMap;
  302. AZStd::string modelAssetName = GetAssetFullName(ModelAsset::TYPEINFO_Uuid());
  303. const AZ::Data::AssetId modelAssetId = CreateAssetId(modelAssetName);
  304. MorphTargetMetaAssetCreator morphTargetMetaCreator;
  305. morphTargetMetaCreator.Begin(MorphTargetMetaAsset::ConstructAssetId(modelAssetId, modelAssetName));
  306. ModelAssetCreator modelAssetCreator;
  307. modelAssetCreator.Begin(modelAssetId);
  308. modelAssetCreator.SetName(modelAssetName);
  309. AZStd::shared_ptr<const SceneAPI::DataTypes::ITagRule> tagRule = context.m_group.GetRuleContainerConst().FindFirstByType<SceneAPI::DataTypes::ITagRule>();
  310. if (tagRule)
  311. {
  312. for (AZStd::string tag : tagRule->GetTags())
  313. {
  314. AZStd::to_lower(tag.begin(), tag.end());
  315. modelAssetCreator.AddTag(AZ::Name{ tag });
  316. }
  317. }
  318. uint32_t lodIndex = 0;
  319. for (const SourceMeshContentList& sourceMeshContentList : sourceMeshContentListsByLod)
  320. {
  321. ModelLodAssetCreator lodAssetCreator;
  322. m_lodName = AZStd::string::format("lod%d", lodIndex);
  323. AZStd::string lodAssetName = GetAssetFullName(ModelLodAsset::TYPEINFO_Uuid());
  324. lodAssetCreator.Begin(CreateAssetId(lodAssetName));
  325. {
  326. AZ::Outcome<ProductMeshContentList> productMeshListOutcome =
  327. SourceMeshListToProductMeshList(context, sourceMeshContentList, jointNameToIndexMap, morphTargetMetaCreator);
  328. if (!productMeshListOutcome.IsSuccess())
  329. {
  330. return AZ::SceneAPI::Events::ProcessingResult::Failure;
  331. }
  332. ProductMeshContentList lodMeshes = productMeshListOutcome.GetValue();
  333. PadVerticesForSkinning(lodMeshes);
  334. // By default, we merge meshes that share the same material
  335. bool canMergeMeshes = true;
  336. AZStd::shared_ptr<const SceneAPI::SceneData::StaticMeshAdvancedRule> staticMeshAdvancedRule = context.m_group.GetRuleContainerConst().FindFirstByType<SceneAPI::SceneData::StaticMeshAdvancedRule>();
  337. if (staticMeshAdvancedRule && !staticMeshAdvancedRule->MergeMeshes())
  338. {
  339. // If the merge meshes option is disabled in the advanced mesh rule, don't merge meshes
  340. canMergeMeshes = false;
  341. }
  342. else
  343. {
  344. for (const SourceMeshContent& sourceMesh : sourceMeshContentList)
  345. {
  346. if (sourceMesh.m_isMorphed)
  347. {
  348. // Merging meshes shuffles around the order of the vertices, but morph targets rely on having an index that tell them which vertices to morph
  349. // We do not merge morphed meshes so that this index is preserved and correct.
  350. // If we keep track of the ordering changes in MergeMeshesByMaterialUid and then re-mapped the MORPHTARGET_VERTEXINDICES buffer
  351. // we could potentially enable merging meshes that are morphed. But for now, disable merging.
  352. canMergeMeshes = false;
  353. break;
  354. }
  355. }
  356. }
  357. if (canMergeMeshes)
  358. {
  359. productMeshListOutcome = MergeMeshesByMaterialUid(lodMeshes);
  360. if (!productMeshListOutcome.IsSuccess())
  361. {
  362. return AZ::SceneAPI::Events::ProcessingResult::Failure;
  363. }
  364. lodMeshes = productMeshListOutcome.GetValue();
  365. }
  366. #if defined(AZ_RPI_MESHES_SHARE_COMMON_BUFFERS)
  367. // We shouldn't need a mesh name for the buffer names since meshed are sharing common buffers
  368. m_meshName = "";
  369. ProductMeshViewList lodMeshViews;
  370. ProductMeshContent mergedMesh;
  371. MergeMeshesToCommonBuffers(lodMeshes, mergedMesh, lodMeshViews);
  372. BufferAssetView indexBuffer;
  373. AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo> streamBuffers;
  374. if (!CreateModelLodBuffers(mergedMesh, indexBuffer, streamBuffers, lodAssetCreator))
  375. {
  376. return AZ::SceneAPI::Events::ProcessingResult::Failure;
  377. }
  378. for (const ProductMeshView& meshView : lodMeshViews)
  379. {
  380. if (!CreateMesh(meshView, indexBuffer, streamBuffers, modelAssetCreator, lodAssetCreator, context.m_materialsByUid))
  381. {
  382. return AZ::SceneAPI::Events::ProcessingResult::Failure;
  383. }
  384. }
  385. #else
  386. uint32_t meshIndex = 0;
  387. for (const ProductMeshContent& mesh : lodMeshes)
  388. {
  389. const ProductMeshView meshView = CreateViewToEntireMesh(mesh);
  390. BufferAssetView indexBuffer;
  391. AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo> streamBuffers;
  392. // Mesh name in ProductMeshContent could be duplicated so generate unique mesh name using index
  393. m_meshName = AZStd::string::format("mesh%d", meshIndex++);
  394. if (!CreateModelLodBuffers(mesh, indexBuffer, streamBuffers, lodAssetCreator))
  395. {
  396. return AZ::SceneAPI::Events::ProcessingResult::Failure;
  397. }
  398. if (!CreateMesh(meshView, indexBuffer, streamBuffers, modelAssetCreator, lodAssetCreator, context.m_materialsByUid))
  399. {
  400. return AZ::SceneAPI::Events::ProcessingResult::Failure;
  401. }
  402. }
  403. #endif
  404. }
  405. if (!lodAssetCreator.End(lodAssets[lodIndex]))
  406. {
  407. return AZ::SceneAPI::Events::ProcessingResult::Failure;
  408. }
  409. lodAssets[lodIndex].SetHint(lodAssetName); // name will be used for file name when export asset
  410. lodIndex++;
  411. }
  412. sourceMeshContentListsByLod.clear();
  413. // Finalize all LOD assets
  414. for (auto& lodAsset : lodAssets)
  415. {
  416. modelAssetCreator.AddLodAsset(AZStd::move(lodAsset));
  417. }
  418. // Finalize the model
  419. if (!modelAssetCreator.End(context.m_outputModelAsset))
  420. {
  421. return AZ::SceneAPI::Events::ProcessingResult::Failure;
  422. }
  423. // Fill the skin meta asset
  424. if (!jointNameToIndexMap.empty())
  425. {
  426. SkinMetaAssetCreator skinCreator;
  427. skinCreator.Begin(SkinMetaAsset::ConstructAssetId(modelAssetId, modelAssetName));
  428. skinCreator.SetJointNameToIndexMap(jointNameToIndexMap);
  429. if (!skinCreator.End(context.m_outputSkinMetaAsset))
  430. {
  431. AZ_Warning(s_builderName, false, "Cannot create skin meta asset. Skinning influences won't be automatically relinked.");
  432. }
  433. }
  434. // Fill the morph target meta asset
  435. if (!morphTargetMetaCreator.IsEmpty())
  436. {
  437. if (!morphTargetMetaCreator.End(context.m_outputMorphTargetMetaAsset))
  438. {
  439. AZ_Warning(s_builderName, false, "Cannot create morph target meta asset for model asset '%s'.", modelAssetName.c_str());
  440. }
  441. }
  442. context.m_outputModelAsset.SetHint(modelAssetName);
  443. return AZ::SceneAPI::Events::ProcessingResult::Success;
  444. }
  445. void ModelAssetBuilderComponent::AddToMeshContent(
  446. const AZStd::shared_ptr<const AZ::SceneAPI::DataTypes::IGraphObject>& data,
  447. SourceMeshContent& content)
  448. {
  449. if (azrtti_istypeof<MeshData>(data.get()))
  450. {
  451. auto meshData = AZStd::static_pointer_cast<const MeshData>(data);
  452. content.m_meshData = meshData;
  453. }
  454. else if (azrtti_istypeof<UVData>(data.get()))
  455. {
  456. auto uvData = AZStd::static_pointer_cast<const UVData>(data);
  457. content.m_meshUVData.push_back(uvData);
  458. }
  459. else if (azrtti_istypeof<ColorData>(data.get()))
  460. {
  461. auto colorData = AZStd::static_pointer_cast<const ColorData>(data);
  462. content.m_meshColorData.push_back(colorData);
  463. }
  464. else if (azrtti_istypeof<TangentData>(data.get()))
  465. {
  466. auto tangentData = AZStd::static_pointer_cast<const TangentData>(data);
  467. if (!content.m_meshTangents)
  468. {
  469. content.m_meshTangents = tangentData;
  470. }
  471. else
  472. {
  473. AZ_Printf(s_builderName,
  474. "Found multiple tangent data sets for mesh '%s'. Only the first will be used.",
  475. content.m_name.GetCStr());
  476. }
  477. }
  478. else if (azrtti_istypeof<BitangentData>(data.get()))
  479. {
  480. auto bitangentData = AZStd::static_pointer_cast<const BitangentData>(data);
  481. if (!content.m_meshBitangents)
  482. {
  483. content.m_meshBitangents = bitangentData;
  484. }
  485. else
  486. {
  487. AZ_Printf(s_builderName,
  488. "Found multiple bitangent data sets for mesh '%s'. Only the first will be used.",
  489. content.m_name.GetCStr());
  490. }
  491. }
  492. else if (azrtti_istypeof<MaterialData>(data.get()))
  493. {
  494. auto materialData = AZStd::static_pointer_cast<const MaterialData>(data);
  495. content.m_materials.push_back(materialData->GetUniqueId());
  496. }
  497. else if (azrtti_istypeof<SkinData>(data.get()))
  498. {
  499. content.m_skinData.emplace_back(data, static_cast<const SkinData*>(data.get()));
  500. }
  501. }
  502. AZ::Outcome<ModelAssetBuilderComponent::ProductMeshContentList> ModelAssetBuilderComponent::SourceMeshListToProductMeshList(
  503. const ModelAssetBuilderContext& context,
  504. const SourceMeshContentList& sourceMeshList,
  505. AZStd::unordered_map<AZStd::string, uint16_t>& jointNameToIndexMap,
  506. MorphTargetMetaAssetCreator& morphTargetMetaCreator)
  507. {
  508. ProductMeshContentList productMeshList;
  509. using Face = SceneAPI::DataTypes::IMeshData::Face;
  510. using FaceList = AZStd::vector<Face>;
  511. struct UidFaceList
  512. {
  513. MaterialUid m_materialUid;
  514. FaceList m_faceList;
  515. };
  516. using FacesByMaterialUid = AZStd::vector<UidFaceList>;
  517. using ProductList = AZStd::vector<FacesByMaterialUid>;
  518. ProductList productList;
  519. productList.resize(sourceMeshList.size());
  520. AZStd::vector<SceneAPI::DataTypes::MatrixType> meshTransforms;
  521. meshTransforms.reserve(sourceMeshList.size());
  522. size_t productMeshCount = 0;
  523. MorphTargetExporter morphTargetExporter;
  524. // Break up source data by material uid. We don't do any merging at this point,
  525. // and we don't sort by material id at this point so that the resulting vertex data
  526. // will have a 1-1 relationship with the source data. This ensures morph target indices
  527. // don't need to be re-mapped, as long as the meshes aren't merged later
  528. // We just can't output a mesh that has faces with multiple materials.
  529. for (size_t i = 0; i < sourceMeshList.size(); ++i)
  530. {
  531. const SourceMeshContent& sourceMeshContent = sourceMeshList[i];
  532. FacesByMaterialUid& productsByMaterialUid = productList[i];
  533. meshTransforms.push_back(sourceMeshContent.m_worldTransform);
  534. const auto& meshData = sourceMeshContent.m_meshData;
  535. const uint32_t faceCount = meshData->GetFaceCount();
  536. MaterialUid currentMaterialId = std::numeric_limits<MaterialUid>::max();
  537. for (uint32_t j = 0; j < faceCount; ++j)
  538. {
  539. const Face& faceInfo = meshData->GetFaceInfo(j);
  540. const MaterialUid matUid = sourceMeshContent.GetMaterialUniqueId(meshData->GetFaceMaterialId(j));
  541. // Start a new product mesh if the material changed
  542. if (currentMaterialId != matUid)
  543. {
  544. UidFaceList uidFaceList;
  545. uidFaceList.m_materialUid = matUid;
  546. productsByMaterialUid.push_back(uidFaceList);
  547. currentMaterialId = matUid;
  548. }
  549. // Add the faceinfo to the current product mesh
  550. UidFaceList& currentFaceList = productsByMaterialUid.back();
  551. currentFaceList.m_faceList.push_back(faceInfo);
  552. }
  553. productMeshCount += productsByMaterialUid.size();
  554. }
  555. productMeshList.reserve(productMeshCount);
  556. // Get the default values if there is no skin rule
  557. m_skinRuleSettings = SceneAPI::DataTypes::GetDefaultSkinRuleSettings();
  558. // Get the skin rule, if it exists
  559. if (const auto* skinRule = context.m_group.GetRuleContainerConst().FindFirstByType<SceneAPI::DataTypes::ISkinRule>().get())
  560. {
  561. m_skinRuleSettings.m_maxInfluencesPerVertex = skinRule->GetMaxWeightsPerVertex();
  562. m_skinRuleSettings.m_weightThreshold = skinRule->GetWeightThreshold();
  563. }
  564. // Keep track of the order of sub-meshes for morph targets.
  565. // We cannot re-order sub-meshes after this unless we also update the morph target data
  566. // This is because one morph target may impact multiple sub-meshes, and there may be
  567. // multiple product sub-meshes for each source mesh, so a given morph target may be
  568. // split into multiple dispatches, and we use this index to track which mesh is associated
  569. // with which dispatch
  570. uint32_t productMeshIndex = 0;
  571. // Once per source-mesh, since productList is 1-1 with source mesh
  572. for (size_t i = 0; i < productList.size(); ++i)
  573. {
  574. const FacesByMaterialUid& productsByMaterialUid = productList[i];
  575. const SceneAPI::DataTypes::MatrixType& meshTransform = meshTransforms[i];
  576. const SceneAPI::DataTypes::MatrixType inverseTranspose = meshTransform.GetInverseFull().GetTranspose();
  577. const SourceMeshContent& sourceMesh = sourceMeshList[i];
  578. const auto& meshData = sourceMesh.m_meshData;
  579. const auto& uvContentCollection = sourceMesh.m_meshUVData;
  580. const size_t uvSetCount = uvContentCollection.size();
  581. const auto& colorContentCollection = sourceMesh.m_meshColorData;
  582. const size_t colorSetCount = colorContentCollection.size();
  583. bool warnedExcessOfSkinInfluences = false;
  584. uint32_t totalVertexCountForThisSourceMesh = 0;
  585. for (const auto& it : productsByMaterialUid)
  586. {
  587. ProductMeshContent productMesh;
  588. productMesh.m_name = sourceMesh.m_name;
  589. productMesh.m_materialUid = it.m_materialUid;
  590. const FaceList& faceInfoList = it.m_faceList;
  591. uint32_t indexCount = static_cast<uint32_t>(faceInfoList.size()) * 3;
  592. productMesh.m_indices.reserve(indexCount);
  593. for (const Face& faceInfo : faceInfoList)
  594. {
  595. productMesh.m_indices.push_back(faceInfo.vertexIndex[0]);
  596. productMesh.m_indices.push_back(faceInfo.vertexIndex[1]);
  597. productMesh.m_indices.push_back(faceInfo.vertexIndex[2]);
  598. }
  599. // We need to both gather a collection of unique
  600. // indices so that we don't gather duplicate vertex data
  601. // while also correcting the collection of indices
  602. // that we have so that they start at 0 and are contiguous.
  603. AZStd::map<uint32_t, uint32_t> oldToNewIndices;
  604. uint32_t newIndex = 0;
  605. // Keep track of the highest value of old indices to validate vertex stream size
  606. uint32_t maxOldIndex = 0;
  607. for (uint32_t& index : productMesh.m_indices)
  608. {
  609. if (oldToNewIndices.find(index) == oldToNewIndices.end())
  610. {
  611. oldToNewIndices[index] = newIndex;
  612. newIndex++;
  613. }
  614. maxOldIndex = AZStd::max(maxOldIndex, index);
  615. index = oldToNewIndices[index];
  616. }
  617. AZStd::vector<float>& positions = productMesh.m_positions;
  618. AZStd::vector<float>& normals = productMesh.m_normals;
  619. AZStd::vector<float>& tangents = productMesh.m_tangents;
  620. AZStd::vector<float>& bitangents = productMesh.m_bitangents;
  621. AZStd::vector<AZStd::vector<float>>& uvSets = productMesh.m_uvSets;
  622. AZStd::vector<AZ::Name>& uvNames = productMesh.m_uvCustomNames;
  623. AZStd::vector<AZStd::vector<float>>& colorSets = productMesh.m_colorSets;
  624. AZStd::vector<AZ::Name>& colorNames = productMesh.m_colorCustomNames;
  625. AZStd::vector<float>& clothData = productMesh.m_clothData;
  626. AZStd::vector<uint16_t>& skinJointIndices = productMesh.m_skinJointIndices;
  627. AZStd::vector<float>& skinWeights = productMesh.m_skinWeights;
  628. const size_t vertexCount = oldToNewIndices.size();
  629. productMesh.m_vertexCount = vertexCount;
  630. positions.reserve(vertexCount * PositionFloatsPerVert);
  631. normals.reserve(vertexCount * NormalFloatsPerVert);
  632. if (sourceMesh.m_meshTangents)
  633. {
  634. if (maxOldIndex >= sourceMesh.m_meshTangents->GetCount())
  635. {
  636. AZ_Assert(false, "Out of bounds access of mesh tangents.");
  637. return AZ::Failure();
  638. }
  639. tangents.reserve(vertexCount * TangentFloatsPerVert);
  640. if (sourceMesh.m_meshBitangents)
  641. {
  642. if (maxOldIndex >= sourceMesh.m_meshBitangents->GetCount())
  643. {
  644. AZ_Assert(false, "Out of bounds access of mesh bitangents.");
  645. return AZ::Failure();
  646. }
  647. bitangents.reserve(vertexCount * BitangentFloatsPerVert);
  648. }
  649. }
  650. uvNames.reserve(uvSetCount);
  651. for (auto& uvContent : uvContentCollection)
  652. {
  653. if (maxOldIndex >= uvContent->GetCount())
  654. {
  655. AZ_Assert(false, "Out of bounds access of uvs.");
  656. return AZ::Failure();
  657. }
  658. uvNames.push_back(uvContent->GetCustomName());
  659. }
  660. uvSets.resize(uvSetCount);
  661. for (auto& uvSet : uvSets)
  662. {
  663. uvSet.reserve(vertexCount * UVFloatsPerVert);
  664. }
  665. colorNames.reserve(colorSetCount);
  666. for (auto& colorContent : colorContentCollection)
  667. {
  668. if (maxOldIndex >= colorContent->GetCount())
  669. {
  670. AZ_Assert(false, "Out of bounds access of colors.");
  671. return AZ::Failure();
  672. }
  673. colorNames.push_back(colorContent->GetCustomName());
  674. }
  675. colorSets.resize(colorSetCount);
  676. for (auto& colorSet : colorSets)
  677. {
  678. colorSet.reserve(vertexCount * ColorFloatsPerVert);
  679. }
  680. const bool hasClothData = !sourceMesh.m_meshClothData.empty();
  681. if (hasClothData)
  682. {
  683. if (sourceMesh.m_meshClothData.size() != vertexCount)
  684. {
  685. AZ_Assert(false, "Vertex Count %d does not match mesh cloth data size %d", vertexCount, sourceMesh.m_meshClothData.size());
  686. return AZ::Failure();
  687. }
  688. clothData.reserve(vertexCount * ClothDataFloatsPerVert);
  689. }
  690. const bool hasSkinData = !sourceMesh.m_skinData.empty();
  691. if (hasSkinData)
  692. {
  693. // Skinned meshes require that positions, normals, tangents, bitangents, all exist and have the same number
  694. // of total elements. Pad buffers with missing data to make them align with positions and normals
  695. if (!sourceMesh.m_meshTangents)
  696. {
  697. tangents.resize(vertexCount * TangentFloatsPerVert, 1.0f);
  698. AZ_Warning(s_builderName, false, "Mesh '%s' is missing tangents and no defaults were generated. Skinned meshes require tangents. Dummy tangents will be inserted, which may result in rendering artifacts.", sourceMesh.m_name.GetCStr());
  699. }
  700. if (!sourceMesh.m_meshBitangents)
  701. {
  702. bitangents.resize(vertexCount * BitangentFloatsPerVert, 1.0f);
  703. AZ_Warning(s_builderName, false, "Mesh '%s' is missing bitangents and no defaults were generated. Skinned meshes require bitangents. Dummy bitangents will be inserted, which may result in rendering artifacts.", sourceMesh.m_name.GetCStr());
  704. }
  705. productMesh.m_influencesPerVertex = CalculateMaxUsedSkinInfluencesPerVertex(
  706. sourceMesh, oldToNewIndices, warnedExcessOfSkinInfluences);
  707. const uint32_t totalInfluences = productMesh.m_influencesPerVertex * aznumeric_cast<uint32_t>(vertexCount);
  708. productMesh.m_skinJointIndices.reserve(totalInfluences);
  709. productMesh.m_skinWeights.reserve(totalInfluences);
  710. }
  711. for (const auto& itr : oldToNewIndices)
  712. {
  713. // We use the 'old' index as that properly indexes
  714. // into the old mesh data. The 'new' index is used for properly
  715. // indexing into this new collection that we're building here.
  716. const uint32_t oldIndex = itr.first;
  717. AZ::Vector3 pos = meshData->GetPosition(oldIndex);
  718. AZ::Vector3 normal = meshData->GetNormal(oldIndex);
  719. // Pre-multiply transform
  720. pos = meshTransform * pos;
  721. pos = context.m_coordSysConverter.ConvertVector3(pos);
  722. positions.push_back(pos.GetX());
  723. positions.push_back(pos.GetY());
  724. positions.push_back(pos.GetZ());
  725. // Multiply normal by inverse transpose to avoid
  726. // incorrect values produced by non-uniformly scaled
  727. // transforms.
  728. normal = inverseTranspose.TransformVector(normal);
  729. normal = context.m_coordSysConverter.ConvertVector3(normal);
  730. normal.Normalize();
  731. normals.push_back(normal.GetX());
  732. normals.push_back(normal.GetY());
  733. normals.push_back(normal.GetZ());
  734. if (sourceMesh.m_meshTangents)
  735. {
  736. AZ::Vector4 tangentWithW = sourceMesh.m_meshTangents->GetTangent(oldIndex);
  737. AZ::Vector3 tangent = tangentWithW.GetAsVector3();
  738. float bitangentSign = tangentWithW.GetW();
  739. tangent = meshTransform.TransformVector(tangent);
  740. tangent = context.m_coordSysConverter.ConvertVector3(tangent);
  741. tangent.Normalize();
  742. tangents.push_back(tangent.GetX());
  743. tangents.push_back(tangent.GetY());
  744. tangents.push_back(tangent.GetZ());
  745. tangents.push_back(bitangentSign);
  746. if (sourceMesh.m_meshBitangents)
  747. {
  748. AZ::Vector3 bitangent = sourceMesh.m_meshBitangents->GetBitangent(oldIndex);
  749. bitangent = meshTransform.TransformVector(bitangent);
  750. bitangent = context.m_coordSysConverter.ConvertVector3(bitangent);
  751. bitangent.Normalize();
  752. bitangents.push_back(bitangent.GetX());
  753. bitangents.push_back(bitangent.GetY());
  754. bitangents.push_back(bitangent.GetZ());
  755. }
  756. }
  757. // Gather UVs
  758. for (uint32_t ii = 0; ii < uvSetCount; ++ii)
  759. {
  760. auto& uvs = uvSets[ii];
  761. const auto& uvContent = uvContentCollection[ii];
  762. AZ::Vector2 uv = uvContent->GetUV(oldIndex);
  763. uvs.push_back(uv.GetX());
  764. uvs.push_back(uv.GetY());
  765. }
  766. // Gather Colors
  767. for (uint32_t ii = 0; ii < colorSetCount; ++ii)
  768. {
  769. auto& colors = colorSets[ii];
  770. const auto& colorContent = colorContentCollection[ii];
  771. SceneAPI::DataTypes::Color color = colorContent->GetColor(oldIndex);
  772. colors.push_back(color.red);
  773. colors.push_back(color.green);
  774. colors.push_back(color.blue);
  775. colors.push_back(color.alpha);
  776. }
  777. // Gather Cloth Data
  778. if (hasClothData)
  779. {
  780. const AZ::Color& vertexClothData = sourceMesh.m_meshClothData[oldIndex];
  781. clothData.push_back(vertexClothData.GetR());
  782. clothData.push_back(vertexClothData.GetG());
  783. clothData.push_back(vertexClothData.GetB());
  784. clothData.push_back(vertexClothData.GetA());
  785. }
  786. // Gather skinning influences
  787. if (hasSkinData)
  788. {
  789. // Warn about excess of skin influences once per-source mesh.
  790. GatherVertexSkinningInfluences(sourceMesh, productMesh, jointNameToIndexMap, oldIndex);
  791. }
  792. }// for each vertex in old to new indices
  793. // Align all the stream buffers to ensure they all padded to SkinnedMeshBufferAlignment byte boundary.
  794. // This is done to ensure that we can respect Metal's requirement of having typed buffers aligned to 64.
  795. // We also need to align to 16 and 12 byte boundary in order to respect RGB32 and RGBA32 buffer views.
  796. if (hasSkinData)
  797. {
  798. RPI::ModelAssetHelpers::AlignStreamBuffer(positions, vertexCount, PositionFormat, SkinnedMeshBufferAlignment);
  799. RPI::ModelAssetHelpers::AlignStreamBuffer(normals, vertexCount, NormalFormat, SkinnedMeshBufferAlignment);
  800. RPI::ModelAssetHelpers::AlignStreamBuffer(tangents, vertexCount, TangentFormat, SkinnedMeshBufferAlignment);
  801. RPI::ModelAssetHelpers::AlignStreamBuffer(bitangents, vertexCount, BitangentFormat, SkinnedMeshBufferAlignment);
  802. const size_t totalVertexInfluences = productMesh.m_influencesPerVertex * vertexCount;
  803. RPI::ModelAssetHelpers::AlignStreamBuffer(
  804. skinJointIndices, totalVertexInfluences, SkinIndicesFormat, SkinnedMeshBufferAlignment);
  805. RPI::ModelAssetHelpers::AlignStreamBuffer(
  806. skinWeights, totalVertexInfluences, SkinWeightFormat, SkinnedMeshBufferAlignment);
  807. }
  808. // A morph target that only influenced one source mesh might be split over multiple product meshes
  809. // if the source mesh had multiple materials and was split up.
  810. // So here, we need to know the start and end indices of the current product mesh within the original source
  811. // mesh, so that when we process a morph target on the source mesh, we can ignore it if it doesn't impact the
  812. // current product mesh and we can include it if it does. Furthermore, this leads to a 1:N relationship between
  813. // morph target animations and actual morph target dispatches
  814. morphTargetExporter.ProduceMorphTargets(
  815. productMeshIndex, totalVertexCountForThisSourceMesh, oldToNewIndices, context.m_scene, sourceMesh, productMesh,
  816. morphTargetMetaCreator, context.m_coordSysConverter);
  817. productMeshIndex++;
  818. totalVertexCountForThisSourceMesh += static_cast<uint32_t>(vertexCount);
  819. productMeshList.emplace_back(productMesh);
  820. }// for each product mesh in productsByMaterialUid
  821. }// for each product in productList (for each source mesh)
  822. return AZ::Success(productMeshList);
  823. }
  824. void ModelAssetBuilderComponent::PadVerticesForSkinning(ProductMeshContentList& productMeshList)
  825. {
  826. // Check if this is a skinned mesh
  827. if (!productMeshList.empty() && !productMeshList[0].m_skinWeights.empty())
  828. {
  829. for (ProductMeshContent& productMesh : productMeshList)
  830. {
  831. size_t vertexCount = productMesh.m_positions.size() / PositionFloatsPerVert;
  832. // Skinned meshes require that positions, normals, tangents, bitangents, all exist and have the same number
  833. // of total elements. Pad buffers with missing data to make them align with positions and normals
  834. if (productMesh.m_tangents.empty())
  835. {
  836. size_t alignedVertexCount =
  837. RPI::ModelAssetHelpers::GetAlignedCount<float>(vertexCount, TangentFormat, SkinnedMeshBufferAlignment);
  838. productMesh.m_tangents.resize(alignedVertexCount, 1.0f);
  839. AZ_Warning(s_builderName, false, "Mesh '%s' is missing tangents and no defaults were generated. Skinned meshes require tangents. Dummy tangents will be inserted, which may result in rendering artifacts.", productMesh.m_name.GetCStr());
  840. }
  841. if (productMesh.m_bitangents.empty())
  842. {
  843. size_t alignedVertexCount =
  844. RPI::ModelAssetHelpers::GetAlignedCount<float>(vertexCount, BitangentFormat, SkinnedMeshBufferAlignment);
  845. productMesh.m_bitangents.resize(alignedVertexCount, 1.0f);
  846. AZ_Warning(s_builderName, false, "Mesh '%s' is missing bitangents and no defaults were generated. Skinned meshes require bitangents. Dummy bitangents will be inserted, which may result in rendering artifacts.", productMesh.m_name.GetCStr());
  847. }
  848. }
  849. }
  850. }
  851. uint32_t ModelAssetBuilderComponent::CalculateMaxUsedSkinInfluencesPerVertex(
  852. const SourceMeshContent& sourceMesh,
  853. const AZStd::map<uint32_t, uint32_t>& oldToNewIndicesMap,
  854. bool& warnedExcessOfSkinInfluences) const
  855. {
  856. uint32_t influencesPerVertex = 0;
  857. for (const auto& [oldIndex, newIndex] : oldToNewIndicesMap)
  858. {
  859. uint32_t influenceCountForCurrentVertex = 0;
  860. for (const auto& skinData : sourceMesh.m_skinData)
  861. {
  862. const size_t numSkinInfluences = skinData->GetLinkCount(oldIndex);
  863. // Check all the links and add any with a weight over the threshold to the running count
  864. for (size_t influenceIndex = 0; influenceIndex < numSkinInfluences; ++influenceIndex)
  865. {
  866. const AZ::SceneAPI::DataTypes::ISkinWeightData::Link& link = skinData->GetLink(oldIndex, influenceIndex);
  867. const float weight = link.weight;
  868. if (weight > m_skinRuleSettings.m_weightThreshold)
  869. {
  870. ++influenceCountForCurrentVertex;
  871. }
  872. }
  873. }
  874. influencesPerVertex = AZStd::max(influencesPerVertex, influenceCountForCurrentVertex);
  875. }
  876. if (influencesPerVertex > m_skinRuleSettings.m_maxInfluencesPerVertex)
  877. {
  878. AZ_Warning(
  879. s_builderName, warnedExcessOfSkinInfluences,
  880. "Mesh %s has more skin influences (%d) than the maximum (%d). Skinning influences won't be normalized. "
  881. "It's also not guaranteed that the excess skin influences that are cut off will be the lowest weight influences. "
  882. "Maximum number of skin influences can be increased with a Skin Modifier in Scene Settings.",
  883. sourceMesh.m_name.GetCStr(), influencesPerVertex,
  884. m_skinRuleSettings.m_maxInfluencesPerVertex);
  885. warnedExcessOfSkinInfluences = true;
  886. }
  887. influencesPerVertex = AZStd::min(influencesPerVertex, m_skinRuleSettings.m_maxInfluencesPerVertex);
  888. // Round up to a multiple of two, since influences are processed two at a time in the shader
  889. return AZ::RoundUpToMultiple(influencesPerVertex, 2u);
  890. }
  891. void ModelAssetBuilderComponent::GatherVertexSkinningInfluences(
  892. const SourceMeshContent& sourceMesh,
  893. ProductMeshContent& productMesh,
  894. AZStd::unordered_map<AZStd::string, uint16_t>& jointNameToIndexMap,
  895. size_t vertexIndex) const
  896. {
  897. AZStd::vector<uint16_t>& skinJointIndices = productMesh.m_skinJointIndices;
  898. AZStd::vector<float>& skinWeights = productMesh.m_skinWeights;
  899. size_t numInfluencesAdded = 0;
  900. for (const auto& skinData : sourceMesh.m_skinData)
  901. {
  902. const size_t numSkinInfluences = skinData->GetLinkCount(vertexIndex);
  903. for (size_t influenceIndex = 0; influenceIndex < numSkinInfluences; ++influenceIndex)
  904. {
  905. const AZ::SceneAPI::DataTypes::ISkinWeightData::Link& link = skinData->GetLink(vertexIndex, influenceIndex);
  906. const float weight = link.weight;
  907. const AZStd::string& boneName = skinData->GetBoneName(link.boneId);
  908. // The bone id is a local bone id to the mesh. Since there could be multiple meshes, we store a global index to this asset,
  909. // which is guaranteed to be unique. Later we will translate those indices back using the skinmetadata.
  910. if (!jointNameToIndexMap.contains(boneName))
  911. {
  912. jointNameToIndexMap[boneName] = aznumeric_caster(jointNameToIndexMap.size());
  913. }
  914. const AZ::u16 jointIndex = jointNameToIndexMap[boneName];
  915. // Add skin influence
  916. if (weight > m_skinRuleSettings.m_weightThreshold)
  917. {
  918. if (numInfluencesAdded < productMesh.m_influencesPerVertex)
  919. {
  920. skinJointIndices.push_back(jointIndex);
  921. skinWeights.push_back(weight);
  922. numInfluencesAdded++;
  923. }
  924. }
  925. }
  926. }
  927. for (size_t influenceIndex = numInfluencesAdded; influenceIndex < productMesh.m_influencesPerVertex; ++influenceIndex)
  928. {
  929. skinJointIndices.push_back(0);
  930. skinWeights.push_back(0.0f);
  931. }
  932. }
  933. AZ::Outcome<ModelAssetBuilderComponent::ProductMeshContentList> ModelAssetBuilderComponent::MergeMeshesByMaterialUid(
  934. const ProductMeshContentList& productMeshList)
  935. {
  936. ProductMeshContentList finalMeshList;
  937. {
  938. AZStd::unordered_map<MaterialUid, ProductMeshContentList> meshesByMatUid;
  939. // First pass to reserve memory
  940. // This saves time with very large meshes
  941. {
  942. AZStd::unordered_map<MaterialUid, size_t> meshCountByMatUid;
  943. for (const ProductMeshContent& mesh : productMeshList)
  944. {
  945. if (mesh.CanBeMerged())
  946. {
  947. meshCountByMatUid[mesh.m_materialUid]++;
  948. }
  949. }
  950. for (const auto& it : meshCountByMatUid)
  951. {
  952. meshesByMatUid[it.first].reserve(it.second);
  953. }
  954. }
  955. size_t unmergeableMeshCount = 0;
  956. for (const ProductMeshContent& mesh : productMeshList)
  957. {
  958. if (mesh.CanBeMerged())
  959. {
  960. meshesByMatUid[mesh.m_materialUid].push_back(mesh);
  961. }
  962. else
  963. {
  964. unmergeableMeshCount++;
  965. }
  966. }
  967. const size_t mergedMeshCount = meshesByMatUid.size();
  968. finalMeshList.reserve(mergedMeshCount + unmergeableMeshCount);
  969. bool mismatchedVertexLayoutsAreErrors = MismatchedVertexLayoutsAreErrors();
  970. // Add the merged meshes
  971. for (auto& it : meshesByMatUid)
  972. {
  973. ProductMeshContentList& meshList = it.second;
  974. for (auto meshIter = meshList.begin(); meshIter < meshList.end() - 1;)
  975. {
  976. // Any mesh that doesn't match the others will not be merged and will just be added directly to the final mesh list
  977. // This could result in multiple meshes that do not match the one before them, but still match each other, not
  978. // getting merged. But it's not worth over complicating things by trying to sort and split the meshList when the
  979. // common case is that everything assigned to the same material uid likely matches already anyways
  980. if (!VertexStreamLayoutMatches(*meshIter, *(meshIter + 1)))
  981. {
  982. if (mismatchedVertexLayoutsAreErrors)
  983. {
  984. return AZ::Failure();
  985. }
  986. // Don't merge the next mesh in the list if it doesn't match the current one
  987. finalMeshList.emplace_back(*(meshIter + 1));
  988. meshList.erase(meshIter + 1);
  989. }
  990. else
  991. {
  992. meshIter++;
  993. }
  994. }
  995. ProductMeshContent mergedMesh = MergeMeshList(meshList, RemapIndices);
  996. mergedMesh.m_materialUid = it.first;
  997. if(!ValidateStreamAlignment(mergedMesh))
  998. {
  999. return AZ::Failure();
  1000. }
  1001. finalMeshList.emplace_back(AZStd::move(mergedMesh));
  1002. }
  1003. // Add the unmergeable meshes
  1004. for (const ProductMeshContent& mesh : productMeshList)
  1005. {
  1006. if (!mesh.CanBeMerged())
  1007. {
  1008. if(!ValidateStreamAlignment(mesh))
  1009. {
  1010. return AZ::Failure();
  1011. }
  1012. finalMeshList.emplace_back(mesh);
  1013. }
  1014. }
  1015. }
  1016. return AZ::Success(finalMeshList);
  1017. }
  1018. bool ModelAssetBuilderComponent::VertexStreamLayoutMatches(const ProductMeshContent& lhs, const ProductMeshContent& rhs) const
  1019. {
  1020. [[maybe_unused]] bool mismatchedVertexLayoutsAreErrors = MismatchedVertexLayoutsAreErrors();
  1021. // Check that the stream counts and types match
  1022. bool layoutMatches =
  1023. lhs.m_positions.empty() == rhs.m_positions.empty() &&
  1024. lhs.m_normals.empty() == rhs.m_normals.empty() &&
  1025. lhs.m_tangents.empty() == rhs.m_tangents.empty() &&
  1026. lhs.m_bitangents.empty() == rhs.m_bitangents.empty() &&
  1027. lhs.m_clothData.empty() == rhs.m_clothData.empty() &&
  1028. lhs.m_skinJointIndices.empty() == rhs.m_skinJointIndices.empty() &&
  1029. lhs.m_skinWeights.empty() == rhs.m_skinWeights.empty() &&
  1030. lhs.m_uvSets.size() == rhs.m_uvSets.size() &&
  1031. lhs.m_colorSets.size() == rhs.m_colorSets.size();
  1032. if (layoutMatches)
  1033. {
  1034. // For the streams that come with names, make sure the names match
  1035. bool namesMatch = true;
  1036. for (size_t i = 0; i < lhs.m_uvCustomNames.size(); ++i)
  1037. {
  1038. if (lhs.m_uvCustomNames[i] != rhs.m_uvCustomNames[i])
  1039. {
  1040. namesMatch = false;
  1041. AZStd::string errorMessage = AZStd::string::format(
  1042. "Two meshes have the same material assignment, but the uv names don't match. "
  1043. "Mesh '%s' uv '%zu' is named '%s'. "
  1044. "Mesh '%s' uv '%zu' is named '%s'. "
  1045. "Consider re-naming the uvs to match. "
  1046. "They will not be merged, but will still show up as a single material slot for material assignments. ",
  1047. lhs.m_name.GetCStr(),
  1048. i,
  1049. lhs.m_uvCustomNames[i].GetCStr(),
  1050. rhs.m_name.GetCStr(),
  1051. i,
  1052. rhs.m_uvCustomNames[i].GetCStr());
  1053. AZ_Error(s_builderName, !mismatchedVertexLayoutsAreErrors, "%s", errorMessage.c_str());
  1054. AZ_Warning(s_builderName, mismatchedVertexLayoutsAreErrors, "%s", errorMessage.c_str());
  1055. }
  1056. }
  1057. for (size_t i = 0; i < lhs.m_colorCustomNames.size(); ++i)
  1058. {
  1059. if (lhs.m_colorCustomNames[i] != rhs.m_colorCustomNames[i])
  1060. {
  1061. namesMatch = false;
  1062. AZStd::string errorMessage = AZStd::string::format(
  1063. "Two meshes have the same material assignment, but the color names don't match. "
  1064. "Mesh '%s' color '%zu' is named '%s'. "
  1065. "Mesh '%s' color '%zu' is named '%s'. "
  1066. "Consider re-naming the colors to match. "
  1067. "They will not be merged, but will still show up as a single material slot for material assignments.",
  1068. lhs.m_name.GetCStr(),
  1069. i,
  1070. lhs.m_colorCustomNames[i].GetCStr(),
  1071. rhs.m_name.GetCStr(),
  1072. i,
  1073. rhs.m_colorCustomNames[i].GetCStr());
  1074. AZ_Error(s_builderName, !mismatchedVertexLayoutsAreErrors, "%s", errorMessage.c_str());
  1075. AZ_Warning(s_builderName, mismatchedVertexLayoutsAreErrors, "%s", errorMessage.c_str());
  1076. }
  1077. }
  1078. layoutMatches = namesMatch;
  1079. }
  1080. else
  1081. {
  1082. AZStd::string errorMessage = AZStd::string::format(
  1083. "Mesh '%s' and '%s' have the same material assignment, but don't have matching vertex streams. "
  1084. "Consider giving them the same vertex streams in the source file or assigning a unique material to each of them. "
  1085. "They will not be merged, but will still show up as a single material slot for material assignments.",
  1086. lhs.m_name.GetCStr(),
  1087. rhs.m_name.GetCStr());
  1088. AZ_Error(s_builderName, !mismatchedVertexLayoutsAreErrors, "%s", errorMessage.c_str());
  1089. AZ_Warning(s_builderName, mismatchedVertexLayoutsAreErrors, "%s", errorMessage.c_str());
  1090. }
  1091. return layoutMatches;
  1092. }
  1093. template<typename T>
  1094. bool ModelAssetBuilderComponent::ValidateStreamSize(
  1095. size_t expectedVertexCount,
  1096. const AZStd::vector<T>& bufferData,
  1097. AZ::RHI::Format format,
  1098. [[maybe_unused]] const char* streamName,
  1099. bool isAligned /*= false*/) const
  1100. {
  1101. size_t actualVertexCount = (bufferData.size() * sizeof(T)) / RHI::GetFormatSize(format);
  1102. bool vertexCountMatchesExpected = expectedVertexCount == actualVertexCount;
  1103. //If the buffer is aligned it will have padded data so need to account for that.
  1104. if (isAligned)
  1105. {
  1106. size_t expectedPaddedVertexCountInT =
  1107. RPI::ModelAssetHelpers::GetAlignedCount<T>(expectedVertexCount, format, SkinnedMeshBufferAlignment);
  1108. size_t expectedPaddedVertexCount = expectedPaddedVertexCountInT / RHI::GetFormatComponentCount(format);
  1109. vertexCountMatchesExpected = expectedPaddedVertexCount == actualVertexCount;
  1110. }
  1111. AZ_Error(
  1112. s_builderName,
  1113. vertexCountMatchesExpected,
  1114. "VertexStream '%s' does not match the expected vertex count. This typically means multiple sub-meshes have mis-matched "
  1115. "vertex stream layouts (such as one having more uv sets than the other) but are assigned the same material in the dcc tool "
  1116. "so they were merged.",
  1117. streamName);
  1118. return vertexCountMatchesExpected;
  1119. }
  1120. bool ModelAssetBuilderComponent::ValidateStreamAlignment(const ProductMeshContent& mesh) const
  1121. {
  1122. bool success = true;
  1123. const bool hasSkinData = !mesh.m_skinJointIndices.empty() && !mesh.m_skinWeights.empty();
  1124. if (!mesh.m_positions.empty())
  1125. {
  1126. success &= ValidateStreamSize(mesh.m_vertexCount, mesh.m_positions, PositionFormat, "POSITION", hasSkinData);
  1127. }
  1128. if (!mesh.m_normals.empty())
  1129. {
  1130. success &= ValidateStreamSize(mesh.m_vertexCount, mesh.m_normals, NormalFormat, "NORMAL", hasSkinData);
  1131. }
  1132. if (!mesh.m_tangents.empty())
  1133. {
  1134. success &= ValidateStreamSize(mesh.m_vertexCount, mesh.m_tangents, TangentFormat, "TANGENT", hasSkinData);
  1135. }
  1136. if (!mesh.m_bitangents.empty())
  1137. {
  1138. success &= ValidateStreamSize(mesh.m_vertexCount, mesh.m_bitangents, BitangentFormat, "BITANGENT", hasSkinData);
  1139. }
  1140. for (size_t i = 0; i < mesh.m_uvSets.size(); ++i)
  1141. {
  1142. success &= ValidateStreamSize(mesh.m_vertexCount, mesh.m_uvSets[i], UVFormat, mesh.m_uvCustomNames[i].GetCStr());
  1143. }
  1144. for (size_t i = 0; i < mesh.m_colorSets.size(); ++i)
  1145. {
  1146. success &=
  1147. ValidateStreamSize(mesh.m_vertexCount, mesh.m_colorSets[i], ColorFormat, mesh.m_colorCustomNames[i].GetCStr());
  1148. }
  1149. if (!mesh.m_clothData.empty())
  1150. {
  1151. success &= ValidateStreamSize(mesh.m_vertexCount, mesh.m_clothData, ClothDataFormat, ShaderSemanticName_ClothData);
  1152. }
  1153. if (!mesh.m_skinJointIndices.empty())
  1154. {
  1155. success &= ValidateStreamSize(
  1156. mesh.m_vertexCount * mesh.m_influencesPerVertex,
  1157. mesh.m_skinJointIndices,
  1158. AZ::RHI::Format::R16_UINT,
  1159. ShaderSemanticName_SkinJointIndices,
  1160. hasSkinData);
  1161. }
  1162. if (!mesh.m_skinWeights.empty())
  1163. {
  1164. success &= ValidateStreamSize(
  1165. mesh.m_vertexCount * mesh.m_influencesPerVertex,
  1166. mesh.m_skinWeights,
  1167. SkinWeightFormat,
  1168. ShaderSemanticName_SkinWeights,
  1169. hasSkinData);
  1170. }
  1171. return success;
  1172. }
  1173. ModelAssetBuilderComponent::ProductMeshView ModelAssetBuilderComponent::CreateViewToEntireMesh(const ProductMeshContent& mesh)
  1174. {
  1175. ProductMeshView meshView;
  1176. meshView.m_name = mesh.m_name.GetStringView();
  1177. auto meshIndexCount = static_cast<uint32_t>(mesh.m_indices.size());
  1178. auto meshPositionsFloatCount = static_cast<uint32_t>(mesh.m_positions.size());
  1179. auto meshNormalsFloatCount = static_cast<uint32_t>(mesh.m_normals.size());
  1180. auto meshPositionCount = meshPositionsFloatCount / PositionFloatsPerVert;
  1181. auto meshNormalsCount = meshNormalsFloatCount / NormalFloatsPerVert;
  1182. const bool hasSkinData = !mesh.m_skinJointIndices.empty() && !mesh.m_skinWeights.empty();
  1183. if (hasSkinData)
  1184. {
  1185. // Skinned buffers are padded so true vertex count is cached a part of the mesh and used here.
  1186. meshPositionCount = aznumeric_cast<uint32_t>(mesh.m_vertexCount);
  1187. meshNormalsCount = aznumeric_cast<uint32_t>(mesh.m_vertexCount);
  1188. }
  1189. meshView.m_indexView = RHI::BufferViewDescriptor::CreateTyped(0, meshIndexCount, IndicesFormat);
  1190. meshView.m_positionView = RHI::BufferViewDescriptor::CreateTyped(0, meshPositionCount, PositionFormat);
  1191. if (meshNormalsCount > 0)
  1192. {
  1193. meshView.m_normalView = RHI::BufferViewDescriptor::CreateTyped(0, meshNormalsCount, NormalFormat);
  1194. }
  1195. const size_t uvSetCount = mesh.m_uvSets.size();
  1196. meshView.m_uvSetViews.reserve(uvSetCount);
  1197. meshView.m_uvCustomNames.resize(uvSetCount);
  1198. meshView.m_uvCustomNames.resize(mesh.m_uvCustomNames.size());
  1199. AZ_Assert(mesh.m_uvSets.size() == mesh.m_uvCustomNames.size(), "UV set size doesn't match the number of custom uv names");
  1200. for (uint32_t uvSetIndex = 0; uvSetIndex < mesh.m_uvSets.size(); uvSetIndex++)
  1201. {
  1202. const auto& uvSet = mesh.m_uvSets[uvSetIndex];
  1203. auto uvFloatCount = static_cast<uint32_t>(uvSet.size());
  1204. auto uvCount = uvFloatCount / UVFloatsPerVert;
  1205. meshView.m_uvSetViews.push_back(RHI::BufferViewDescriptor::CreateTyped(0, uvCount, UVFormat));
  1206. meshView.m_uvCustomNames.push_back(mesh.m_uvCustomNames[uvSetIndex]);
  1207. }
  1208. meshView.m_colorSetViews.reserve(mesh.m_colorSets.size());
  1209. meshView.m_colorCustomNames.resize(mesh.m_colorCustomNames.size());
  1210. for (uint32_t colorSetIndex = 0; colorSetIndex < mesh.m_colorSets.size(); colorSetIndex++)
  1211. {
  1212. const auto& colorSet = mesh.m_colorSets[colorSetIndex];
  1213. auto colorFloatCount = static_cast<uint32_t>(colorSet.size());
  1214. auto colorCount = colorFloatCount / ColorFloatsPerVert;
  1215. meshView.m_colorSetViews.push_back(RHI::BufferViewDescriptor::CreateTyped(0, colorCount, ColorFormat));
  1216. meshView.m_colorCustomNames.push_back(mesh.m_colorCustomNames[colorSetIndex]);
  1217. }
  1218. if (!mesh.m_tangents.empty())
  1219. {
  1220. meshView.m_tangentView = RHI::BufferViewDescriptor::CreateTyped(0, meshNormalsCount, TangentFormat);
  1221. }
  1222. if (!mesh.m_bitangents.empty())
  1223. {
  1224. meshView.m_bitangentView = RHI::BufferViewDescriptor::CreateTyped(0, meshNormalsCount, BitangentFormat);
  1225. }
  1226. if (!mesh.m_skinJointIndices.empty() && !mesh.m_skinWeights.empty())
  1227. {
  1228. const size_t numSkinInfluences = meshPositionCount * mesh.m_influencesPerVertex;
  1229. const uint32_t jointIndicesSizeInBytes = static_cast<uint32_t>(numSkinInfluences * sizeof(uint16_t));
  1230. meshView.m_skinJointIndicesView = RHI::BufferViewDescriptor::CreateRaw(0, jointIndicesSizeInBytes);
  1231. meshView.m_skinWeightsView = RHI::BufferViewDescriptor::CreateTyped(0, aznumeric_cast<uint32_t>(numSkinInfluences),SkinWeightFormat);
  1232. }
  1233. if (!mesh.m_morphTargetVertexData.empty())
  1234. {
  1235. const size_t numTotalVertices = mesh.m_morphTargetVertexData.size();
  1236. meshView.m_morphTargetVertexDataView = RHI::BufferViewDescriptor::CreateStructured(0, static_cast<uint32_t>(numTotalVertices), sizeof(PackedCompressedMorphTargetDelta));
  1237. }
  1238. if (!mesh.m_clothData.empty())
  1239. {
  1240. auto meshClothDataFloatCount = static_cast<uint32_t>(mesh.m_clothData.size());
  1241. AZ_Assert((meshClothDataFloatCount % ClothDataFloatsPerVert) == 0,
  1242. "Unexpected number of cloth data elements (%d), it should contain a multiple of %d elements.", meshClothDataFloatCount, ClothDataFloatsPerVert);
  1243. auto meshClothDataCount = meshClothDataFloatCount / ClothDataFloatsPerVert;
  1244. AZ_Assert(meshClothDataCount == meshPositionCount,
  1245. "Number of cloth data elements (%d) does not match the number of positions (%d) in the mesh", meshClothDataCount, meshPositionCount);
  1246. meshView.m_clothDataView = RHI::BufferViewDescriptor::CreateTyped(0, meshClothDataCount, ClothDataFormat);
  1247. }
  1248. meshView.m_materialUid = mesh.m_materialUid;
  1249. return meshView;
  1250. }
  1251. void ModelAssetBuilderComponent::MergeMeshesToCommonBuffers(
  1252. ProductMeshContentList& lodMeshList,
  1253. ProductMeshContent& lodMeshContent,
  1254. ProductMeshViewList& meshViews)
  1255. {
  1256. meshViews.reserve(lodMeshList.size());
  1257. // We want to merge these meshes into one large
  1258. // ProductMesh. That large buffer gets set on the LOD directly
  1259. // rather than a Mesh in the LOD.
  1260. ProductMeshContentAllocInfo lodBufferInfo;
  1261. bool isFirstMesh = true;
  1262. for (ProductMeshContent& mesh : lodMeshList)
  1263. {
  1264. if (lodBufferInfo.m_uvSetFloatCounts.size() < mesh.m_uvSets.size())
  1265. {
  1266. lodBufferInfo.m_uvSetFloatCounts.resize(mesh.m_uvSets.size());
  1267. }
  1268. if (lodBufferInfo.m_colorSetFloatCounts.size() < mesh.m_colorSets.size())
  1269. {
  1270. lodBufferInfo.m_colorSetFloatCounts.resize(mesh.m_colorSets.size());
  1271. }
  1272. // Once again we save a lot of time and memory by determining what we
  1273. // need to allocate up-front
  1274. auto meshIndexCount = static_cast<uint32_t>(mesh.m_indices.size());
  1275. auto meshPositionsFloatCount = static_cast<uint32_t>(mesh.m_positions.size());
  1276. auto meshNormalsFloatCount = static_cast<uint32_t>(mesh.m_normals.size());
  1277. auto meshTangentsFloatCount = static_cast<uint32_t>(mesh.m_tangents.size());
  1278. auto meshBitangentsFloatCount = static_cast<uint32_t>(mesh.m_bitangents.size());
  1279. auto meshClothDataFloatCount = static_cast<uint32_t>(mesh.m_clothData.size());
  1280. // For each element we need to:
  1281. // record the offset for the view
  1282. // accumulate the allocation info
  1283. // fill the rest of the data for the view
  1284. ProductMeshView meshView;
  1285. meshView.m_name = mesh.m_name;
  1286. meshView.m_indexView = RHI::BufferViewDescriptor::CreateTyped(static_cast<uint32_t>(lodBufferInfo.m_indexCount), meshIndexCount, IndicesFormat);
  1287. lodBufferInfo.m_indexCount += meshIndexCount;
  1288. const bool hasSkinData = !mesh.m_skinJointIndices.empty() && !mesh.m_skinWeights.empty();
  1289. uint32_t meshVertexCount = meshPositionsFloatCount / PositionFloatsPerVert;
  1290. if (hasSkinData)
  1291. {
  1292. // Skinned buffers are padded so true vertex count is cached as part of the mesh and used here.
  1293. meshVertexCount = aznumeric_cast<uint32_t>(mesh.m_vertexCount);
  1294. }
  1295. if (!mesh.m_positions.empty())
  1296. {
  1297. const uint32_t elementOffset = static_cast<uint32_t>(lodBufferInfo.m_positionsFloatCount) / PositionFloatsPerVert;
  1298. meshView.m_positionView = RHI::BufferViewDescriptor::CreateTyped(elementOffset, meshVertexCount, PositionFormat);
  1299. lodBufferInfo.m_positionsFloatCount += meshPositionsFloatCount;
  1300. }
  1301. if (!mesh.m_normals.empty())
  1302. {
  1303. const uint32_t elementOffset = static_cast<uint32_t>(lodBufferInfo.m_normalsFloatCount) / NormalFloatsPerVert;
  1304. meshView.m_normalView = RHI::BufferViewDescriptor::CreateTyped(elementOffset, meshVertexCount, NormalFormat);
  1305. lodBufferInfo.m_normalsFloatCount += meshNormalsFloatCount;
  1306. }
  1307. if (!mesh.m_tangents.empty())
  1308. {
  1309. const uint32_t elementOffset = static_cast<uint32_t>(lodBufferInfo.m_tangentsFloatCount) / TangentFloatsPerVert;
  1310. meshView.m_tangentView = RHI::BufferViewDescriptor::CreateTyped(elementOffset, meshVertexCount, TangentFormat);
  1311. lodBufferInfo.m_tangentsFloatCount += meshTangentsFloatCount;
  1312. }
  1313. if (!mesh.m_bitangents.empty())
  1314. {
  1315. const uint32_t elementOffset = static_cast<uint32_t>(lodBufferInfo.m_bitangentsFloatCount) / BitangentFloatsPerVert;
  1316. meshView.m_bitangentView = RHI::BufferViewDescriptor::CreateTyped(elementOffset, meshVertexCount, BitangentFormat);
  1317. lodBufferInfo.m_bitangentsFloatCount += meshBitangentsFloatCount;
  1318. }
  1319. const size_t uvSetCount = mesh.m_uvSets.size();
  1320. if (uvSetCount > 0)
  1321. {
  1322. meshView.m_uvSetViews.resize(uvSetCount);
  1323. meshView.m_uvCustomNames.resize(uvSetCount);
  1324. for (size_t i = 0; i < uvSetCount; ++i)
  1325. {
  1326. meshView.m_uvCustomNames[i] = mesh.m_uvCustomNames[i];
  1327. auto& uvSetView = meshView.m_uvSetViews[i];
  1328. const uint32_t elementOffset = static_cast<uint32_t>(lodBufferInfo.m_uvSetFloatCounts[i]) / UVFloatsPerVert;
  1329. uvSetView = RHI::BufferViewDescriptor::CreateTyped(elementOffset, meshVertexCount, UVFormat);
  1330. const auto uvCount = static_cast<uint32_t>(mesh.m_uvSets[i].size());
  1331. lodBufferInfo.m_uvSetFloatCounts[i] += uvCount;
  1332. }
  1333. }
  1334. const size_t colorSetCount = mesh.m_colorSets.size();
  1335. if (colorSetCount > 0)
  1336. {
  1337. meshView.m_colorSetViews.resize(colorSetCount);
  1338. meshView.m_colorCustomNames.resize(colorSetCount);
  1339. for (size_t i = 0; i < colorSetCount; ++i)
  1340. {
  1341. meshView.m_colorCustomNames[i] = mesh.m_colorCustomNames[i];
  1342. auto& colorSetView = meshView.m_colorSetViews[i];
  1343. const uint32_t elementOffset = static_cast<uint32_t>(lodBufferInfo.m_colorSetFloatCounts[i]) / ColorFloatsPerVert;
  1344. colorSetView = RHI::BufferViewDescriptor::CreateTyped(elementOffset, meshVertexCount, ColorFormat);
  1345. const auto colorCount = static_cast<uint32_t>(mesh.m_colorSets[i].size());
  1346. lodBufferInfo.m_colorSetFloatCounts[i] += colorCount;
  1347. }
  1348. }
  1349. if (!mesh.m_clothData.empty())
  1350. {
  1351. const uint32_t elementOffset = static_cast<uint32_t>(lodBufferInfo.m_clothDataFloatCount) / ClothDataFloatsPerVert;
  1352. meshView.m_clothDataView = RHI::BufferViewDescriptor::CreateTyped(elementOffset, meshVertexCount, ClothDataFormat);
  1353. lodBufferInfo.m_clothDataFloatCount += meshClothDataFloatCount;
  1354. }
  1355. meshView.m_materialUid = mesh.m_materialUid;
  1356. if (!mesh.m_skinJointIndices.empty() && !mesh.m_skinWeights.empty())
  1357. {
  1358. if (!isFirstMesh && lodBufferInfo.m_jointIdsCount == 0)
  1359. {
  1360. AZ_Error(
  1361. s_builderName, false,
  1362. "Attempting to merge a mix of static and skinned meshes, this will fail on buffer generation later. Mesh with "
  1363. "name %s is skinned, but previous meshes were not skinned.",
  1364. mesh.m_name.GetCStr());
  1365. }
  1366. [[maybe_unused]] const size_t totalVertexInfluences = mesh.m_influencesPerVertex * mesh.m_vertexCount;
  1367. AZ_Assert(
  1368. mesh.m_skinJointIndices.size() >= totalVertexInfluences && mesh.m_skinWeights.size() >= totalVertexInfluences,
  1369. "Number of allocated skin influence joint indices (%zu) and the number of weights (%zu) should be above (%zu) .",
  1370. mesh.m_skinJointIndices.size(),
  1371. mesh.m_skinWeights.size(),
  1372. totalVertexInfluences);
  1373. AZ_Assert(mesh.m_skinWeights.size() % mesh.m_influencesPerVertex == 0,
  1374. "The number of skin influences per vertex (%d) is not a multiple of the total number of skinning weights (%d). This means that not every vertex has exactly (%d) skinning weights and invalidates the data.",
  1375. mesh.m_skinWeights.size(), mesh.m_influencesPerVertex, mesh.m_influencesPerVertex);
  1376. uint32_t prevJointIdCount = aznumeric_cast<uint32_t>(lodBufferInfo.m_jointIdsCount);
  1377. uint32_t newJointIdCount = aznumeric_cast<uint32_t>(mesh.m_vertexCount * mesh.m_influencesPerVertex);
  1378. AZ_Assert(prevJointIdCount * sizeof(uint16_t) % 16 == 0, "Failed to align the joint id offset along a 16-byte boundary");
  1379. // For the view itself, we only want a view that includes the real ids, not the padding, so use newJointIdCount
  1380. meshView.m_skinJointIndicesView = RHI::BufferViewDescriptor::CreateRaw(
  1381. /*byteOffset=*/prevJointIdCount * sizeof(uint16_t),
  1382. /*byteCount*/ newJointIdCount * sizeof(uint16_t));
  1383. lodBufferInfo.m_jointIdsCount += aznumeric_cast<uint32_t>(mesh.m_skinJointIndices.size());
  1384. // Weights are more straightforward, just add any new weights
  1385. const uint32_t prevJointWeightCount = aznumeric_cast<uint32_t>(lodBufferInfo.m_jointWeightsCount);
  1386. const uint32_t newJointWeightCount = aznumeric_cast<uint32_t>(mesh.m_vertexCount * mesh.m_influencesPerVertex);
  1387. meshView.m_skinWeightsView = RHI::BufferViewDescriptor::CreateTyped(prevJointWeightCount, newJointWeightCount, SkinWeightFormat);
  1388. lodBufferInfo.m_jointWeightsCount += aznumeric_cast<uint32_t>(mesh.m_skinWeights.size());
  1389. }
  1390. else if (lodBufferInfo.m_jointIdsCount > 0)
  1391. {
  1392. AZ_Error(s_builderName, false, "Attempting to merge a mix of static and skinned meshes, this will fail on buffer generation later. Mesh with name %s is not skinned, but previous meshes were skinned.",
  1393. mesh.m_name.GetCStr());
  1394. }
  1395. if (!mesh.m_morphTargetVertexData.empty())
  1396. {
  1397. const size_t numPrevVertexDeltas = lodBufferInfo.m_morphTargetVertexDeltaCount;
  1398. const size_t numNewVertexDeltas = mesh.m_morphTargetVertexData.size();
  1399. meshView.m_morphTargetVertexDataView = RHI::BufferViewDescriptor::CreateStructured(/*elementOffset=*/ static_cast<uint32_t>(numPrevVertexDeltas), static_cast<uint32_t>(numNewVertexDeltas), sizeof(PackedCompressedMorphTargetDelta));
  1400. lodBufferInfo.m_morphTargetVertexDeltaCount += numNewVertexDeltas;
  1401. }
  1402. meshViews.emplace_back(AZStd::move(meshView));
  1403. isFirstMesh = false;
  1404. }
  1405. // Now that we have the views settled, we can just merge the mesh
  1406. lodMeshContent = MergeMeshList(lodMeshList, PreserveIndices);
  1407. }
  1408. ModelAssetBuilderComponent::ProductMeshContent ModelAssetBuilderComponent::MergeMeshList(
  1409. const ProductMeshContentList& productMeshList,
  1410. IndicesOperation indicesOp)
  1411. {
  1412. ProductMeshContent mergedMesh;
  1413. // A preallocation pass for the merged mesh
  1414. {
  1415. size_t indexCount = 0;
  1416. size_t positionCount = 0;
  1417. size_t normalCount = 0;
  1418. size_t tangentCount = 0;
  1419. size_t bitangentCount = 0;
  1420. size_t clothDataCount = 0;
  1421. AZStd::vector<size_t> uvSetCounts;
  1422. AZStd::vector<size_t> colorSetCounts;
  1423. for (const ProductMeshContent& mesh : productMeshList)
  1424. {
  1425. indexCount += mesh.m_indices.size();
  1426. positionCount += mesh.m_positions.size();
  1427. normalCount += mesh.m_normals.size();
  1428. tangentCount += mesh.m_tangents.size();
  1429. bitangentCount += mesh.m_bitangents.size();
  1430. clothDataCount += mesh.m_clothData.size();
  1431. if (mesh.m_uvSets.size() > uvSetCounts.size())
  1432. {
  1433. uvSetCounts.resize(mesh.m_uvSets.size());
  1434. }
  1435. for (size_t i = 0; i < mesh.m_uvSets.size(); ++i)
  1436. {
  1437. uvSetCounts[i] += mesh.m_uvSets[i].size();
  1438. }
  1439. if (mesh.m_colorSets.size() > colorSetCounts.size())
  1440. {
  1441. colorSetCounts.resize(mesh.m_colorSets.size());
  1442. }
  1443. for (size_t i = 0; i < mesh.m_colorSets.size(); ++i)
  1444. {
  1445. colorSetCounts[i] += mesh.m_colorSets[i].size();
  1446. }
  1447. mergedMesh.m_vertexCount += mesh.m_vertexCount;
  1448. }
  1449. mergedMesh.m_indices.reserve(indexCount);
  1450. mergedMesh.m_positions.reserve(positionCount);
  1451. mergedMesh.m_normals.reserve(normalCount);
  1452. mergedMesh.m_tangents.reserve(tangentCount);
  1453. mergedMesh.m_bitangents.reserve(bitangentCount);
  1454. mergedMesh.m_clothData.reserve(clothDataCount);
  1455. mergedMesh.m_uvCustomNames.resize(uvSetCounts.size());
  1456. for (auto& mesh : productMeshList)
  1457. {
  1458. int32_t nameCount = aznumeric_cast<int32_t>(mesh.m_uvCustomNames.size());
  1459. // Backward stack, the first mesh defines the name.
  1460. for (int32_t i = nameCount - 1; i >= 0; --i)
  1461. {
  1462. mergedMesh.m_uvCustomNames[i] = mesh.m_uvCustomNames[i];
  1463. }
  1464. }
  1465. mergedMesh.m_uvSets.resize(uvSetCounts.size());
  1466. for (size_t i = 0; i < uvSetCounts.size(); ++i)
  1467. {
  1468. mergedMesh.m_uvSets[i].reserve(uvSetCounts[i]);
  1469. }
  1470. mergedMesh.m_colorCustomNames.resize(colorSetCounts.size());
  1471. for (auto& mesh : productMeshList)
  1472. {
  1473. int32_t nameCount = aznumeric_cast<int32_t>(mesh.m_colorCustomNames.size());
  1474. // Backward stack, the first mesh defines the name.
  1475. for (int32_t i = nameCount - 1; i >= 0; --i)
  1476. {
  1477. mergedMesh.m_colorCustomNames[i] = mesh.m_colorCustomNames[i];
  1478. }
  1479. }
  1480. mergedMesh.m_colorSets.resize(colorSetCounts.size());
  1481. for (size_t i = 0; i < colorSetCounts.size(); ++i)
  1482. {
  1483. mergedMesh.m_colorSets[i].reserve(colorSetCounts[i]);
  1484. }
  1485. }
  1486. uint32_t tailIndex = 0;
  1487. // Append each common mesh onto this LOD-wide mesh
  1488. for (const ProductMeshContent& mesh : productMeshList)
  1489. {
  1490. if(mergedMesh.m_name.IsEmpty())
  1491. {
  1492. mergedMesh.m_name = mesh.m_name;
  1493. }
  1494. else
  1495. {
  1496. mergedMesh.m_name = AZStd::string::format("%s+%s", mergedMesh.m_name.GetCStr(), mesh.m_name.GetCStr());
  1497. }
  1498. AZStd::vector<uint32_t> indices = mesh.m_indices;
  1499. if (indicesOp == RemapIndices)
  1500. {
  1501. /**
  1502. * Remap indices to start where the last mesh left off
  1503. * If mesh 0 has indices 0,1,2 and mesh 1 has indices 0,1,2
  1504. * we need to rescale them so that mesh 1 has indices 3,4,5
  1505. */
  1506. uint32_t largestIndex = 0;
  1507. for (uint32_t& index : indices)
  1508. {
  1509. index += tailIndex;
  1510. if (index > largestIndex)
  1511. {
  1512. largestIndex = index;
  1513. }
  1514. }
  1515. // +1 because if the largest index is 5 we want the next index to start at 6
  1516. tailIndex = largestIndex + 1;
  1517. }
  1518. mergedMesh.m_indices.insert(
  1519. mergedMesh.m_indices.end(), indices.begin(), indices.end());
  1520. if (!mesh.m_positions.empty())
  1521. {
  1522. mergedMesh.m_positions.insert(
  1523. mergedMesh.m_positions.end(), mesh.m_positions.begin(), mesh.m_positions.end());
  1524. }
  1525. if (!mesh.m_normals.empty())
  1526. {
  1527. mergedMesh.m_normals.insert(
  1528. mergedMesh.m_normals.end(), mesh.m_normals.begin(), mesh.m_normals.end());
  1529. }
  1530. if (!mesh.m_tangents.empty())
  1531. {
  1532. mergedMesh.m_tangents.insert(
  1533. mergedMesh.m_tangents.end(), mesh.m_tangents.begin(), mesh.m_tangents.end());
  1534. }
  1535. if (!mesh.m_bitangents.empty())
  1536. {
  1537. mergedMesh.m_bitangents.insert(
  1538. mergedMesh.m_bitangents.end(), mesh.m_bitangents.begin(), mesh.m_bitangents.end());
  1539. }
  1540. const size_t uvSetCount = mesh.m_uvSets.size();
  1541. for (size_t i = 0; i < uvSetCount; ++i)
  1542. {
  1543. mergedMesh.m_uvSets[i].insert(
  1544. mergedMesh.m_uvSets[i].end(), mesh.m_uvSets[i].begin(), mesh.m_uvSets[i].end());
  1545. }
  1546. const size_t colorSetCount = mesh.m_colorSets.size();
  1547. for (size_t i = 0; i < colorSetCount; ++i)
  1548. {
  1549. mergedMesh.m_colorSets[i].insert(
  1550. mergedMesh.m_colorSets[i].end(), mesh.m_colorSets[i].begin(), mesh.m_colorSets[i].end());
  1551. }
  1552. if (!mesh.m_skinJointIndices.empty())
  1553. {
  1554. mergedMesh.m_skinJointIndices.insert(
  1555. mergedMesh.m_skinJointIndices.end(), mesh.m_skinJointIndices.begin(), mesh.m_skinJointIndices.end());
  1556. }
  1557. if (!mesh.m_skinWeights.empty())
  1558. {
  1559. mergedMesh.m_skinWeights.insert(
  1560. mergedMesh.m_skinWeights.end(), mesh.m_skinWeights.begin(), mesh.m_skinWeights.end());
  1561. }
  1562. if (!mesh.m_morphTargetVertexData.empty())
  1563. {
  1564. const auto& sourceMorphTargetData = mesh.m_morphTargetVertexData;
  1565. auto& mergedMorphTargetData = mergedMesh.m_morphTargetVertexData;
  1566. mergedMorphTargetData.insert(mergedMorphTargetData.end(), sourceMorphTargetData.begin(), sourceMorphTargetData.end());
  1567. }
  1568. if (!mesh.m_clothData.empty())
  1569. {
  1570. mergedMesh.m_clothData.insert(
  1571. mergedMesh.m_clothData.end(), mesh.m_clothData.begin(), mesh.m_clothData.end());
  1572. }
  1573. }
  1574. return mergedMesh;
  1575. }
  1576. template<typename T>
  1577. bool ModelAssetBuilderComponent::BuildStructuredStreamBuffer(
  1578. AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo>& outStreamBuffers,
  1579. const AZStd::vector<T>& bufferData,
  1580. const RHI::ShaderSemantic& semantic,
  1581. const AZ::Name& customStreamName)
  1582. {
  1583. AZStd::string bufferName = semantic.ToString();
  1584. size_t elementCount = bufferData.size();
  1585. size_t elementSize = sizeof(T);
  1586. Outcome<Data::Asset<BufferAsset>> bufferOutcome = CreateStructuredBufferAsset(bufferData.data(), elementCount, elementSize, bufferName);
  1587. if (!bufferOutcome.IsSuccess())
  1588. {
  1589. AZ_Error(s_builderName, false, "Failed to build %s stream", semantic.ToString().data());
  1590. return false;
  1591. }
  1592. outStreamBuffers.push_back({ semantic, customStreamName, {bufferOutcome.GetValue(), bufferOutcome.GetValue()->GetBufferViewDescriptor()} });
  1593. return true;
  1594. };
  1595. template<typename T>
  1596. bool ModelAssetBuilderComponent::BuildRawStreamBuffer(
  1597. AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo>& outStreamBuffers,
  1598. const AZStd::vector<T>& bufferData,
  1599. const RHI::ShaderSemantic& semantic,
  1600. const AZ::Name& customStreamName)
  1601. {
  1602. AZStd::string bufferName = semantic.ToString();
  1603. size_t sizeInBytes = bufferData.size() * sizeof(T);
  1604. Outcome<Data::Asset<BufferAsset>> bufferOutcome = CreateRawBufferAsset(bufferData.data(), sizeInBytes, bufferName);
  1605. if (!bufferOutcome.IsSuccess())
  1606. {
  1607. AZ_Error(s_builderName, false, "Failed to build %s stream", semantic.ToString().data());
  1608. return false;
  1609. }
  1610. outStreamBuffers.push_back({ semantic, customStreamName, {bufferOutcome.GetValue(), bufferOutcome.GetValue()->GetBufferViewDescriptor()} });
  1611. return true;
  1612. };
  1613. template<typename T>
  1614. bool ModelAssetBuilderComponent::BuildTypedStreamBuffer(
  1615. AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo>& outStreamBuffers,
  1616. const AZStd::vector<T>& bufferData,
  1617. AZ::RHI::Format format,
  1618. const RHI::ShaderSemantic& semantic,
  1619. const AZ::Name& customStreamName)
  1620. {
  1621. AZStd::string bufferName = semantic.ToString();
  1622. size_t floatsPerElement = RHI::GetFormatSize(format) / sizeof(T);
  1623. Outcome<Data::Asset<BufferAsset>> bufferOutcome = CreateTypedBufferAsset(bufferData.data(), bufferData.size() / floatsPerElement, format, bufferName);
  1624. if (!bufferOutcome.IsSuccess())
  1625. {
  1626. AZ_Error(s_builderName, false, "Failed to build %s stream", semantic.ToString().data());
  1627. return false;
  1628. }
  1629. outStreamBuffers.push_back({semantic, customStreamName, {bufferOutcome.GetValue(), bufferOutcome.GetValue()->GetBufferViewDescriptor()}});
  1630. return true;
  1631. };
  1632. template<typename T>
  1633. bool ModelAssetBuilderComponent::BuildStreamBuffer(size_t vertexCount,
  1634. AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo>& outStreamBuffers,
  1635. const AZStd::vector<T>& bufferData,
  1636. AZ::RHI::Format format,
  1637. const RHI::ShaderSemantic& semantic,
  1638. const AZ::Name& customStreamName)
  1639. {
  1640. size_t expectedElementCount = vertexCount * RHI::GetFormatComponentCount(format);
  1641. if (expectedElementCount != bufferData.size())
  1642. {
  1643. AZ_Error(s_builderName, false, "Failed to build %s stream. Expected %d elements but found %d.", semantic.ToString().data(), expectedElementCount, bufferData.size());
  1644. return false;
  1645. }
  1646. AZStd::string bufferName = semantic.ToString();
  1647. Outcome<Data::Asset<BufferAsset>> bufferOutcome = CreateTypedBufferAsset(bufferData.data(), vertexCount, format, bufferName);
  1648. if (!bufferOutcome.IsSuccess())
  1649. {
  1650. AZ_Error(s_builderName, false, "Failed to build %s stream", semantic.ToString().data());
  1651. return false;
  1652. }
  1653. outStreamBuffers.push_back({semantic, customStreamName, {bufferOutcome.GetValue(), bufferOutcome.GetValue()->GetBufferViewDescriptor()}});
  1654. return true;
  1655. };
  1656. bool ModelAssetBuilderComponent::CreateModelLodBuffers(
  1657. const ProductMeshContent& lodBufferContent,
  1658. BufferAssetView& outIndexBuffer,
  1659. AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo>& outStreamBuffers,
  1660. ModelLodAssetCreator& lodAssetCreator)
  1661. {
  1662. const AZStd::vector<uint32_t>& indices = lodBufferContent.m_indices;
  1663. const AZStd::vector<float>& positions = lodBufferContent.m_positions;
  1664. const AZStd::vector<float>& normals = lodBufferContent.m_normals;
  1665. const AZStd::vector<float>& tangents = lodBufferContent.m_tangents;
  1666. const AZStd::vector<float>& bitangents = lodBufferContent.m_bitangents;
  1667. const AZStd::vector<AZStd::vector<float>>& uvSets = lodBufferContent.m_uvSets;
  1668. const AZStd::vector<AZ::Name>& uvCustomNames = lodBufferContent.m_uvCustomNames;
  1669. const AZStd::vector<AZStd::vector<float>>& colorSets = lodBufferContent.m_colorSets;
  1670. const AZStd::vector<AZ::Name>& colorCustomNames = lodBufferContent.m_colorCustomNames;
  1671. const AZStd::vector<float>& clothData = lodBufferContent.m_clothData;
  1672. // Build Index Buffer ...
  1673. {
  1674. Outcome<Data::Asset<BufferAsset>> indexBufferOutcome = CreateTypedBufferAsset(indices.data(), indices.size(), IndicesFormat, "index");
  1675. if (!indexBufferOutcome.IsSuccess())
  1676. {
  1677. AZ_Error(s_builderName, false, "Failed to build index stream");
  1678. return false;
  1679. }
  1680. outIndexBuffer = { indexBufferOutcome.GetValue(), indexBufferOutcome.GetValue()->GetBufferViewDescriptor() };
  1681. }
  1682. // Build various stream buffers ...
  1683. if (!BuildTypedStreamBuffer<float>(outStreamBuffers, positions, PositionFormat, RHI::ShaderSemantic{"POSITION"}))
  1684. {
  1685. return false;
  1686. }
  1687. if (!BuildTypedStreamBuffer<float>(outStreamBuffers, normals, NormalFormat, RHI::ShaderSemantic{"NORMAL"}))
  1688. {
  1689. return false;
  1690. }
  1691. if (!tangents.empty())
  1692. {
  1693. if (!BuildTypedStreamBuffer<float>(outStreamBuffers, tangents, TangentFormat, RHI::ShaderSemantic{"TANGENT"}))
  1694. {
  1695. return false;
  1696. }
  1697. }
  1698. if (!bitangents.empty())
  1699. {
  1700. if (!BuildTypedStreamBuffer<float>(outStreamBuffers, bitangents, BitangentFormat, RHI::ShaderSemantic{"BITANGENT"}))
  1701. {
  1702. return false;
  1703. }
  1704. }
  1705. for (size_t i = 0; i < uvSets.size(); ++i)
  1706. {
  1707. if (!BuildTypedStreamBuffer<float>(outStreamBuffers, uvSets[i], UVFormat, RHI::ShaderSemantic{"UV", i}, uvCustomNames[i]))
  1708. {
  1709. return false;
  1710. }
  1711. }
  1712. for (size_t i = 0; i < colorSets.size(); ++i)
  1713. {
  1714. if (!BuildTypedStreamBuffer<float>(outStreamBuffers, colorSets[i], ColorFormat, RHI::ShaderSemantic{"COLOR", i}, colorCustomNames[i]))
  1715. {
  1716. return false;
  1717. }
  1718. }
  1719. // Skinning buffers
  1720. const AZStd::vector<uint16_t>& skinJointIndices = lodBufferContent.m_skinJointIndices;
  1721. const AZStd::vector<float>& skinWeights = lodBufferContent.m_skinWeights;
  1722. if (!skinJointIndices.empty() && !skinWeights.empty())
  1723. {
  1724. if (!BuildRawStreamBuffer<uint16_t>(outStreamBuffers, skinJointIndices, RHI::ShaderSemantic{ShaderSemanticName_SkinJointIndices}))
  1725. {
  1726. return false;
  1727. }
  1728. if (!BuildStreamBuffer<float>(skinWeights.size(), outStreamBuffers, skinWeights, SkinWeightFormat, RHI::ShaderSemantic{ShaderSemanticName_SkinWeights}))
  1729. {
  1730. return false;
  1731. }
  1732. }
  1733. // Morph target buffers
  1734. const AZStd::vector<PackedCompressedMorphTargetDelta>& morphTargetVertexDeltas = lodBufferContent.m_morphTargetVertexData;
  1735. if (!morphTargetVertexDeltas.empty())
  1736. {
  1737. if (!BuildStructuredStreamBuffer<PackedCompressedMorphTargetDelta>(outStreamBuffers, morphTargetVertexDeltas,
  1738. RHI::ShaderSemantic{ ShaderSemanticName_MorphTargetDeltas }))
  1739. {
  1740. return false;
  1741. }
  1742. }
  1743. if (!clothData.empty())
  1744. {
  1745. if (!BuildTypedStreamBuffer<float>(outStreamBuffers, clothData, ClothDataFormat, RHI::ShaderSemantic{ ShaderSemanticName_ClothData }))
  1746. {
  1747. return false;
  1748. }
  1749. }
  1750. lodAssetCreator.SetLodIndexBuffer(outIndexBuffer.GetBufferAsset());
  1751. for (const auto& streamBufferInfo : outStreamBuffers)
  1752. {
  1753. lodAssetCreator.AddLodStreamBuffer(streamBufferInfo.m_bufferAssetView.GetBufferAsset());
  1754. }
  1755. return true;
  1756. }
  1757. bool ModelAssetBuilderComponent::CreateMesh(
  1758. const ProductMeshView& meshView,
  1759. const BufferAssetView& lodIndexBuffer,
  1760. const AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo>& lodStreamBuffers,
  1761. ModelAssetCreator& modelAssetCreator,
  1762. ModelLodAssetCreator& lodAssetCreator,
  1763. const MaterialAssetsByUid& materialAssetsByUid)
  1764. {
  1765. lodAssetCreator.BeginMesh();
  1766. if (meshView.m_materialUid != s_invalidMaterialUid)
  1767. {
  1768. auto iter = materialAssetsByUid.find(meshView.m_materialUid);
  1769. if (iter != materialAssetsByUid.end())
  1770. {
  1771. ModelMaterialSlot materialSlot;
  1772. materialSlot.m_stableId = static_cast<AZ::RPI::ModelMaterialSlot::StableId>(meshView.m_materialUid);
  1773. materialSlot.m_displayName = iter->second.m_name;
  1774. materialSlot.m_defaultMaterialAsset = iter->second.m_asset;
  1775. modelAssetCreator.AddMaterialSlot(materialSlot);
  1776. lodAssetCreator.SetMeshMaterialSlot(materialSlot.m_stableId);
  1777. }
  1778. }
  1779. lodAssetCreator.SetMeshName(meshView.m_name);
  1780. // Set the index stream
  1781. BufferAssetView indexBufferAssetView(lodIndexBuffer.GetBufferAsset(), meshView.m_indexView);
  1782. lodAssetCreator.SetMeshIndexBuffer(AZStd::move(indexBufferAssetView));
  1783. {
  1784. // Build the mesh's Aabb
  1785. ModelLodAsset::Mesh::StreamBufferInfo positionStreamBufferInfo;
  1786. const RHI::ShaderSemantic& positionSemantic = RHI::ShaderSemantic{"POSITION"};
  1787. if (!FindStreamBufferById(lodStreamBuffers, positionSemantic, positionStreamBufferInfo))
  1788. {
  1789. return false;
  1790. }
  1791. const RHI::BufferViewDescriptor& positionBufferViewDescriptor = meshView.m_positionView;
  1792. // Calculate SubMesh's AABB from position stream
  1793. AZ::Aabb subMeshAabb = AZ::Aabb::CreateNull();
  1794. if (CalculateAABB(positionBufferViewDescriptor, *positionStreamBufferInfo.m_bufferAssetView.GetBufferAsset().Get(), subMeshAabb))
  1795. {
  1796. lodAssetCreator.SetMeshAabb(AZStd::move(subMeshAabb));
  1797. }
  1798. else
  1799. {
  1800. AZ_Warning(s_builderName, false, "Failed to calculate AABB for Mesh");
  1801. }
  1802. // Set position buffer
  1803. BufferAssetView meshPositionBufferAssetView(
  1804. positionStreamBufferInfo.m_bufferAssetView.GetBufferAsset(),
  1805. meshView.m_positionView);
  1806. lodAssetCreator.AddMeshStreamBuffer(positionSemantic, AZ::Name(), meshPositionBufferAssetView);
  1807. }
  1808. // Set normal buffer
  1809. if (meshView.m_normalView.m_elementCount > 0)
  1810. {
  1811. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{"NORMAL"}, AZ::Name(), meshView.m_normalView, lodStreamBuffers, lodAssetCreator))
  1812. {
  1813. return false;
  1814. }
  1815. }
  1816. // Set UV buffers
  1817. for (size_t i = 0; i < meshView.m_uvSetViews.size(); ++i)
  1818. {
  1819. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{"UV", i}, meshView.m_uvCustomNames[i], meshView.m_uvSetViews[i], lodStreamBuffers, lodAssetCreator))
  1820. {
  1821. return false;
  1822. }
  1823. }
  1824. // Set Color buffers
  1825. for (size_t i = 0; i < meshView.m_colorSetViews.size(); ++i)
  1826. {
  1827. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{"COLOR", i}, meshView.m_colorCustomNames[i], meshView.m_colorSetViews[i], lodStreamBuffers, lodAssetCreator))
  1828. {
  1829. return false;
  1830. }
  1831. }
  1832. // Set Tangent/Bitangent buffer
  1833. if (meshView.m_tangentView.m_elementCount > 0)
  1834. {
  1835. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{"TANGENT"}, AZ::Name(), meshView.m_tangentView, lodStreamBuffers, lodAssetCreator))
  1836. {
  1837. return false;
  1838. }
  1839. }
  1840. if (meshView.m_bitangentView.m_elementCount > 0)
  1841. {
  1842. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{"BITANGENT"}, AZ::Name(), meshView.m_bitangentView, lodStreamBuffers, lodAssetCreator))
  1843. {
  1844. return false;
  1845. }
  1846. }
  1847. // Set skin buffers
  1848. if (meshView.m_skinJointIndicesView.m_elementCount > 0 && meshView.m_skinWeightsView.m_elementCount > 0)
  1849. {
  1850. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{ShaderSemanticName_SkinJointIndices}, AZ::Name(), meshView.m_skinJointIndicesView, lodStreamBuffers, lodAssetCreator))
  1851. {
  1852. return false;
  1853. }
  1854. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{ShaderSemanticName_SkinWeights}, AZ::Name(), meshView.m_skinWeightsView, lodStreamBuffers, lodAssetCreator))
  1855. {
  1856. return false;
  1857. }
  1858. }
  1859. // Set morph target buffers
  1860. if (meshView.m_morphTargetVertexDataView.m_elementCount > 0)
  1861. {
  1862. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{ShaderSemanticName_MorphTargetDeltas}, AZ::Name(),
  1863. meshView.m_morphTargetVertexDataView, lodStreamBuffers, lodAssetCreator))
  1864. {
  1865. return false;
  1866. }
  1867. }
  1868. // Set cloth data buffer
  1869. if (meshView.m_clothDataView.m_elementCount > 0)
  1870. {
  1871. if (!SetMeshStreamBufferById(RHI::ShaderSemantic{ ShaderSemanticName_ClothData }, AZ::Name(), meshView.m_clothDataView, lodStreamBuffers, lodAssetCreator))
  1872. {
  1873. return false;
  1874. }
  1875. }
  1876. lodAssetCreator.EndMesh();
  1877. return true;
  1878. }
  1879. Outcome<Data::Asset<BufferAsset>> ModelAssetBuilderComponent::CreateTypedBufferAsset(
  1880. const void* data, const size_t elementCount, RHI::Format format, const AZStd::string& bufferName)
  1881. {
  1882. RHI::BufferViewDescriptor bufferViewDescriptor =
  1883. RHI::BufferViewDescriptor::CreateTyped(0, static_cast<uint32_t>(elementCount), format);
  1884. return CreateBufferAsset(data, bufferViewDescriptor, bufferName);
  1885. }
  1886. Outcome<Data::Asset<BufferAsset>> ModelAssetBuilderComponent::CreateStructuredBufferAsset(
  1887. const void* data, const size_t elementCount, const size_t elementSize, const AZStd::string& bufferName)
  1888. {
  1889. RHI::BufferViewDescriptor bufferViewDescriptor =
  1890. RHI::BufferViewDescriptor::CreateStructured(0, static_cast<uint32_t>(elementCount), static_cast<uint32_t>(elementSize));
  1891. return CreateBufferAsset(data, bufferViewDescriptor, bufferName);
  1892. }
  1893. Outcome<Data::Asset<BufferAsset>> ModelAssetBuilderComponent::CreateRawBufferAsset(
  1894. const void* data, const size_t totalSizeInBytes, const AZStd::string& bufferName)
  1895. {
  1896. RHI::BufferViewDescriptor bufferViewDescriptor =
  1897. RHI::BufferViewDescriptor::CreateRaw(0, static_cast<uint32_t>(totalSizeInBytes));
  1898. return CreateBufferAsset(data, bufferViewDescriptor, bufferName);
  1899. }
  1900. Outcome<Data::Asset<BufferAsset>> ModelAssetBuilderComponent::CreateBufferAsset(
  1901. const void* data, const RHI::BufferViewDescriptor& bufferViewDescriptor, const AZStd::string& bufferName)
  1902. {
  1903. BufferAssetCreator creator;
  1904. AZStd::string bufferAssetName = GetAssetFullName(BufferAsset::TYPEINFO_Uuid(), bufferName);
  1905. creator.Begin(CreateAssetId(bufferAssetName));
  1906. RHI::BufferDescriptor bufferDescriptor;
  1907. bufferDescriptor.m_bindFlags = RHI::BufferBindFlags::InputAssembly | RHI::BufferBindFlags::ShaderRead;
  1908. bufferDescriptor.m_byteCount = static_cast<uint64_t>(bufferViewDescriptor.m_elementSize) * static_cast<uint64_t>(bufferViewDescriptor.m_elementCount);
  1909. creator.SetBuffer(data, bufferDescriptor.m_byteCount, bufferDescriptor);
  1910. creator.SetBufferViewDescriptor(bufferViewDescriptor);
  1911. creator.SetPoolAsset({ m_systemInputAssemblyBufferPoolId, azrtti_typeid<RPI::ResourcePoolAsset>() });
  1912. Data::Asset<BufferAsset> bufferAsset;
  1913. if (creator.End(bufferAsset))
  1914. {
  1915. bufferAsset.SetHint(bufferAssetName);
  1916. return AZ::Success(bufferAsset);
  1917. }
  1918. return AZ::Failure();
  1919. }
  1920. bool ModelAssetBuilderComponent::SetMeshStreamBufferById(
  1921. const RHI::ShaderSemantic& semantic,
  1922. const AZ::Name& customName,
  1923. const RHI::BufferViewDescriptor& bufferViewDescriptor,
  1924. const AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo>& lodStreamBuffers,
  1925. ModelLodAssetCreator& lodAssetCreator)
  1926. {
  1927. ModelLodAsset::Mesh::StreamBufferInfo streamBufferInfo;
  1928. if (FindStreamBufferById(lodStreamBuffers, semantic, streamBufferInfo))
  1929. {
  1930. Data::Asset<BufferAsset> bufferAsset = streamBufferInfo.m_bufferAssetView.GetBufferAsset();
  1931. lodAssetCreator.AddMeshStreamBuffer(semantic, customName, { bufferAsset, bufferViewDescriptor });
  1932. return true;
  1933. }
  1934. AZ_Error(s_builderName, false, "Failed to apply the %s buffer to the mesh", semantic.ToString().data());
  1935. return false;
  1936. }
  1937. AZStd::string ModelAssetBuilderComponent::GetAssetFullName(const TypeId& assetType, const AZStd::string& bufferName)
  1938. {
  1939. AZStd::string fullName;
  1940. if (assetType == ModelAsset::TYPEINFO_Uuid())
  1941. {
  1942. fullName = m_modelName;
  1943. }
  1944. else if (assetType == ModelLodAsset::TYPEINFO_Uuid())
  1945. {
  1946. fullName = AZStd::string::format("%s_%s", m_modelName.c_str(), m_lodName.c_str());
  1947. }
  1948. else
  1949. {
  1950. if (m_meshName.empty())
  1951. {
  1952. fullName = AZStd::string::format("%s_%s_%s", m_modelName.c_str(), m_lodName.c_str(), bufferName.c_str());
  1953. }
  1954. else
  1955. {
  1956. fullName = AZStd::string::format("%s_%s_%s_%s", m_modelName.c_str(), m_lodName.c_str(), m_meshName.c_str(), bufferName.c_str());
  1957. }
  1958. }
  1959. return fullName;
  1960. }
  1961. Data::AssetId ModelAssetBuilderComponent::CreateAssetId(const AZStd::string& assetName)
  1962. {
  1963. // The sub id of any model related assets starts with the same prefix 0x10 for first 8 bits
  1964. // And it uses the name hash for the last 24 bits
  1965. static const uint32_t prefix = 0x10000000;
  1966. uint32_t productSubId;
  1967. Data::AssetId assetId;
  1968. assetId.SetInvalid();
  1969. productSubId = prefix | AZ::Crc32(assetName) & 0xffffff;
  1970. if (m_createdSubId.find(productSubId) != m_createdSubId.end())
  1971. {
  1972. AZ_Error("Mesh builder", false, "Duplicate asset sub id for asset [%s]", assetName.c_str());
  1973. return assetId;
  1974. }
  1975. m_createdSubId.insert(productSubId);
  1976. assetId.m_guid = m_sourceUuid;
  1977. assetId.m_subId = productSubId;
  1978. return assetId;
  1979. }
  1980. bool ModelAssetBuilderComponent::CalculateAABB(const RHI::BufferViewDescriptor& bufferViewDesc, const BufferAsset& bufferAsset, AZ::Aabb& aabb)
  1981. {
  1982. const uint32_t elementSize = bufferViewDesc.m_elementSize;
  1983. const uint32_t elementCount = bufferViewDesc.m_elementCount;
  1984. const uint32_t elementOffset = bufferViewDesc.m_elementOffset;
  1985. AZ_Assert(elementOffset + elementCount <= bufferAsset.GetBufferViewDescriptor().m_elementCount, "bufferViewDesc is out of range of bufferAsset");
  1986. // Position is 3 floats
  1987. if (elementSize == sizeof(float) * 3)
  1988. {
  1989. AZ_Assert(bufferViewDesc.m_elementFormat == RHI::Format::R32G32B32_FLOAT, "position buffer format does not match element size");
  1990. struct Position { float x,y,z; };
  1991. const Position* buffer = reinterpret_cast<const Position*>(&bufferAsset.GetBuffer()[0]) + elementOffset;
  1992. AZ::Vector3 vpos; //note: it seems to be fastest to reuse a local Vector3 rather than constructing new ones each loop iteration
  1993. for (uint32_t i = 0; i < elementCount; ++i)
  1994. {
  1995. vpos.Set(reinterpret_cast<const float*>(&buffer[i]));
  1996. aabb.AddPoint(vpos);
  1997. }
  1998. }
  1999. // Position is 4 halfs
  2000. else if (elementSize == sizeof(uint16_t) * 4)
  2001. {
  2002. // Can't handle this yet since we have no way to do math on
  2003. // halfs
  2004. AZ_Error(
  2005. s_builderName, false,
  2006. "Can't calculate AABB for SubMesh; positions stored "
  2007. "in halfs not supported.");
  2008. return false;
  2009. }
  2010. else
  2011. {
  2012. // No idea what type of position stream this is
  2013. AZ_Error(
  2014. s_builderName, false,
  2015. "Can't calculate AABB for SubMesh; can't determine "
  2016. "element type of stream.");
  2017. return false;
  2018. }
  2019. return true;
  2020. }
  2021. ModelAssetBuilderComponent::MaterialUid ModelAssetBuilderComponent::SourceMeshContent::GetMaterialUniqueId(uint32_t index) const
  2022. {
  2023. if (index >= m_materials.size())
  2024. {
  2025. return s_invalidMaterialUid;
  2026. }
  2027. return m_materials[index];
  2028. }
  2029. bool ModelAssetBuilderComponent::FindStreamBufferById(
  2030. const AZStd::vector<ModelLodAsset::Mesh::StreamBufferInfo>& streamBufferInfoList,
  2031. const RHI::ShaderSemantic& streamSemantic,
  2032. ModelLodAsset::Mesh::StreamBufferInfo& outStreamBufferInfo)
  2033. {
  2034. for (const auto& streamBufferInfo : streamBufferInfoList)
  2035. {
  2036. if (streamBufferInfo.m_semantic == streamSemantic)
  2037. {
  2038. outStreamBufferInfo = streamBufferInfo;
  2039. return true;
  2040. }
  2041. }
  2042. AZ_Error(s_builderName, false, "Attempted to find a buffer for stream %s but failed!", streamSemantic.ToString().data());
  2043. return false;
  2044. }
  2045. bool ModelAssetBuilderComponent::GetIsMorphed(const AZ::SceneAPI::Containers::SceneGraph& graph, const AZ::SceneAPI::Containers::SceneGraph::NodeIndex& nodeIndex) const
  2046. {
  2047. // Note: In here we are checking directly in the scene graph. We are also suppose to check if user selected those morph target in blendshape rule, that work
  2048. // will be done when the mesh group support blendshape rule.
  2049. auto contentStorage = graph.GetContentStorage();
  2050. auto downwardsView = AZ::SceneAPI::Containers::Views::MakeSceneGraphDownwardsView<AZ::SceneAPI::Containers::Views::BreadthFirst>(graph, nodeIndex, contentStorage.begin(), true);
  2051. auto filteredView = AZ::SceneAPI::Containers::Views::MakeFilterView(downwardsView, AZ::SceneAPI::Containers::DerivedTypeFilter<AZ::SceneAPI::DataTypes::IBlendShapeData>());
  2052. return (filteredView.begin() != filteredView.end());
  2053. }
  2054. SceneAPI::DataTypes::MatrixType ModelAssetBuilderComponent::GetWorldTransform(const SceneAPI::Containers::SceneGraph& sceneGraph, SceneAPI::Containers::SceneGraph::NodeIndex node)
  2055. {
  2056. // the logic here copies the logic in @AZ::RC::WorldMatrixExporter::ConcatenateMatricesUpwards
  2057. namespace SceneDataTypes = AZ::SceneAPI::DataTypes;
  2058. namespace SceneViews = AZ::SceneAPI::Containers::Views;
  2059. SceneAPI::DataTypes::MatrixType transform = SceneAPI::DataTypes::MatrixType::CreateIdentity();
  2060. const SceneAPI::Containers::SceneGraph::NodeHeader* nodeIterator = sceneGraph.ConvertToHierarchyIterator(node);
  2061. auto upwardsView = SceneViews::MakeSceneGraphUpwardsView(sceneGraph, nodeIterator, sceneGraph.GetContentStorage().cbegin(), true);
  2062. for (auto it = upwardsView.begin(); it != upwardsView.end(); ++it)
  2063. {
  2064. if (!(*it))
  2065. {
  2066. continue;
  2067. }
  2068. const SceneAPI::DataTypes::IGraphObject* nodeTemp = it->get();
  2069. const SceneDataTypes::ITransform* nodeTransform = azrtti_cast<const SceneDataTypes::ITransform*>(nodeTemp);
  2070. if (nodeTransform)
  2071. {
  2072. transform = nodeTransform->GetMatrix() * transform;
  2073. }
  2074. else
  2075. {
  2076. // If the translation is not an end point it means it's its own group as opposed to being
  2077. // a component of the parent, so only list end point children.
  2078. auto view = SceneViews::MakeSceneGraphChildView<SceneViews::AcceptEndPointsOnly>(sceneGraph, it.GetHierarchyIterator(),
  2079. sceneGraph.GetContentStorage().begin(), true);
  2080. auto result = AZStd::find_if(view.begin(), view.end(), SceneAPI::Containers::DerivedTypeFilter<SceneDataTypes::ITransform>());
  2081. if (result != view.end())
  2082. {
  2083. transform = azrtti_cast<const SceneDataTypes::ITransform*>(result->get())->GetMatrix() * transform;
  2084. }
  2085. }
  2086. }
  2087. return transform;
  2088. }
  2089. void ModelAssetDependenciesComponent::Reflect(ReflectContext* context)
  2090. {
  2091. if (auto* serialize = azrtti_cast<SerializeContext*>(context))
  2092. {
  2093. serialize->Class<ModelAssetDependenciesComponent, Component>()
  2094. // If you have made changes to the model code and need to force scene files to reprocess,
  2095. // change the version number in ModelAssetBuilderComponent, not this version number.
  2096. ->Version(0)
  2097. ->Attribute(
  2098. Edit::Attributes::SystemComponentTags, AZStd::vector<Crc32>({ AssetBuilderSDK::ComponentTags::AssetBuilder }));
  2099. }
  2100. }
  2101. void ModelAssetDependenciesComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  2102. {
  2103. provided.push_back(AZ_CRC_CE("ModelAssetDependenciesService"));
  2104. }
  2105. void ModelAssetDependenciesComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  2106. {
  2107. incompatible.push_back(AZ_CRC_CE("ModelAssetDependenciesService"));
  2108. }
  2109. void ModelAssetDependenciesComponent::Activate()
  2110. {
  2111. SceneAPI::SceneBuilderDependencyBus::Handler::BusConnect();
  2112. }
  2113. void ModelAssetDependenciesComponent::Deactivate()
  2114. {
  2115. SceneAPI::SceneBuilderDependencyBus::Handler::BusDisconnect();
  2116. }
  2117. void ModelAssetDependenciesComponent::ReportJobDependencies(
  2118. SceneAPI::JobDependencyList& jobDependencyList, const char* platformIdentifier)
  2119. {
  2120. // Currently, the only implicit job dependency in model building is the dependency on the DefaultVertexBufferPool asset.
  2121. // It needs to get listed here to ensure that models aren't marked as complete and loadable by the engine before the
  2122. // DefaultVertexBufferPool has been processed.
  2123. AssetBuilderSDK::SourceFileDependency defaultVertexBufferPoolSource;
  2124. defaultVertexBufferPoolSource.m_sourceFileDependencyPath = ModelAssetBuilderComponent::s_defaultVertexBufferPoolSourcePath;
  2125. constexpr AZ::u32 ResourcePoolDefaultSubId = 0;
  2126. AssetBuilderSDK::JobDependency jobDependency;
  2127. jobDependency.m_jobKey = "Atom Resource Pool";
  2128. jobDependency.m_sourceFile = defaultVertexBufferPoolSource;
  2129. jobDependency.m_platformIdentifier = platformIdentifier;
  2130. jobDependency.m_productSubIds.push_back(ResourcePoolDefaultSubId);
  2131. jobDependency.m_type = AssetBuilderSDK::JobDependencyType::Order;
  2132. jobDependencyList.push_back(jobDependency);
  2133. }
  2134. } // namespace RPI
  2135. } // namespace AZ