assetImporter.cpp 133 KB

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