assetImporter.cpp 136 KB

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