ModelAssetBuilderComponent.cpp 135 KB

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