BlenderLoader.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2022, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file BlenderLoader.cpp
  34. * @brief Implementation of the Blender3D importer class.
  35. */
  36. //#define ASSIMP_BUILD_NO_COMPRESSED_BLEND
  37. // Uncomment this to disable support for (gzip)compressed .BLEND files
  38. #ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
  39. #include "BlenderBMesh.h"
  40. #include "BlenderCustomData.h"
  41. #include "BlenderIntermediate.h"
  42. #include "BlenderModifier.h"
  43. #include <assimp/StringUtils.h>
  44. #include <assimp/importerdesc.h>
  45. #include <assimp/scene.h>
  46. #include <assimp/MemoryIOWrapper.h>
  47. #include <assimp/StreamReader.h>
  48. #include <assimp/StringComparison.h>
  49. #include <cctype>
  50. // zlib is needed for compressed blend files
  51. #ifndef ASSIMP_BUILD_NO_COMPRESSED_BLEND
  52. # ifdef ASSIMP_BUILD_NO_OWN_ZLIB
  53. # include <zlib.h>
  54. # else
  55. # include "../contrib/zlib/zlib.h"
  56. # endif
  57. #endif
  58. namespace Assimp {
  59. template <>
  60. const char *LogFunctions<BlenderImporter>::Prefix() {
  61. static auto prefix = "BLEND: ";
  62. return prefix;
  63. }
  64. } // namespace Assimp
  65. using namespace Assimp;
  66. using namespace Assimp::Blender;
  67. using namespace Assimp::Formatter;
  68. static const aiImporterDesc blenderDesc = {
  69. "Blender 3D Importer (http://www.blender3d.org)",
  70. "",
  71. "",
  72. "No animation support yet",
  73. aiImporterFlags_SupportBinaryFlavour,
  74. 0,
  75. 0,
  76. 2,
  77. 50,
  78. "blend"
  79. };
  80. // ------------------------------------------------------------------------------------------------
  81. // Constructor to be privately used by Importer
  82. BlenderImporter::BlenderImporter() :
  83. modifier_cache(new BlenderModifierShowcase()) {
  84. // empty
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. // Destructor, private as well
  88. BlenderImporter::~BlenderImporter() {
  89. delete modifier_cache;
  90. }
  91. static const char * const Tokens[] = { "BLENDER" };
  92. // ------------------------------------------------------------------------------------------------
  93. // Returns whether the class can handle the format of the given file.
  94. bool BlenderImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
  95. // note: this won't catch compressed files
  96. static const char *tokens[] = { "<BLENDER", "blender" };
  97. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. // Loader registry entry
  101. const aiImporterDesc *BlenderImporter::GetInfo() const {
  102. return &blenderDesc;
  103. }
  104. // ------------------------------------------------------------------------------------------------
  105. // Setup configuration properties for the loader
  106. void BlenderImporter::SetupProperties(const Importer * /*pImp*/) {
  107. // nothing to be done for the moment
  108. }
  109. // ------------------------------------------------------------------------------------------------
  110. // Imports the given file into the given scene structure.
  111. void BlenderImporter::InternReadFile(const std::string &pFile,
  112. aiScene *pScene, IOSystem *pIOHandler) {
  113. #ifndef ASSIMP_BUILD_NO_COMPRESSED_BLEND
  114. std::vector<Bytef> uncompressed;
  115. #endif
  116. FileDatabase file;
  117. std::shared_ptr<IOStream> stream(pIOHandler->Open(pFile, "rb"));
  118. if (!stream) {
  119. ThrowException("Could not open file for reading");
  120. }
  121. char magic[8] = { 0 };
  122. stream->Read(magic, 7, 1);
  123. if (strcmp(magic, Tokens[0])) {
  124. // Check for presence of the gzip header. If yes, assume it is a
  125. // compressed blend file and try uncompressing it, else fail. This is to
  126. // avoid uncompressing random files which our loader might end up with.
  127. #ifdef ASSIMP_BUILD_NO_COMPRESSED_BLEND
  128. ThrowException("BLENDER magic bytes are missing, is this file compressed (Assimp was built without decompression support)?");
  129. #else
  130. if (magic[0] != 0x1f || static_cast<uint8_t>(magic[1]) != 0x8b) {
  131. ThrowException("BLENDER magic bytes are missing, couldn't find GZIP header either");
  132. }
  133. LogDebug("Found no BLENDER magic word but a GZIP header, might be a compressed file");
  134. if (magic[2] != 8) {
  135. ThrowException("Unsupported GZIP compression method");
  136. }
  137. // http://www.gzip.org/zlib/rfc-gzip.html#header-trailer
  138. stream->Seek(0L, aiOrigin_SET);
  139. std::shared_ptr<StreamReaderLE> reader = std::shared_ptr<StreamReaderLE>(new StreamReaderLE(stream));
  140. // build a zlib stream
  141. z_stream zstream;
  142. zstream.opaque = Z_NULL;
  143. zstream.zalloc = Z_NULL;
  144. zstream.zfree = Z_NULL;
  145. zstream.data_type = Z_BINARY;
  146. // http://hewgill.com/journal/entries/349-how-to-decompress-gzip-stream-with-zlib
  147. inflateInit2(&zstream, 16 + MAX_WBITS);
  148. zstream.next_in = reinterpret_cast<Bytef *>(reader->GetPtr());
  149. zstream.avail_in = (uInt)reader->GetRemainingSize();
  150. size_t total = 0l;
  151. // TODO: be smarter about this, decompress directly into heap buffer
  152. // and decompress the data .... do 1k chunks in the hope that we won't kill the stack
  153. #define MYBLOCK 1024
  154. Bytef block[MYBLOCK];
  155. int ret;
  156. do {
  157. zstream.avail_out = MYBLOCK;
  158. zstream.next_out = block;
  159. ret = inflate(&zstream, Z_NO_FLUSH);
  160. if (ret != Z_STREAM_END && ret != Z_OK) {
  161. ThrowException("Failure decompressing this file using gzip, seemingly it is NOT a compressed .BLEND file");
  162. }
  163. const size_t have = MYBLOCK - zstream.avail_out;
  164. total += have;
  165. uncompressed.resize(total);
  166. memcpy(uncompressed.data() + total - have, block, have);
  167. } while (ret != Z_STREAM_END);
  168. // terminate zlib
  169. inflateEnd(&zstream);
  170. // replace the input stream with a memory stream
  171. stream.reset(new MemoryIOStream(reinterpret_cast<uint8_t *>(uncompressed.data()), total));
  172. // .. and retry
  173. stream->Read(magic, 7, 1);
  174. if (strcmp(magic, "BLENDER")) {
  175. ThrowException("Found no BLENDER magic word in decompressed GZIP file");
  176. }
  177. #endif
  178. }
  179. file.i64bit = (stream->Read(magic, 1, 1), magic[0] == '-');
  180. file.little = (stream->Read(magic, 1, 1), magic[0] == 'v');
  181. stream->Read(magic, 3, 1);
  182. magic[3] = '\0';
  183. LogInfo("Blender version is ", magic[0], ".", magic + 1,
  184. " (64bit: ", file.i64bit ? "true" : "false",
  185. ", little endian: ", file.little ? "true" : "false", ")");
  186. ParseBlendFile(file, stream);
  187. Scene scene;
  188. ExtractScene(scene, file);
  189. ConvertBlendFile(pScene, scene, file);
  190. }
  191. // ------------------------------------------------------------------------------------------------
  192. void BlenderImporter::ParseBlendFile(FileDatabase &out, std::shared_ptr<IOStream> stream) {
  193. out.reader = std::make_shared<StreamReaderAny>(stream, out.little);
  194. DNAParser dna_reader(out);
  195. const DNA *dna = nullptr;
  196. out.entries.reserve(128);
  197. { // even small BLEND files tend to consist of many file blocks
  198. SectionParser parser(*out.reader.get(), out.i64bit);
  199. // first parse the file in search for the DNA and insert all other sections into the database
  200. while ((parser.Next(), 1)) {
  201. const FileBlockHead &head = parser.GetCurrent();
  202. if (head.id == "ENDB") {
  203. break; // only valid end of the file
  204. } else if (head.id == "DNA1") {
  205. dna_reader.Parse();
  206. dna = &dna_reader.GetDNA();
  207. continue;
  208. }
  209. out.entries.push_back(head);
  210. }
  211. }
  212. if (!dna) {
  213. ThrowException("SDNA not found");
  214. }
  215. std::sort(out.entries.begin(), out.entries.end());
  216. }
  217. // ------------------------------------------------------------------------------------------------
  218. void BlenderImporter::ExtractScene(Scene &out, const FileDatabase &file) {
  219. const FileBlockHead *block = nullptr;
  220. std::map<std::string, size_t>::const_iterator it = file.dna.indices.find("Scene");
  221. if (it == file.dna.indices.end()) {
  222. ThrowException("There is no `Scene` structure record");
  223. }
  224. const Structure &ss = file.dna.structures[(*it).second];
  225. // we need a scene somewhere to start with.
  226. for (const FileBlockHead &bl : file.entries) {
  227. // Fix: using the DNA index is more reliable to locate scenes
  228. //if (bl.id == "SC") {
  229. if (bl.dna_index == (*it).second) {
  230. block = &bl;
  231. break;
  232. }
  233. }
  234. if (!block) {
  235. ThrowException("There is not a single `Scene` record to load");
  236. }
  237. file.reader->SetCurrentPos(block->start);
  238. ss.Convert(out, file);
  239. #ifndef ASSIMP_BUILD_BLENDER_NO_STATS
  240. ASSIMP_LOG_INFO(
  241. "(Stats) Fields read: ", file.stats().fields_read,
  242. ", pointers resolved: ", file.stats().pointers_resolved,
  243. ", cache hits: ", file.stats().cache_hits,
  244. ", cached objects: ", file.stats().cached_objects);
  245. #endif
  246. }
  247. // ------------------------------------------------------------------------------------------------
  248. void BlenderImporter::ParseSubCollection(const Blender::Scene &in, aiNode *root, std::shared_ptr<Collection> collection, ConversionData &conv_data) {
  249. std::deque<Object *> root_objects;
  250. // Count number of objects
  251. for (std::shared_ptr<CollectionObject> cur = std::static_pointer_cast<CollectionObject>(collection->gobject.first); cur; cur = cur->next) {
  252. if (cur->ob) {
  253. root_objects.push_back(cur->ob);
  254. }
  255. }
  256. std::deque<Collection *> root_children;
  257. // Count number of child nodes
  258. for (std::shared_ptr<CollectionChild> cur = std::static_pointer_cast<CollectionChild>(collection->children.first); cur; cur = cur->next) {
  259. if (cur->collection) {
  260. root_children.push_back(cur->collection.get());
  261. }
  262. }
  263. root->mNumChildren = static_cast<unsigned int>(root_objects.size() + root_children.size());
  264. root->mChildren = new aiNode *[root->mNumChildren]();
  265. for (unsigned int i = 0; i < static_cast<unsigned int>(root_objects.size()); ++i) {
  266. root->mChildren[i] = ConvertNode(in, root_objects[i], conv_data, aiMatrix4x4());
  267. root->mChildren[i]->mParent = root;
  268. }
  269. // For each subcollection create a new node to represent it
  270. unsigned int iterator = static_cast<unsigned int>(root_objects.size());
  271. for (std::shared_ptr<CollectionChild> cur = std::static_pointer_cast<CollectionChild>(collection->children.first); cur; cur = cur->next) {
  272. if (cur->collection) {
  273. root->mChildren[iterator] = new aiNode(cur->collection->id.name + 2); // skip over the name prefix 'OB'
  274. root->mChildren[iterator]->mParent = root;
  275. ParseSubCollection(in, root->mChildren[iterator], cur->collection, conv_data);
  276. }
  277. iterator += 1;
  278. }
  279. }
  280. // ------------------------------------------------------------------------------------------------
  281. void BlenderImporter::ConvertBlendFile(aiScene *out, const Scene &in, const FileDatabase &file) {
  282. ConversionData conv(file);
  283. aiNode *root = out->mRootNode = new aiNode("<BlenderRoot>");
  284. // Iterate over all objects directly under master_collection,
  285. // If in.master_collection == null, then we're parsing something older.
  286. if (in.master_collection) {
  287. ParseSubCollection(in, root, in.master_collection, conv);
  288. } else {
  289. std::deque<const Object *> no_parents;
  290. for (std::shared_ptr<Base> cur = std::static_pointer_cast<Base>(in.base.first); cur; cur = cur->next) {
  291. if (cur->object) {
  292. if (!cur->object->parent) {
  293. no_parents.push_back(cur->object.get());
  294. } else {
  295. conv.objects.insert(cur->object.get());
  296. }
  297. }
  298. }
  299. for (std::shared_ptr<Base> cur = in.basact; cur; cur = cur->next) {
  300. if (cur->object) {
  301. if (cur->object->parent) {
  302. conv.objects.insert(cur->object.get());
  303. }
  304. }
  305. }
  306. if (no_parents.empty()) {
  307. ThrowException("Expected at least one object with no parent");
  308. }
  309. root->mNumChildren = static_cast<unsigned int>(no_parents.size());
  310. root->mChildren = new aiNode *[root->mNumChildren]();
  311. for (unsigned int i = 0; i < root->mNumChildren; ++i) {
  312. root->mChildren[i] = ConvertNode(in, no_parents[i], conv, aiMatrix4x4());
  313. root->mChildren[i]->mParent = root;
  314. }
  315. }
  316. BuildMaterials(conv);
  317. if (conv.meshes->size()) {
  318. out->mMeshes = new aiMesh *[out->mNumMeshes = static_cast<unsigned int>(conv.meshes->size())];
  319. std::copy(conv.meshes->begin(), conv.meshes->end(), out->mMeshes);
  320. conv.meshes.dismiss();
  321. }
  322. if (conv.lights->size()) {
  323. out->mLights = new aiLight *[out->mNumLights = static_cast<unsigned int>(conv.lights->size())];
  324. std::copy(conv.lights->begin(), conv.lights->end(), out->mLights);
  325. conv.lights.dismiss();
  326. }
  327. if (conv.cameras->size()) {
  328. out->mCameras = new aiCamera *[out->mNumCameras = static_cast<unsigned int>(conv.cameras->size())];
  329. std::copy(conv.cameras->begin(), conv.cameras->end(), out->mCameras);
  330. conv.cameras.dismiss();
  331. }
  332. if (conv.materials->size()) {
  333. out->mMaterials = new aiMaterial *[out->mNumMaterials = static_cast<unsigned int>(conv.materials->size())];
  334. std::copy(conv.materials->begin(), conv.materials->end(), out->mMaterials);
  335. conv.materials.dismiss();
  336. }
  337. if (conv.textures->size()) {
  338. out->mTextures = new aiTexture *[out->mNumTextures = static_cast<unsigned int>(conv.textures->size())];
  339. std::copy(conv.textures->begin(), conv.textures->end(), out->mTextures);
  340. conv.textures.dismiss();
  341. }
  342. // acknowledge that the scene might come out incomplete
  343. // by Assimp's definition of `complete`: blender scenes
  344. // can consist of thousands of cameras or lights with
  345. // not a single mesh between them.
  346. if (!out->mNumMeshes) {
  347. out->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
  348. }
  349. }
  350. // ------------------------------------------------------------------------------------------------
  351. void BlenderImporter::ResolveImage(aiMaterial *out, const Material *mat, const MTex *tex, const Image *img, ConversionData &conv_data) {
  352. (void)mat;
  353. (void)tex;
  354. (void)conv_data;
  355. aiString name;
  356. // check if the file contents are bundled with the BLEND file
  357. if (img->packedfile) {
  358. name.data[0] = '*';
  359. name.length = 1 + ASSIMP_itoa10(name.data + 1, static_cast<unsigned int>(MAXLEN - 1), static_cast<int32_t>(conv_data.textures->size()));
  360. conv_data.textures->push_back(new aiTexture());
  361. aiTexture *curTex = conv_data.textures->back();
  362. // usually 'img->name' will be the original file name of the embedded textures,
  363. // so we can extract the file extension from it.
  364. const size_t nlen = strlen(img->name);
  365. const char *s = img->name + nlen, *e = s;
  366. while (s >= img->name && *s != '.') {
  367. --s;
  368. }
  369. curTex->achFormatHint[0] = s + 1 > e ? '\0' : (char)::tolower((unsigned char)s[1]);
  370. curTex->achFormatHint[1] = s + 2 > e ? '\0' : (char)::tolower((unsigned char)s[2]);
  371. curTex->achFormatHint[2] = s + 3 > e ? '\0' : (char)::tolower((unsigned char)s[3]);
  372. curTex->achFormatHint[3] = '\0';
  373. // tex->mHeight = 0;
  374. curTex->mWidth = img->packedfile->size;
  375. uint8_t *ch = new uint8_t[curTex->mWidth];
  376. conv_data.db.reader->SetCurrentPos(static_cast<size_t>(img->packedfile->data->val));
  377. conv_data.db.reader->CopyAndAdvance(ch, curTex->mWidth);
  378. curTex->pcData = reinterpret_cast<aiTexel *>(ch);
  379. LogInfo("Reading embedded texture, original file was ", img->name);
  380. } else {
  381. name = aiString(img->name);
  382. }
  383. aiTextureType texture_type = aiTextureType_UNKNOWN;
  384. MTex::MapType map_type = tex->mapto;
  385. if (map_type & MTex::MapType_COL)
  386. texture_type = aiTextureType_DIFFUSE;
  387. else if (map_type & MTex::MapType_NORM) {
  388. if (tex->tex->imaflag & Tex::ImageFlags_NORMALMAP) {
  389. texture_type = aiTextureType_NORMALS;
  390. } else {
  391. texture_type = aiTextureType_HEIGHT;
  392. }
  393. out->AddProperty(&tex->norfac, 1, AI_MATKEY_BUMPSCALING);
  394. } else if (map_type & MTex::MapType_COLSPEC)
  395. texture_type = aiTextureType_SPECULAR;
  396. else if (map_type & MTex::MapType_COLMIR)
  397. texture_type = aiTextureType_REFLECTION;
  398. //else if (map_type & MTex::MapType_REF)
  399. else if (map_type & MTex::MapType_SPEC)
  400. texture_type = aiTextureType_SHININESS;
  401. else if (map_type & MTex::MapType_EMIT)
  402. texture_type = aiTextureType_EMISSIVE;
  403. //else if (map_type & MTex::MapType_ALPHA)
  404. //else if (map_type & MTex::MapType_HAR)
  405. //else if (map_type & MTex::MapType_RAYMIRR)
  406. //else if (map_type & MTex::MapType_TRANSLU)
  407. else if (map_type & MTex::MapType_AMB)
  408. texture_type = aiTextureType_AMBIENT;
  409. else if (map_type & MTex::MapType_DISPLACE)
  410. texture_type = aiTextureType_DISPLACEMENT;
  411. //else if (map_type & MTex::MapType_WARP)
  412. out->AddProperty(&name, AI_MATKEY_TEXTURE(texture_type,
  413. conv_data.next_texture[texture_type]++));
  414. }
  415. // ------------------------------------------------------------------------------------------------
  416. void BlenderImporter::AddSentinelTexture(aiMaterial *out, const Material *mat, const MTex *tex, ConversionData &conv_data) {
  417. (void)mat;
  418. (void)tex;
  419. (void)conv_data;
  420. aiString name;
  421. name.length = ai_snprintf(name.data, MAXLEN, "Procedural,num=%i,type=%s", conv_data.sentinel_cnt++,
  422. GetTextureTypeDisplayString(tex->tex->type));
  423. out->AddProperty(&name, AI_MATKEY_TEXTURE_DIFFUSE(
  424. conv_data.next_texture[aiTextureType_DIFFUSE]++));
  425. }
  426. // ------------------------------------------------------------------------------------------------
  427. void BlenderImporter::ResolveTexture(aiMaterial *out, const Material *mat, const MTex *tex, ConversionData &conv_data) {
  428. const Tex *rtex = tex->tex.get();
  429. if (!rtex || !rtex->type) {
  430. return;
  431. }
  432. // We can't support most of the texture types because they're mostly procedural.
  433. // These are substituted by a dummy texture.
  434. const char *dispnam = "";
  435. switch (rtex->type) {
  436. // these are listed in blender's UI
  437. case Tex::Type_CLOUDS:
  438. case Tex::Type_WOOD:
  439. case Tex::Type_MARBLE:
  440. case Tex::Type_MAGIC:
  441. case Tex::Type_BLEND:
  442. case Tex::Type_STUCCI:
  443. case Tex::Type_NOISE:
  444. case Tex::Type_PLUGIN:
  445. case Tex::Type_MUSGRAVE:
  446. case Tex::Type_VORONOI:
  447. case Tex::Type_DISTNOISE:
  448. case Tex::Type_ENVMAP:
  449. // these do no appear in the UI, why?
  450. case Tex::Type_POINTDENSITY:
  451. case Tex::Type_VOXELDATA:
  452. LogWarn("Encountered a texture with an unsupported type: ", dispnam);
  453. AddSentinelTexture(out, mat, tex, conv_data);
  454. break;
  455. case Tex::Type_IMAGE:
  456. if (!rtex->ima) {
  457. LogError("A texture claims to be an Image, but no image reference is given");
  458. break;
  459. }
  460. ResolveImage(out, mat, tex, rtex->ima.get(), conv_data);
  461. break;
  462. default:
  463. ai_assert(false);
  464. };
  465. }
  466. // ------------------------------------------------------------------------------------------------
  467. void BlenderImporter::BuildDefaultMaterial(Blender::ConversionData &conv_data) {
  468. // add a default material if necessary
  469. unsigned int index = static_cast<unsigned int>(-1);
  470. for (aiMesh *mesh : conv_data.meshes.get()) {
  471. if (mesh->mMaterialIndex == static_cast<unsigned int>(-1)) {
  472. if (index == static_cast<unsigned int>(-1)) {
  473. // Setup a default material.
  474. std::shared_ptr<Material> p(new Material());
  475. ai_assert(::strlen(AI_DEFAULT_MATERIAL_NAME) < sizeof(p->id.name) - 2);
  476. strcpy(p->id.name + 2, AI_DEFAULT_MATERIAL_NAME);
  477. // Note: MSVC11 does not zero-initialize Material here, although it should.
  478. // Thus all relevant fields should be explicitly initialized. We cannot add
  479. // a default constructor to Material since the DNA codegen does not support
  480. // parsing it.
  481. p->r = p->g = p->b = 0.6f;
  482. p->specr = p->specg = p->specb = 0.6f;
  483. p->ambr = p->ambg = p->ambb = 0.0f;
  484. p->mirr = p->mirg = p->mirb = 0.0f;
  485. p->emit = 0.f;
  486. p->alpha = 0.f;
  487. p->har = 0;
  488. index = static_cast<unsigned int>(conv_data.materials_raw.size());
  489. conv_data.materials_raw.push_back(p);
  490. LogInfo("Adding default material");
  491. }
  492. mesh->mMaterialIndex = index;
  493. }
  494. }
  495. }
  496. void BlenderImporter::AddBlendParams(aiMaterial *result, const Material *source) {
  497. aiColor3D diffuseColor(source->r, source->g, source->b);
  498. result->AddProperty(&diffuseColor, 1, "$mat.blend.diffuse.color", 0, 0);
  499. float diffuseIntensity = source->ref;
  500. result->AddProperty(&diffuseIntensity, 1, "$mat.blend.diffuse.intensity", 0, 0);
  501. int diffuseShader = source->diff_shader;
  502. result->AddProperty(&diffuseShader, 1, "$mat.blend.diffuse.shader", 0, 0);
  503. int diffuseRamp = 0;
  504. result->AddProperty(&diffuseRamp, 1, "$mat.blend.diffuse.ramp", 0, 0);
  505. aiColor3D specularColor(source->specr, source->specg, source->specb);
  506. result->AddProperty(&specularColor, 1, "$mat.blend.specular.color", 0, 0);
  507. float specularIntensity = source->spec;
  508. result->AddProperty(&specularIntensity, 1, "$mat.blend.specular.intensity", 0, 0);
  509. int specularShader = source->spec_shader;
  510. result->AddProperty(&specularShader, 1, "$mat.blend.specular.shader", 0, 0);
  511. int specularRamp = 0;
  512. result->AddProperty(&specularRamp, 1, "$mat.blend.specular.ramp", 0, 0);
  513. int specularHardness = source->har;
  514. result->AddProperty(&specularHardness, 1, "$mat.blend.specular.hardness", 0, 0);
  515. int transparencyUse = source->mode & MA_TRANSPARENCY ? 1 : 0;
  516. result->AddProperty(&transparencyUse, 1, "$mat.blend.transparency.use", 0, 0);
  517. int transparencyMethod = source->mode & MA_RAYTRANSP ? 2 : (source->mode & MA_ZTRANSP ? 1 : 0);
  518. result->AddProperty(&transparencyMethod, 1, "$mat.blend.transparency.method", 0, 0);
  519. float transparencyAlpha = source->alpha;
  520. result->AddProperty(&transparencyAlpha, 1, "$mat.blend.transparency.alpha", 0, 0);
  521. float transparencySpecular = source->spectra;
  522. result->AddProperty(&transparencySpecular, 1, "$mat.blend.transparency.specular", 0, 0);
  523. float transparencyFresnel = source->fresnel_tra;
  524. result->AddProperty(&transparencyFresnel, 1, "$mat.blend.transparency.fresnel", 0, 0);
  525. float transparencyBlend = source->fresnel_tra_i;
  526. result->AddProperty(&transparencyBlend, 1, "$mat.blend.transparency.blend", 0, 0);
  527. float transparencyIor = source->ang;
  528. result->AddProperty(&transparencyIor, 1, "$mat.blend.transparency.ior", 0, 0);
  529. float transparencyFilter = source->filter;
  530. result->AddProperty(&transparencyFilter, 1, "$mat.blend.transparency.filter", 0, 0);
  531. float transparencyFalloff = source->tx_falloff;
  532. result->AddProperty(&transparencyFalloff, 1, "$mat.blend.transparency.falloff", 0, 0);
  533. float transparencyLimit = source->tx_limit;
  534. result->AddProperty(&transparencyLimit, 1, "$mat.blend.transparency.limit", 0, 0);
  535. int transparencyDepth = source->ray_depth_tra;
  536. result->AddProperty(&transparencyDepth, 1, "$mat.blend.transparency.depth", 0, 0);
  537. float transparencyGlossAmount = source->gloss_tra;
  538. result->AddProperty(&transparencyGlossAmount, 1, "$mat.blend.transparency.glossAmount", 0, 0);
  539. float transparencyGlossThreshold = source->adapt_thresh_tra;
  540. result->AddProperty(&transparencyGlossThreshold, 1, "$mat.blend.transparency.glossThreshold", 0, 0);
  541. int transparencyGlossSamples = source->samp_gloss_tra;
  542. result->AddProperty(&transparencyGlossSamples, 1, "$mat.blend.transparency.glossSamples", 0, 0);
  543. int mirrorUse = source->mode & MA_RAYMIRROR ? 1 : 0;
  544. result->AddProperty(&mirrorUse, 1, "$mat.blend.mirror.use", 0, 0);
  545. float mirrorReflectivity = source->ray_mirror;
  546. result->AddProperty(&mirrorReflectivity, 1, "$mat.blend.mirror.reflectivity", 0, 0);
  547. aiColor3D mirrorColor(source->mirr, source->mirg, source->mirb);
  548. result->AddProperty(&mirrorColor, 1, "$mat.blend.mirror.color", 0, 0);
  549. float mirrorFresnel = source->fresnel_mir;
  550. result->AddProperty(&mirrorFresnel, 1, "$mat.blend.mirror.fresnel", 0, 0);
  551. float mirrorBlend = source->fresnel_mir_i;
  552. result->AddProperty(&mirrorBlend, 1, "$mat.blend.mirror.blend", 0, 0);
  553. int mirrorDepth = source->ray_depth;
  554. result->AddProperty(&mirrorDepth, 1, "$mat.blend.mirror.depth", 0, 0);
  555. float mirrorMaxDist = source->dist_mir;
  556. result->AddProperty(&mirrorMaxDist, 1, "$mat.blend.mirror.maxDist", 0, 0);
  557. int mirrorFadeTo = source->fadeto_mir;
  558. result->AddProperty(&mirrorFadeTo, 1, "$mat.blend.mirror.fadeTo", 0, 0);
  559. float mirrorGlossAmount = source->gloss_mir;
  560. result->AddProperty(&mirrorGlossAmount, 1, "$mat.blend.mirror.glossAmount", 0, 0);
  561. float mirrorGlossThreshold = source->adapt_thresh_mir;
  562. result->AddProperty(&mirrorGlossThreshold, 1, "$mat.blend.mirror.glossThreshold", 0, 0);
  563. int mirrorGlossSamples = source->samp_gloss_mir;
  564. result->AddProperty(&mirrorGlossSamples, 1, "$mat.blend.mirror.glossSamples", 0, 0);
  565. float mirrorGlossAnisotropic = source->aniso_gloss_mir;
  566. result->AddProperty(&mirrorGlossAnisotropic, 1, "$mat.blend.mirror.glossAnisotropic", 0, 0);
  567. }
  568. void BlenderImporter::BuildMaterials(ConversionData &conv_data) {
  569. conv_data.materials->reserve(conv_data.materials_raw.size());
  570. BuildDefaultMaterial(conv_data);
  571. for (const std::shared_ptr<Material> &mat : conv_data.materials_raw) {
  572. // reset per material global counters
  573. for (size_t i = 0; i < sizeof(conv_data.next_texture) / sizeof(conv_data.next_texture[0]); ++i) {
  574. conv_data.next_texture[i] = 0;
  575. }
  576. aiMaterial *mout = new aiMaterial();
  577. conv_data.materials->push_back(mout);
  578. // For any new material field handled here, the default material above must be updated with an appropriate default value.
  579. // set material name
  580. aiString name = aiString(mat->id.name + 2); // skip over the name prefix 'MA'
  581. mout->AddProperty(&name, AI_MATKEY_NAME);
  582. // basic material colors
  583. aiColor3D col(mat->r, mat->g, mat->b);
  584. if (mat->r || mat->g || mat->b) {
  585. // Usually, zero diffuse color means no diffuse color at all in the equation.
  586. // So we omit this member to express this intent.
  587. mout->AddProperty(&col, 1, AI_MATKEY_COLOR_DIFFUSE);
  588. if (mat->emit) {
  589. aiColor3D emit_col(mat->emit * mat->r, mat->emit * mat->g, mat->emit * mat->b);
  590. mout->AddProperty(&emit_col, 1, AI_MATKEY_COLOR_EMISSIVE);
  591. }
  592. }
  593. col = aiColor3D(mat->specr, mat->specg, mat->specb);
  594. mout->AddProperty(&col, 1, AI_MATKEY_COLOR_SPECULAR);
  595. // is hardness/shininess set?
  596. if (mat->har) {
  597. const float har = mat->har;
  598. mout->AddProperty(&har, 1, AI_MATKEY_SHININESS);
  599. }
  600. col = aiColor3D(mat->ambr, mat->ambg, mat->ambb);
  601. mout->AddProperty(&col, 1, AI_MATKEY_COLOR_AMBIENT);
  602. // is mirror enabled?
  603. if (mat->mode & MA_RAYMIRROR) {
  604. const float ray_mirror = mat->ray_mirror;
  605. mout->AddProperty(&ray_mirror, 1, AI_MATKEY_REFLECTIVITY);
  606. }
  607. col = aiColor3D(mat->mirr, mat->mirg, mat->mirb);
  608. mout->AddProperty(&col, 1, AI_MATKEY_COLOR_REFLECTIVE);
  609. for (size_t i = 0; i < sizeof(mat->mtex) / sizeof(mat->mtex[0]); ++i) {
  610. if (!mat->mtex[i]) {
  611. continue;
  612. }
  613. ResolveTexture(mout, mat.get(), mat->mtex[i].get(), conv_data);
  614. }
  615. AddBlendParams(mout, mat.get());
  616. }
  617. }
  618. // ------------------------------------------------------------------------------------------------
  619. void BlenderImporter::CheckActualType(const ElemBase *dt, const char *check) {
  620. ai_assert(dt);
  621. if (strcmp(dt->dna_type, check)) {
  622. ThrowException("Expected object at ", std::hex, dt, " to be of type `", check,
  623. "`, but it claims to be a `", dt->dna_type, "`instead");
  624. }
  625. }
  626. // ------------------------------------------------------------------------------------------------
  627. void BlenderImporter::NotSupportedObjectType(const Object *obj, const char *type) {
  628. LogWarn("Object `", obj->id.name, "` - type is unsupported: `", type, "`, skipping");
  629. }
  630. // ------------------------------------------------------------------------------------------------
  631. void BlenderImporter::ConvertMesh(const Scene & /*in*/, const Object * /*obj*/, const Mesh *mesh,
  632. ConversionData &conv_data, TempArray<std::vector, aiMesh> &temp) {
  633. // TODO: Resolve various problems with BMesh triangulation before re-enabling.
  634. // See issues #400, #373, #318 #315 and #132.
  635. #if defined(TODO_FIX_BMESH_CONVERSION)
  636. BlenderBMeshConverter BMeshConverter(mesh);
  637. if (BMeshConverter.ContainsBMesh()) {
  638. mesh = BMeshConverter.TriangulateBMesh();
  639. }
  640. #endif
  641. typedef std::pair<const int, size_t> MyPair;
  642. if ((!mesh->totface && !mesh->totloop) || !mesh->totvert) {
  643. return;
  644. }
  645. // some sanity checks
  646. if (static_cast<size_t>(mesh->totface) > mesh->mface.size()) {
  647. ThrowException("Number of faces is larger than the corresponding array");
  648. }
  649. if (static_cast<size_t>(mesh->totvert) > mesh->mvert.size()) {
  650. ThrowException("Number of vertices is larger than the corresponding array");
  651. }
  652. if (static_cast<size_t>(mesh->totloop) > mesh->mloop.size()) {
  653. ThrowException("Number of vertices is larger than the corresponding array");
  654. }
  655. // collect per-submesh numbers
  656. std::map<int, size_t> per_mat;
  657. std::map<int, size_t> per_mat_verts;
  658. for (int i = 0; i < mesh->totface; ++i) {
  659. const MFace &mf = mesh->mface[i];
  660. per_mat[mf.mat_nr]++;
  661. per_mat_verts[mf.mat_nr] += mf.v4 ? 4 : 3;
  662. }
  663. for (int i = 0; i < mesh->totpoly; ++i) {
  664. const MPoly &mp = mesh->mpoly[i];
  665. per_mat[mp.mat_nr]++;
  666. per_mat_verts[mp.mat_nr] += mp.totloop;
  667. }
  668. // ... and allocate the corresponding meshes
  669. const size_t old = temp->size();
  670. temp->reserve(temp->size() + per_mat.size());
  671. std::map<size_t, size_t> mat_num_to_mesh_idx;
  672. for (MyPair &it : per_mat) {
  673. mat_num_to_mesh_idx[it.first] = temp->size();
  674. temp->push_back(new aiMesh());
  675. aiMesh *out = temp->back();
  676. out->mVertices = new aiVector3D[per_mat_verts[it.first]];
  677. out->mNormals = new aiVector3D[per_mat_verts[it.first]];
  678. //out->mNumFaces = 0
  679. //out->mNumVertices = 0
  680. out->mFaces = new aiFace[it.second]();
  681. // all sub-meshes created from this mesh are named equally. this allows
  682. // curious users to recover the original adjacency.
  683. out->mName = aiString(mesh->id.name + 2);
  684. // skip over the name prefix 'ME'
  685. // resolve the material reference and add this material to the set of
  686. // output materials. The (temporary) material index is the index
  687. // of the material entry within the list of resolved materials.
  688. if (mesh->mat) {
  689. if (static_cast<size_t>(it.first) >= mesh->mat.size()) {
  690. ThrowException("Material index is out of range");
  691. }
  692. std::shared_ptr<Material> mat = mesh->mat[it.first];
  693. const std::deque<std::shared_ptr<Material>>::iterator has = std::find(
  694. conv_data.materials_raw.begin(),
  695. conv_data.materials_raw.end(), mat);
  696. if (has != conv_data.materials_raw.end()) {
  697. out->mMaterialIndex = static_cast<unsigned int>(std::distance(conv_data.materials_raw.begin(), has));
  698. } else {
  699. out->mMaterialIndex = static_cast<unsigned int>(conv_data.materials_raw.size());
  700. conv_data.materials_raw.push_back(mat);
  701. }
  702. } else
  703. out->mMaterialIndex = static_cast<unsigned int>(-1);
  704. }
  705. for (int i = 0; i < mesh->totface; ++i) {
  706. const MFace &mf = mesh->mface[i];
  707. aiMesh *const out = temp[mat_num_to_mesh_idx[mf.mat_nr]];
  708. aiFace &f = out->mFaces[out->mNumFaces++];
  709. f.mIndices = new unsigned int[f.mNumIndices = mf.v4 ? 4 : 3];
  710. aiVector3D *vo = out->mVertices + out->mNumVertices;
  711. aiVector3D *vn = out->mNormals + out->mNumVertices;
  712. // XXX we can't fold this easily, because we are restricted
  713. // to the member names from the BLEND file (v1,v2,v3,v4)
  714. // which are assigned by the genblenddna.py script and
  715. // cannot be changed without breaking the entire
  716. // import process.
  717. if (mf.v1 >= mesh->totvert) {
  718. ThrowException("Vertex index v1 out of range");
  719. }
  720. const MVert *v = &mesh->mvert[mf.v1];
  721. vo->x = v->co[0];
  722. vo->y = v->co[1];
  723. vo->z = v->co[2];
  724. vn->x = v->no[0];
  725. vn->y = v->no[1];
  726. vn->z = v->no[2];
  727. f.mIndices[0] = out->mNumVertices++;
  728. ++vo;
  729. ++vn;
  730. // if (f.mNumIndices >= 2) {
  731. if (mf.v2 >= mesh->totvert) {
  732. ThrowException("Vertex index v2 out of range");
  733. }
  734. v = &mesh->mvert[mf.v2];
  735. vo->x = v->co[0];
  736. vo->y = v->co[1];
  737. vo->z = v->co[2];
  738. vn->x = v->no[0];
  739. vn->y = v->no[1];
  740. vn->z = v->no[2];
  741. f.mIndices[1] = out->mNumVertices++;
  742. ++vo;
  743. ++vn;
  744. if (mf.v3 >= mesh->totvert) {
  745. ThrowException("Vertex index v3 out of range");
  746. }
  747. // if (f.mNumIndices >= 3) {
  748. v = &mesh->mvert[mf.v3];
  749. vo->x = v->co[0];
  750. vo->y = v->co[1];
  751. vo->z = v->co[2];
  752. vn->x = v->no[0];
  753. vn->y = v->no[1];
  754. vn->z = v->no[2];
  755. f.mIndices[2] = out->mNumVertices++;
  756. ++vo;
  757. ++vn;
  758. if (mf.v4 >= mesh->totvert) {
  759. ThrowException("Vertex index v4 out of range");
  760. }
  761. // if (f.mNumIndices >= 4) {
  762. if (mf.v4) {
  763. v = &mesh->mvert[mf.v4];
  764. vo->x = v->co[0];
  765. vo->y = v->co[1];
  766. vo->z = v->co[2];
  767. vn->x = v->no[0];
  768. vn->y = v->no[1];
  769. vn->z = v->no[2];
  770. f.mIndices[3] = out->mNumVertices++;
  771. ++vo;
  772. ++vn;
  773. out->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  774. } else
  775. out->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  776. // }
  777. // }
  778. // }
  779. }
  780. for (int i = 0; i < mesh->totpoly; ++i) {
  781. const MPoly &mf = mesh->mpoly[i];
  782. aiMesh *const out = temp[mat_num_to_mesh_idx[mf.mat_nr]];
  783. aiFace &f = out->mFaces[out->mNumFaces++];
  784. f.mIndices = new unsigned int[f.mNumIndices = mf.totloop];
  785. aiVector3D *vo = out->mVertices + out->mNumVertices;
  786. aiVector3D *vn = out->mNormals + out->mNumVertices;
  787. // XXX we can't fold this easily, because we are restricted
  788. // to the member names from the BLEND file (v1,v2,v3,v4)
  789. // which are assigned by the genblenddna.py script and
  790. // cannot be changed without breaking the entire
  791. // import process.
  792. for (int j = 0; j < mf.totloop; ++j) {
  793. const MLoop &loop = mesh->mloop[mf.loopstart + j];
  794. if (loop.v >= mesh->totvert) {
  795. ThrowException("Vertex index out of range");
  796. }
  797. const MVert &v = mesh->mvert[loop.v];
  798. vo->x = v.co[0];
  799. vo->y = v.co[1];
  800. vo->z = v.co[2];
  801. vn->x = v.no[0];
  802. vn->y = v.no[1];
  803. vn->z = v.no[2];
  804. f.mIndices[j] = out->mNumVertices++;
  805. ++vo;
  806. ++vn;
  807. }
  808. if (mf.totloop == 3) {
  809. out->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
  810. } else {
  811. out->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
  812. }
  813. }
  814. // TODO should we create the TextureUVMapping map in Convert<Material> to prevent redundant processing?
  815. // create texture <-> uvname mapping for all materials
  816. // key is texture number, value is data *
  817. typedef std::map<uint32_t, const MLoopUV *> TextureUVMapping;
  818. // key is material number, value is the TextureUVMapping for the material
  819. typedef std::map<uint32_t, TextureUVMapping> MaterialTextureUVMappings;
  820. MaterialTextureUVMappings matTexUvMappings;
  821. const uint32_t maxMat = static_cast<const uint32_t>(mesh->mat.size());
  822. for (uint32_t m = 0; m < maxMat; ++m) {
  823. // get material by index
  824. const std::shared_ptr<Material> pMat = mesh->mat[m];
  825. TextureUVMapping texuv;
  826. const uint32_t maxTex = sizeof(pMat->mtex) / sizeof(pMat->mtex[0]);
  827. for (uint32_t t = 0; t < maxTex; ++t) {
  828. if (pMat->mtex[t] && pMat->mtex[t]->uvname[0]) {
  829. // get the CustomData layer for given uvname and correct type
  830. const ElemBase *pLoop = getCustomDataLayerData(mesh->ldata, CD_MLOOPUV, pMat->mtex[t]->uvname);
  831. if (pLoop) {
  832. texuv.insert(std::make_pair(t, dynamic_cast<const MLoopUV *>(pLoop)));
  833. }
  834. }
  835. }
  836. if (texuv.size()) {
  837. matTexUvMappings.insert(std::make_pair(m, texuv));
  838. }
  839. }
  840. // collect texture coordinates, they're stored in a separate per-face buffer
  841. if (mesh->mtface || mesh->mloopuv) {
  842. if (mesh->totface > static_cast<int>(mesh->mtface.size())) {
  843. ThrowException("Number of UV faces is larger than the corresponding UV face array (#1)");
  844. }
  845. for (std::vector<aiMesh *>::iterator it = temp->begin() + old; it != temp->end(); ++it) {
  846. ai_assert(0 != (*it)->mNumVertices);
  847. ai_assert(0 != (*it)->mNumFaces);
  848. const auto itMatTexUvMapping = matTexUvMappings.find((*it)->mMaterialIndex);
  849. if (itMatTexUvMapping == matTexUvMappings.end()) {
  850. // default behaviour like before
  851. (*it)->mTextureCoords[0] = new aiVector3D[(*it)->mNumVertices];
  852. } else {
  853. // create texture coords for every mapped tex
  854. for (uint32_t i = 0; i < itMatTexUvMapping->second.size(); ++i) {
  855. (*it)->mTextureCoords[i] = new aiVector3D[(*it)->mNumVertices];
  856. }
  857. }
  858. (*it)->mNumFaces = (*it)->mNumVertices = 0;
  859. }
  860. for (int i = 0; i < mesh->totface; ++i) {
  861. const MTFace *v = &mesh->mtface[i];
  862. aiMesh *const out = temp[mat_num_to_mesh_idx[mesh->mface[i].mat_nr]];
  863. const aiFace &f = out->mFaces[out->mNumFaces++];
  864. aiVector3D *vo = &out->mTextureCoords[0][out->mNumVertices];
  865. for (unsigned int j = 0; j < f.mNumIndices; ++j, ++vo, ++out->mNumVertices) {
  866. vo->x = v->uv[j][0];
  867. vo->y = v->uv[j][1];
  868. }
  869. }
  870. for (int i = 0; i < mesh->totpoly; ++i) {
  871. const MPoly &v = mesh->mpoly[i];
  872. aiMesh *const out = temp[mat_num_to_mesh_idx[v.mat_nr]];
  873. const aiFace &f = out->mFaces[out->mNumFaces++];
  874. const auto itMatTexUvMapping = matTexUvMappings.find(v.mat_nr);
  875. if (itMatTexUvMapping == matTexUvMappings.end()) {
  876. // old behavior
  877. aiVector3D *vo = &out->mTextureCoords[0][out->mNumVertices];
  878. for (unsigned int j = 0; j < f.mNumIndices; ++j, ++vo, ++out->mNumVertices) {
  879. const MLoopUV &uv = mesh->mloopuv[v.loopstart + j];
  880. vo->x = uv.uv[0];
  881. vo->y = uv.uv[1];
  882. }
  883. } else {
  884. // create textureCoords for every mapped tex
  885. for (uint32_t m = 0; m < itMatTexUvMapping->second.size(); ++m) {
  886. const MLoopUV *tm = itMatTexUvMapping->second[m];
  887. aiVector3D *vo = &out->mTextureCoords[m][out->mNumVertices];
  888. uint32_t j = 0;
  889. for (; j < f.mNumIndices; ++j, ++vo) {
  890. const MLoopUV &uv = tm[v.loopstart + j];
  891. vo->x = uv.uv[0];
  892. vo->y = uv.uv[1];
  893. }
  894. // only update written mNumVertices in last loop
  895. // TODO why must the numVertices be incremented here?
  896. if (m == itMatTexUvMapping->second.size() - 1) {
  897. out->mNumVertices += j;
  898. }
  899. }
  900. }
  901. }
  902. }
  903. // collect texture coordinates, old-style (marked as deprecated in current blender sources)
  904. if (mesh->tface) {
  905. if (mesh->totface > static_cast<int>(mesh->tface.size())) {
  906. ThrowException("Number of faces is larger than the corresponding UV face array (#2)");
  907. }
  908. for (std::vector<aiMesh *>::iterator it = temp->begin() + old; it != temp->end(); ++it) {
  909. ai_assert(0 != (*it)->mNumVertices);
  910. ai_assert(0 != (*it)->mNumFaces);
  911. (*it)->mTextureCoords[0] = new aiVector3D[(*it)->mNumVertices];
  912. (*it)->mNumFaces = (*it)->mNumVertices = 0;
  913. }
  914. for (int i = 0; i < mesh->totface; ++i) {
  915. const TFace *v = &mesh->tface[i];
  916. aiMesh *const out = temp[mat_num_to_mesh_idx[mesh->mface[i].mat_nr]];
  917. const aiFace &f = out->mFaces[out->mNumFaces++];
  918. aiVector3D *vo = &out->mTextureCoords[0][out->mNumVertices];
  919. for (unsigned int j = 0; j < f.mNumIndices; ++j, ++vo, ++out->mNumVertices) {
  920. vo->x = v->uv[j][0];
  921. vo->y = v->uv[j][1];
  922. }
  923. }
  924. }
  925. // collect vertex colors, stored separately as well
  926. if (mesh->mcol || mesh->mloopcol) {
  927. if (mesh->totface > static_cast<int>((mesh->mcol.size() / 4))) {
  928. ThrowException("Number of faces is larger than the corresponding color face array");
  929. }
  930. for (std::vector<aiMesh *>::iterator it = temp->begin() + old; it != temp->end(); ++it) {
  931. ai_assert(0 != (*it)->mNumVertices);
  932. ai_assert(0 != (*it)->mNumFaces);
  933. (*it)->mColors[0] = new aiColor4D[(*it)->mNumVertices];
  934. (*it)->mNumFaces = (*it)->mNumVertices = 0;
  935. }
  936. for (int i = 0; i < mesh->totface; ++i) {
  937. aiMesh *const out = temp[mat_num_to_mesh_idx[mesh->mface[i].mat_nr]];
  938. const aiFace &f = out->mFaces[out->mNumFaces++];
  939. aiColor4D *vo = &out->mColors[0][out->mNumVertices];
  940. for (unsigned int n = 0; n < f.mNumIndices; ++n, ++vo, ++out->mNumVertices) {
  941. const MCol *col = &mesh->mcol[(i << 2) + n];
  942. vo->r = col->r;
  943. vo->g = col->g;
  944. vo->b = col->b;
  945. vo->a = col->a;
  946. }
  947. for (unsigned int n = f.mNumIndices; n < 4; ++n)
  948. ;
  949. }
  950. for (int i = 0; i < mesh->totpoly; ++i) {
  951. const MPoly &v = mesh->mpoly[i];
  952. aiMesh *const out = temp[mat_num_to_mesh_idx[v.mat_nr]];
  953. const aiFace &f = out->mFaces[out->mNumFaces++];
  954. aiColor4D *vo = &out->mColors[0][out->mNumVertices];
  955. const ai_real scaleZeroToOne = 1.f / 255.f;
  956. for (unsigned int j = 0; j < f.mNumIndices; ++j, ++vo, ++out->mNumVertices) {
  957. const MLoopCol &col = mesh->mloopcol[v.loopstart + j];
  958. vo->r = ai_real(col.r) * scaleZeroToOne;
  959. vo->g = ai_real(col.g) * scaleZeroToOne;
  960. vo->b = ai_real(col.b) * scaleZeroToOne;
  961. vo->a = ai_real(col.a) * scaleZeroToOne;
  962. }
  963. }
  964. }
  965. return;
  966. }
  967. // ------------------------------------------------------------------------------------------------
  968. aiCamera *BlenderImporter::ConvertCamera(const Scene & /*in*/, const Object *obj, const Camera *cam, ConversionData & /*conv_data*/) {
  969. std::unique_ptr<aiCamera> out(new aiCamera());
  970. out->mName = obj->id.name + 2;
  971. out->mPosition = aiVector3D(0.f, 0.f, 0.f);
  972. out->mUp = aiVector3D(0.f, 1.f, 0.f);
  973. out->mLookAt = aiVector3D(0.f, 0.f, -1.f);
  974. if (cam->sensor_x && cam->lens) {
  975. out->mHorizontalFOV = 2.f * std::atan2(cam->sensor_x, 2.f * cam->lens);
  976. }
  977. out->mClipPlaneNear = cam->clipsta;
  978. out->mClipPlaneFar = cam->clipend;
  979. return out.release();
  980. }
  981. // ------------------------------------------------------------------------------------------------
  982. aiLight *BlenderImporter::ConvertLight(const Scene & /*in*/, const Object *obj, const Lamp *lamp, ConversionData & /*conv_data*/) {
  983. std::unique_ptr<aiLight> out(new aiLight());
  984. out->mName = obj->id.name + 2;
  985. switch (lamp->type) {
  986. case Lamp::Type_Local:
  987. out->mType = aiLightSource_POINT;
  988. break;
  989. case Lamp::Type_Spot:
  990. out->mType = aiLightSource_SPOT;
  991. // blender orients directional lights as facing toward -z
  992. out->mDirection = aiVector3D(0.f, 0.f, -1.f);
  993. out->mUp = aiVector3D(0.f, 1.f, 0.f);
  994. out->mAngleInnerCone = lamp->spotsize * (1.0f - lamp->spotblend);
  995. out->mAngleOuterCone = lamp->spotsize;
  996. break;
  997. case Lamp::Type_Sun:
  998. out->mType = aiLightSource_DIRECTIONAL;
  999. // blender orients directional lights as facing toward -z
  1000. out->mDirection = aiVector3D(0.f, 0.f, -1.f);
  1001. out->mUp = aiVector3D(0.f, 1.f, 0.f);
  1002. break;
  1003. case Lamp::Type_Area:
  1004. out->mType = aiLightSource_AREA;
  1005. if (lamp->area_shape == 0) {
  1006. out->mSize = aiVector2D(lamp->area_size, lamp->area_size);
  1007. } else {
  1008. out->mSize = aiVector2D(lamp->area_size, lamp->area_sizey);
  1009. }
  1010. // blender orients directional lights as facing toward -z
  1011. out->mDirection = aiVector3D(0.f, 0.f, -1.f);
  1012. out->mUp = aiVector3D(0.f, 1.f, 0.f);
  1013. break;
  1014. default:
  1015. break;
  1016. }
  1017. out->mColorAmbient = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy;
  1018. out->mColorSpecular = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy;
  1019. out->mColorDiffuse = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy;
  1020. // If default values are supplied, compute the coefficients from light's max distance
  1021. // Read this: https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
  1022. //
  1023. if (lamp->constant_coefficient == 1.0f && lamp->linear_coefficient == 0.0f && lamp->quadratic_coefficient == 0.0f && lamp->dist > 0.0f) {
  1024. out->mAttenuationConstant = 1.0f;
  1025. out->mAttenuationLinear = 2.0f / lamp->dist;
  1026. out->mAttenuationQuadratic = 1.0f / (lamp->dist * lamp->dist);
  1027. } else {
  1028. out->mAttenuationConstant = lamp->constant_coefficient;
  1029. out->mAttenuationLinear = lamp->linear_coefficient;
  1030. out->mAttenuationQuadratic = lamp->quadratic_coefficient;
  1031. }
  1032. return out.release();
  1033. }
  1034. // ------------------------------------------------------------------------------------------------
  1035. aiNode *BlenderImporter::ConvertNode(const Scene &in, const Object *obj, ConversionData &conv_data, const aiMatrix4x4 &parentTransform) {
  1036. std::deque<const Object *> children;
  1037. for (ObjectSet::iterator it = conv_data.objects.begin(); it != conv_data.objects.end();) {
  1038. const Object *object = *it;
  1039. if (object->parent == obj) {
  1040. children.push_back(object);
  1041. conv_data.objects.erase(it++);
  1042. continue;
  1043. }
  1044. ++it;
  1045. }
  1046. std::unique_ptr<aiNode> node(new aiNode(obj->id.name + 2)); // skip over the name prefix 'OB'
  1047. if (obj->data) {
  1048. switch (obj->type) {
  1049. case Object ::Type_EMPTY:
  1050. break; // do nothing
  1051. // supported object types
  1052. case Object ::Type_MESH: {
  1053. const size_t old = conv_data.meshes->size();
  1054. CheckActualType(obj->data.get(), "Mesh");
  1055. ConvertMesh(in, obj, static_cast<const Mesh *>(obj->data.get()), conv_data, conv_data.meshes);
  1056. if (conv_data.meshes->size() > old) {
  1057. node->mMeshes = new unsigned int[node->mNumMeshes = static_cast<unsigned int>(conv_data.meshes->size() - old)];
  1058. for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
  1059. node->mMeshes[i] = static_cast<unsigned int>(i + old);
  1060. }
  1061. }
  1062. } break;
  1063. case Object ::Type_LAMP: {
  1064. CheckActualType(obj->data.get(), "Lamp");
  1065. aiLight *mesh = ConvertLight(in, obj, static_cast<const Lamp *>(obj->data.get()), conv_data);
  1066. if (mesh) {
  1067. conv_data.lights->push_back(mesh);
  1068. }
  1069. } break;
  1070. case Object ::Type_CAMERA: {
  1071. CheckActualType(obj->data.get(), "Camera");
  1072. aiCamera *mesh = ConvertCamera(in, obj, static_cast<const Camera *>(obj->data.get()), conv_data);
  1073. if (mesh) {
  1074. conv_data.cameras->push_back(mesh);
  1075. }
  1076. } break;
  1077. // unsupported object types / log, but do not break
  1078. case Object ::Type_CURVE:
  1079. NotSupportedObjectType(obj, "Curve");
  1080. break;
  1081. case Object ::Type_SURF:
  1082. NotSupportedObjectType(obj, "Surface");
  1083. break;
  1084. case Object ::Type_FONT:
  1085. NotSupportedObjectType(obj, "Font");
  1086. break;
  1087. case Object ::Type_MBALL:
  1088. NotSupportedObjectType(obj, "MetaBall");
  1089. break;
  1090. case Object ::Type_WAVE:
  1091. NotSupportedObjectType(obj, "Wave");
  1092. break;
  1093. case Object ::Type_LATTICE:
  1094. NotSupportedObjectType(obj, "Lattice");
  1095. break;
  1096. // invalid or unknown type
  1097. default:
  1098. break;
  1099. }
  1100. }
  1101. for (unsigned int x = 0; x < 4; ++x) {
  1102. for (unsigned int y = 0; y < 4; ++y) {
  1103. node->mTransformation[y][x] = obj->obmat[x][y];
  1104. }
  1105. }
  1106. aiMatrix4x4 m = parentTransform;
  1107. m = m.Inverse();
  1108. node->mTransformation = m * node->mTransformation;
  1109. if (children.size()) {
  1110. node->mNumChildren = static_cast<unsigned int>(children.size());
  1111. aiNode **nd = node->mChildren = new aiNode *[node->mNumChildren]();
  1112. for (const Object *nobj : children) {
  1113. *nd = ConvertNode(in, nobj, conv_data, node->mTransformation * parentTransform);
  1114. (*nd++)->mParent = node.get();
  1115. }
  1116. }
  1117. // apply modifiers
  1118. modifier_cache->ApplyModifiers(*node, conv_data, in, *obj);
  1119. return node.release();
  1120. }
  1121. #endif // ASSIMP_BUILD_NO_BLEND_IMPORTER