assetImporter.cpp 135 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222
  1. #include "assetImporter.h"
  2. #include "assetImporter_ScriptBinding.h"
  3. #include "core/strings/findMatch.h"
  4. #include "ImageAsset.h"
  5. #include "ShapeAsset.h"
  6. #include "SoundAsset.h"
  7. #include "MaterialAsset.h"
  8. #include "ShapeAnimationAsset.h"
  9. #include "ts/collada/colladaUtils.h"
  10. #include "ts/collada/colladaAppNode.h"
  11. #include "ts/collada/colladaShapeLoader.h"
  12. #include "ts/assimp/assimpShapeLoader.h"
  13. #include "ts/tsShapeConstruct.h"
  14. #include "core/resourceManager.h"
  15. #include "materials/materialManager.h"
  16. #include "console/persistenceManager.h"
  17. ConsoleDocClass(AssetImportConfig,
  18. "@brief Defines properties for an AssetImprotConfig object.\n"
  19. "@AssetImportConfig is a SimObject derived object intended to act as a container for all the necessary configuration data when running the Asset Importer.\n"
  20. "@It dictates if and how any given asset type will be processed when running an import action. This is because the Asset Importer utilizes a lot of informed logic\n"
  21. "@to try and automate as much of the import process as possible. In theory, you would run the import on a given file, and based on your config the importer will do\n"
  22. "@everything from importing the designated file, as well as finding and importing any associated files such as images or materials, and prepping the objects at time\n"
  23. "@of import to avoid as much manual post-processing as possible.\n\n"
  24. "@ingroup Assets\n"
  25. );
  26. IMPLEMENT_CONOBJECT(AssetImportConfig);
  27. AssetImportConfig::AssetImportConfig() :
  28. DuplicateAutoResolution("AutoRename"),
  29. WarningsAsErrors(false),
  30. PreventImportWithErrors(true),
  31. AutomaticallyPromptMissingFiles(false),
  32. AddDirectoryPrefixToAssetName(false),
  33. ImportMesh(true),
  34. AlwaysAddShapeSuffix(false),
  35. AddedShapeSuffix("_shape"),
  36. UseManualShapeConfigRules(false),
  37. DoUpAxisOverride(false),
  38. UpAxisOverride("Z_AXIS"),
  39. DoScaleOverride(false),
  40. ScaleOverride(false),
  41. IgnoreNodeScale(false),
  42. AdjustCenter(false),
  43. AdjustFloor(false),
  44. CollapseSubmeshes(false),
  45. LODType("TrailingNumber"),
  46. AlwaysImportedNodes(""),
  47. AlwaysIgnoreNodes(""),
  48. AlwaysImportMeshes(""),
  49. AlwaysIgnoreMeshes(""),
  50. convertLeftHanded(false),
  51. calcTangentSpace(false),
  52. removeRedundantMats(false),
  53. genUVCoords(false),
  54. TransformUVs(false),
  55. flipUVCoords(false),
  56. findInstances(false),
  57. limitBoneWeights(false),
  58. JoinIdenticalVerts(false),
  59. reverseWindingOrder(false),
  60. invertNormals(false),
  61. ImportMaterials(true),
  62. AlwaysAddMaterialSuffix(true),
  63. AddedMaterialSuffix("_mat"),
  64. CreateORMConfig(true),
  65. UseDiffuseSuffixOnOriginImage(false),
  66. UseExistingMaterials(false),
  67. IgnoreMaterials(""),
  68. PopulateMaterialMaps(true),
  69. ImportAnimations(true),
  70. SeparateAnimations(false),
  71. SeparateAnimationPrefix(""),
  72. animTiming("FrameCount"),
  73. animFPS(false),
  74. GenerateCollisions(false),
  75. GenCollisionType(""),
  76. CollisionMeshPrefix(""),
  77. GenerateLOSCollisions(false),
  78. GenLOSCollisionType(""),
  79. LOSCollisionMeshPrefix(""),
  80. importImages(true),
  81. AlwaysAddImageSuffix(true),
  82. AddedImageSuffix("_image"),
  83. ImageType("GUI"),
  84. DiffuseTypeSuffixes("_ALBEDO,_DIFFUSE,_ALB,_DIF,_COLOR,_COL,_A,_C,-ALBEDO,-DIFFUSE,-ALB,-DIF,-COLOR,-COL,-A,-C"),
  85. NormalTypeSuffixes("_NORMAL,_NORM,_N,-NORMAL,-NORM,-N"),
  86. MetalnessTypeSuffixes("_METAL,_MET,_METALNESS,_METALLIC,_M,-METAL,-MET,-METALNESS,-METALLIC,-M"),
  87. RoughnessTypeSuffixes("_ROUGH,_ROUGHNESS,_R,-ROUGH,-ROUGHNESS,-R"),
  88. SmoothnessTypeSuffixes("_SMOOTH,_SMOOTHNESS,_S,-SMOOTH,-SMOOTHNESS,-S"),
  89. AOTypeSuffixes("_AO,_AMBIENT,_AMBIENTOCCLUSION,-AO,-AMBIENT,-AMBIENTOCCLUSION"),
  90. PBRTypeSuffixes("_COMP,_COMPOSITE,_PBR,-COMP,-COMPOSITE,-PBR,_ORM,-ORM"),
  91. TextureFilteringMode("Bilinear"),
  92. UseMips(true),
  93. IsHDR(false),
  94. Scaling(false),
  95. ImagesCompressed(false),
  96. GenerateMaterialOnImport(true),
  97. importSounds(true),
  98. VolumeAdjust(false),
  99. PitchAdjust(false),
  100. SoundsCompressed(false),
  101. AlwaysAddSoundSuffix(false),
  102. AddedSoundSuffix("_sound")
  103. {
  104. }
  105. AssetImportConfig::~AssetImportConfig()
  106. {
  107. }
  108. bool AssetImportConfig::onAdd()
  109. {
  110. if (!Parent::onAdd())
  111. return false;
  112. return true;
  113. }
  114. void AssetImportConfig::onRemove()
  115. {
  116. Parent::onRemove();
  117. }
  118. /// Engine.
  119. void AssetImportConfig::initPersistFields()
  120. {
  121. Parent::initPersistFields();
  122. addGroup("General");
  123. addField("DuplicateAutoResolution", TypeRealString, Offset(DuplicateAutoResolution, AssetImportConfig), "Duplicate Asset Auto-Resolution Action. Options are None, AutoPrune, AutoRename, FolderPrefix");
  124. addField("WarningsAsErrors", TypeBool, Offset(WarningsAsErrors, AssetImportConfig), "Indicates if warnings should be treated as errors");
  125. addField("PreventImportWithErrors", TypeBool, Offset(PreventImportWithErrors, AssetImportConfig), "Indicates if importing should be prevented from completing if any errors are detected at all");
  126. addField("AutomaticallyPromptMissingFiles", TypeBool, Offset(AutomaticallyPromptMissingFiles, AssetImportConfig), "Should the importer automatically prompt to find missing files if they are not detected automatically by the importer");
  127. addField("AddDirectoryPrefixToAssetName", TypeBool, Offset(AddDirectoryPrefixToAssetName, AssetImportConfig), "Should the importer add the folder name as a prefix to the assetName. Helps prevent name collisions.");
  128. endGroup("General");
  129. addGroup("Meshes");
  130. addField("ImportMesh", TypeBool, Offset(ImportMesh, AssetImportConfig), "Indicates if this config supports importing meshes");
  131. addField("AlwaysAddShapeSuffix", TypeBool, Offset(AlwaysAddShapeSuffix, AssetImportConfig), "When importing a shape, this indicates if it should automatically add a standard suffix onto the name");
  132. addField("AddedShapeSuffix", TypeString, Offset(AddedShapeSuffix, AssetImportConfig), " If AlwaysAddShapeSuffix is on, this is the suffix to be added");
  133. addField("UseManualShapeConfigRules", TypeBool, Offset(UseManualShapeConfigRules, AssetImportConfig), "Indicates if this config should override the per-format sis files with the config's specific settings");
  134. addField("DoUpAxisOverride", TypeBool, Offset(DoUpAxisOverride, AssetImportConfig), "Indicates if the up axis in the model file should be overridden");
  135. addField("UpAxisOverride", TypeRealString, Offset(UpAxisOverride, AssetImportConfig), "If overriding, what axis should be used as up. Options are X_AXIS, Y_AXIS, Z_AXIS");
  136. addField("DoScaleOverride", TypeBool, Offset(DoScaleOverride, AssetImportConfig), "Indicates if the scale in the model file should be overridden");
  137. addField("ScaleOverride", TypeF32, Offset(ScaleOverride, AssetImportConfig), "If overriding, what scale should be used");
  138. addField("IgnoreNodeScale", TypeBool, Offset(IgnoreNodeScale, AssetImportConfig), "Indicates if scale of nodes should be ignored");
  139. addField("AdjustCenter", TypeBool, Offset(AdjustCenter, AssetImportConfig), "Indicates if the center of the model file should be automatically recentered");
  140. addField("AdjustFloor", TypeBool, Offset(AdjustFloor, AssetImportConfig), "Indicates if the floor height of the model file should be automatically zero'd");
  141. addField("CollapseSubmeshes", TypeBool, Offset(CollapseSubmeshes, AssetImportConfig), "Indicates if submeshes should be collapsed down into a single main mesh");
  142. addField("LODType", TypeRealString, Offset(LODType, AssetImportConfig), "Indicates what LOD mode the model file should utilize to process out LODs. Options are TrailingNumber, DetectDTS, SingleSize");
  143. addField("AlwaysImportedNodes", TypeRealString, Offset(AlwaysImportedNodes, AssetImportConfig), " A list of what nodes should be guaranteed to be imported if found in the model file. Separated by either , or ;");
  144. addField("AlwaysIgnoreNodes", TypeRealString, Offset(AlwaysIgnoreNodes, AssetImportConfig), "A list of what nodes should be guaranteed to not be imported if found in the model file. Separated by either , or ;");
  145. addField("AlwaysImportMeshes", TypeRealString, Offset(AlwaysImportMeshes, AssetImportConfig), "A list of what mesh objects should be guaranteed to be imported if found in the model file. Separated by either , or ;");
  146. addField("AlwaysIgnoreMeshes", TypeRealString, Offset(AlwaysIgnoreMeshes, AssetImportConfig), "A list of what mesh objects should be guaranteed to not be imported if found in the model file. Separated by either , or ;");
  147. addField("convertLeftHanded", TypeBool, Offset(convertLeftHanded, AssetImportConfig), "Flag to indicate the shape loader should convert to a left-handed coordinate system");
  148. addField("calcTangentSpace", TypeBool, Offset(calcTangentSpace, AssetImportConfig), "Should the shape loader calculate tangent space values");
  149. addField("removeRedundantMats", TypeBool, Offset(removeRedundantMats, AssetImportConfig), "Should the shape loader automatically prune redundant/duplicate materials");
  150. addField("genUVCoords", TypeBool, Offset(genUVCoords, AssetImportConfig), "Should the shape loader auto-generate UV Coordinates for the mesh.");
  151. addField("TransformUVs", TypeBool, Offset(TransformUVs, AssetImportConfig), "Should the UV coordinates be transformed");
  152. addField("flipUVCoords", TypeBool, Offset(flipUVCoords, AssetImportConfig), "Should the UV coordinates be flipped");
  153. addField("findInstances", TypeBool, Offset(findInstances, AssetImportConfig), "Should the shape loader automatically look for instanced submeshes in the model file");
  154. addField("limitBoneWeights", TypeBool, Offset(limitBoneWeights, AssetImportConfig), "Should the shape loader limit the bone weights");
  155. addField("JoinIdenticalVerts", TypeBool, Offset(JoinIdenticalVerts, AssetImportConfig), "Should the shape loader automatically merge identical/duplicate verts");
  156. addField("reverseWindingOrder", TypeBool, Offset(reverseWindingOrder, AssetImportConfig), "Should the shape loader reverse the winding order of the mesh's face indicies");
  157. addField("invertNormals", TypeBool, Offset(invertNormals, AssetImportConfig), "Should the normals on the model be inverted");
  158. endGroup("Meshes");
  159. addGroup("Materials");
  160. addField("ImportMaterials", TypeBool, Offset(ImportMaterials, AssetImportConfig), "Does this config allow for importing of materials");
  161. addField("AlwaysAddMaterialSuffix", TypeBool, Offset(AlwaysAddMaterialSuffix, AssetImportConfig), "When importing a material, this indicates if it should automatically add a standard suffix onto the name");
  162. addField("AddedMaterialSuffix", TypeString, Offset(AddedMaterialSuffix, AssetImportConfig), " If AlwaysAddMaterialSuffix is on, this is the suffix to be added");
  163. addField("CreateORMConfig", TypeBool, Offset(PreventImportWithErrors, AssetImportConfig), "When importing a material, should it automatically attempt to merge Roughness, AO and Metalness maps into a single, composited PBR Configuration map");
  164. addField("UseDiffuseSuffixOnOriginImage", TypeBool, Offset(UseDiffuseSuffixOnOriginImage, AssetImportConfig), "When generating a material off of an importing image, should the importer force appending a diffusemap suffix onto the end to avoid potential naming confusion.\n e.g. MyCoolStuff.png is imported, generating MyCoolStuff material asset and MyCoolStuff_Diffuse image asset");
  165. addField("UseExistingMaterials", TypeBool, Offset(UseExistingMaterials, AssetImportConfig), "Should the importer try and use existing material assets in the game directory if at all possible. (Not currently utilized)");
  166. addField("IgnoreMaterials", TypeRealString, Offset(IgnoreMaterials, AssetImportConfig), "A list of material names that should not be imported. Separated by either , or ;");
  167. addField("PopulateMaterialMaps", TypeBool, Offset(PopulateMaterialMaps, AssetImportConfig), "When processing a material asset, should the importer attempt to populate the various material maps on it by looking up common naming conventions for potentially relevent image files.\n e.g. If MyCoolStuff_Diffuse.png is imported, generating MyCoolStuff material, it would also find MyCoolStuff_Normal and MyCoolStuff_PBR images and map them to the normal and ORMConfig maps respectively automatically");
  168. endGroup("Materials");
  169. addGroup("Animation");
  170. addField("ImportAnimations", TypeBool, Offset(ImportAnimations, AssetImportConfig), "Does this config allow for importing Shape Animations");
  171. addField("SeparateAnimations", TypeBool, Offset(SeparateAnimations, AssetImportConfig), "When importing a shape file, should the animations within be separated out into unique files");
  172. addField("SeparateAnimationPrefix", TypeRealString, Offset(SeparateAnimationPrefix, AssetImportConfig), "If separating animations out from a source file, what prefix should be added to the names for grouping association");
  173. addField("animTiming", TypeRealString, Offset(animTiming, AssetImportConfig), "Defines the animation timing for the given animation sequence. Options are FrameTime, Seconds, Milliseconds");
  174. addField("animFPS", TypeBool, Offset(animFPS, AssetImportConfig), "The FPS of the animation sequence");
  175. endGroup("Animation");
  176. addGroup("Collision");
  177. addField("GenerateCollisions", TypeBool, Offset(GenerateCollisions, AssetImportConfig), "Does this configuration generate collision geometry when importing. (Not currently enabled)");
  178. addField("GenCollisionType", TypeRealString, Offset(GenCollisionType, AssetImportConfig), "What sort of collision geometry is generated. (Not currently enabled)");
  179. addField("CollisionMeshPrefix", TypeRealString, Offset(CollisionMeshPrefix, AssetImportConfig), "What prefix is added to the collision geometry generated. (Not currently enabled)");
  180. addField("GenerateLOSCollisions", TypeBool, Offset(GenerateLOSCollisions, AssetImportConfig), "Does this configuration generate Line of Sight collision geometry. (Not currently enabled)");
  181. addField("GenLOSCollisionType", TypeRealString, Offset(GenLOSCollisionType, AssetImportConfig), "What sort of Line of Sight collision geometry is generated. (Not currently enabled)");
  182. addField("LOSCollisionMeshPrefix", TypeRealString, Offset(LOSCollisionMeshPrefix, AssetImportConfig), "What prefix is added to the Line of Sight collision geometry generated. (Not currently enabled)");
  183. endGroup("Collision");
  184. addGroup("Images");
  185. addField("importImages", TypeBool, Offset(importImages, AssetImportConfig), "Does this configuration support importing images.");
  186. addField("AlwaysAddImageSuffix", TypeBool, Offset(AlwaysAddImageSuffix, AssetImportConfig), "When importing an image, this indicates if it should automatically add a standard suffix onto the name");
  187. addField("AddedImageSuffix", TypeString, Offset(AddedImageSuffix, AssetImportConfig), " If AlwaysAddImageSuffix is on, this is the suffix to be added");
  188. addField("ImageType", TypeRealString, Offset(ImageType, AssetImportConfig), "What is the default ImageType images are imported as. Options are: N/A, Diffuse, Normal, Metalness, Roughness, AO, ORMConfig, GUI, Cubemap");
  189. addField("DiffuseTypeSuffixes", TypeRealString, Offset(DiffuseTypeSuffixes, AssetImportConfig), "What type of suffixes are scanned to detect if an importing image is a diffuse map. \n e.g. _Albedo or _Color");
  190. addField("NormalTypeSuffixes", TypeRealString, Offset(NormalTypeSuffixes, AssetImportConfig), "What type of suffixes are scanned to detect if an importing image is a normal map. \n e.g. _Normal or _Norm");
  191. addField("MetalnessTypeSuffixes", TypeRealString, Offset(MetalnessTypeSuffixes, AssetImportConfig), "What type of suffixes are scanned to detect if an importing image is a metalness map. \n e.g. _Metalness or _Metal");
  192. addField("RoughnessTypeSuffixes", TypeRealString, Offset(RoughnessTypeSuffixes, AssetImportConfig), "What type of suffixes are scanned to detect if an importing image is a roughness map.\n e.g. _roughness or _rough");
  193. addField("SmoothnessTypeSuffixes", TypeRealString, Offset(SmoothnessTypeSuffixes, AssetImportConfig), "What type of suffixes are scanned to detect if an importing image is a smoothness map. \n e.g. _smoothness or _smooth");
  194. addField("AOTypeSuffixes", TypeRealString, Offset(AOTypeSuffixes, AssetImportConfig), "What type of suffixes are scanned to detect if an importing image is a ambient occlusion map. \n e.g. _ambient or _ao");
  195. addField("PBRTypeSuffixes", TypeRealString, Offset(PBRTypeSuffixes, AssetImportConfig), "What type of suffixes are scanned to detect if an importing image is a ORMConfig map.\n e.g. _Composite or _PBR");
  196. addField("TextureFilteringMode", TypeRealString, Offset(TextureFilteringMode, AssetImportConfig), "Indicates what filter mode images imported with this configuration utilizes. Options are Linear, Bilinear, Trilinear");
  197. addField("UseMips", TypeBool, Offset(UseMips, AssetImportConfig), "Indicates if images imported with this configuration utilize mipmaps");
  198. addField("IsHDR", TypeBool, Offset(IsHDR, AssetImportConfig), "Indicates if images imported with this configuration are in an HDR format");
  199. addField("Scaling", TypeF32, Offset(Scaling, AssetImportConfig), "Indicates what amount of scaling images imported with this configuration use");
  200. addField("ImagesCompressed", TypeBool, Offset(ImagesCompressed, AssetImportConfig), "Indicates if images imported with this configuration are compressed");
  201. addField("GenerateMaterialOnImport", TypeBool, Offset(GenerateMaterialOnImport, AssetImportConfig), "Indicates if images imported with this configuration generate a parent material for it as well");
  202. endGroup("Images");
  203. addGroup("Sounds");
  204. addField("importSounds", TypeBool, Offset(importSounds, AssetImportConfig), "Indicates if sounds are imported with this configuration");
  205. addField("VolumeAdjust", TypeF32, Offset(VolumeAdjust, AssetImportConfig), "Indicates what amount the volume is adjusted on sounds imported with this configuration");
  206. addField("PitchAdjust", TypeF32, Offset(PitchAdjust, AssetImportConfig), "Indicates what amount the pitch is adjusted on sounds imported with this configuration");
  207. addField("SoundsCompressed", TypeBool, Offset(SoundsCompressed, AssetImportConfig), "Indicates if sounds imported with this configuration are compressed");
  208. endGroup("Sounds");
  209. }
  210. void AssetImportConfig::loadImportConfig(Settings* configSettings, String configName)
  211. {
  212. //General
  213. DuplicateAutoResolution = configSettings->value(String(configName + "/General/DuplicateAutoResolution").c_str());
  214. WarningsAsErrors = dAtob(configSettings->value(String(configName + "/General/WarningsAsErrors").c_str()));
  215. PreventImportWithErrors = dAtob(configSettings->value(String(configName + "/General/PreventImportWithErrors").c_str()));
  216. AutomaticallyPromptMissingFiles = dAtob(configSettings->value(String(configName + "/General/AutomaticallyPromptMissingFiles").c_str()));
  217. AddDirectoryPrefixToAssetName = dAtob(configSettings->value(String(configName + "/General/AddDirectoryPrefixToAssetName").c_str()));
  218. //Meshes
  219. ImportMesh = dAtob(configSettings->value(String(configName + "/Meshes/ImportMesh").c_str()));
  220. AlwaysAddShapeSuffix = dAtob(configSettings->value(String(configName + "/Meshes/AlwaysAddShapeSuffix").c_str()));
  221. AddedShapeSuffix = configSettings->value(String(configName + "/Meshes/AddedShapeSuffix").c_str());
  222. UseManualShapeConfigRules = dAtob(configSettings->value(String(configName + "/Meshes/UseManualShapeConfigRules").c_str()));
  223. DoUpAxisOverride = dAtob(configSettings->value(String(configName + "/Meshes/DoUpAxisOverride").c_str()));
  224. UpAxisOverride = configSettings->value(String(configName + "/Meshes/UpAxisOverride").c_str());
  225. DoScaleOverride = dAtob(configSettings->value(String(configName + "/Meshes/DoScaleOverride").c_str()));
  226. ScaleOverride = dAtof(configSettings->value(String(configName + "/Meshes/ScaleOverride").c_str()));
  227. IgnoreNodeScale = dAtob(configSettings->value(String(configName + "/Meshes/IgnoreNodeScale").c_str()));
  228. AdjustCenter = dAtob(configSettings->value(String(configName + "/Meshes/AdjustCenter").c_str()));
  229. AdjustFloor = dAtob(configSettings->value(String(configName + "/Meshes/AdjustFloor").c_str()));
  230. CollapseSubmeshes = dAtob(configSettings->value(String(configName + "/Meshes/CollapseSubmeshes").c_str()));
  231. LODType = configSettings->value(String(configName + "/Meshes/LODType").c_str());
  232. AlwaysImportedNodes = configSettings->value(String(configName + "/Meshes/AlwaysImportedNodes").c_str());
  233. AlwaysIgnoreNodes = configSettings->value(String(configName + "/Meshes/AlwaysIgnoreNodes").c_str());
  234. AlwaysImportMeshes = configSettings->value(String(configName + "/Meshes/AlwaysImportMeshes").c_str());
  235. AlwaysIgnoreMeshes = configSettings->value(String(configName + "/Meshes/AlwaysIgnoreMeshes").c_str());
  236. //Assimp/Collada
  237. convertLeftHanded = dAtob(configSettings->value(String(configName + "/Meshes/convertLeftHanded").c_str()));
  238. calcTangentSpace = dAtob(configSettings->value(String(configName + "/Meshes/calcTangentSpace").c_str()));
  239. removeRedundantMats = dAtob(configSettings->value(String(configName + "/Meshes/removeRedundantMats").c_str()));
  240. genUVCoords = dAtob(configSettings->value(String(configName + "/Meshes/genUVCoords").c_str()));
  241. TransformUVs = dAtob(configSettings->value(String(configName + "/Meshes/TransformUVs").c_str()));
  242. flipUVCoords = dAtob(configSettings->value(String(configName + "/Meshes/flipUVCoords").c_str()));
  243. findInstances = dAtob(configSettings->value(String(configName + "/Meshes/findInstances").c_str()));
  244. limitBoneWeights = dAtob(configSettings->value(String(configName + "/Meshes/limitBoneWeights").c_str()));
  245. JoinIdenticalVerts = dAtob(configSettings->value(String(configName + "/Meshes/JoinIdenticalVerts").c_str()));
  246. reverseWindingOrder = dAtob(configSettings->value(String(configName + "/Meshes/reverseWindingOrder").c_str()));
  247. invertNormals = dAtob(configSettings->value(String(configName + "/Meshes/invertNormals").c_str()));
  248. //Materials
  249. ImportMaterials = dAtob(configSettings->value(String(configName + "/Materials/ImportMaterials").c_str()));
  250. AlwaysAddMaterialSuffix = dAtob(configSettings->value(String(configName + "/Materials/AlwaysAddMaterialSuffix").c_str()));
  251. AddedMaterialSuffix = configSettings->value(String(configName + "/Materials/AddedMaterialSuffix").c_str());
  252. CreateORMConfig = dAtob(configSettings->value(String(configName + "/Materials/CreateORMConfig").c_str()));
  253. UseDiffuseSuffixOnOriginImage = dAtob(configSettings->value(String(configName + "/Materials/UseDiffuseSuffixOnOriginImage").c_str()));
  254. UseExistingMaterials = dAtob(configSettings->value(String(configName + "/Materials/UseExistingMaterials").c_str()));
  255. IgnoreMaterials = configSettings->value(String(configName + "/Materials/IgnoreMaterials").c_str());
  256. PopulateMaterialMaps = dAtob(configSettings->value(String(configName + "/Materials/PopulateMaterialMaps").c_str()));
  257. //Animations
  258. ImportAnimations = dAtob(configSettings->value(String(configName + "/Animations/ImportAnimations").c_str()));
  259. SeparateAnimations = dAtob(configSettings->value(String(configName + "/Animations/SeparateAnimations").c_str()));
  260. SeparateAnimationPrefix = configSettings->value(String(configName + "/Animations/SeparateAnimationPrefix").c_str());
  261. animTiming = configSettings->value(String(configName + "/Animations/animTiming").c_str());
  262. animFPS = dAtof(configSettings->value(String(configName + "/Animations/animFPS").c_str()));
  263. //Collisions
  264. GenerateCollisions = dAtob(configSettings->value(String(configName + "/Collision/GenerateCollisions").c_str()));
  265. GenCollisionType = configSettings->value(String(configName + "/Collision/GenCollisionType").c_str());
  266. CollisionMeshPrefix = configSettings->value(String(configName + "/Collision/CollisionMeshPrefix").c_str());
  267. GenerateLOSCollisions = dAtob(configSettings->value(String(configName + "/Collision/GenerateLOSCollisions").c_str()));
  268. GenLOSCollisionType = configSettings->value(String(configName + "/Collision/GenLOSCollisionType").c_str());
  269. LOSCollisionMeshPrefix = configSettings->value(String(configName + "/Collision/LOSCollisionMeshPrefix").c_str());
  270. //Images
  271. importImages = dAtob(configSettings->value(String(configName + "/Images/importImages").c_str()));
  272. AlwaysAddImageSuffix = dAtob(configSettings->value(String(configName + "/Images/AlwaysAddImageSuffix").c_str()));
  273. AddedImageSuffix = configSettings->value(String(configName + "/Images/AddedImageSuffix").c_str());
  274. ImageType = configSettings->value(String(configName + "/Images/ImageType").c_str());
  275. DiffuseTypeSuffixes = configSettings->value(String(configName + "/Images/DiffuseTypeSuffixes").c_str());
  276. NormalTypeSuffixes = configSettings->value(String(configName + "/Images/NormalTypeSuffixes").c_str());
  277. MetalnessTypeSuffixes = configSettings->value(String(configName + "/Images/MetalnessTypeSuffixes").c_str());
  278. RoughnessTypeSuffixes = configSettings->value(String(configName + "/Images/RoughnessTypeSuffixes").c_str());
  279. SmoothnessTypeSuffixes = configSettings->value(String(configName + "/Images/SmoothnessTypeSuffixes").c_str());
  280. AOTypeSuffixes = configSettings->value(String(configName + "/Images/AOTypeSuffixes").c_str());
  281. PBRTypeSuffixes = configSettings->value(String(configName + "/Images/PBRTypeSuffixes").c_str());
  282. TextureFilteringMode = configSettings->value(String(configName + "/Images/TextureFilteringMode").c_str());
  283. UseMips = dAtob(configSettings->value(String(configName + "/Images/UseMips").c_str()));
  284. IsHDR = dAtob(configSettings->value(String(configName + "/Images/IsHDR").c_str()));
  285. Scaling = dAtof(configSettings->value(String(configName + "/Images/Scaling").c_str()));
  286. ImagesCompressed = dAtob(configSettings->value(String(configName + "/Images/Compressed").c_str()));
  287. GenerateMaterialOnImport = dAtob(configSettings->value(String(configName + "/Images/GenerateMaterialOnImport").c_str()));
  288. //Sounds
  289. VolumeAdjust = dAtof(configSettings->value(String(configName + "/Sounds/VolumeAdjust").c_str()));
  290. PitchAdjust = dAtof(configSettings->value(String(configName + "/Sounds/PitchAdjust").c_str()));
  291. SoundsCompressed = dAtob(configSettings->value(String(configName + "/Sounds/Compressed").c_str()));
  292. AlwaysAddSoundSuffix = dAtob(configSettings->value(String(configName + "/Sounds/AlwaysAddSoundSuffix").c_str()));
  293. AddedSoundSuffix = configSettings->value(String(configName + "/Sounds/AddedSoundSuffix").c_str());
  294. }
  295. void AssetImportConfig::CopyTo(AssetImportConfig* target) const
  296. {
  297. target->DuplicateAutoResolution = DuplicateAutoResolution;
  298. target->WarningsAsErrors = WarningsAsErrors;
  299. target->PreventImportWithErrors = PreventImportWithErrors;
  300. target->AutomaticallyPromptMissingFiles = AutomaticallyPromptMissingFiles;
  301. target->AddDirectoryPrefixToAssetName = AddDirectoryPrefixToAssetName;
  302. //Meshes
  303. target->ImportMesh = ImportMesh;
  304. target->AlwaysAddShapeSuffix = AlwaysAddShapeSuffix;
  305. target->AddedShapeSuffix = AddedShapeSuffix;
  306. target->UseManualShapeConfigRules = UseManualShapeConfigRules;
  307. target->DoUpAxisOverride = DoUpAxisOverride;
  308. target->UpAxisOverride = UpAxisOverride;
  309. target->DoScaleOverride = DoScaleOverride;
  310. target->ScaleOverride = ScaleOverride;
  311. target->IgnoreNodeScale = IgnoreNodeScale;
  312. target->AdjustCenter = AdjustCenter;
  313. target->AdjustFloor = AdjustFloor;
  314. target->CollapseSubmeshes = CollapseSubmeshes;
  315. target->LODType = LODType;
  316. target->AlwaysImportedNodes = AlwaysImportedNodes;
  317. target->AlwaysIgnoreNodes = AlwaysIgnoreNodes;
  318. target->AlwaysImportMeshes = AlwaysImportMeshes;
  319. target->AlwaysIgnoreMeshes = AlwaysIgnoreMeshes;
  320. //Assimp/Collada
  321. target->convertLeftHanded = convertLeftHanded;
  322. target->calcTangentSpace = calcTangentSpace;
  323. target->removeRedundantMats = removeRedundantMats;
  324. target->genUVCoords = genUVCoords;
  325. target->TransformUVs = TransformUVs;
  326. target->flipUVCoords = flipUVCoords;
  327. target->findInstances = findInstances;
  328. target->limitBoneWeights = limitBoneWeights;
  329. target->JoinIdenticalVerts = JoinIdenticalVerts;
  330. target->reverseWindingOrder = reverseWindingOrder;
  331. target->invertNormals = invertNormals;
  332. //Materials
  333. target->ImportMaterials = ImportMaterials;
  334. target->AlwaysAddMaterialSuffix = AlwaysAddMaterialSuffix;
  335. target->AddedMaterialSuffix = AddedMaterialSuffix;
  336. target->CreateORMConfig = CreateORMConfig;
  337. target->UseDiffuseSuffixOnOriginImage = UseDiffuseSuffixOnOriginImage;
  338. target->UseExistingMaterials = UseExistingMaterials;
  339. target->IgnoreMaterials = IgnoreMaterials;
  340. target->PopulateMaterialMaps = PopulateMaterialMaps;
  341. //Animations
  342. target->ImportAnimations = ImportAnimations;
  343. target->SeparateAnimations = SeparateAnimations;
  344. target->SeparateAnimationPrefix = SeparateAnimationPrefix;
  345. target->animTiming = animTiming;
  346. target->animFPS = animFPS;
  347. //Collisions
  348. target->GenerateCollisions = GenerateCollisions;
  349. target->GenCollisionType = GenCollisionType;
  350. target->CollisionMeshPrefix = CollisionMeshPrefix;
  351. target->GenerateLOSCollisions = GenerateLOSCollisions;
  352. target->GenLOSCollisionType = GenLOSCollisionType;
  353. target->LOSCollisionMeshPrefix = LOSCollisionMeshPrefix;
  354. //Images
  355. target->importImages = importImages;
  356. target->AlwaysAddImageSuffix = AlwaysAddImageSuffix;
  357. target->AddedImageSuffix = AddedImageSuffix;
  358. target->ImageType = ImageType;
  359. target->DiffuseTypeSuffixes = DiffuseTypeSuffixes;
  360. target->NormalTypeSuffixes = NormalTypeSuffixes;
  361. target->MetalnessTypeSuffixes = MetalnessTypeSuffixes;
  362. target->RoughnessTypeSuffixes = RoughnessTypeSuffixes;
  363. target->SmoothnessTypeSuffixes = SmoothnessTypeSuffixes;
  364. target->AOTypeSuffixes = AOTypeSuffixes;
  365. target->PBRTypeSuffixes = PBRTypeSuffixes;
  366. target->TextureFilteringMode = TextureFilteringMode;
  367. target->UseMips = UseMips;
  368. target->IsHDR = IsHDR;
  369. target->Scaling = Scaling;
  370. target->ImagesCompressed = ImagesCompressed;
  371. target->GenerateMaterialOnImport = GenerateMaterialOnImport;
  372. //Sounds
  373. target->VolumeAdjust = VolumeAdjust;
  374. target->PitchAdjust = PitchAdjust;
  375. target->SoundsCompressed = SoundsCompressed;
  376. target->AlwaysAddSoundSuffix = AlwaysAddSoundSuffix;
  377. target->AddedSoundSuffix = AddedSoundSuffix;
  378. }
  379. ConsoleDocClass(AssetImportObject,
  380. "@brief Defines properties for an AssetImportObject object.\n"
  381. "@AssetImportObject is a SimObject derived object intended to act as a stand-in for the to-be imported objects.\n"
  382. "@It contains important info such as dependencies, if it's been processed, any error/status issues and the originating file\n"
  383. "@or if it's been programmatically generated as part of the import process.\n\n"
  384. "@ingroup Assets\n"
  385. );
  386. IMPLEMENT_CONOBJECT(AssetImportObject);
  387. AssetImportObject::AssetImportObject() :
  388. dirty(false),
  389. importStatus(AssetImportObject::NotProcessed),
  390. generatedAsset(false),
  391. parentAssetItem(nullptr),
  392. tamlFilePath(""),
  393. imageSuffixType(""),
  394. shapeInfo(nullptr),
  395. filePathString(StringTable->EmptyString())
  396. {
  397. }
  398. AssetImportObject::~AssetImportObject()
  399. {
  400. }
  401. bool AssetImportObject::onAdd()
  402. {
  403. if (!Parent::onAdd())
  404. return false;
  405. return true;
  406. }
  407. void AssetImportObject::onRemove()
  408. {
  409. Parent::onRemove();
  410. }
  411. void AssetImportObject::initPersistFields()
  412. {
  413. Parent::initPersistFields();
  414. addField("assetType", TypeRealString, Offset(assetType, AssetImportObject), "What type is the importing asset");
  415. addProtectedField("filePath", TypeFilename, Offset(filePathString, AssetImportObject), &_setFilePath, &defaultProtectedGetFn, "What is the source file path of the importing asset");
  416. addField("assetName", TypeRealString, Offset(assetName, AssetImportObject), "What is the asset's name");
  417. addField("cleanAssetName", TypeRealString, Offset(cleanAssetName, AssetImportObject), "What is the original, unmodified by processing, asset name");
  418. addField("status", TypeRealString, Offset(status, AssetImportObject), "What is the current status of this asset item in it's import process");
  419. addField("statusType", TypeRealString, Offset(statusType, AssetImportObject), "If there is a warning or error status, what type is the condition for this asset item");
  420. addField("statusInfo", TypeRealString, Offset(statusInfo, AssetImportObject), "What is the articulated information of the status of the asset. Contains the error or warning log data");
  421. addField("dirty", TypeBool, Offset(dirty, AssetImportObject), "Is the asset item currently flagged as dirty");
  422. addField("generatedAsset", TypeBool, Offset(generatedAsset, AssetImportObject), "Is this specific asset item generated as part of the import process of another item");
  423. addField("tamlFilePath", TypeRealString, Offset(tamlFilePath, AssetImportObject), "What is the ultimate asset taml file path for this import item");
  424. addField("imageType", TypeRealString, Offset(imageSuffixType, AssetImportObject), "Specific to ImageAsset type. What is the image asset's suffix type. Options are: Albedo, Normal, Roughness, AO, Metalness, ORMConfig");
  425. addField("shapeInfo", TYPEID< GuiTreeViewCtrl >(), Offset(shapeInfo, AssetImportObject), "Specific to ShapeAsset type. Processed information about the shape file. Contains numbers and lists of meshes, materials and animations");
  426. }
  427. bool AssetImportObject::_setFilePath(void* obj, const char* index, const char* data)
  428. {
  429. AssetImportObject* importObj = static_cast<AssetImportObject*>(obj);
  430. importObj->setFilePath(StringTable->insert(data));
  431. return false;
  432. }
  433. void AssetImportObject::setFilePath(StringTableEntry pFilePath)
  434. {
  435. filePathString = pFilePath;
  436. filePath = Torque::Path(pFilePath);
  437. }
  438. ConsoleDocClass(AssetImporter,
  439. "@brief Defines properties for an AssetImportObject object.\n"
  440. "@AssetImportObject is a SimObject derived object intended to act as a stand-in for the to-be imported objects.\n"
  441. "@It contains important info such as dependencies, if it's been processed, any error/status issues and the originating file\n"
  442. "@or if it's been programmatically generated as part of the import process.\n\n"
  443. "@ingroup Assets\n"
  444. );
  445. IMPLEMENT_CONOBJECT(AssetImporter);
  446. AssetImporter::AssetImporter() :
  447. importIssues(false),
  448. isReimport(false),
  449. assetHeirarchyChanged(false),
  450. importLogBuffer(""),
  451. activeImportConfig(nullptr)
  452. {
  453. }
  454. AssetImporter::~AssetImporter()
  455. {
  456. }
  457. bool AssetImporter::onAdd()
  458. {
  459. if (!Parent::onAdd())
  460. return false;
  461. return true;
  462. }
  463. void AssetImporter::onRemove()
  464. {
  465. Parent::onRemove();
  466. }
  467. void AssetImporter::initPersistFields()
  468. {
  469. Parent::initPersistFields();
  470. addField("targetModuleId", TypeRealString, Offset(targetModuleId, AssetImporter), "The Id of the module the assets are to be imported into");
  471. addField("finalImportedAssetPath", TypeRealString, Offset(finalImportedAssetPath, AssetImporter), "The Id of the module the assets are to be imported into");
  472. addField("targetPath", TypeRealString, Offset(targetPath, AssetImporter), "The path any imported assets are placed in as their destination");
  473. }
  474. //
  475. // Utility Functions
  476. //
  477. AssetImportObject* AssetImporter::addImportingFile(Torque::Path filePath)
  478. {
  479. String assetType = getAssetTypeByFile(filePath);
  480. if (assetType.isEmpty())
  481. {
  482. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Unable to import file %s because it is of an unrecognized/unsupported type.", filePath.getFullPath().c_str());
  483. activityLog.push_back(importLogBuffer);
  484. return nullptr;
  485. }
  486. AssetImportObject* newAssetItem = addImportingAsset(assetType, filePath, nullptr, "");
  487. originalImportingFiles.push_back(filePath);
  488. return newAssetItem;
  489. }
  490. void AssetImporter::addImportingAssetItem(AssetImportObject* assetItem, AssetImportObject* parentItem)
  491. {
  492. if (assetItem == nullptr)
  493. {
  494. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Cannot add a null AssetImportObject to import session!");
  495. activityLog.push_back(importLogBuffer);
  496. return;
  497. }
  498. if (parentItem != nullptr)
  499. {
  500. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Added Child Importing Asset to %s", parentItem->assetName.c_str());
  501. activityLog.push_back(importLogBuffer);
  502. parentItem->childAssetItems.push_back(assetItem);
  503. assetItem->parentAssetItem = parentItem;
  504. }
  505. else
  506. {
  507. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Added Importing Asset");
  508. activityLog.push_back(importLogBuffer);
  509. importingAssets.push_back(assetItem);
  510. }
  511. dSprintf(importLogBuffer, sizeof(importLogBuffer), " Asset Info: Name: %s | Type: %s", assetItem->assetName.c_str(), assetItem->assetType.c_str());
  512. activityLog.push_back(importLogBuffer);
  513. if (!assetItem->filePath.isEmpty())
  514. {
  515. dSprintf(importLogBuffer, sizeof(importLogBuffer), " File: %s", assetItem->filePath.getFullPath().c_str());
  516. activityLog.push_back(importLogBuffer);
  517. }
  518. }
  519. AssetImportObject* AssetImporter::addImportingAsset(String assetType, Torque::Path filePath, AssetImportObject* parentItem, String assetNameOverride)
  520. {
  521. String assetName;
  522. //In some cases(usually generated assets on import, like materials) we'll want to specifically define the asset name instead of peeled from the filePath
  523. if (assetNameOverride.isNotEmpty())
  524. assetName = assetNameOverride;
  525. else
  526. assetName = filePath.getFileName();
  527. AssetImportObject* assetImportObj = new AssetImportObject();
  528. assetImportObj->registerObject();
  529. //sanitize
  530. assetName.replace(' ', '_');
  531. assetName.replace('~', '_');
  532. assetName.replace('`', '_');
  533. assetName.replace('-', '_');
  534. assetName.replace('*', '_');
  535. assetName.replace('-', '_');
  536. assetName.replace('+', '_');
  537. assetName.replace('&', '_');
  538. assetImportObj->assetType = assetType;
  539. assetImportObj->filePath = filePath;
  540. assetImportObj->filePathString = StringTable->insert(filePath.getFullPath().c_str());
  541. assetImportObj->assetName = assetName;
  542. assetImportObj->cleanAssetName = assetName;
  543. assetImportObj->moduleName = targetModuleId;
  544. assetImportObj->status = "";
  545. assetImportObj->statusType = "";
  546. assetImportObj->statusInfo = "";
  547. assetImportObj->dirty = false;
  548. assetImportObj->importStatus = AssetImportObject::NotProcessed;
  549. assetImportObj->generatedAsset = false;
  550. //If the config is marked to always set the directory prefix, do that now
  551. if (activeImportConfig->AddDirectoryPrefixToAssetName)
  552. {
  553. assetName = getFolderPrefixedName(assetImportObj);
  554. assetImportObj->assetName = assetName;
  555. assetImportObj->cleanAssetName = assetName;
  556. }
  557. if (parentItem != nullptr)
  558. {
  559. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Added Child Importing Asset to %s", parentItem->assetName.c_str());
  560. activityLog.push_back(importLogBuffer);
  561. parentItem->childAssetItems.push_back(assetImportObj);
  562. assetImportObj->parentAssetItem = parentItem;
  563. }
  564. else
  565. {
  566. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Added Importing Asset");
  567. activityLog.push_back(importLogBuffer);
  568. importingAssets.push_back(assetImportObj);
  569. }
  570. dSprintf(importLogBuffer, sizeof(importLogBuffer), " Asset Info: Name: %s | Type: %s", assetImportObj->assetName.c_str(), assetImportObj->assetType.c_str());
  571. activityLog.push_back(importLogBuffer);
  572. if (!filePath.isEmpty())
  573. {
  574. dSprintf(importLogBuffer, sizeof(importLogBuffer), " File: %s", filePath.getFullPath().c_str());
  575. activityLog.push_back(importLogBuffer);
  576. }
  577. return assetImportObj;
  578. }
  579. void AssetImporter::deleteImportingAsset(AssetImportObject* assetItem)
  580. {
  581. assetItem->importStatus = AssetImportObject::Skipped;
  582. //log it
  583. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Deleting Importing Asset %s and all it's child items", assetItem->assetName.c_str());
  584. activityLog.push_back(importLogBuffer);
  585. }
  586. AssetImportObject* AssetImporter::findImportingAssetByName(String assetName, AssetImportObject* assetItem)
  587. {
  588. Vector<AssetImportObject*> itemList = importingAssets;
  589. if (assetItem != nullptr)
  590. itemList = assetItem->childAssetItems;
  591. for (U32 i = 0; i < itemList.size(); i++)
  592. {
  593. if (itemList[i]->cleanAssetName == assetName)
  594. {
  595. return itemList[i];
  596. }
  597. //If it wasn't a match, try recusing on the children(if any)
  598. AssetImportObject* retItem = findImportingAssetByName(assetName, itemList[i]);
  599. if (retItem != nullptr)
  600. return retItem;
  601. }
  602. return nullptr;
  603. }
  604. ModuleDefinition* AssetImporter::getModuleFromPath(Torque::Path filePath)
  605. {
  606. // Use a relative path so modules on mounted file systems will be found.
  607. ModuleDefinition* moduleDef = ModuleDatabase.findModuleByFilePath(Platform::makeRelativePathName(filePath.getFullPath().c_str(), NULL));
  608. return moduleDef;
  609. }
  610. String AssetImporter::parseImageSuffixes(String assetName, String* suffixType)
  611. {
  612. //Here, we loop over our different suffix lists progressively.
  613. //This lets us walk through a list of suffixes in the Import Config, such as DiffuseTypeSuffixes
  614. //And then iterate over the delinated list of items within it to look for a match.
  615. //If we don't find a match, we then increment our list switch index and scan through the next list.
  616. U32 suffixTypeIdx = 0;
  617. while (suffixTypeIdx < 6)
  618. {
  619. String suffixList;
  620. switch (suffixTypeIdx)
  621. {
  622. case 0:
  623. suffixList = activeImportConfig->DiffuseTypeSuffixes;
  624. suffixType->insert(0, "Albedo", 10);
  625. break;
  626. case 1:
  627. suffixList = activeImportConfig->NormalTypeSuffixes;
  628. suffixType->insert(0, "Normal", 10);
  629. break;
  630. case 2:
  631. suffixList = activeImportConfig->RoughnessTypeSuffixes;
  632. suffixType->insert(0, "Roughness", 10);
  633. break;
  634. case 3:
  635. suffixList = activeImportConfig->AOTypeSuffixes;
  636. suffixType->insert(0, "AO", 10);
  637. break;
  638. case 4:
  639. suffixList = activeImportConfig->MetalnessTypeSuffixes;
  640. suffixType->insert(0, "Metalness", 10);
  641. break;
  642. case 5:
  643. suffixList = activeImportConfig->PBRTypeSuffixes;
  644. suffixType->insert(0, "ORMConfig", 10);
  645. break;
  646. default:
  647. suffixList = "";
  648. }
  649. suffixTypeIdx++;
  650. U32 suffixCount = StringUnit::getUnitCount(suffixList, ",;\t");
  651. for (U32 i = 0; i < suffixCount; i++)
  652. {
  653. String suffix = StringUnit::getUnit(suffixList, i, ",;\t");
  654. String searchSuffix = String("*") + suffix;
  655. if (FindMatch::isMatch(searchSuffix.c_str(), assetName.c_str(), false))
  656. {
  657. //We have a match, so indicate as such
  658. S32 pos = assetName.length();
  659. pos -= searchSuffix.length();
  660. suffix = assetName.substr(pos+1);
  661. return suffix;
  662. }
  663. }
  664. }
  665. suffixType->clear();
  666. return "";
  667. }
  668. String AssetImporter::getAssetTypeByFile(Torque::Path filePath)
  669. {
  670. String fileExt = String::ToLower(filePath.getExtension());
  671. String fileName = String::ToLower(filePath.getFileName());
  672. if (fileExt == String("dts") && fileName.endsWith("cached"))
  673. return "";
  674. if (fileExt == String("png") || fileExt == String("jpg") || fileExt == String("jpeg") || fileExt == String("dds"))
  675. return "ImageAsset";
  676. else if (fileExt == String("dae") || fileExt == String("fbx") || fileExt == String("blend") || fileExt == String("obj") || fileExt == String("dts") || fileExt == String("gltf") || fileExt == String("gltb"))
  677. return "ShapeAsset";
  678. else if (fileExt == String("dsq"))
  679. return "ShapeAnimationAsset";
  680. else if (fileExt == String("ogg") || fileExt == String("wav") || fileExt == String("mp3"))
  681. return "SoundAsset";
  682. else if (fileExt == String("zip"))
  683. return "Zip";
  684. else if (fileExt.isEmpty())
  685. return "Folder";
  686. return "";
  687. }
  688. String AssetImporter::getTrueFilename(const String& fileName)
  689. {
  690. Torque::Path pth(fileName);
  691. String pattern = pth.getFullPath() + "*";
  692. static const String sSlash("/");
  693. Vector<String> findFilesResults;
  694. String sPattern(Torque::Path::CleanSeparators(pattern));
  695. if (sPattern.isEmpty())
  696. {
  697. Con::errorf("findFirstFile() requires a search pattern");
  698. return "";
  699. }
  700. char scriptFilenameBuffer[1024];
  701. if (!Con::expandScriptFilename(scriptFilenameBuffer, sizeof(scriptFilenameBuffer), sPattern.c_str()))
  702. {
  703. Con::errorf("findFirstFile() given initial directory cannot be expanded: '%s'", pattern.c_str());
  704. return "";
  705. }
  706. sPattern = String::ToString(scriptFilenameBuffer);
  707. String::SizeType slashPos = sPattern.find('/', 0, String::Right);
  708. // if(slashPos == String::NPos)
  709. // {
  710. // Con::errorf("findFirstFile() missing search directory or expression: '%s'", sPattern.c_str());
  711. // return -1;
  712. // }
  713. // Build the initial search path
  714. Torque::Path givenPath(Torque::Path::CompressPath(sPattern));
  715. givenPath.setFileName("*");
  716. givenPath.setExtension("*");
  717. if (givenPath.getPath().length() > 0 && givenPath.getPath().find('*', 0, String::Right) == givenPath.getPath().length() - 1)
  718. {
  719. // Deal with legacy searches of the form '*/*.*'
  720. String suspectPath = givenPath.getPath();
  721. String::SizeType newLen = suspectPath.length() - 1;
  722. if (newLen > 0 && suspectPath.find('/', 0, String::Right) == suspectPath.length() - 2)
  723. {
  724. --newLen;
  725. }
  726. givenPath.setPath(suspectPath.substr(0, newLen));
  727. }
  728. Torque::FS::FileSystemRef fs = Torque::FS::GetFileSystem(givenPath);
  729. //Torque::Path path = fs->mapTo(givenPath);
  730. Torque::Path path = givenPath;
  731. // Make sure that we have a root so the correct file system can be determined when using zips
  732. if (givenPath.isRelative())
  733. path = Torque::Path::Join(Torque::FS::GetCwd(), '/', givenPath);
  734. path.setFileName(String::EmptyString);
  735. path.setExtension(String::EmptyString);
  736. if (!Torque::FS::IsDirectory(path))
  737. {
  738. Con::errorf("findFirstFile() invalid initial search directory: '%s'", path.getFullPath().c_str());
  739. return "";
  740. }
  741. // Build the search expression
  742. const String expression(slashPos != String::NPos ? sPattern.substr(slashPos + 1) : sPattern);
  743. if (expression.isEmpty())
  744. {
  745. Con::errorf("findFirstFile() requires a search expression: '%s'", sPattern.c_str());
  746. return "";
  747. }
  748. S32 results = Torque::FS::FindByPattern(path, expression, false, findFilesResults, false);
  749. if (givenPath.isRelative() && results > 0)
  750. {
  751. // Strip the CWD out of the returned paths
  752. // MakeRelativePath() returns incorrect results (it adds a leading ..) so doing this the dirty way
  753. const String cwd = Torque::FS::GetCwd().getFullPath();
  754. for (S32 i = 0; i < findFilesResults.size(); ++i)
  755. {
  756. String str = findFilesResults[i];
  757. if (str.compare(cwd, cwd.length(), String::NoCase) == 0)
  758. str = str.substr(cwd.length());
  759. findFilesResults[i] = str;
  760. }
  761. }
  762. for (U32 i = 0; i < findFilesResults.size(); i++)
  763. {
  764. if (!findFilesResults[i].compare(fileName, 0, String::NoCase|String::Left))
  765. return findFilesResults[i];
  766. }
  767. return "";
  768. }
  769. void AssetImporter::resetImportSession(bool hardClearSession)
  770. {
  771. importingAssets.clear();
  772. activityLog.clear();
  773. if (hardClearSession)
  774. {
  775. originalImportingFiles.clear();
  776. }
  777. else
  778. {
  779. Vector<Torque::Path> tempImportingFiles = originalImportingFiles;
  780. originalImportingFiles.clear();
  781. for (U32 i = 0; i < tempImportingFiles.size(); i++)
  782. {
  783. addImportingFile(tempImportingFiles[i]);
  784. }
  785. }
  786. }
  787. S32 AssetImporter::getActivityLogLineCount()
  788. {
  789. return activityLog.size();
  790. }
  791. String AssetImporter::getActivityLogLine(U32 line)
  792. {
  793. if (line >= activityLog.size())
  794. return "";
  795. return activityLog[line];
  796. }
  797. void AssetImporter::dumpActivityLog()
  798. {
  799. for (U32 i = 0; i < activityLog.size(); i++)
  800. {
  801. Con::printf(activityLog[i].c_str());
  802. }
  803. }
  804. S32 AssetImporter::getAssetItemCount()
  805. {
  806. return importingAssets.size();
  807. }
  808. AssetImportObject* AssetImporter::getAssetItem(U32 index)
  809. {
  810. if (index >= importingAssets.size())
  811. return nullptr;
  812. return importingAssets[index];
  813. }
  814. S32 AssetImporter::getAssetItemChildCount(AssetImportObject* assetItem)
  815. {
  816. return assetItem->childAssetItems.size();
  817. }
  818. AssetImportObject* AssetImporter::getAssetItemChild(AssetImportObject* assetItem, U32 index)
  819. {
  820. if (index >= assetItem->childAssetItems.size())
  821. return nullptr;
  822. return assetItem->childAssetItems[index];
  823. }
  824. //
  825. // Processing
  826. //
  827. // Helper struct for counting nodes, meshes and polygons down through the scene
  828. // hierarchy
  829. struct SceneStats
  830. {
  831. S32 numNodes;
  832. S32 numMeshes;
  833. S32 numPolygons;
  834. S32 numMaterials;
  835. S32 numLights;
  836. S32 numClips;
  837. SceneStats() : numNodes(0), numMeshes(0), numPolygons(0), numMaterials(0), numLights(0), numClips(0) { }
  838. };
  839. // Recurse through the <visual_scene> adding nodes and geometry to the GuiTreeView control
  840. static void processNode(GuiTreeViewCtrl* tree, domNode* node, S32 parentID, SceneStats& stats)
  841. {
  842. stats.numNodes++;
  843. S32 nodeID = tree->insertItem(parentID, _GetNameOrId(node), "node", "", 0, 0);
  844. // Update mesh and poly counts
  845. for (S32 i = 0; i < node->getContents().getCount(); i++)
  846. {
  847. domGeometry* geom = 0;
  848. const char* elemName = "";
  849. daeElement* child = node->getContents()[i];
  850. switch (child->getElementType())
  851. {
  852. case COLLADA_TYPE::INSTANCE_GEOMETRY:
  853. {
  854. domInstance_geometry* instgeom = daeSafeCast<domInstance_geometry>(child);
  855. if (instgeom)
  856. {
  857. geom = daeSafeCast<domGeometry>(instgeom->getUrl().getElement());
  858. elemName = _GetNameOrId(geom);
  859. }
  860. break;
  861. }
  862. case COLLADA_TYPE::INSTANCE_CONTROLLER:
  863. {
  864. domInstance_controller* instctrl = daeSafeCast<domInstance_controller>(child);
  865. if (instctrl)
  866. {
  867. domController* ctrl = daeSafeCast<domController>(instctrl->getUrl().getElement());
  868. elemName = _GetNameOrId(ctrl);
  869. if (ctrl && ctrl->getSkin())
  870. geom = daeSafeCast<domGeometry>(ctrl->getSkin()->getSource().getElement());
  871. else if (ctrl && ctrl->getMorph())
  872. geom = daeSafeCast<domGeometry>(ctrl->getMorph()->getSource().getElement());
  873. }
  874. break;
  875. }
  876. case COLLADA_TYPE::INSTANCE_LIGHT:
  877. stats.numLights++;
  878. tree->insertItem(nodeID, _GetNameOrId(node), "light", "", 0, 0);
  879. break;
  880. }
  881. if (geom && geom->getMesh())
  882. {
  883. const char* name = _GetNameOrId(node);
  884. if (dStrEqual(name, "null") || dStrEndsWith(name, "PIVOT"))
  885. name = _GetNameOrId(daeSafeCast<domNode>(node->getParent()));
  886. stats.numMeshes++;
  887. tree->insertItem(nodeID, name, "mesh", "", 0, 0);
  888. for (S32 j = 0; j < geom->getMesh()->getTriangles_array().getCount(); j++)
  889. stats.numPolygons += geom->getMesh()->getTriangles_array()[j]->getCount();
  890. for (S32 j = 0; j < geom->getMesh()->getTristrips_array().getCount(); j++)
  891. stats.numPolygons += geom->getMesh()->getTristrips_array()[j]->getCount();
  892. for (S32 j = 0; j < geom->getMesh()->getTrifans_array().getCount(); j++)
  893. stats.numPolygons += geom->getMesh()->getTrifans_array()[j]->getCount();
  894. for (S32 j = 0; j < geom->getMesh()->getPolygons_array().getCount(); j++)
  895. stats.numPolygons += geom->getMesh()->getPolygons_array()[j]->getCount();
  896. for (S32 j = 0; j < geom->getMesh()->getPolylist_array().getCount(); j++)
  897. stats.numPolygons += geom->getMesh()->getPolylist_array()[j]->getCount();
  898. }
  899. }
  900. // Recurse into child nodes
  901. for (S32 i = 0; i < node->getNode_array().getCount(); i++)
  902. processNode(tree, node->getNode_array()[i], nodeID, stats);
  903. for (S32 i = 0; i < node->getInstance_node_array().getCount(); i++)
  904. {
  905. domInstance_node* instnode = node->getInstance_node_array()[i];
  906. domNode* dNode = daeSafeCast<domNode>(instnode->getUrl().getElement());
  907. if (dNode)
  908. processNode(tree, dNode, nodeID, stats);
  909. }
  910. }
  911. static bool enumColladaForImport(const char* shapePath, GuiTreeViewCtrl* tree, bool loadCachedDts)
  912. {
  913. // Check if a cached DTS is available => no need to import the collada file
  914. // if we can load the DTS instead
  915. Torque::Path path(shapePath);
  916. if (loadCachedDts && ColladaShapeLoader::canLoadCachedDTS(path))
  917. return false;
  918. // Check if this is a Sketchup file (.kmz) and if so, mount the zip filesystem
  919. // and get the path to the DAE file.
  920. String mountPoint;
  921. Torque::Path daePath;
  922. bool isSketchup = ColladaShapeLoader::checkAndMountSketchup(path, mountPoint, daePath);
  923. // Load the Collada file into memory
  924. domCOLLADA* root = ColladaShapeLoader::getDomCOLLADA(daePath);
  925. if (!root)
  926. {
  927. TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Load complete");
  928. return false;
  929. }
  930. if (isSketchup)
  931. {
  932. // Unmount the zip if we mounted it
  933. Torque::FS::Unmount(mountPoint);
  934. }
  935. // Initialize tree
  936. tree->removeItem(0);
  937. S32 nodesID = tree->insertItem(0, "Shape", "", "", 0, 0);
  938. S32 matsID = tree->insertItem(0, "Materials", "", "", 0, 0);
  939. S32 animsID = tree->insertItem(0, "Animations", "", "", 0, 0);
  940. SceneStats stats;
  941. // Query DOM for shape summary details
  942. for (S32 i = 0; i < root->getLibrary_visual_scenes_array().getCount(); i++)
  943. {
  944. const domLibrary_visual_scenes* libScenes = root->getLibrary_visual_scenes_array()[i];
  945. for (S32 j = 0; j < libScenes->getVisual_scene_array().getCount(); j++)
  946. {
  947. const domVisual_scene* visualScene = libScenes->getVisual_scene_array()[j];
  948. for (S32 k = 0; k < visualScene->getNode_array().getCount(); k++)
  949. processNode(tree, visualScene->getNode_array()[k], nodesID, stats);
  950. }
  951. }
  952. // Get material count
  953. for (S32 i = 0; i < root->getLibrary_materials_array().getCount(); i++)
  954. {
  955. const domLibrary_materials* libraryMats = root->getLibrary_materials_array()[i];
  956. stats.numMaterials += libraryMats->getMaterial_array().getCount();
  957. for (S32 j = 0; j < libraryMats->getMaterial_array().getCount(); j++)
  958. {
  959. domMaterial* mat = libraryMats->getMaterial_array()[j];
  960. tree->insertItem(matsID, _GetNameOrId(mat), "", "", 0, 0);
  961. }
  962. }
  963. // Get images count
  964. for (S32 i = 0; i < root->getLibrary_images_array().getCount(); i++)
  965. {
  966. const domLibrary_images* libraryImages = root->getLibrary_images_array()[i];
  967. for (S32 j = 0; j < libraryImages->getImage_array().getCount(); j++)
  968. {
  969. domImage* img = libraryImages->getImage_array()[j];
  970. S32 materialID = tree->findItemByName(_GetNameOrId(img));
  971. if (materialID == 0)
  972. continue;
  973. tree->setItemValue(materialID, img->getInit_from()->getValue().str().c_str());
  974. }
  975. }
  976. // Get animation count
  977. for (S32 i = 0; i < root->getLibrary_animation_clips_array().getCount(); i++)
  978. {
  979. const domLibrary_animation_clips* libraryClips = root->getLibrary_animation_clips_array()[i];
  980. stats.numClips += libraryClips->getAnimation_clip_array().getCount();
  981. for (S32 j = 0; j < libraryClips->getAnimation_clip_array().getCount(); j++)
  982. {
  983. domAnimation_clip* clip = libraryClips->getAnimation_clip_array()[j];
  984. tree->insertItem(animsID, _GetNameOrId(clip), "animation", "", 0, 0);
  985. }
  986. }
  987. if (stats.numClips == 0)
  988. {
  989. // No clips => check if there are any animations (these will be added to a default clip)
  990. for (S32 i = 0; i < root->getLibrary_animations_array().getCount(); i++)
  991. {
  992. const domLibrary_animations* libraryAnims = root->getLibrary_animations_array()[i];
  993. if (libraryAnims->getAnimation_array().getCount())
  994. {
  995. stats.numClips = 1;
  996. tree->insertItem(animsID, "ambient", "animation", "", 0, 0);
  997. break;
  998. }
  999. }
  1000. }
  1001. // Extract the global scale and up_axis from the top level <asset> element,
  1002. F32 unit = 1.0f;
  1003. domUpAxisType upAxis = UPAXISTYPE_Z_UP;
  1004. if (root->getAsset()) {
  1005. if (root->getAsset()->getUnit())
  1006. unit = root->getAsset()->getUnit()->getMeter();
  1007. if (root->getAsset()->getUp_axis())
  1008. upAxis = root->getAsset()->getUp_axis()->getValue();
  1009. }
  1010. TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Load complete");
  1011. // Store shape information in the tree control
  1012. tree->setDataField(StringTable->insert("_nodeCount"), 0, avar("%d", stats.numNodes));
  1013. tree->setDataField(StringTable->insert("_meshCount"), 0, avar("%d", stats.numMeshes));
  1014. tree->setDataField(StringTable->insert("_polygonCount"), 0, avar("%d", stats.numPolygons));
  1015. tree->setDataField(StringTable->insert("_materialCount"), 0, avar("%d", stats.numMaterials));
  1016. tree->setDataField(StringTable->insert("_lightCount"), 0, avar("%d", stats.numLights));
  1017. tree->setDataField(StringTable->insert("_animCount"), 0, avar("%d", stats.numClips));
  1018. tree->setDataField(StringTable->insert("_unit"), 0, avar("%g", unit));
  1019. if (upAxis == UPAXISTYPE_X_UP)
  1020. tree->setDataField(StringTable->insert("_upAxis"), 0, "X_AXIS");
  1021. else if (upAxis == UPAXISTYPE_Y_UP)
  1022. tree->setDataField(StringTable->insert("_upAxis"), 0, "Y_AXIS");
  1023. else
  1024. tree->setDataField(StringTable->insert("_upAxis"), 0, "Z_AXIS");
  1025. char shapesStr[16];
  1026. dSprintf(shapesStr, 16, "%i", stats.numMeshes);
  1027. char materialsStr[16];
  1028. dSprintf(materialsStr, 16, "%i", stats.numMaterials);
  1029. char animationsStr[16];
  1030. dSprintf(animationsStr, 16, "%i", stats.numClips);
  1031. tree->setItemValue(nodesID, StringTable->insert(shapesStr));
  1032. tree->setItemValue(matsID, StringTable->insert(materialsStr));
  1033. tree->setItemValue(animsID, StringTable->insert(animationsStr));
  1034. return true;
  1035. }
  1036. static bool enumDTSForImport(const char* shapePath, GuiTreeViewCtrl* tree)
  1037. {
  1038. // Check if a cached DTS is available => no need to import the collada file
  1039. // if we can load the DTS instead
  1040. Torque::Path path(shapePath);
  1041. Resource<TSShape> dtsShape = ResourceManager::get().load(shapePath);
  1042. if (!dtsShape)
  1043. return false;
  1044. // Initialize tree
  1045. tree->removeItem(0);
  1046. S32 nodesID = tree->insertItem(0, "Shape", "", "", 0, 0);
  1047. S32 matsID = tree->insertItem(0, "Materials", "", "", 0, 0);
  1048. S32 animsID = tree->insertItem(0, "Animations", "", "", 0, 0);
  1049. SceneStats stats;
  1050. // Query DOM for shape summary details
  1051. for (S32 i = 0; i < dtsShape->objects.size(); i++)
  1052. {
  1053. tree->insertItem(nodesID, dtsShape->names[dtsShape->objects[i].nameIndex], "", "", 0, 0);
  1054. stats.numMeshes++;
  1055. }
  1056. // Get material count
  1057. for (S32 i = 0; i < dtsShape->materialList->size(); i++)
  1058. {
  1059. S32 matId = tree->insertItem(matsID, dtsShape->materialList->getMaterialName(i).c_str(), "", "", 0, 0);
  1060. stats.numMaterials++;
  1061. GFXTextureObject* difTex = dtsShape->materialList->getDiffuseTexture(i);
  1062. if (difTex)
  1063. {
  1064. tree->insertItem(matId, difTex->getPath().c_str(), "", "", 0, 0);
  1065. }
  1066. }
  1067. // Get animation count
  1068. for (S32 i = 0; i < dtsShape->sequences.size(); i++)
  1069. {
  1070. tree->insertItem(animsID, dtsShape->names[dtsShape->sequences[i].nameIndex], "animation", "", 0, 0);
  1071. stats.numClips++;
  1072. }
  1073. /*if (stats.numClips == 0)
  1074. {
  1075. // No clips => check if there are any animations (these will be added to a default clip)
  1076. for (S32 i = 0; i < root->getLibrary_animations_array().getCount(); i++)
  1077. {
  1078. const domLibrary_animations* libraryAnims = root->getLibrary_animations_array()[i];
  1079. if (libraryAnims->getAnimation_array().getCount())
  1080. {
  1081. stats.numClips = 1;
  1082. tree->insertItem(animsID, "ambient", "animation", "", 0, 0);
  1083. break;
  1084. }
  1085. }
  1086. }*/
  1087. F32 unit = 1.0f;
  1088. // Store shape information in the tree control
  1089. tree->setDataField(StringTable->insert("_nodeCount"), 0, avar("%d", stats.numNodes));
  1090. tree->setDataField(StringTable->insert("_meshCount"), 0, avar("%d", stats.numMeshes));
  1091. tree->setDataField(StringTable->insert("_polygonCount"), 0, avar("%d", stats.numPolygons));
  1092. tree->setDataField(StringTable->insert("_materialCount"), 0, avar("%d", stats.numMaterials));
  1093. tree->setDataField(StringTable->insert("_lightCount"), 0, avar("%d", stats.numLights));
  1094. tree->setDataField(StringTable->insert("_animCount"), 0, avar("%d", stats.numClips));
  1095. tree->setDataField(StringTable->insert("_unit"), 0, avar("%g", unit));
  1096. tree->setDataField(StringTable->insert("_upAxis"), 0, "Z_AXIS");
  1097. char shapesStr[16];
  1098. dSprintf(shapesStr, 16, "%i", stats.numMeshes);
  1099. char materialsStr[16];
  1100. dSprintf(materialsStr, 16, "%i", stats.numMaterials);
  1101. char animationsStr[16];
  1102. dSprintf(animationsStr, 16, "%i", stats.numClips);
  1103. tree->setItemValue(nodesID, StringTable->insert(shapesStr));
  1104. tree->setItemValue(matsID, StringTable->insert(materialsStr));
  1105. tree->setItemValue(animsID, StringTable->insert(animationsStr));
  1106. return true;
  1107. }
  1108. void AssetImportConfig::loadSISFile(Torque::Path filePath)
  1109. {
  1110. String settingsFilePath = "Tools";
  1111. Settings* editorSettings;
  1112. //See if we can get our editor settings
  1113. if (Sim::findObject("EditorSettings", editorSettings))
  1114. {
  1115. settingsFilePath = editorSettings->value("defaultSettingsPath", "Tools");
  1116. }
  1117. String fileExtension = filePath.getExtension();
  1118. String settingsFile = settingsFilePath + "/" + fileExtension + ".sis";
  1119. FileObject* fileObj = new FileObject();
  1120. if (Platform::isFile(settingsFile))
  1121. {
  1122. if (!fileObj->readMemory(settingsFile.c_str()))
  1123. {
  1124. Con::errorf("AssetImporter::loadSISFile() - Error opening file to load settings: %s", settingsFile.c_str());
  1125. fileObj->deleteObject();
  1126. return;
  1127. }
  1128. }
  1129. else
  1130. {
  1131. return;
  1132. }
  1133. String headerLine = (const char*)fileObj->readLine();
  1134. if (headerLine.substr(0, 4).compare("SISV", 0U, String::NoCase) != 0)
  1135. return; //not a sis file?
  1136. while (!fileObj->isEOF())
  1137. {
  1138. const char* line = (const char*)fileObj->readLine();
  1139. String key = StringUnit::getUnit(line, 0, "\t");
  1140. String value = StringUnit::getUnit(line, 1, "\t");
  1141. if (key.compare("DoUpAxisOverride", 0U, String::NoCase) == 0)
  1142. DoUpAxisOverride = dAtob(value.c_str());
  1143. else if (key.compare("UpAxisOverride", 0U, String::NoCase) == 0)
  1144. UpAxisOverride = value.c_str();
  1145. else if (key.compare("DoScaleOverride", 0U, String::NoCase) == 0)
  1146. DoScaleOverride = dAtob(value.c_str());
  1147. else if (key.compare("ScaleOverride", 0U, String::NoCase) == 0)
  1148. ScaleOverride = dAtof(value.c_str());
  1149. else if (key.compare("IgnoreNodeScale", 0U, String::NoCase) == 0)
  1150. IgnoreNodeScale = dAtob(value.c_str());
  1151. else if (key.compare("AdjustCenter", 0U, String::NoCase) == 0)
  1152. AdjustCenter = dAtob(value.c_str());
  1153. else if (key.compare("AdjustFloor", 0U, String::NoCase) == 0)
  1154. AdjustFloor = dAtob(value.c_str());
  1155. else if (key.compare("CollapseSubmeshes", 0U, String::NoCase) == 0)
  1156. CollapseSubmeshes = dAtob(value.c_str());
  1157. else if (key.compare("LODType", 0U, String::NoCase) == 0)
  1158. LODType = value.c_str();
  1159. else if (key.compare("AlwaysImportedNodes", 0U, String::NoCase) == 0)
  1160. AlwaysImportedNodes = value.c_str();
  1161. else if (key.compare("AlwaysIgnoreNodes", 0U, String::NoCase) == 0)
  1162. AlwaysIgnoreNodes = value.c_str();
  1163. else if (key.compare("AlwaysImportMeshes", 0U, String::NoCase) == 0)
  1164. AlwaysImportMeshes = value.c_str();
  1165. else if (key.compare("AlwaysIgnoreMeshes", 0U, String::NoCase) == 0)
  1166. AlwaysIgnoreMeshes = value.c_str();
  1167. else if (key.compare("convertLeftHanded", 0U, String::NoCase) == 0)
  1168. convertLeftHanded = dAtob(value.c_str());
  1169. else if (key.compare("calcTangentSpace", 0U, String::NoCase) == 0)
  1170. calcTangentSpace = dAtob(value.c_str());
  1171. else if (key.compare("removeRedundantMats", 0U, String::NoCase) == 0)
  1172. removeRedundantMats = dAtob(value.c_str());
  1173. else if (key.compare("genUVCoords", 0U, String::NoCase) == 0)
  1174. genUVCoords = dAtob(value.c_str());
  1175. else if (key.compare("TransformUVs", 0U, String::NoCase) == 0)
  1176. TransformUVs = dAtob(value.c_str());
  1177. else if (key.compare("flipUVCoords", 0U, String::NoCase) == 0)
  1178. flipUVCoords = dAtob(value.c_str());
  1179. else if (key.compare("findInstances", 0U, String::NoCase) == 0)
  1180. findInstances = dAtob(value.c_str());
  1181. else if (key.compare("limitBoneWeights", 0U, String::NoCase) == 0)
  1182. limitBoneWeights = dAtob(value.c_str());
  1183. else if (key.compare("JoinIdenticalVerts", 0U, String::NoCase) == 0)
  1184. JoinIdenticalVerts = dAtob(value.c_str());
  1185. else if (key.compare("reverseWindingOrder", 0U, String::NoCase) == 0)
  1186. reverseWindingOrder = dAtob(value.c_str());
  1187. else if (key.compare("invertNormals", 0U, String::NoCase) == 0)
  1188. invertNormals = dAtob(value.c_str());
  1189. }
  1190. fileObj->close();
  1191. fileObj->deleteObject();
  1192. }
  1193. void AssetImporter::processImportAssets(AssetImportObject* assetItem)
  1194. {
  1195. Vector<AssetImportObject*> itemList = importingAssets;
  1196. if (assetItem != nullptr)
  1197. itemList = assetItem->childAssetItems;
  1198. assetHeirarchyChanged = false;
  1199. for (U32 i = 0; i < itemList.size(); i++)
  1200. {
  1201. AssetImportObject* item = itemList[i];
  1202. if (item->importStatus != AssetImportObject::NotProcessed)
  1203. continue;
  1204. //Sanitize before modifying our asset name(suffix additions, etc)
  1205. if (item->assetName != item->cleanAssetName)
  1206. item->assetName = item->cleanAssetName;
  1207. //process the asset items
  1208. if (item->assetType == String("ImageAsset"))
  1209. {
  1210. processImageAsset(item);
  1211. }
  1212. else if (item->assetType == String("ShapeAsset"))
  1213. {
  1214. processShapeAsset(item);
  1215. }
  1216. else if (item->assetType == String("SoundAsset"))
  1217. {
  1218. processSoundAsset(item);
  1219. }
  1220. else if (item->assetType == String("MaterialAsset"))
  1221. {
  1222. processMaterialAsset(item);
  1223. }
  1224. /*else if (item->assetType == String("ShapeAnimationAsset"))
  1225. ShapeAnimationAsset::prepareAssetForImport(this, item);*/
  1226. else
  1227. {
  1228. String processCommand = "process";
  1229. processCommand += item->assetType;
  1230. if(isMethod(processCommand.c_str()))
  1231. Con::executef(this, processCommand.c_str(), item);
  1232. }
  1233. item->importStatus = AssetImportObject::Processed;
  1234. //try recusing on the children(if any)
  1235. processImportAssets(item);
  1236. }
  1237. //If our hierarchy changed, it's because we did so during processing
  1238. //so we'll loop back through again until everything has been processed
  1239. if (assetHeirarchyChanged)
  1240. processImportAssets();
  1241. }
  1242. void AssetImporter::processImageAsset(AssetImportObject* assetItem)
  1243. {
  1244. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Preparing Image for Import: %s", assetItem->assetName.c_str());
  1245. activityLog.push_back(importLogBuffer);
  1246. if ((activeImportConfig->GenerateMaterialOnImport && assetItem->parentAssetItem == nullptr)/* || assetItem->parentAssetItem != nullptr*/)
  1247. {
  1248. //find our suffix match, if any
  1249. String noSuffixName = assetItem->assetName;
  1250. String suffixType;
  1251. String suffix = parseImageSuffixes(assetItem->assetName, &suffixType);
  1252. if (suffix.isNotEmpty())
  1253. {
  1254. assetItem->imageSuffixType = suffixType;
  1255. S32 suffixPos =assetItem->assetName.find(suffix, 0, String::NoCase|String::Left);
  1256. noSuffixName = assetItem->assetName.substr(0, suffixPos);
  1257. }
  1258. //We try to automatically populate materials under the naming convention: materialName: Rock, image maps: Rock_Albedo, Rock_Normal, etc
  1259. AssetImportObject* materialAsset = findImportingAssetByName(noSuffixName);
  1260. if (materialAsset != nullptr && materialAsset->assetType != String("MaterialAsset"))
  1261. {
  1262. //We may have a situation where an asset matches the no-suffix name, but it's not a material asset. Ignore this
  1263. //asset item for now
  1264. materialAsset = nullptr;
  1265. }
  1266. //If we didn't find a matching material asset in our current items, we'll make one now
  1267. if (materialAsset == nullptr)
  1268. {
  1269. if (!assetItem->filePath.isEmpty())
  1270. {
  1271. materialAsset = addImportingAsset("MaterialAsset", assetItem->filePath, nullptr, noSuffixName);
  1272. }
  1273. }
  1274. //Not that, one way or another, we have the generated material asset, lets move on to associating our image with it
  1275. if (materialAsset != nullptr && materialAsset != assetItem->parentAssetItem)
  1276. {
  1277. if (assetItem->parentAssetItem != nullptr)
  1278. {
  1279. //If the image had an existing parent, it gets removed from that parent's child item list
  1280. assetItem->parentAssetItem->childAssetItems.remove(assetItem);
  1281. }
  1282. else
  1283. {
  1284. //If it didn't have one, we're going to pull it from the importingAssets list
  1285. importingAssets.remove(assetItem);
  1286. }
  1287. //Now we can add it to the correct material asset
  1288. materialAsset->childAssetItems.push_back(assetItem);
  1289. assetItem->parentAssetItem = materialAsset;
  1290. assetHeirarchyChanged = true;
  1291. }
  1292. //Now to do some cleverness. If we're generating a material, we can parse like assets being imported(similar filenames) but different suffixes
  1293. //If we find these, we'll just populate into the original's material
  1294. //if we need to append the diffuse suffix and indeed didn't find a suffix on the name, do that here
  1295. if (suffixType.isEmpty())
  1296. {
  1297. if (activeImportConfig->UseDiffuseSuffixOnOriginImage)
  1298. {
  1299. String diffuseToken = StringUnit::getUnit(activeImportConfig->DiffuseTypeSuffixes, 0, ",;\t");
  1300. assetItem->assetName = assetItem->assetName + diffuseToken;
  1301. assetItem->cleanAssetName = assetItem->assetName;
  1302. }
  1303. else
  1304. {
  1305. //We need to ensure that our image asset doesn't match the same name as the material asset, so if we're not trying to force the diffuse suffix
  1306. //we'll give it a generic one
  1307. if ((materialAsset && materialAsset->assetName.compare(assetItem->assetName) == 0) || activeImportConfig->AlwaysAddImageSuffix)
  1308. {
  1309. assetItem->assetName = assetItem->assetName + activeImportConfig->AddedImageSuffix;
  1310. assetItem->cleanAssetName = assetItem->assetName;
  1311. }
  1312. }
  1313. //Assume for abledo if it has no suffix matches
  1314. assetItem->imageSuffixType = "Albedo";
  1315. }
  1316. else
  1317. {
  1318. }
  1319. }
  1320. else
  1321. {
  1322. //If we're processing an unaffiliated image without generating materials for it, we can check some other bits
  1323. if (assetItem->parentAssetItem == nullptr)
  1324. {
  1325. if (assetItem->typeHint != String::EmptyString)
  1326. {
  1327. ImageAssetType type = ImageAsset::getImageTypeFromName(StringTable->insert(assetItem->typeHint.c_str()));
  1328. if (type == ImageAssetType::GUI)
  1329. {
  1330. }
  1331. }
  1332. }
  1333. }
  1334. if(assetItem->assetName == assetItem->cleanAssetName && activeImportConfig->AlwaysAddImageSuffix)
  1335. {
  1336. assetItem->assetName = assetItem->assetName + activeImportConfig->AddedImageSuffix;
  1337. assetItem->cleanAssetName = assetItem->assetName;
  1338. }
  1339. assetItem->importStatus = AssetImportObject::Processed;
  1340. }
  1341. void AssetImporter::processMaterialAsset(AssetImportObject* assetItem)
  1342. {
  1343. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Preparing Material for Import: %s", assetItem->assetName.c_str());
  1344. activityLog.push_back(importLogBuffer);
  1345. String filePath = assetItem->filePath.getFullPath();
  1346. String fileName = assetItem->filePath.getFileName();
  1347. String fileExt = assetItem->filePath.getExtension();
  1348. const char* assetName = assetItem->assetName.c_str();
  1349. assetItem->generatedAsset = true;
  1350. if (activeImportConfig->IgnoreMaterials.isNotEmpty())
  1351. {
  1352. U32 ignoredMatNameCount = StringUnit::getUnitCount(activeImportConfig->IgnoreMaterials, ",;\t");
  1353. for (U32 i = 0; i < ignoredMatNameCount; i++)
  1354. {
  1355. String ignoredName = StringUnit::getUnit(activeImportConfig->IgnoreMaterials, i, ",;\t");
  1356. if (FindMatch::isMatch(ignoredName.c_str(), assetName, false))
  1357. {
  1358. assetItem->importStatus = AssetImportObject::Skipped;
  1359. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Material %s has been ignored due to it's name being listed in the IgnoreMaterials list in the Import Config.", assetItem->assetName.c_str());
  1360. activityLog.push_back(importLogBuffer);
  1361. return;
  1362. }
  1363. }
  1364. }
  1365. if (activeImportConfig->UseExistingMaterials)
  1366. {
  1367. //So if the material already exists, we should just use that. So first, let's find out if it already exists
  1368. //check to see if the definition for this already exists
  1369. StringTableEntry existingMatAsset = MaterialAsset::getAssetIdByMaterialName(StringTable->insert(assetName));
  1370. if (existingMatAsset != StringTable->EmptyString() && existingMatAsset != StringTable->insert("Core_Rendering:NoMaterial"))
  1371. {
  1372. assetItem->importStatus = AssetImportObject::UseForDependencies;
  1373. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Material %s has been skipped because we already found an asset Id that uses that material definition. The found assetId is: %s", assetItem->assetName.c_str(), existingMatAsset);
  1374. activityLog.push_back(importLogBuffer);
  1375. return;
  1376. }
  1377. //If there was no existing assetId, then lets see if it already exists in a legacy file, like a materials.cs or materials.tscript
  1378. //If it does, we'll just make our asset point to that instead of a new file
  1379. Material* mat = MATMGR->getMaterialDefinitionByName(assetName);
  1380. if (!mat)
  1381. mat = MATMGR->getMaterialDefinitionByMapTo(assetName);
  1382. if (!mat && assetItem->assetName != assetItem->cleanAssetName)
  1383. {
  1384. mat = MATMGR->getMaterialDefinitionByName(assetItem->cleanAssetName);
  1385. if (!mat)
  1386. mat = MATMGR->getMaterialDefinitionByMapTo(assetItem->cleanAssetName);
  1387. }
  1388. if(mat)
  1389. {
  1390. //We found a match, so just modify our asset item's info to point against it. This will create the asset definition, but otherwise leave the material definition as-is.
  1391. assetItem->filePath = (Torque::Path)(mat->getFilename());
  1392. }
  1393. }
  1394. else
  1395. {
  1396. if (activeImportConfig->AlwaysAddMaterialSuffix) //we only opt to force on the suffix if we're not obligating using the original material defs
  1397. {
  1398. assetItem->assetName += activeImportConfig->AddedMaterialSuffix;
  1399. assetItem->cleanAssetName = assetItem->assetName;
  1400. }
  1401. if (activeImportConfig->PopulateMaterialMaps)
  1402. {
  1403. //If we're trying to populate the rest of our material maps, we need to go looking
  1404. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Attempting to Auto-Populate Material Maps");
  1405. activityLog.push_back(importLogBuffer);
  1406. AssetImportObject* matchedImageTypes[ImageAsset::ImageTypeCount] = { nullptr };
  1407. String materialImageNoSuffix;
  1408. for (U32 i = 0; i < assetItem->childAssetItems.size(); i++)
  1409. {
  1410. AssetImportObject* childAssetItem = assetItem->childAssetItems[i];
  1411. if (childAssetItem->importStatus == AssetImportObject::Skipped || childAssetItem->assetType != String("ImageAsset"))
  1412. continue;
  1413. for (S32 t = 0; t < ImageAsset::ImageTypeCount; t++)
  1414. {
  1415. //If the imageType name and child asset image type match, check it off our list
  1416. if (!dStricmp(ImageAsset::getImageTypeNameFromType((ImageAsset::ImageTypes)t), childAssetItem->imageSuffixType.c_str()))
  1417. {
  1418. matchedImageTypes[t] = childAssetItem;
  1419. if (t == ImageAsset::ImageTypes::Albedo)
  1420. {
  1421. String sufType;
  1422. String suffix = parseImageSuffixes(childAssetItem->assetName, &sufType);
  1423. String imageAssetName = childAssetItem->assetName;
  1424. if (suffix.isEmpty())
  1425. materialImageNoSuffix = imageAssetName;
  1426. else
  1427. materialImageNoSuffix = imageAssetName.erase(imageAssetName.length() - suffix.length(), suffix.length());//cache this for later as we may need it for file association lookups
  1428. }
  1429. }
  1430. }
  1431. }
  1432. //Now that we've checked off any existingly matched image types, process through the unmatched to look for files that associate
  1433. for (S32 t = 0; t < ImageAsset::ImageTypeCount; t++)
  1434. {
  1435. //This type wasn't found, so try and find a match based on suffix
  1436. String suffixList;
  1437. switch (t)
  1438. {
  1439. case ImageAsset::Albedo:
  1440. suffixList = activeImportConfig->DiffuseTypeSuffixes;
  1441. break;
  1442. case ImageAsset::Normal:
  1443. suffixList = activeImportConfig->NormalTypeSuffixes;
  1444. break;
  1445. case ImageAsset::ORMConfig:
  1446. suffixList = activeImportConfig->PBRTypeSuffixes;
  1447. break;
  1448. case ImageAsset::Metalness:
  1449. suffixList = activeImportConfig->MetalnessTypeSuffixes;
  1450. break;
  1451. case ImageAsset::AO:
  1452. suffixList = activeImportConfig->AOTypeSuffixes;
  1453. break;
  1454. case ImageAsset::Roughness:
  1455. suffixList = activeImportConfig->RoughnessTypeSuffixes;
  1456. break;
  1457. //TODO: Glow map lookup too
  1458. }
  1459. if (!matchedImageTypes[t])
  1460. {
  1461. U32 suffixCount = StringUnit::getUnitCount(suffixList.c_str(), ",;\t");
  1462. for (U32 i = 0; i < suffixCount; i++)
  1463. {
  1464. //First, try checking based on the material's assetName for our patternbase
  1465. String testPath = assetItem->filePath.getRootAndPath();
  1466. testPath += "/" + assetItem->cleanAssetName + StringUnit::getUnit(suffixList.c_str(), i, ",;\t");
  1467. String imagePath = AssetImporter::findImagePath(testPath);
  1468. if (imagePath.isNotEmpty())
  1469. {
  1470. //got a match!
  1471. AssetImportObject* newImageAssetObj = addImportingAsset("ImageAsset", imagePath, assetItem, "");
  1472. newImageAssetObj->imageSuffixType = ImageAsset::getImageTypeNameFromType((ImageAsset::ImageTypes)t);
  1473. matchedImageTypes[t] = newImageAssetObj;
  1474. break;
  1475. }
  1476. else
  1477. {
  1478. if (materialImageNoSuffix.isNotEmpty())
  1479. {
  1480. testPath = assetItem->filePath.getRootAndPath();
  1481. testPath += "/" + materialImageNoSuffix + StringUnit::getUnit(suffixList.c_str(), i, ",;\t");
  1482. imagePath = AssetImporter::findImagePath(testPath);
  1483. if (imagePath.isNotEmpty())
  1484. {
  1485. //got a match!
  1486. AssetImportObject* newImageAssetObj = addImportingAsset("ImageAsset", imagePath, assetItem, "");
  1487. newImageAssetObj->imageSuffixType = ImageAsset::getImageTypeNameFromType((ImageAsset::ImageTypes)t);
  1488. matchedImageTypes[t] = newImageAssetObj;
  1489. break;
  1490. }
  1491. }
  1492. }
  1493. }
  1494. //If we're the abledo slot and after all that we didn't find anything, it probably is a suffixless image
  1495. if (t == ImageAsset::Albedo && matchedImageTypes[t] == nullptr)
  1496. {
  1497. String testPath = assetItem->filePath.getRootAndPath() + "/" + assetItem->cleanAssetName;
  1498. String imagePath = AssetImporter::findImagePath(testPath);
  1499. if (imagePath.isNotEmpty())
  1500. {
  1501. //got a match!
  1502. AssetImportObject* newImageAssetObj = addImportingAsset("ImageAsset", imagePath, assetItem, "");
  1503. //In the event that the names match, we want to avoid duplications, so we'll go ahead and append a suffix onto our new image asset
  1504. if (newImageAssetObj->assetName == assetItem->assetName)
  1505. {
  1506. newImageAssetObj->assetName += StringUnit::getUnit(suffixList.c_str(), 0, ",;\t");
  1507. newImageAssetObj->cleanAssetName = newImageAssetObj->assetName;
  1508. }
  1509. newImageAssetObj->imageSuffixType = ImageAsset::getImageTypeNameFromType(ImageAsset::ImageTypes::Albedo);
  1510. matchedImageTypes[t] = newImageAssetObj;
  1511. }
  1512. }
  1513. }
  1514. else
  1515. {
  1516. //just a bit of cleanup and logical testing for matches
  1517. //in the event we KNOW what the type is, but we don't have a suffix, such as a found image on a material lookup
  1518. //that doesn't have a suffix, we assume it to be the albedo, so we'll just append the suffix to avoid collisions if
  1519. //the name already matches our material name, similar to above logic
  1520. if (matchedImageTypes[t]->assetName == assetItem->assetName)
  1521. {
  1522. matchedImageTypes[t]->assetName += StringUnit::getUnit(suffixList.c_str(), 0, ",;\t");
  1523. matchedImageTypes[t]->cleanAssetName = matchedImageTypes[t]->assetName;
  1524. }
  1525. }
  1526. }
  1527. /*for (U32 i = 0; i < assetItem->childAssetItems.size(); i++)
  1528. {
  1529. AssetImportObject* childAssetItem = assetItem->childAssetItems[i];
  1530. if (childAssetItem->skip || childAssetItem->processed || childAssetItem->assetType != String("ImageAsset"))
  1531. continue;
  1532. if (childAssetItem->imageSuffixType == String("Albedo"))
  1533. {
  1534. assetItem->diffuseImageAsset = % childAssetItem;
  1535. }
  1536. }*/
  1537. }
  1538. }
  1539. assetItem->importStatus = AssetImportObject::Processed;
  1540. }
  1541. void AssetImporter::processShapeAsset(AssetImportObject* assetItem)
  1542. {
  1543. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Preparing Shape for Import: %s", assetItem->assetName.c_str());
  1544. activityLog.push_back(importLogBuffer);
  1545. String filePath = assetItem->filePath.getFullPath();
  1546. String fileName = assetItem->filePath.getFileName();
  1547. String fileExt = assetItem->filePath.getExtension();
  1548. if (assetItem->shapeInfo == nullptr)
  1549. {
  1550. GuiTreeViewCtrl* shapeInfo = new GuiTreeViewCtrl();
  1551. shapeInfo->registerObject();
  1552. if (fileExt.compare("dae") == 0)
  1553. {
  1554. enumColladaForImport(filePath, shapeInfo, false);
  1555. }
  1556. else if (fileExt.compare("dts") == 0)
  1557. {
  1558. enumDTSForImport(filePath, shapeInfo);
  1559. }
  1560. else
  1561. {
  1562. // Check if a cached DTS is available => no need to import the source file
  1563. // if we can load the DTS instead
  1564. AssimpShapeLoader loader;
  1565. loader.fillGuiTreeView(filePath.c_str(), shapeInfo);
  1566. }
  1567. assetItem->shapeInfo = shapeInfo;
  1568. }
  1569. if (activeImportConfig->AlwaysAddShapeSuffix)
  1570. {
  1571. assetItem->assetName += activeImportConfig->AddedShapeSuffix;
  1572. assetItem->cleanAssetName = assetItem->assetName;
  1573. }
  1574. S32 meshCount = dAtoi(assetItem->shapeInfo->getDataField(StringTable->insert("_meshCount"), nullptr));
  1575. S32 animCount = dAtoi(assetItem->shapeInfo->getDataField(StringTable->insert("_animCount"), nullptr));
  1576. S32 materialCount = dAtoi(assetItem->shapeInfo->getDataField(StringTable->insert("_materialCount"), nullptr));
  1577. S32 matItem = assetItem->shapeInfo->findItemByName("Materials");
  1578. dSprintf(importLogBuffer, sizeof(importLogBuffer), " Shape Info: Mesh Count: %i | Material Count: %i | Anim Count: %i", meshCount, animCount, materialCount);
  1579. activityLog.push_back(importLogBuffer);
  1580. AssetImportConfig* cachedConfig = new AssetImportConfig();;
  1581. cachedConfig->registerObject();
  1582. activeImportConfig->CopyTo(cachedConfig);
  1583. if (!activeImportConfig->UseManualShapeConfigRules)
  1584. {
  1585. //Try and load a sis file if it exists for this format
  1586. activeImportConfig->loadSISFile(assetItem->filePath);
  1587. }
  1588. if (activeImportConfig->ImportMesh && meshCount > 0)
  1589. {
  1590. }
  1591. if (activeImportConfig->ImportAnimations && animCount > 0)
  1592. {
  1593. //If we have animations but no meshes, then this is a pure animation file so we can swap the asset type here
  1594. if (meshCount == 0)
  1595. {
  1596. assetItem->assetType = "ShapeAnimationAsset";
  1597. }
  1598. }
  1599. if (activeImportConfig->ImportMaterials && materialCount > 0)
  1600. {
  1601. S32 materialId = assetItem->shapeInfo->getChildItem(matItem);
  1602. processShapeMaterialInfo(assetItem, materialId);
  1603. materialId = assetItem->shapeInfo->getNextSiblingItem(materialId);
  1604. while (materialId != 0)
  1605. {
  1606. processShapeMaterialInfo(assetItem, materialId);
  1607. materialId = assetItem->shapeInfo->getNextSiblingItem(materialId);
  1608. }
  1609. }
  1610. //restore the cached version just in case we loaded a sis file
  1611. cachedConfig->CopyTo(activeImportConfig);
  1612. cachedConfig->deleteObject();
  1613. assetItem->importStatus = AssetImportObject::Processed;
  1614. }
  1615. void AssetImporter::processShapeMaterialInfo(AssetImportObject* assetItem, S32 materialItemId)
  1616. {
  1617. String matName = assetItem->shapeInfo->getItemText(materialItemId);
  1618. String matAssetName = matName;
  1619. if (matName == assetItem->assetName)
  1620. {
  1621. //So apparently we managed to name the material the same as the shape. So we'll tweak the name
  1622. matAssetName += activeImportConfig->AddedMaterialSuffix;
  1623. }
  1624. //Do a check so we don't import materials that are on our ignore list
  1625. if (activeImportConfig->IgnoreMaterials.isNotEmpty())
  1626. {
  1627. U32 ignoredMatNamesCount = StringUnit::getUnitCount(activeImportConfig->IgnoreMaterials, ",;\t");
  1628. for (U32 i = 0; i < ignoredMatNamesCount; i++)
  1629. {
  1630. const char* ignoreMatName = StringUnit::getUnit(activeImportConfig->IgnoreMaterials, i, ",;\t");
  1631. if (FindMatch::isMatch(ignoreMatName, matName.c_str(), false))
  1632. {
  1633. //If we have a match to one of our ignore names, just bail out here and skip the material wholesale
  1634. return;
  1635. }
  1636. }
  1637. }
  1638. String materialItemValue = assetItem->shapeInfo->getItemValue(materialItemId);
  1639. AssetImportObject* matAssetItem = nullptr;
  1640. //If it happens to just be a color value instead of an image, we'll go ahead and skip doing any lookups
  1641. //TODO: properly map the 'texture' color to the material that gets created
  1642. if (materialItemValue.startsWith("Color:"))
  1643. {
  1644. matAssetItem = addImportingAsset("MaterialAsset", "", assetItem, matName);
  1645. }
  1646. else
  1647. {
  1648. Torque::Path filePath = materialItemValue;
  1649. String fullFilePath = filePath.getFullFileName().c_str();
  1650. String shapePathBase = assetItem->filePath.getRootAndPath();
  1651. if (fullFilePath.isNotEmpty())
  1652. {
  1653. if (!Platform::isFile(fullFilePath.c_str()))
  1654. {
  1655. //could be a stale path reference, such as if it was downloaded elsewhere. Trim to just the filename and see
  1656. //if we can find it there
  1657. //trim (not found) if needbe
  1658. fullFilePath = fullFilePath.replace(" (Not Found)", "");
  1659. fullFilePath = fullFilePath.replace(" (not found)", "");
  1660. String testFileName = shapePathBase + "/" + fullFilePath;
  1661. if (Platform::isFile(testFileName.c_str()))
  1662. {
  1663. filePath = testFileName;
  1664. }
  1665. else
  1666. {
  1667. //Hmm, didn't find it. It could be that the in-model filename could be different by virtue of
  1668. //image extension. Some files have source content files like psd's, but the mesh was exported to use
  1669. //a dds or png, etc
  1670. Torque::Path testFilePath = testFileName;
  1671. String imgFileName = AssetImporter::findImagePath(testFilePath.getPath() + "/" + testFilePath.getFileName());
  1672. if (imgFileName.isNotEmpty())
  1673. filePath = imgFileName;
  1674. }
  1675. }
  1676. matAssetItem = addImportingAsset("MaterialAsset", shapePathBase + "/", assetItem, matName);
  1677. AssetImportObject* imageAssetItem = addImportingAsset("ImageAsset", filePath, matAssetItem, "");
  1678. String suffixType;
  1679. String suffix = parseImageSuffixes(imageAssetItem->assetName, &suffixType);
  1680. if (suffix.isNotEmpty())
  1681. {
  1682. imageAssetItem->imageSuffixType = suffixType;
  1683. }
  1684. else
  1685. {
  1686. //we'll assume it's albedo
  1687. imageAssetItem->imageSuffixType = "Albedo";
  1688. }
  1689. }
  1690. else
  1691. {
  1692. matAssetItem = addImportingAsset("MaterialAsset", shapePathBase + "/" + matName, assetItem, matName);
  1693. }
  1694. }
  1695. //In the event we modified the asset name(such as appending _Mat to avoid naming conflicts) update the name here
  1696. //This preseves the 'clean asset name' which we can later use for lookups and the like as needed
  1697. if (matAssetItem && matAssetName != matName)
  1698. matAssetItem->assetName = matAssetName;
  1699. }
  1700. void AssetImporter::processSoundAsset(AssetImportObject* assetItem)
  1701. {
  1702. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Preparing Sound for Import: %s", assetItem->assetName.c_str());
  1703. activityLog.push_back(importLogBuffer);
  1704. if (activeImportConfig->AlwaysAddSoundSuffix)
  1705. {
  1706. assetItem->assetName += activeImportConfig->AddedSoundSuffix;
  1707. assetItem->cleanAssetName = assetItem->assetName;
  1708. }
  1709. assetItem->importStatus = AssetImportObject::Processed;
  1710. }
  1711. //
  1712. // Validation
  1713. //
  1714. bool AssetImporter::validateAssets()
  1715. {
  1716. importIssues = false;
  1717. resetAssetValidationStatus();
  1718. for (U32 i = 0; i < importingAssets.size(); i++)
  1719. {
  1720. validateAsset(importingAssets[i]);
  1721. resolveAssetItemIssues(importingAssets[i]);
  1722. }
  1723. return importIssues;
  1724. }
  1725. void AssetImporter::validateAsset(AssetImportObject* assetItem)
  1726. {
  1727. if (assetItem->importStatus == AssetImportObject::Skipped || assetItem->importStatus == AssetImportObject::NotProcessed)
  1728. return;
  1729. if (checkAssetForCollision(assetItem))
  1730. {
  1731. importIssues = true;
  1732. return;
  1733. }
  1734. if (!isReimport)
  1735. {
  1736. AssetQuery aQuery;
  1737. U32 numAssetsFound = AssetDatabase.findAllAssets(&aQuery);
  1738. for (U32 i = 0; i < numAssetsFound; i++)
  1739. {
  1740. StringTableEntry assetId = aQuery.mAssetList[i];
  1741. ModuleDefinition* moduleDef = AssetDatabase.getAssetModuleDefinition(assetId);
  1742. if ((moduleDef == NULL) || moduleDef->getModuleId() != StringTable->insert(targetModuleId.c_str()))
  1743. continue;
  1744. StringTableEntry assetName = AssetDatabase.getAssetName(assetId);
  1745. if (assetName == StringTable->insert(assetItem->assetName.c_str()))
  1746. {
  1747. assetItem->status = "Error";
  1748. assetItem->statusType = "DuplicateAsset";
  1749. assetItem->statusInfo = "Duplicate asset names found within the target module!\nAsset \"" + assetItem->assetName + "\" of type \"" + assetItem->assetType + "\" has a matching name.\nPlease rename it and try again!";
  1750. //log it
  1751. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Asset %s has an identically named asset in the target module.", assetItem->assetName.c_str());
  1752. activityLog.push_back(importLogBuffer);
  1753. break;
  1754. }
  1755. }
  1756. }
  1757. if (!assetItem->filePath.isEmpty() && !assetItem->generatedAsset && !Platform::isFile(assetItem->filePath.getFullPath().c_str()))
  1758. {
  1759. assetItem->status = "Error";
  1760. assetItem->statusType = "MissingFile";
  1761. assetItem->statusInfo = "Unable to find file to be imported with provided path: " + assetItem->filePath + "\n Please select a valid file.";
  1762. //log it
  1763. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Asset %s's file at %s was not found.", assetItem->assetName.c_str(), assetItem->filePath.getFullPath().c_str());
  1764. activityLog.push_back(importLogBuffer);
  1765. }
  1766. if (assetItem->status == String("Warning"))
  1767. {
  1768. if (activeImportConfig->WarningsAsErrors)
  1769. {
  1770. assetItem->status = "Error";
  1771. //log it
  1772. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Import configuration has treated an import warning as an error.", assetItem->assetName.c_str());
  1773. activityLog.push_back(importLogBuffer);
  1774. }
  1775. }
  1776. if (assetItem->status == String("Error"))
  1777. importIssues = true;
  1778. for (U32 i = 0; i < assetItem->childAssetItems.size(); i++)
  1779. {
  1780. validateAsset(assetItem->childAssetItems[i]);
  1781. resolveAssetItemIssues(assetItem->childAssetItems[i]);
  1782. }
  1783. return;
  1784. }
  1785. void AssetImporter::resetAssetValidationStatus(AssetImportObject* assetItem)
  1786. {
  1787. Vector<AssetImportObject*> itemList = importingAssets;
  1788. if (assetItem != nullptr)
  1789. itemList = assetItem->childAssetItems;
  1790. for (U32 i = 0; i < itemList.size(); i++)
  1791. {
  1792. if (itemList[i]->importStatus == AssetImportObject::Skipped)
  1793. continue;
  1794. itemList[i]->status = "";
  1795. itemList[i]->statusType = "";
  1796. itemList[i]->statusInfo = "";
  1797. //If it wasn't a match, try recusing on the children(if any)
  1798. resetAssetValidationStatus(itemList[i]);
  1799. }
  1800. }
  1801. bool AssetImporter::checkAssetForCollision(AssetImportObject* assetItemToCheck, AssetImportObject* assetItem)
  1802. {
  1803. bool results = false;
  1804. Vector<AssetImportObject*> itemList = importingAssets;
  1805. if (assetItem != nullptr)
  1806. itemList = assetItem->childAssetItems;
  1807. for (U32 i = 0; i < itemList.size(); i++)
  1808. {
  1809. AssetImportObject* importingAsset = itemList[i];
  1810. if (importingAsset->importStatus == AssetImportObject::Skipped)
  1811. continue;
  1812. if ((assetItemToCheck->assetName.compare(importingAsset->assetName) == 0) && (assetItemToCheck->getId() != importingAsset->getId()))
  1813. {
  1814. //we do have a collision, note the collsion and bail out
  1815. assetItemToCheck->status = "Warning";
  1816. assetItemToCheck->statusType = "DuplicateImportAsset";
  1817. assetItemToCheck->statusInfo = "Duplicate asset names found with importing assets!\nAsset \"" + importingAsset->assetName + "\" of the type \"" + importingAsset->assetType + "\" and \"" +
  1818. assetItemToCheck->assetName + "\" of the type \"" + assetItemToCheck->assetType + "\" have matching names.\nPlease rename one of them.";
  1819. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Warning! Asset %s, type %s has a naming collision with another importing asset: %s, type %s",
  1820. assetItemToCheck->assetName.c_str(), assetItemToCheck->assetType.c_str(),
  1821. importingAsset->assetName.c_str(), importingAsset->assetType.c_str());
  1822. activityLog.push_back(importLogBuffer);
  1823. return true;
  1824. }
  1825. //If it wasn't a match, try recusing on the children(if any)
  1826. results = checkAssetForCollision(assetItemToCheck, importingAsset);
  1827. if (results)
  1828. return results;
  1829. }
  1830. return results;
  1831. }
  1832. void AssetImporter::resolveAssetItemIssues(AssetImportObject* assetItem)
  1833. {
  1834. if (assetItem->statusType == String("DuplicateImportAsset") || assetItem->statusType == String("DuplicateAsset"))
  1835. {
  1836. String humanReadableReason = assetItem->statusType == String("DuplicateImportAsset") ? "Importing asset was duplicate of another importing asset" : "Importing asset was duplicate of an existing asset";
  1837. //get the config value for duplicateAutoResolution
  1838. if (activeImportConfig->DuplicateAutoResolution == String("AutoPrune"))
  1839. {
  1840. //delete the item
  1841. if (assetItem->parentAssetItem == nullptr)
  1842. {
  1843. //if there's no parent, just delete
  1844. deleteImportingAsset(assetItem);
  1845. }
  1846. else
  1847. {
  1848. //otherwise, we'll likely want to retain our dependency for our parent
  1849. assetItem->importStatus = AssetImportObject::UseForDependencies;
  1850. }
  1851. //log it's deletion
  1852. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Asset %s was autopruned due to %s as part of the Import Configuration", assetItem->assetName.c_str(), humanReadableReason.c_str());
  1853. activityLog.push_back(importLogBuffer);
  1854. importIssues = false;
  1855. }
  1856. else if (activeImportConfig->DuplicateAutoResolution == String("AutoRename"))
  1857. {
  1858. //Set trailing number
  1859. String renamedAssetName = assetItem->assetName;
  1860. String renamedAssetId = assetItem->moduleName + ":" + renamedAssetName;
  1861. String addedSuffix;
  1862. if (assetItem->assetType == String("ShapeAsset"))
  1863. addedSuffix = activeImportConfig->AddedShapeSuffix;
  1864. else if (assetItem->assetType == String("MaterialAsset"))
  1865. addedSuffix = activeImportConfig->AddedMaterialSuffix;
  1866. else if (assetItem->assetType == String("ImageAsset"))
  1867. addedSuffix = activeImportConfig->AddedImageSuffix;
  1868. else if (assetItem->assetType == String("SoundAsset"))
  1869. addedSuffix = activeImportConfig->AddedSoundSuffix;
  1870. //do the suffix if it isn't already on it
  1871. if (!renamedAssetName.endsWith(addedSuffix.c_str()))
  1872. {
  1873. renamedAssetName += addedSuffix;
  1874. renamedAssetId = assetItem->moduleName + ":" + renamedAssetName;
  1875. assetItem->assetName = renamedAssetName;
  1876. }
  1877. //if still conflicted
  1878. //add the directory prefix
  1879. if (AssetDatabase.isDeclaredAsset(renamedAssetId.c_str()))
  1880. {
  1881. renamedAssetName = getFolderPrefixedName(assetItem);
  1882. renamedAssetId = assetItem->moduleName + ":" + renamedAssetName;
  1883. assetItem->assetName = renamedAssetName;
  1884. }
  1885. bool appendedNumber = false;
  1886. U32 uniqueNumber = 0;
  1887. while (AssetDatabase.isDeclaredAsset(renamedAssetId.c_str()))
  1888. {
  1889. uniqueNumber++;
  1890. renamedAssetId = assetItem->moduleName + ":" + renamedAssetName + String::ToString(uniqueNumber);
  1891. appendedNumber = true;
  1892. }
  1893. if (appendedNumber)
  1894. {
  1895. renamedAssetName += String::ToString(uniqueNumber);
  1896. }
  1897. //Log it's renaming
  1898. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Asset %s was renamed due to %s as part of the Import Configuration", assetItem->assetName.c_str(), humanReadableReason.c_str());
  1899. activityLog.push_back(importLogBuffer);
  1900. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Asset %s was renamed to %s", assetItem->assetName.c_str(), renamedAssetName.c_str());
  1901. activityLog.push_back(importLogBuffer);
  1902. assetItem->assetName = renamedAssetName;
  1903. //Whatever status we had prior is no longer relevent, so reset the status
  1904. resetAssetValidationStatus(assetItem);
  1905. importIssues = false;
  1906. }
  1907. else if (activeImportConfig->DuplicateAutoResolution == String("UseExisting"))
  1908. {
  1909. }
  1910. else if (activeImportConfig->DuplicateAutoResolution == String("FolderPrefix"))
  1911. {
  1912. String renamedAssetName = getFolderPrefixedName(assetItem);
  1913. //Log it's renaming
  1914. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Asset %s was renamed due to %s as part of the Import Configuration", assetItem->assetName.c_str(), humanReadableReason.c_str());
  1915. activityLog.push_back(importLogBuffer);
  1916. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Asset %s was renamed to %s", assetItem->assetName.c_str(), renamedAssetName.c_str());
  1917. activityLog.push_back(importLogBuffer);
  1918. assetItem->assetName = renamedAssetName;
  1919. //Whatever status we had prior is no longer relevent, so reset the status
  1920. resetAssetValidationStatus(assetItem);
  1921. importIssues = false;
  1922. }
  1923. }
  1924. else if (assetItem->statusType == String("MissingFile"))
  1925. {
  1926. //Trigger callback to look?
  1927. }
  1928. }
  1929. void AssetImporter::resetImportConfig()
  1930. {
  1931. //use a default import config
  1932. if (activeImportConfig == nullptr)
  1933. {
  1934. activeImportConfig = new AssetImportConfig();
  1935. activeImportConfig->registerObject();
  1936. }
  1937. Settings* editorSettings;
  1938. //See if we can get our editor settings
  1939. if (Sim::findObject("EditorSettings", editorSettings))
  1940. {
  1941. String defaultImportConfig = editorSettings->value("Assets/AssetImporDefaultConfig");
  1942. //If we found it, grab the import configs
  1943. Settings* importConfigs;
  1944. if (Sim::findObject("AssetImportSettings", importConfigs))
  1945. {
  1946. //Now load the editor setting-deigned config!
  1947. activeImportConfig->loadImportConfig(importConfigs, defaultImportConfig.c_str());
  1948. }
  1949. }
  1950. }
  1951. //
  1952. // Importing
  1953. //
  1954. StringTableEntry AssetImporter::autoImportFile(Torque::Path filePath, String typeHint)
  1955. {
  1956. //Just in case we're reusing the same importer object from another import session, nuke any existing files
  1957. resetImportSession(true);
  1958. String assetType = getAssetTypeByFile(filePath);
  1959. if (assetType == String("Folder") || assetType == String("Zip"))
  1960. {
  1961. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Unable to import file %s because it is a folder or zip.", filePath.getFullPath().c_str());
  1962. activityLog.push_back(importLogBuffer);
  1963. dumpActivityLog();
  1964. return StringTable->EmptyString();
  1965. }
  1966. if (assetType.isEmpty())
  1967. {
  1968. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Unable to import file %s because it is of an unrecognized/unsupported type.", filePath.getFullPath().c_str());
  1969. activityLog.push_back(importLogBuffer);
  1970. dumpActivityLog();
  1971. return StringTable->EmptyString();
  1972. }
  1973. //Find out if the filepath has an associated module to it. If we're importing in-place, it needs to be within a module's directory
  1974. ModuleDefinition* targetModuleDef = AssetImporter::getModuleFromPath(filePath);
  1975. if (targetModuleDef == nullptr)
  1976. {
  1977. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Unable to import file %s because it is not in a valid module folder.", filePath.getFullPath().c_str());
  1978. activityLog.push_back(importLogBuffer);
  1979. dumpActivityLog();
  1980. return StringTable->EmptyString();
  1981. }
  1982. else
  1983. {
  1984. targetModuleId = targetModuleDef->getModuleId();
  1985. }
  1986. //set our path
  1987. targetPath = filePath.getPath();
  1988. resetImportConfig();
  1989. AssetImportObject* assetItem = addImportingAsset(assetType, filePath, nullptr, "");
  1990. processImportAssets();
  1991. bool hasIssues = validateAssets();
  1992. if (hasIssues)
  1993. {
  1994. //log it
  1995. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Import process has failed due to issues discovered during validation!");
  1996. activityLog.push_back(importLogBuffer);
  1997. }
  1998. else
  1999. {
  2000. importAssets();
  2001. }
  2002. dumpActivityLog();
  2003. if (hasIssues)
  2004. {
  2005. return StringTable->EmptyString();
  2006. }
  2007. else
  2008. {
  2009. String assetId = targetModuleId + ":" + assetItem->assetName;
  2010. return StringTable->insert(assetId.c_str());
  2011. }
  2012. }
  2013. void AssetImporter::importAssets(AssetImportObject* assetItem)
  2014. {
  2015. ModuleDefinition* moduleDef = ModuleDatabase.findModule(targetModuleId.c_str(), 1);
  2016. if (moduleDef == nullptr)
  2017. {
  2018. dSprintf(importLogBuffer, sizeof(importLogBuffer), "AssetImporter::importAssets - Unable to find moduleId %s", targetModuleId.c_str());
  2019. activityLog.push_back(importLogBuffer);
  2020. return;
  2021. }
  2022. Vector<AssetImportObject*> itemList = importingAssets;
  2023. if (assetItem != nullptr)
  2024. itemList = assetItem->childAssetItems;
  2025. for (U32 i = 0; i < itemList.size(); i++)
  2026. {
  2027. AssetImportObject* item = itemList[i];
  2028. if (!item->canImport())
  2029. continue;
  2030. Torque::Path assetPath;
  2031. if (item->assetType == String("ImageAsset"))
  2032. {
  2033. assetPath = importImageAsset(item);
  2034. }
  2035. else if (item->assetType == String("ShapeAsset"))
  2036. {
  2037. assetPath = importShapeAsset(item);
  2038. }
  2039. else if (item->assetType == String("SoundAsset"))
  2040. {
  2041. assetPath = importSoundAsset(item);
  2042. }
  2043. else if (item->assetType == String("MaterialAsset"))
  2044. {
  2045. assetPath = importMaterialAsset(item);
  2046. }
  2047. else
  2048. {
  2049. finalImportedAssetPath = String::EmptyString;
  2050. String processCommand = "import";
  2051. processCommand += item->assetType;
  2052. if (isMethod(processCommand.c_str()))
  2053. {
  2054. Con::executef(this, processCommand.c_str(), item);
  2055. assetPath = finalImportedAssetPath;
  2056. }
  2057. }
  2058. /*else if (importingAssets[i]->assetType == String("ShapeAnimationAsset"))
  2059. assetPath = ShapeAnimationAsset::importAsset(importingAssets[i]);*/
  2060. if (assetPath.isEmpty() && item->assetType != String("MaterialAsset"))
  2061. {
  2062. dSprintf(importLogBuffer, sizeof(importLogBuffer), "AssetImporter::importAssets - Import attempt of %s failed, so skipping asset.", item->assetName.c_str());
  2063. activityLog.push_back(importLogBuffer);
  2064. continue;
  2065. }
  2066. else
  2067. {
  2068. //If we got a valid filepath back from the import action, then we know we're good to go and we can go ahead and register the asset!
  2069. if (!isReimport)
  2070. {
  2071. bool registerSuccess = AssetDatabase.addDeclaredAsset(moduleDef, assetPath.getFullPath().c_str());
  2072. if (!registerSuccess)
  2073. {
  2074. dSprintf(importLogBuffer, sizeof(importLogBuffer), "AssetImporter::importAssets - Failed to successfully register new asset at path %s to moduleId %s", assetPath.getFullPath().c_str(), targetModuleId.c_str());
  2075. activityLog.push_back(importLogBuffer);
  2076. }
  2077. else
  2078. {
  2079. //Any special-case post-reg stuff here
  2080. if (item->assetType == String("ShapeAsset"))
  2081. {
  2082. String assetIdStr = item->moduleName + ":" + item->assetName;
  2083. StringTableEntry assetId = StringTable->insert(assetIdStr.c_str());
  2084. //forcefully update it's shape constructor
  2085. TSShapeConstructor* tss = TSShapeConstructor::findShapeConstructorByAssetId(assetId);
  2086. if(tss)
  2087. tss->setShapeAssetId(assetId);
  2088. }
  2089. }
  2090. }
  2091. else
  2092. {
  2093. String assetId = item->moduleName + ":" + item->assetName;
  2094. bool refreshSuccess = AssetDatabase.refreshAsset(assetId.c_str());
  2095. if (!refreshSuccess)
  2096. {
  2097. dSprintf(importLogBuffer, sizeof(importLogBuffer), "AssetImporter::importAssets - Failed to refresh reimporting asset %s.", item->assetName.c_str());
  2098. activityLog.push_back(importLogBuffer);
  2099. }
  2100. }
  2101. }
  2102. //Mark us as successfully imported
  2103. item->importStatus = AssetImportObject::Imported;
  2104. //recurse if needed
  2105. importAssets(item);
  2106. }
  2107. }
  2108. //
  2109. // Type-specific import logic
  2110. //
  2111. Torque::Path AssetImporter::importImageAsset(AssetImportObject* assetItem)
  2112. {
  2113. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Beginning importing of Image Asset: %s", assetItem->assetName.c_str());
  2114. activityLog.push_back(importLogBuffer);
  2115. ImageAsset* newAsset = new ImageAsset();
  2116. newAsset->registerObject();
  2117. StringTableEntry assetName = StringTable->insert(assetItem->assetName.c_str());
  2118. String imageFileName = assetItem->filePath.getFileName() + "." + assetItem->filePath.getExtension();
  2119. String assetPath = targetPath + "/" + imageFileName;
  2120. String tamlPath = targetPath + "/" + assetName + ".asset.taml";
  2121. String originalPath = assetItem->filePath.getFullPath().c_str();
  2122. char qualifiedFromFile[2048];
  2123. char qualifiedToFile[2048];
  2124. Platform::makeFullPathName(originalPath.c_str(), qualifiedFromFile, sizeof(qualifiedFromFile));
  2125. Platform::makeFullPathName(assetPath.c_str(), qualifiedToFile, sizeof(qualifiedToFile));
  2126. newAsset->setAssetName(assetName);
  2127. newAsset->setImageFileName(imageFileName.c_str());
  2128. //If it's not a re-import, check that the file isn't being in-place imported. If it isn't, store off the original
  2129. //file path for reimporting support later
  2130. if (!isReimport && String::compare(qualifiedFromFile, qualifiedToFile))
  2131. {
  2132. newAsset->setDataField(StringTable->insert("originalFilePath"), nullptr, qualifiedFromFile);
  2133. }
  2134. if (assetItem->typeHint != String::EmptyString)
  2135. {
  2136. newAsset->setImageType(ImageAsset::getImageTypeFromName(StringTable->insert(assetItem->typeHint.c_str())));
  2137. }
  2138. else
  2139. {
  2140. ImageAsset::ImageTypes imageType = ImageAsset::getImageTypeFromName(assetItem->imageSuffixType.c_str());
  2141. newAsset->setImageType(imageType);
  2142. }
  2143. Taml tamlWriter;
  2144. bool importSuccessful = tamlWriter.write(newAsset, tamlPath.c_str());
  2145. if (!importSuccessful)
  2146. {
  2147. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to write asset taml file %s", tamlPath.c_str());
  2148. activityLog.push_back(importLogBuffer);
  2149. return "";
  2150. }
  2151. if (!isReimport)
  2152. {
  2153. bool isInPlace = !String::compare(qualifiedFromFile, qualifiedToFile);
  2154. if (!isInPlace && !dPathCopy(qualifiedFromFile, qualifiedToFile, !isReimport))
  2155. {
  2156. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to copy file %s", assetItem->filePath.getFullPath().c_str());
  2157. activityLog.push_back(importLogBuffer);
  2158. return "";
  2159. }
  2160. }
  2161. return tamlPath;
  2162. }
  2163. Torque::Path AssetImporter::importMaterialAsset(AssetImportObject* assetItem)
  2164. {
  2165. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Beginning importing of Material Asset: %s", assetItem->assetName.c_str());
  2166. activityLog.push_back(importLogBuffer);
  2167. MaterialAsset* newAsset = new MaterialAsset();
  2168. newAsset->registerObject();
  2169. StringTableEntry assetName = StringTable->insert(assetItem->assetName.c_str());
  2170. String tamlPath = targetPath + "/" + assetName + ".asset.taml";
  2171. String scriptName = assetItem->assetName + "." TORQUE_SCRIPT_EXTENSION;
  2172. String scriptPath = targetPath + "/" + scriptName;
  2173. String originalPath = assetItem->filePath.getFullPath().c_str();
  2174. char qualifiedFromFile[2048];
  2175. Platform::makeFullPathName(originalPath.c_str(), qualifiedFromFile, sizeof(qualifiedFromFile));
  2176. newAsset->setAssetName(assetName);
  2177. newAsset->setScriptFile(scriptName.c_str());
  2178. newAsset->setDataField(StringTable->insert("originalFilePath"), nullptr, qualifiedFromFile);
  2179. newAsset->setDataField(StringTable->insert("materialDefinitionName"), nullptr, assetName);
  2180. //iterate through and write out the material maps dependencies
  2181. S32 dependencySlotId = 0;
  2182. for (U32 i = 0; i < assetItem->childAssetItems.size(); i++)
  2183. {
  2184. AssetImportObject* childItem = assetItem->childAssetItems[i];
  2185. if ((!childItem->canImport() && childItem->importStatus != AssetImportObject::UseForDependencies) || childItem->assetType.compare("ImageAsset") != 0)
  2186. continue;
  2187. char dependencyFieldName[64];
  2188. dSprintf(dependencyFieldName, 64, "imageMap%i", dependencySlotId);
  2189. char dependencyFieldDef[512];
  2190. dSprintf(dependencyFieldDef, 512, "%s=%s:%s", ASSET_ID_SIGNATURE, targetModuleId.c_str(), childItem->assetName.c_str());
  2191. newAsset->setDataField(StringTable->insert(dependencyFieldName), nullptr, dependencyFieldDef);
  2192. dependencySlotId++;
  2193. }
  2194. Taml tamlWriter;
  2195. bool importSuccessful = tamlWriter.write(newAsset, tamlPath.c_str());
  2196. if (!importSuccessful)
  2197. {
  2198. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to write asset taml file %s", tamlPath.c_str());
  2199. activityLog.push_back(importLogBuffer);
  2200. return "";
  2201. }
  2202. //build the ORMConfig file if we're flagged to and have valid image maps
  2203. if (activeImportConfig->CreateORMConfig)
  2204. {
  2205. AssetImportObject* ormMap = nullptr;
  2206. AssetImportObject* roughnessMap = nullptr;
  2207. AssetImportObject* metalnessMap = nullptr;
  2208. AssetImportObject* aoMap = nullptr;
  2209. //We need to find any/all respective image maps for the given channels
  2210. for (U32 i = 0; i < assetItem->childAssetItems.size(); i++)
  2211. {
  2212. AssetImportObject* childItem = assetItem->childAssetItems[i];
  2213. if (childItem->canImport() || childItem->assetType.compare("ImageAsset") != 0)
  2214. continue;
  2215. if (childItem->imageSuffixType.compare("ORMConfig") == 0)
  2216. ormMap = childItem;
  2217. else if(childItem->imageSuffixType.compare("Roughness") == 0)
  2218. roughnessMap = childItem;
  2219. else if (childItem->imageSuffixType.compare("Metalness") == 0)
  2220. metalnessMap = childItem;
  2221. else if (childItem->imageSuffixType.compare("AO") == 0)
  2222. aoMap = childItem;
  2223. }
  2224. if (ormMap != nullptr && ormMap->generatedAsset)
  2225. {
  2226. if (roughnessMap != nullptr || metalnessMap != nullptr || aoMap != nullptr)
  2227. {
  2228. U32 channelKey[4] = { 0,1,2,3 };
  2229. GFX->getTextureManager()->saveCompositeTexture(aoMap->filePath.getFullPath(), roughnessMap->filePath.getFullPath(), metalnessMap->filePath.getFullPath(), "",
  2230. channelKey, ormMap->filePath.getFullPath(), &GFXTexturePersistentProfile);
  2231. }
  2232. }
  2233. }
  2234. FileObject* file = new FileObject();
  2235. file->registerObject();
  2236. if (activeImportConfig->UseExistingMaterials && Platform::isFile(qualifiedFromFile))
  2237. {
  2238. //Now write the script file containing our material out
  2239. //There's 2 ways to do this. If we're in-place importing an existing asset, we can see if the definition existed already, like in an old
  2240. //materials.tscript file. if it does, we can just find the object by name, and save it out to our new file
  2241. //If not, we'll just generate one
  2242. Material* existingMat = MATMGR->getMaterialDefinitionByName(assetName);
  2243. //It's also possible that, for legacy models, the material hooks in via the material's mapTo field, and the material name is something completely different
  2244. //So we'll check for that as well if we didn't find it by name up above
  2245. if (existingMat == nullptr)
  2246. existingMat = MATMGR->getMaterialDefinitionByMapTo(assetName);
  2247. if (existingMat == nullptr && assetItem->assetName != assetItem->cleanAssetName)
  2248. {
  2249. existingMat = MATMGR->getMaterialDefinitionByName(assetItem->cleanAssetName);
  2250. if (existingMat == nullptr)
  2251. existingMat = MATMGR->getMaterialDefinitionByMapTo(assetItem->cleanAssetName);
  2252. }
  2253. if (existingMat)
  2254. {
  2255. PersistenceManager* persistMgr;
  2256. if (Sim::findObject("ImageAssetValidator", persistMgr))
  2257. {
  2258. for (U32 i = 0; i < assetItem->childAssetItems.size(); i++)
  2259. {
  2260. AssetImportObject* childItem = assetItem->childAssetItems[i];
  2261. if (childItem->canImport() || childItem->assetType.compare("ImageAsset") != 0)
  2262. continue;
  2263. String path = childItem->filePath.getFullFileName();
  2264. String mapFieldName = "";
  2265. String assetFieldName = "";
  2266. ImageAsset::ImageTypes imageType = ImageAsset::getImageTypeFromName(childItem->imageSuffixType);
  2267. if (imageType == ImageAsset::ImageTypes::Albedo || childItem->imageSuffixType.isEmpty())
  2268. {
  2269. mapFieldName = "DiffuseMap";
  2270. }
  2271. else if (imageType == ImageAsset::ImageTypes::Normal)
  2272. {
  2273. mapFieldName = "NormalMap";
  2274. }
  2275. else if (imageType == ImageAsset::ImageTypes::ORMConfig)
  2276. {
  2277. mapFieldName = "ORMConfig";
  2278. }
  2279. else if (imageType == ImageAsset::ImageTypes::Metalness)
  2280. {
  2281. mapFieldName = "MetalnessMap";
  2282. }
  2283. else if (imageType == ImageAsset::ImageTypes::AO)
  2284. {
  2285. mapFieldName = "AOMap";
  2286. }
  2287. else if (imageType == ImageAsset::ImageTypes::Roughness)
  2288. {
  2289. mapFieldName = "RoughnessMap";
  2290. }
  2291. assetFieldName = mapFieldName + "Asset[0]";
  2292. mapFieldName += "[0]";
  2293. //If there's already an existing image map file on the material definition in this slot, don't override it
  2294. if (!path.isEmpty())
  2295. existingMat->writeField(mapFieldName.c_str(), path.c_str());
  2296. String targetAsset = targetModuleId + ":" + childItem->assetName;
  2297. existingMat->writeField(assetFieldName.c_str(), targetAsset.c_str());
  2298. }
  2299. persistMgr->setDirty(existingMat);
  2300. }
  2301. else
  2302. {
  2303. Con::errorf("ImageAssetValidator not found!");
  2304. }
  2305. }
  2306. else
  2307. {
  2308. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Failed to find original material definition %s!", assetName);
  2309. activityLog.push_back(importLogBuffer);
  2310. return tamlPath;
  2311. }
  2312. }
  2313. else if (file->openForWrite(scriptPath.c_str()))
  2314. {
  2315. file->writeLine((U8*)"//--- OBJECT WRITE BEGIN ---");
  2316. char lineBuffer[1024];
  2317. dSprintf(lineBuffer, 1024, "singleton Material(%s) {", assetName);
  2318. file->writeLine((U8*)lineBuffer);
  2319. dSprintf(lineBuffer, 1024, " mapTo=\"%s\";", assetName);
  2320. file->writeLine((U8*)lineBuffer);
  2321. bool hasRoughness = false;
  2322. for (U32 i = 0; i < assetItem->childAssetItems.size(); i++)
  2323. {
  2324. AssetImportObject* childItem = assetItem->childAssetItems[i];
  2325. if ((!childItem->canImport() && childItem->importStatus != AssetImportObject::UseForDependencies) || childItem->assetType.compare("ImageAsset") != 0)
  2326. continue;
  2327. String mapFieldName = "";
  2328. String assetFieldName = "";
  2329. ImageAsset::ImageTypes imageType = ImageAsset::getImageTypeFromName(childItem->imageSuffixType);
  2330. if (imageType == ImageAsset::ImageTypes::Albedo || childItem->imageSuffixType.isEmpty())
  2331. {
  2332. mapFieldName = "DiffuseMap";
  2333. }
  2334. else if (imageType == ImageAsset::ImageTypes::Normal)
  2335. {
  2336. mapFieldName = "NormalMap";
  2337. }
  2338. else if (imageType == ImageAsset::ImageTypes::ORMConfig)
  2339. {
  2340. mapFieldName = "ORMConfigMap";
  2341. }
  2342. else if (imageType == ImageAsset::ImageTypes::Metalness)
  2343. {
  2344. mapFieldName = "MetalnessMap";
  2345. }
  2346. else if (imageType == ImageAsset::ImageTypes::AO)
  2347. {
  2348. mapFieldName = "AOMap";
  2349. }
  2350. else if (imageType == ImageAsset::ImageTypes::Roughness)
  2351. {
  2352. mapFieldName = "RoughnessMap";
  2353. hasRoughness = true;
  2354. }
  2355. assetFieldName = mapFieldName + "Asset";
  2356. mapFieldName += "[0]";
  2357. //String path = childItem->filePath.getFullFileName();
  2358. //dSprintf(lineBuffer, 1024, " %s = \"%s\";", mapFieldName.c_str(), path.c_str());
  2359. //file->writeLine((U8*)lineBuffer);
  2360. dSprintf(lineBuffer, 1024, " %s = \"%s:%s\";", assetFieldName.c_str(), targetModuleId.c_str(), childItem->assetName.c_str());
  2361. file->writeLine((U8*)lineBuffer);
  2362. }
  2363. if (hasRoughness)
  2364. {
  2365. file->writeLine((U8*)" invertSmoothness = true;");
  2366. }
  2367. file->writeLine((U8*)"};");
  2368. file->writeLine((U8*)"//--- OBJECT WRITE END ---");
  2369. file->close();
  2370. }
  2371. else
  2372. {
  2373. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to write asset script file %s", scriptPath.c_str());
  2374. activityLog.push_back(importLogBuffer);
  2375. return "";
  2376. }
  2377. return tamlPath;
  2378. }
  2379. Torque::Path AssetImporter::importShapeAsset(AssetImportObject* assetItem)
  2380. {
  2381. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Beginning importing of Shape Asset: %s", assetItem->assetName.c_str());
  2382. activityLog.push_back(importLogBuffer);
  2383. ShapeAsset* newAsset = new ShapeAsset();
  2384. newAsset->registerObject();
  2385. StringTableEntry assetName = StringTable->insert(assetItem->assetName.c_str());
  2386. String shapeFileName = assetItem->filePath.getFileName() + "." + assetItem->filePath.getExtension();
  2387. String constructorFileName = assetItem->filePath.getFileName() + "." TORQUE_SCRIPT_EXTENSION;
  2388. String assetPath = targetPath + "/" + shapeFileName;
  2389. String constructorPath = targetPath + "/" + constructorFileName;
  2390. String tamlPath = targetPath + "/" + assetName + ".asset.taml";
  2391. String originalPath = assetItem->filePath.getFullPath().c_str();
  2392. String originalConstructorPath = assetItem->filePath.getPath() + "/" + constructorFileName;
  2393. char qualifiedFromFile[2048];
  2394. char qualifiedToFile[2048];
  2395. char qualifiedFromCSFile[2048];
  2396. char qualifiedToCSFile[2048];
  2397. Platform::makeFullPathName(originalPath.c_str(), qualifiedFromFile, sizeof(qualifiedFromFile));
  2398. Platform::makeFullPathName(assetPath.c_str(), qualifiedToFile, sizeof(qualifiedToFile));
  2399. Platform::makeFullPathName(originalConstructorPath.c_str(), qualifiedFromCSFile, sizeof(qualifiedFromCSFile));
  2400. Platform::makeFullPathName(constructorPath.c_str(), qualifiedToCSFile, sizeof(qualifiedToCSFile));
  2401. newAsset->setAssetName(assetName);
  2402. newAsset->setShapeFile(shapeFileName.c_str());
  2403. newAsset->setShapeConstructorFile(constructorFileName.c_str());
  2404. AssetImportConfig* cachedConfig = new AssetImportConfig();
  2405. cachedConfig->registerObject();
  2406. activeImportConfig->CopyTo(cachedConfig);
  2407. if (!activeImportConfig->UseManualShapeConfigRules)
  2408. {
  2409. //Try and load a sis file if it exists for this format
  2410. activeImportConfig->loadSISFile(assetItem->filePath);
  2411. }
  2412. //If it's not a re-import, check that the file isn't being in-place imported. If it isn't, store off the original
  2413. //file path for reimporting support later
  2414. if (!isReimport && String::compare(qualifiedFromFile, qualifiedToFile))
  2415. {
  2416. newAsset->setDataField(StringTable->insert("originalFilePath"), nullptr, qualifiedFromFile);
  2417. }
  2418. //iterate through and write out the material maps dependencies
  2419. S32 dependencySlotId = 0;
  2420. for (U32 i = 0; i < assetItem->childAssetItems.size(); i++)
  2421. {
  2422. AssetImportObject* childItem = assetItem->childAssetItems[i];
  2423. if (!childItem->canImport() && childItem->importStatus != AssetImportObject::UseForDependencies)
  2424. continue;
  2425. if (childItem->assetType.compare("MaterialAsset") == 0)
  2426. {
  2427. char dependencyFieldName[64];
  2428. dSprintf(dependencyFieldName, 64, "materialSlot%i", dependencySlotId);
  2429. char dependencyFieldDef[512];
  2430. dSprintf(dependencyFieldDef, 512, "%s=%s:%s", ASSET_ID_SIGNATURE, targetModuleId.c_str(), childItem->assetName.c_str());
  2431. newAsset->setDataField(StringTable->insert(dependencyFieldName), nullptr, dependencyFieldDef);
  2432. dependencySlotId++;
  2433. }
  2434. else if (childItem->assetType.compare("ShapeAnimationAsset") == 0)
  2435. {
  2436. char dependencyFieldName[64];
  2437. dSprintf(dependencyFieldName, 64, "animationSequence%i", dependencySlotId);
  2438. char dependencyFieldDef[512];
  2439. dSprintf(dependencyFieldDef, 512, "%s=%s:%s", ASSET_ID_SIGNATURE, targetModuleId.c_str(), childItem->assetName.c_str());
  2440. newAsset->setDataField(StringTable->insert(dependencyFieldName), nullptr, dependencyFieldDef);
  2441. dependencySlotId++;
  2442. }
  2443. }
  2444. Taml tamlWriter;
  2445. bool importSuccessful = tamlWriter.write(newAsset, tamlPath.c_str());
  2446. if (!importSuccessful)
  2447. {
  2448. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to write asset taml file %s", tamlPath.c_str());
  2449. activityLog.push_back(importLogBuffer);
  2450. return "";
  2451. }
  2452. bool makeNewConstructor = true;
  2453. if (!isReimport)
  2454. {
  2455. bool isInPlace = !String::compare(qualifiedFromFile, qualifiedToFile);
  2456. if (!isInPlace && !dPathCopy(qualifiedFromFile, qualifiedToFile, !isReimport))
  2457. {
  2458. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to copy file %s", qualifiedFromFile);
  2459. activityLog.push_back(importLogBuffer);
  2460. return "";
  2461. }
  2462. if (!isInPlace)
  2463. {
  2464. if (Platform::isFile(qualifiedFromCSFile))
  2465. {
  2466. if (!dPathCopy(qualifiedFromCSFile, qualifiedToCSFile, !isReimport))
  2467. {
  2468. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to copy file %s", qualifiedFromCSFile);
  2469. activityLog.push_back(importLogBuffer);
  2470. }
  2471. else
  2472. {
  2473. //We successfully copied the original constructor file, so no extra work required
  2474. makeNewConstructor = false;
  2475. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Successfully copied original TSShape Constructor file %s", qualifiedFromCSFile);
  2476. activityLog.push_back(importLogBuffer);
  2477. }
  2478. }
  2479. }
  2480. else
  2481. {
  2482. //We're doing an in-place import, so double check we've already got a constructor file in the expected spot
  2483. if (Platform::isFile(qualifiedFromCSFile))
  2484. {
  2485. //Yup, found it, we're good to go
  2486. makeNewConstructor = false;
  2487. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Existing TSShape Constructor file %s found", qualifiedFromCSFile);
  2488. activityLog.push_back(importLogBuffer);
  2489. }
  2490. else
  2491. {
  2492. //Didn't work, but it's possible it's using the old .cs extension when our extension variable is set to something else, so check that one as well just to be sure
  2493. Torque::Path constrFilePath = qualifiedFromCSFile;
  2494. constrFilePath.setExtension("cs");
  2495. if (Platform::isFile(constrFilePath.getFullPath().c_str()))
  2496. {
  2497. //Yup, found it, we're good to go
  2498. makeNewConstructor = false;
  2499. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Existing TSShape Constructor file %s found", constrFilePath.getFullPath().c_str());
  2500. activityLog.push_back(importLogBuffer);
  2501. }
  2502. }
  2503. }
  2504. }
  2505. if (makeNewConstructor)
  2506. {
  2507. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Beginning creation of new TSShapeConstructor file: %s", qualifiedToCSFile);
  2508. activityLog.push_back(importLogBuffer);
  2509. //find/create shape constructor
  2510. TSShapeConstructor* constructor = TSShapeConstructor::findShapeConstructorByFilename(Torque::Path(qualifiedToFile).getFullPath());
  2511. if (constructor == nullptr)
  2512. {
  2513. String fullAssetName = assetItem->moduleName + ":" + assetItem->assetName;
  2514. constructor = new TSShapeConstructor(StringTable->insert(fullAssetName.c_str()));
  2515. String constructorName = assetItem->filePath.getFileName() + assetItem->filePath.getExtension().substr(0, 3);
  2516. constructorName.replace(" ", "_");
  2517. constructorName.replace("-", "_");
  2518. constructorName.replace(".", "_");
  2519. constructorName = Sim::getUniqueName(constructorName.c_str());
  2520. constructor->registerObject(constructorName.c_str());
  2521. }
  2522. //now we write the import config logic into the constructor itself to ensure we load like we wanted it to
  2523. String neverImportMats;
  2524. if (activeImportConfig->IgnoreMaterials.isNotEmpty())
  2525. {
  2526. U32 ignoredMatNamesCount = StringUnit::getUnitCount(activeImportConfig->IgnoreMaterials, ",;\t");
  2527. for (U32 i = 0; i < ignoredMatNamesCount; i++)
  2528. {
  2529. if (i == 0)
  2530. neverImportMats = StringUnit::getUnit(activeImportConfig->IgnoreMaterials, i, ",;\t");
  2531. else
  2532. neverImportMats += String("\t") + StringUnit::getUnit(activeImportConfig->IgnoreMaterials, i, ",;\t");
  2533. }
  2534. }
  2535. if (activeImportConfig->DoUpAxisOverride)
  2536. {
  2537. S32 upAxis = domUpAxisType::UPAXISTYPE_Z_UP;
  2538. if (activeImportConfig->UpAxisOverride.compare("X_AXIS") == 0)
  2539. {
  2540. upAxis = domUpAxisType::UPAXISTYPE_X_UP;
  2541. }
  2542. else if (activeImportConfig->UpAxisOverride.compare("Y_AXIS") == 0)
  2543. {
  2544. upAxis = domUpAxisType::UPAXISTYPE_Y_UP;
  2545. }
  2546. else if (activeImportConfig->UpAxisOverride.compare("Z_AXIS") == 0)
  2547. {
  2548. upAxis = domUpAxisType::UPAXISTYPE_Z_UP;
  2549. }
  2550. constructor->mOptions.upAxis = (domUpAxisType)upAxis;
  2551. }
  2552. if (activeImportConfig->DoScaleOverride)
  2553. constructor->mOptions.unit = activeImportConfig->ScaleOverride;
  2554. else
  2555. constructor->mOptions.unit = -1;
  2556. enum eAnimTimingType
  2557. {
  2558. FrameCount = 0,
  2559. Seconds = 1,
  2560. Milliseconds = 1000
  2561. };
  2562. S32 lodType = ColladaUtils::ImportOptions::eLodType::TrailingNumber;
  2563. if (activeImportConfig->LODType.compare("TrailingNumber") == 0)
  2564. lodType = ColladaUtils::ImportOptions::eLodType::TrailingNumber;
  2565. else if (activeImportConfig->LODType.compare("SingleSize") == 0)
  2566. lodType = ColladaUtils::ImportOptions::eLodType::SingleSize;
  2567. else if (activeImportConfig->LODType.compare("DetectDTS") == 0)
  2568. lodType = ColladaUtils::ImportOptions::eLodType::DetectDTS;
  2569. constructor->mOptions.lodType = (ColladaUtils::ImportOptions::eLodType)lodType;
  2570. constructor->mOptions.singleDetailSize = activeImportConfig->convertLeftHanded;
  2571. constructor->mOptions.alwaysImport = activeImportConfig->AlwaysImportedNodes;
  2572. constructor->mOptions.neverImport = activeImportConfig->AlwaysIgnoreNodes;
  2573. constructor->mOptions.alwaysImportMesh = activeImportConfig->AlwaysImportMeshes;
  2574. constructor->mOptions.neverImportMesh = activeImportConfig->AlwaysIgnoreMeshes;
  2575. constructor->mOptions.ignoreNodeScale = activeImportConfig->IgnoreNodeScale;
  2576. constructor->mOptions.adjustCenter = activeImportConfig->AdjustCenter;
  2577. constructor->mOptions.adjustFloor = activeImportConfig->AdjustFloor;
  2578. constructor->mOptions.convertLeftHanded = activeImportConfig->convertLeftHanded;
  2579. constructor->mOptions.calcTangentSpace = activeImportConfig->calcTangentSpace;
  2580. constructor->mOptions.genUVCoords = activeImportConfig->genUVCoords;
  2581. constructor->mOptions.flipUVCoords = activeImportConfig->flipUVCoords;
  2582. constructor->mOptions.findInstances = activeImportConfig->findInstances;
  2583. constructor->mOptions.limitBoneWeights = activeImportConfig->limitBoneWeights;
  2584. constructor->mOptions.joinIdenticalVerts = activeImportConfig->JoinIdenticalVerts;
  2585. constructor->mOptions.reverseWindingOrder = activeImportConfig->reverseWindingOrder;
  2586. constructor->mOptions.invertNormals = activeImportConfig->invertNormals;
  2587. constructor->mOptions.removeRedundantMats = activeImportConfig->removeRedundantMats;
  2588. S32 animTimingType;
  2589. if (activeImportConfig->animTiming.compare("FrameCount") == 0)
  2590. animTimingType = ColladaUtils::ImportOptions::eAnimTimingType::FrameCount;
  2591. else if (activeImportConfig->animTiming.compare("Seconds") == 0)
  2592. animTimingType = ColladaUtils::ImportOptions::eAnimTimingType::Seconds;
  2593. else// (activeImportConfig->animTiming.compare("Milliseconds") == 0)
  2594. animTimingType = ColladaUtils::ImportOptions::eAnimTimingType::Milliseconds;
  2595. constructor->mOptions.animTiming = (ColladaUtils::ImportOptions::eAnimTimingType)animTimingType;
  2596. constructor->mOptions.animFPS = activeImportConfig->animFPS;
  2597. constructor->mOptions.neverImportMat = neverImportMats;
  2598. PersistenceManager* constructorPersist = new PersistenceManager();
  2599. constructorPersist->registerObject();
  2600. constructorPersist->setDirty(constructor, qualifiedToCSFile);
  2601. if (!constructorPersist->saveDirtyObject(constructor))
  2602. {
  2603. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Failed to save shape constructor file to %s", constructorPath.c_str());
  2604. activityLog.push_back(importLogBuffer);
  2605. }
  2606. else
  2607. {
  2608. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Finished creating shape constructor file to %s", constructorPath.c_str());
  2609. activityLog.push_back(importLogBuffer);
  2610. }
  2611. constructorPersist->destroySelf();
  2612. }
  2613. //restore the cached version just in case we loaded a sis file
  2614. cachedConfig->CopyTo(activeImportConfig);
  2615. cachedConfig->deleteObject();
  2616. return tamlPath;
  2617. }
  2618. Torque::Path AssetImporter::importSoundAsset(AssetImportObject* assetItem)
  2619. {
  2620. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Beginning importing of Sound Asset: %s", assetItem->assetName.c_str());
  2621. activityLog.push_back(importLogBuffer);
  2622. SoundAsset* newAsset = new SoundAsset();
  2623. newAsset->registerObject();
  2624. StringTableEntry assetName = StringTable->insert(assetItem->assetName.c_str());
  2625. String imageFileName = assetItem->filePath.getFileName() + "." + assetItem->filePath.getExtension();
  2626. String assetPath = targetPath + "/" + imageFileName;
  2627. String tamlPath = targetPath + "/" + assetName + ".asset.taml";
  2628. String originalPath = assetItem->filePath.getFullPath().c_str();
  2629. char qualifiedFromFile[2048];
  2630. char qualifiedToFile[2048];
  2631. Platform::makeFullPathName(originalPath.c_str(), qualifiedFromFile, sizeof(qualifiedFromFile));
  2632. Platform::makeFullPathName(assetPath.c_str(), qualifiedToFile, sizeof(qualifiedToFile));
  2633. newAsset->setAssetName(assetName);
  2634. newAsset->setSoundFile(imageFileName.c_str());
  2635. //If it's not a re-import, check that the file isn't being in-place imported. If it isn't, store off the original
  2636. //file path for reimporting support later
  2637. if (!isReimport && String::compare(qualifiedFromFile, qualifiedToFile))
  2638. {
  2639. newAsset->setDataField(StringTable->insert("originalFilePath"), nullptr, qualifiedFromFile);
  2640. }
  2641. Taml tamlWriter;
  2642. bool importSuccessful = tamlWriter.write(newAsset, tamlPath.c_str());
  2643. if (!importSuccessful)
  2644. {
  2645. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to write asset taml file %s", tamlPath.c_str());
  2646. activityLog.push_back(importLogBuffer);
  2647. return "";
  2648. }
  2649. if (!isReimport)
  2650. {
  2651. bool isInPlace = !String::compare(qualifiedFromFile, qualifiedToFile);
  2652. if (!isInPlace && !dPathCopy(qualifiedFromFile, qualifiedToFile, !isReimport))
  2653. {
  2654. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to copy file %s", assetItem->filePath.getFullPath().c_str());
  2655. activityLog.push_back(importLogBuffer);
  2656. return "";
  2657. }
  2658. }
  2659. return tamlPath;
  2660. }
  2661. Torque::Path AssetImporter::importShapeAnimationAsset(AssetImportObject* assetItem)
  2662. {
  2663. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Beginning importing of Shape Animation Asset: %s", assetItem->assetName.c_str());
  2664. activityLog.push_back(importLogBuffer);
  2665. ShapeAnimationAsset* newAsset = new ShapeAnimationAsset();
  2666. newAsset->registerObject();
  2667. StringTableEntry assetName = StringTable->insert(assetItem->assetName.c_str());
  2668. String imageFileName = assetItem->filePath.getFileName() + "." + assetItem->filePath.getExtension();
  2669. String assetPath = targetPath + "/" + imageFileName;
  2670. String tamlPath = targetPath + "/" + assetName + ".asset.taml";
  2671. String originalPath = assetItem->filePath.getFullPath().c_str();
  2672. char qualifiedFromFile[2048];
  2673. char qualifiedToFile[2048];
  2674. Platform::makeFullPathName(originalPath.c_str(), qualifiedFromFile, sizeof(qualifiedFromFile));
  2675. Platform::makeFullPathName(assetPath.c_str(), qualifiedToFile, sizeof(qualifiedToFile));
  2676. newAsset->setAssetName(assetName);
  2677. newAsset->setAnimationFile(imageFileName.c_str());
  2678. //If it's not a re-import, check that the file isn't being in-place imported. If it isn't, store off the original
  2679. //file path for reimporting support later
  2680. if (!isReimport && String::compare(qualifiedFromFile, qualifiedToFile))
  2681. {
  2682. newAsset->setDataField(StringTable->insert("originalFilePath"), nullptr, qualifiedFromFile);
  2683. }
  2684. Taml tamlWriter;
  2685. bool importSuccessful = tamlWriter.write(newAsset, tamlPath.c_str());
  2686. if (!importSuccessful)
  2687. {
  2688. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to write asset taml file %s", tamlPath.c_str());
  2689. activityLog.push_back(importLogBuffer);
  2690. return "";
  2691. }
  2692. if (!isReimport)
  2693. {
  2694. bool isInPlace = !String::compare(qualifiedFromFile, qualifiedToFile);
  2695. if (!isInPlace && !dPathCopy(qualifiedFromFile, qualifiedToFile, !isReimport))
  2696. {
  2697. dSprintf(importLogBuffer, sizeof(importLogBuffer), "Error! Unable to copy file %s", assetItem->filePath.getFullPath().c_str());
  2698. activityLog.push_back(importLogBuffer);
  2699. return "";
  2700. }
  2701. }
  2702. return tamlPath;
  2703. }