ObjFileMtlImporter.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
  35. #include "ObjFileMtlImporter.h"
  36. #include "ObjFileData.h"
  37. #include "ObjTools.h"
  38. #include <assimp/DefaultIOSystem.h>
  39. #include <assimp/ParsingUtils.h>
  40. #include <assimp/fast_atof.h>
  41. #include <assimp/material.h>
  42. #include <stdlib.h>
  43. #include <assimp/DefaultLogger.hpp>
  44. namespace Assimp {
  45. // Material specific token (case insensitive compare)
  46. static constexpr char DiffuseTexture[] = "map_Kd";
  47. static constexpr char AmbientTexture[] = "map_Ka";
  48. static constexpr char SpecularTexture[] = "map_Ks";
  49. static constexpr char OpacityTexture[] = "map_d";
  50. static constexpr char EmissiveTexture1[] = "map_emissive";
  51. static constexpr char EmissiveTexture2[] = "map_Ke";
  52. static constexpr char BumpTexture1[] = "map_bump";
  53. static constexpr char BumpTexture2[] = "bump";
  54. static constexpr char NormalTextureV1[] = "map_Kn";
  55. static constexpr char NormalTextureV2[] = "norm";
  56. static constexpr char ReflectionTexture[] = "refl";
  57. static constexpr char DisplacementTexture1[] = "map_disp";
  58. static constexpr char DisplacementTexture2[] = "disp";
  59. static constexpr char SpecularityTexture[] = "map_ns";
  60. static constexpr char RoughnessTexture[] = "map_Pr";
  61. static constexpr char MetallicTexture[] = "map_Pm";
  62. static constexpr char SheenTexture[] = "map_Ps";
  63. static constexpr char RMATexture[] = "map_Ps";
  64. // texture option specific token
  65. static constexpr char BlendUOption[] = "-blendu";
  66. static constexpr char BlendVOption[] = "-blendv";
  67. static constexpr char BoostOption[] = "-boost";
  68. static constexpr char ModifyMapOption[] = "-mm";
  69. static constexpr char OffsetOption[] = "-o";
  70. static constexpr char ScaleOption[] = "-s";
  71. static constexpr char TurbulenceOption[] = "-t";
  72. static constexpr char ResolutionOption[] = "-texres";
  73. static constexpr char ClampOption[] = "-clamp";
  74. static constexpr char BumpOption[] = "-bm";
  75. static constexpr char ChannelOption[] = "-imfchan";
  76. static constexpr char TypeOption[] = "-type";
  77. // -------------------------------------------------------------------
  78. // Constructor
  79. ObjFileMtlImporter::ObjFileMtlImporter(std::vector<char> &buffer,
  80. const std::string &strAbsPath,
  81. ObjFile::Model *pModel) :
  82. m_strAbsPath(strAbsPath),
  83. m_DataIt(buffer.begin()),
  84. m_DataItEnd(buffer.end()),
  85. m_pModel(pModel),
  86. m_uiLine(0),
  87. m_buffer() {
  88. ai_assert(nullptr != m_pModel);
  89. m_buffer.resize(BUFFERSIZE);
  90. std::fill(m_buffer.begin(), m_buffer.end(), '\0');
  91. if (nullptr == m_pModel->mDefaultMaterial) {
  92. m_pModel->mDefaultMaterial = new ObjFile::Material;
  93. m_pModel->mDefaultMaterial->MaterialName.Set("default");
  94. }
  95. // Try with OS folder separator first
  96. char folderSeparator = DefaultIOSystem().getOsSeparator();
  97. std::size_t found = m_strAbsPath.find_last_of(folderSeparator);
  98. if (found == std::string::npos) {
  99. // Not found, try alternative folder separator
  100. folderSeparator = (folderSeparator == '/' ? '\\' : '/');
  101. found = m_strAbsPath.find_last_of(folderSeparator);
  102. }
  103. if (found != std::string::npos) {
  104. m_strAbsPath = m_strAbsPath.substr(0, found + 1);
  105. } else {
  106. m_strAbsPath = "";
  107. }
  108. load();
  109. }
  110. // -------------------------------------------------------------------
  111. // Loads the material description
  112. void ObjFileMtlImporter::load() {
  113. if (m_DataIt == m_DataItEnd)
  114. return;
  115. while (m_DataIt != m_DataItEnd) {
  116. switch (*m_DataIt) {
  117. case 'k':
  118. case 'K': {
  119. ++m_DataIt;
  120. if (*m_DataIt == 'a') // Ambient color
  121. {
  122. ++m_DataIt;
  123. if (m_pModel->mCurrentMaterial != nullptr)
  124. getColorRGBA(&m_pModel->mCurrentMaterial->ambient);
  125. } else if (*m_DataIt == 'd') {
  126. // Diffuse color
  127. ++m_DataIt;
  128. if (m_pModel->mCurrentMaterial != nullptr)
  129. getColorRGBA(&m_pModel->mCurrentMaterial->diffuse);
  130. } else if (*m_DataIt == 's') {
  131. ++m_DataIt;
  132. if (m_pModel->mCurrentMaterial != nullptr)
  133. getColorRGBA(&m_pModel->mCurrentMaterial->specular);
  134. } else if (*m_DataIt == 'e') {
  135. ++m_DataIt;
  136. if (m_pModel->mCurrentMaterial != nullptr)
  137. getColorRGBA(&m_pModel->mCurrentMaterial->emissive);
  138. }
  139. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  140. } break;
  141. case 'T': {
  142. ++m_DataIt;
  143. // Material transmission color
  144. if (*m_DataIt == 'f') {
  145. ++m_DataIt;
  146. if (m_pModel->mCurrentMaterial != nullptr)
  147. getColorRGBA(&m_pModel->mCurrentMaterial->transparent);
  148. } else if (*m_DataIt == 'r') {
  149. // Material transmission alpha value
  150. ++m_DataIt;
  151. ai_real d;
  152. getFloatValue(d);
  153. if (m_pModel->mCurrentMaterial != nullptr)
  154. m_pModel->mCurrentMaterial->alpha = static_cast<ai_real>(1.0) - d;
  155. }
  156. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  157. } break;
  158. case 'd': {
  159. if (*(m_DataIt + 1) == 'i' && *(m_DataIt + 2) == 's' && *(m_DataIt + 3) == 'p') {
  160. // A displacement map
  161. getTexture();
  162. } else {
  163. // Alpha value
  164. ++m_DataIt;
  165. if (m_pModel->mCurrentMaterial != nullptr)
  166. getFloatValue(m_pModel->mCurrentMaterial->alpha);
  167. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  168. }
  169. } break;
  170. case 'N':
  171. case 'n': {
  172. ++m_DataIt;
  173. switch (*m_DataIt) {
  174. case 's': // Specular exponent
  175. ++m_DataIt;
  176. if (m_pModel->mCurrentMaterial != nullptr)
  177. getFloatValue(m_pModel->mCurrentMaterial->shineness);
  178. break;
  179. case 'i': // Index Of refraction
  180. ++m_DataIt;
  181. if (m_pModel->mCurrentMaterial != nullptr)
  182. getFloatValue(m_pModel->mCurrentMaterial->ior);
  183. break;
  184. case 'e': // New material
  185. createMaterial();
  186. break;
  187. case 'o': // Norm texture
  188. --m_DataIt;
  189. getTexture();
  190. break;
  191. }
  192. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  193. } break;
  194. case 'P':
  195. {
  196. ++m_DataIt;
  197. switch(*m_DataIt)
  198. {
  199. case 'r':
  200. ++m_DataIt;
  201. if (m_pModel->mCurrentMaterial != nullptr)
  202. getFloatValue(m_pModel->mCurrentMaterial->roughness);
  203. break;
  204. case 'm':
  205. ++m_DataIt;
  206. if (m_pModel->mCurrentMaterial != nullptr)
  207. getFloatValue(m_pModel->mCurrentMaterial->metallic);
  208. break;
  209. case 's':
  210. ++m_DataIt;
  211. if (m_pModel->mCurrentMaterial != nullptr)
  212. getColorRGBA(m_pModel->mCurrentMaterial->sheen);
  213. break;
  214. case 'c':
  215. ++m_DataIt;
  216. if (*m_DataIt == 'r') {
  217. ++m_DataIt;
  218. if (m_pModel->mCurrentMaterial != nullptr)
  219. getFloatValue(m_pModel->mCurrentMaterial->clearcoat_roughness);
  220. } else if (*m_DataIt == 't') {
  221. ++m_DataIt;
  222. if (m_pModel->mCurrentMaterial != nullptr)
  223. getFloatValue(m_pModel->mCurrentMaterial->clearcoat_thickness);
  224. } else {
  225. if (m_pModel->mCurrentMaterial != nullptr)
  226. getFloatValue(m_pModel->mCurrentMaterial->clearcoat);
  227. }
  228. break;
  229. }
  230. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  231. }
  232. break;
  233. case 'm': // Texture or metallic
  234. {
  235. // Save start of token (after 'm')
  236. auto tokenStart = m_DataIt; // points to 'm'
  237. auto tokenEnd = getNextToken(m_DataIt, m_DataItEnd); // move iterator to end of token
  238. std::string keyword(tokenStart, tokenEnd);
  239. m_DataIt = tokenEnd; // advance iterator
  240. if (keyword.compare(0, 3, "map") == 0) {
  241. // starts with "map", treat as texture map
  242. m_DataIt = tokenStart;
  243. getTexture();
  244. } else if (keyword == "metallic" || keyword == "metal" || keyword == "metalness") {
  245. // parse metallic float value instead of texture
  246. getFloatIfMaterialValid(&ObjFile::Material::metallic);
  247. }
  248. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  249. } break;
  250. case 'b': // quick'n'dirty - for 'bump' sections
  251. {
  252. getTexture();
  253. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  254. } break;
  255. case 'r': // refl (map) or roughness (float)
  256. {
  257. auto tokenStart = m_DataIt; // points to 'r'
  258. auto tokenEnd = getNextToken(m_DataIt, m_DataItEnd);
  259. std::string keyword(tokenStart, tokenEnd);
  260. m_DataIt = tokenEnd;
  261. if (keyword == "roughness" || keyword == "rough") {
  262. getFloatIfMaterialValid(&ObjFile::Material::roughness);
  263. } else if (keyword == "refl" || keyword == "reflection") {
  264. m_DataIt = tokenStart;
  265. getTexture();
  266. }
  267. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  268. } break;
  269. case 'i': // Illumination model
  270. {
  271. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  272. if (m_pModel->mCurrentMaterial != nullptr)
  273. getIlluminationModel(m_pModel->mCurrentMaterial->illumination_model);
  274. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  275. } break;
  276. case 'a': {
  277. auto tokenStart = m_DataIt;
  278. auto tokenEnd = getNextToken(m_DataIt, m_DataItEnd);
  279. std::string keyword(tokenStart, tokenEnd);
  280. m_DataIt = tokenEnd;
  281. if (keyword == "aniso" || keyword == "anisotropy") {
  282. getFloatIfMaterialValid(&ObjFile::Material::anisotropy);
  283. } else if (keyword == "ao") {
  284. getFloatIfMaterialValid(&ObjFile::Material::ambient_occlusion);
  285. } else if (keyword == "anisor" || ai_stdStrToLower(keyword) == "anisotropicrotation") {
  286. getFloatIfMaterialValid(&ObjFile::Material::anisotropy_rotation);
  287. } else {
  288. ASSIMP_LOG_WARN("Unhandled keyword: ", keyword );
  289. }
  290. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  291. } break;
  292. case 's': {
  293. auto tokenStart = m_DataIt;
  294. auto tokenEnd = getNextToken(m_DataIt, m_DataItEnd);
  295. std::string keyword(tokenStart, tokenEnd);
  296. m_DataIt = tokenEnd;
  297. if (keyword == "subsurface" || keyword == "scattering") {
  298. getFloatIfMaterialValid(&ObjFile::Material::subsurface_scattering);
  299. } else if (ai_stdStrToLower(keyword) == "speculartint") {
  300. getFloatIfMaterialValid(&ObjFile::Material::specular_tint);
  301. } else if (keyword == "sheen") {
  302. getFloatIfMaterialValid(&ObjFile::Material::sheen_grazing);
  303. } else if (ai_stdStrToLower(keyword) == "sheentint") {
  304. getFloatIfMaterialValid(&ObjFile::Material::sheen_tint);
  305. } else {
  306. ASSIMP_LOG_WARN("Unhandled keyword: ", keyword );
  307. }
  308. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  309. } break;
  310. case 'c': {
  311. auto tokenStart = m_DataIt;
  312. auto tokenEnd = getNextToken(m_DataIt, m_DataItEnd);
  313. std::string keyword(tokenStart, tokenEnd);
  314. m_DataIt = tokenEnd;
  315. if (ai_stdStrToLower(keyword) == "clearcoat") {
  316. getFloatIfMaterialValid(&ObjFile::Material::clearcoat);
  317. } else if (ai_stdStrToLower(keyword) == "clearcoatgloss") {
  318. getFloatIfMaterialValid(&ObjFile::Material::clearcoat_gloss);
  319. } else {
  320. ASSIMP_LOG_WARN("Unhandled keyword: ", keyword );
  321. }
  322. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  323. } break;
  324. default: {
  325. m_DataIt = skipLine<DataArrayIt>(m_DataIt, m_DataItEnd, m_uiLine);
  326. } break;
  327. }
  328. }
  329. }
  330. // -------------------------------------------------------------------
  331. // Loads a color definition
  332. void ObjFileMtlImporter::getColorRGBA(aiColor3D *pColor) {
  333. ai_assert(nullptr != pColor);
  334. ai_real r(0.0), g(0.0), b(0.0);
  335. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, r);
  336. pColor->r = r;
  337. // we have to check if color is default 0 with only one token
  338. if (!IsLineEnd(*m_DataIt)) {
  339. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, g);
  340. m_DataIt = getFloat<DataArrayIt>(m_DataIt, m_DataItEnd, b);
  341. }
  342. pColor->g = g;
  343. pColor->b = b;
  344. }
  345. // -------------------------------------------------------------------
  346. void ObjFileMtlImporter::getColorRGBA(Maybe<aiColor3D> &value) {
  347. aiColor3D v;
  348. getColorRGBA(&v);
  349. value = Maybe<aiColor3D>(v);
  350. }
  351. // -------------------------------------------------------------------
  352. // Loads the kind of illumination model.
  353. void ObjFileMtlImporter::getIlluminationModel(int &illum_model) {
  354. m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE);
  355. illum_model = atoi(&m_buffer[0]);
  356. }
  357. // -------------------------------------------------------------------
  358. // Loads a single float value.
  359. void ObjFileMtlImporter::getFloatValue(ai_real &value) {
  360. m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE);
  361. size_t len = std::strlen(&m_buffer[0]);
  362. if (0 == len) {
  363. value = 0.0f;
  364. return;
  365. }
  366. value = (ai_real)fast_atof(&m_buffer[0]);
  367. }
  368. // -------------------------------------------------------------------
  369. void ObjFileMtlImporter::getFloatValue(Maybe<ai_real> &value) {
  370. m_DataIt = CopyNextWord<DataArrayIt>(m_DataIt, m_DataItEnd, &m_buffer[0], BUFFERSIZE);
  371. size_t len = std::strlen(&m_buffer[0]);
  372. if (len)
  373. value = Maybe<ai_real>(fast_atof(&m_buffer[0]));
  374. else
  375. value = Maybe<ai_real>();
  376. }
  377. // -------------------------------------------------------------------
  378. // Writes a loaded single float value if material not null
  379. void ObjFileMtlImporter::getFloatIfMaterialValid(ai_real ObjFile::Material::*member) {
  380. if (m_pModel != nullptr && m_pModel->mCurrentMaterial != nullptr) {
  381. // This will call getFloatValue(ai_real&)
  382. getFloatValue(m_pModel->mCurrentMaterial->*member);
  383. }
  384. }
  385. // -------------------------------------------------------------------
  386. void ObjFileMtlImporter::getFloatIfMaterialValid(Maybe<ai_real> ObjFile::Material::*member) {
  387. // It can directly access `m_pModel` because it's part of the class
  388. if (m_pModel != nullptr && m_pModel->mCurrentMaterial != nullptr) {
  389. getFloatValue(m_pModel->mCurrentMaterial->*member);
  390. }
  391. }
  392. // -------------------------------------------------------------------
  393. // Creates a material from loaded data.
  394. void ObjFileMtlImporter::createMaterial() {
  395. std::string line;
  396. while (!IsLineEnd(*m_DataIt)) {
  397. line += *m_DataIt;
  398. ++m_DataIt;
  399. }
  400. std::vector<std::string> token;
  401. const unsigned int numToken = tokenize<std::string>(line, token, " \t");
  402. std::string name;
  403. if (numToken == 1) {
  404. name = AI_DEFAULT_MATERIAL_NAME;
  405. } else {
  406. // skip newmtl and all following white spaces
  407. std::size_t first_ws_pos = line.find_first_of(" \t");
  408. std::size_t first_non_ws_pos = line.find_first_not_of(" \t", first_ws_pos);
  409. if (first_non_ws_pos != std::string::npos) {
  410. name = line.substr(first_non_ws_pos);
  411. }
  412. }
  413. name = ai_trim(name);
  414. std::map<std::string, ObjFile::Material *>::iterator it = m_pModel->mMaterialMap.find(name);
  415. if (m_pModel->mMaterialMap.end() == it) {
  416. // New Material created
  417. m_pModel->mCurrentMaterial = new ObjFile::Material();
  418. m_pModel->mCurrentMaterial->MaterialName.Set(name);
  419. m_pModel->mMaterialLib.push_back(name);
  420. m_pModel->mMaterialMap[name] = m_pModel->mCurrentMaterial;
  421. if (m_pModel->mCurrentMesh) {
  422. m_pModel->mCurrentMesh->m_uiMaterialIndex = static_cast<unsigned int>(m_pModel->mMaterialLib.size() - 1);
  423. }
  424. } else {
  425. // Use older material
  426. m_pModel->mCurrentMaterial = it->second;
  427. }
  428. }
  429. // -------------------------------------------------------------------
  430. // Gets a texture name from data.
  431. void ObjFileMtlImporter::getTexture() {
  432. aiString *out = nullptr;
  433. int clampIndex = -1;
  434. if (m_pModel->mCurrentMaterial == nullptr) {
  435. m_pModel->mCurrentMaterial = new ObjFile::Material();
  436. m_pModel->mCurrentMaterial->MaterialName.Set("Empty_Material");
  437. m_pModel->mMaterialMap["Empty_Material"] = m_pModel->mCurrentMaterial;
  438. }
  439. const char *pPtr(&(*m_DataIt));
  440. if (!ASSIMP_strincmp(pPtr, DiffuseTexture, static_cast<unsigned int>(strlen(DiffuseTexture)))) {
  441. // Diffuse texture
  442. out = &m_pModel->mCurrentMaterial->texture;
  443. clampIndex = ObjFile::Material::TextureDiffuseType;
  444. } else if (!ASSIMP_strincmp(pPtr, AmbientTexture, static_cast<unsigned int>(strlen(AmbientTexture)))) {
  445. // Ambient texture
  446. out = &m_pModel->mCurrentMaterial->textureAmbient;
  447. clampIndex = ObjFile::Material::TextureAmbientType;
  448. } else if (!ASSIMP_strincmp(pPtr, SpecularTexture, static_cast<unsigned int>(strlen(SpecularTexture)))) {
  449. // Specular texture
  450. out = &m_pModel->mCurrentMaterial->textureSpecular;
  451. clampIndex = ObjFile::Material::TextureSpecularType;
  452. } else if (!ASSIMP_strincmp(pPtr, DisplacementTexture1, static_cast<unsigned int>(strlen(DisplacementTexture1))) ||
  453. !ASSIMP_strincmp(pPtr, DisplacementTexture2, static_cast<unsigned int>(strlen(DisplacementTexture2)))) {
  454. // Displacement texture
  455. out = &m_pModel->mCurrentMaterial->textureDisp;
  456. clampIndex = ObjFile::Material::TextureDispType;
  457. } else if (!ASSIMP_strincmp(pPtr, OpacityTexture, static_cast<unsigned int>(strlen(OpacityTexture)))) {
  458. // Opacity texture
  459. out = &m_pModel->mCurrentMaterial->textureOpacity;
  460. clampIndex = ObjFile::Material::TextureOpacityType;
  461. } else if (!ASSIMP_strincmp(pPtr, EmissiveTexture1, static_cast<unsigned int>(strlen(EmissiveTexture1))) ||
  462. !ASSIMP_strincmp(pPtr, EmissiveTexture2, static_cast<unsigned int>(strlen(EmissiveTexture2)))) {
  463. // Emissive texture
  464. out = &m_pModel->mCurrentMaterial->textureEmissive;
  465. clampIndex = ObjFile::Material::TextureEmissiveType;
  466. } else if (!ASSIMP_strincmp(pPtr, BumpTexture1, static_cast<unsigned int>(strlen(BumpTexture1))) ||
  467. !ASSIMP_strincmp(pPtr, BumpTexture2, static_cast<unsigned int>(strlen(BumpTexture2)))) {
  468. // Bump texture
  469. out = &m_pModel->mCurrentMaterial->textureBump;
  470. clampIndex = ObjFile::Material::TextureBumpType;
  471. } else if (!ASSIMP_strincmp(pPtr, NormalTextureV1, static_cast<unsigned int>(strlen(NormalTextureV1))) || !ASSIMP_strincmp(pPtr, NormalTextureV2, static_cast<unsigned int>(strlen(NormalTextureV2)))) {
  472. // Normal map
  473. out = &m_pModel->mCurrentMaterial->textureNormal;
  474. clampIndex = ObjFile::Material::TextureNormalType;
  475. } else if (!ASSIMP_strincmp(pPtr, ReflectionTexture, static_cast<unsigned int>(strlen(ReflectionTexture)))) {
  476. // Reflection texture(s)
  477. //Do nothing here
  478. return;
  479. } else if (!ASSIMP_strincmp(pPtr, SpecularityTexture, static_cast<unsigned int>(strlen(SpecularityTexture)))) {
  480. // Specularity scaling (glossiness)
  481. out = &m_pModel->mCurrentMaterial->textureSpecularity;
  482. clampIndex = ObjFile::Material::TextureSpecularityType;
  483. } else if ( !ASSIMP_strincmp( pPtr, RoughnessTexture, static_cast<unsigned int>(strlen(RoughnessTexture)))) {
  484. // PBR Roughness texture
  485. out = & m_pModel->mCurrentMaterial->textureRoughness;
  486. clampIndex = ObjFile::Material::TextureRoughnessType;
  487. } else if ( !ASSIMP_strincmp( pPtr, MetallicTexture, static_cast<unsigned int>(strlen(MetallicTexture)))) {
  488. // PBR Metallic texture
  489. out = & m_pModel->mCurrentMaterial->textureMetallic;
  490. clampIndex = ObjFile::Material::TextureMetallicType;
  491. } else if (!ASSIMP_strincmp( pPtr, SheenTexture, static_cast<unsigned int>(strlen(SheenTexture)))) {
  492. // PBR Sheen (reflectance) texture
  493. out = & m_pModel->mCurrentMaterial->textureSheen;
  494. clampIndex = ObjFile::Material::TextureSheenType;
  495. } else if (!ASSIMP_strincmp( pPtr, RMATexture, static_cast<unsigned int>(strlen(RMATexture)))) {
  496. // PBR Rough/Metal/AO texture
  497. out = & m_pModel->mCurrentMaterial->textureRMA;
  498. clampIndex = ObjFile::Material::TextureRMAType;
  499. } else {
  500. ASSIMP_LOG_ERROR("OBJ/MTL: Encountered unknown texture type");
  501. return;
  502. }
  503. bool clamp = false;
  504. getTextureOption(clamp, clampIndex, out);
  505. m_pModel->mCurrentMaterial->clamp[clampIndex] = clamp;
  506. std::string texture;
  507. m_DataIt = getName<DataArrayIt>(m_DataIt, m_DataItEnd, texture);
  508. if (nullptr != out) {
  509. out->Set(m_strAbsPath + texture);
  510. }
  511. }
  512. /* /////////////////////////////////////////////////////////////////////////////
  513. * Texture Option
  514. * /////////////////////////////////////////////////////////////////////////////
  515. * According to http://en.wikipedia.org/wiki/Wavefront_.obj_file#Texture_options
  516. * Texture map statement can contains various texture option, for example:
  517. *
  518. * map_Ka -o 1 1 1 some.png
  519. * map_Kd -clamp on some.png
  520. *
  521. * So we need to parse and skip these options, and leave the last part which is
  522. * the url of image, otherwise we will get a wrong url like "-clamp on some.png".
  523. *
  524. * Because aiMaterial supports clamp option, so we also want to return it
  525. * /////////////////////////////////////////////////////////////////////////////
  526. */
  527. void ObjFileMtlImporter::getTextureOption(bool &clamp, int &clampIndex, aiString *&out) {
  528. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  529. // If there is any more texture option
  530. while (!isEndOfBuffer(m_DataIt, m_DataItEnd) && *m_DataIt == '-') {
  531. const char *pPtr(&(*m_DataIt));
  532. //skip option key and value
  533. int skipToken = 1;
  534. if (!ASSIMP_strincmp(pPtr, ClampOption, static_cast<unsigned int>(strlen(ClampOption)))) {
  535. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  536. char value[3];
  537. CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
  538. if (!ASSIMP_strincmp(value, "on", 2)) {
  539. clamp = true;
  540. }
  541. skipToken = 2;
  542. } else if (!ASSIMP_strincmp(pPtr, TypeOption, static_cast<unsigned int>(strlen(TypeOption)))) {
  543. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  544. char value[12];
  545. CopyNextWord(it, m_DataItEnd, value, sizeof(value) / sizeof(*value));
  546. if (!ASSIMP_strincmp(value, "cube_top", 8)) {
  547. clampIndex = ObjFile::Material::TextureReflectionCubeTopType;
  548. out = &m_pModel->mCurrentMaterial->textureReflection[0];
  549. } else if (!ASSIMP_strincmp(value, "cube_bottom", 11)) {
  550. clampIndex = ObjFile::Material::TextureReflectionCubeBottomType;
  551. out = &m_pModel->mCurrentMaterial->textureReflection[1];
  552. } else if (!ASSIMP_strincmp(value, "cube_front", 10)) {
  553. clampIndex = ObjFile::Material::TextureReflectionCubeFrontType;
  554. out = &m_pModel->mCurrentMaterial->textureReflection[2];
  555. } else if (!ASSIMP_strincmp(value, "cube_back", 9)) {
  556. clampIndex = ObjFile::Material::TextureReflectionCubeBackType;
  557. out = &m_pModel->mCurrentMaterial->textureReflection[3];
  558. } else if (!ASSIMP_strincmp(value, "cube_left", 9)) {
  559. clampIndex = ObjFile::Material::TextureReflectionCubeLeftType;
  560. out = &m_pModel->mCurrentMaterial->textureReflection[4];
  561. } else if (!ASSIMP_strincmp(value, "cube_right", 10)) {
  562. clampIndex = ObjFile::Material::TextureReflectionCubeRightType;
  563. out = &m_pModel->mCurrentMaterial->textureReflection[5];
  564. } else if (!ASSIMP_strincmp(value, "sphere", 6)) {
  565. clampIndex = ObjFile::Material::TextureReflectionSphereType;
  566. out = &m_pModel->mCurrentMaterial->textureReflection[0];
  567. }
  568. skipToken = 2;
  569. } else if (!ASSIMP_strincmp(pPtr, BumpOption, static_cast<unsigned int>(strlen(BumpOption)))) {
  570. DataArrayIt it = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  571. getFloat(it, m_DataItEnd, m_pModel->mCurrentMaterial->bump_multiplier);
  572. skipToken = 2;
  573. } else if (!ASSIMP_strincmp(pPtr, BlendUOption, static_cast<unsigned int>(strlen(BlendUOption))) ||
  574. !ASSIMP_strincmp(pPtr, BlendVOption, static_cast<unsigned int>(strlen(BlendVOption))) ||
  575. !ASSIMP_strincmp(pPtr, BoostOption, static_cast<unsigned int>(strlen(BoostOption))) ||
  576. !ASSIMP_strincmp(pPtr, ResolutionOption, static_cast<unsigned int>(strlen(ResolutionOption))) ||
  577. !ASSIMP_strincmp(pPtr, ChannelOption, static_cast<unsigned int>(strlen(ChannelOption)))) {
  578. skipToken = 2;
  579. } else if (!ASSIMP_strincmp(pPtr, ModifyMapOption, static_cast<unsigned int>(strlen(ModifyMapOption)))) {
  580. skipToken = 3;
  581. } else if (!ASSIMP_strincmp(pPtr, OffsetOption, static_cast<unsigned int>(strlen(OffsetOption))) ||
  582. !ASSIMP_strincmp(pPtr, ScaleOption, static_cast<unsigned int>(strlen(ScaleOption))) ||
  583. !ASSIMP_strincmp(pPtr, TurbulenceOption, static_cast<unsigned int>(strlen(TurbulenceOption)))) {
  584. skipToken = 4;
  585. }
  586. for (int i = 0; i < skipToken; ++i) {
  587. m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
  588. }
  589. }
  590. }
  591. // -------------------------------------------------------------------
  592. } // Namespace Assimp
  593. #endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER