2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file BlenderLoader.cpp
|
|
|
|
* @brief Implementation of the Blender3D importer class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define ASSIMP_BUILD_NO_COMPRESSED_BLEND
|
|
|
|
// Uncomment this to disable support for (gzip)compressed .BLEND files
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_BLEND_IMPORTER
|
|
|
|
|
|
|
|
#include "BlenderBMesh.h"
|
2018-06-09 13:45:09 +00:00
|
|
|
#include "BlenderCustomData.h"
|
2020-06-23 19:05:42 +00:00
|
|
|
#include "BlenderIntermediate.h"
|
|
|
|
#include "BlenderModifier.h"
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
2017-02-22 16:20:26 +00:00
|
|
|
#include <assimp/importerdesc.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/scene.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/MemoryIOWrapper.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/StreamReader.h>
|
|
|
|
#include <assimp/StringComparison.h>
|
2017-02-22 16:20:26 +00:00
|
|
|
|
2015-04-16 00:55:41 +00:00
|
|
|
#include <cctype>
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
// zlib is needed for compressed blend files
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_COMPRESSED_BLEND
|
2021-02-28 11:17:54 +00:00
|
|
|
# ifdef ASSIMP_BUILD_NO_OWN_ZLIB
|
|
|
|
# include <zlib.h>
|
|
|
|
# else
|
|
|
|
# include "../contrib/zlib/zlib.h"
|
|
|
|
# endif
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Assimp {
|
2021-02-28 11:17:54 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
template <>
|
|
|
|
const char *LogFunctions<BlenderImporter>::Prefix() {
|
|
|
|
static auto prefix = "BLEND: ";
|
|
|
|
return prefix;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
2021-02-28 11:17:54 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
} // namespace Assimp
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
using namespace Assimp::Blender;
|
|
|
|
using namespace Assimp::Formatter;
|
|
|
|
|
|
|
|
static const aiImporterDesc blenderDesc = {
|
2021-04-27 00:27:28 +00:00
|
|
|
"Blender 3D Importer (http://www.blender3d.org)",
|
2015-05-19 03:57:13 +00:00
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"No animation support yet",
|
|
|
|
aiImporterFlags_SupportBinaryFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
2,
|
|
|
|
50,
|
|
|
|
"blend"
|
2015-05-19 03:48:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
2020-06-23 19:05:42 +00:00
|
|
|
BlenderImporter::BlenderImporter() :
|
|
|
|
modifier_cache(new BlenderModifierShowcase()) {
|
2017-02-04 17:32:04 +00:00
|
|
|
// empty
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Destructor, private as well
|
2020-06-23 19:05:42 +00:00
|
|
|
BlenderImporter::~BlenderImporter() {
|
2015-05-19 03:57:13 +00:00
|
|
|
delete modifier_cache;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
static const char *Tokens[] = { "BLENDER" };
|
|
|
|
static const char *TokensForSearch[] = { "blender" };
|
2016-09-04 18:41:20 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Returns whether the class can handle the format of the given file.
|
2020-06-23 19:05:42 +00:00
|
|
|
bool BlenderImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
|
|
|
|
const std::string &extension = GetExtension(pFile);
|
2015-05-19 03:57:13 +00:00
|
|
|
if (extension == "blend") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
if ((!extension.length() || checkSig) && pIOHandler) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// note: this won't catch compressed files
|
2020-06-23 19:05:42 +00:00
|
|
|
return SearchFileHeaderForToken(pIOHandler, pFile, TokensForSearch, 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2021-02-28 11:17:54 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Loader registry entry
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiImporterDesc *BlenderImporter::GetInfo() const {
|
2015-05-19 03:57:13 +00:00
|
|
|
return &blenderDesc;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Setup configuration properties for the loader
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::SetupProperties(const Importer * /*pImp*/) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to be done for the moment
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Imports the given file into the given scene structure.
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::InternReadFile(const std::string &pFile,
|
|
|
|
aiScene *pScene, IOSystem *pIOHandler) {
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_COMPRESSED_BLEND
|
2018-02-06 16:52:23 +00:00
|
|
|
std::vector<Bytef> uncompressed;
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
FileDatabase file;
|
2020-06-23 19:05:42 +00:00
|
|
|
std::shared_ptr<IOStream> stream(pIOHandler->Open(pFile, "rb"));
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!stream) {
|
|
|
|
ThrowException("Could not open file for reading");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
char magic[8] = { 0 };
|
|
|
|
stream->Read(magic, 7, 1);
|
|
|
|
if (strcmp(magic, Tokens[0])) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Check for presence of the gzip header. If yes, assume it is a
|
|
|
|
// compressed blend file and try uncompressing it, else fail. This is to
|
|
|
|
// avoid uncompressing random files which our loader might end up with.
|
2015-05-19 03:48:29 +00:00
|
|
|
#ifdef ASSIMP_BUILD_NO_COMPRESSED_BLEND
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("BLENDER magic bytes are missing, is this file compressed (Assimp was built without decompression support)?");
|
2015-05-19 03:48:29 +00:00
|
|
|
#else
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (magic[0] != 0x1f || static_cast<uint8_t>(magic[1]) != 0x8b) {
|
|
|
|
ThrowException("BLENDER magic bytes are missing, couldn't find GZIP header either");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
LogDebug("Found no BLENDER magic word but a GZIP header, might be a compressed file");
|
|
|
|
if (magic[2] != 8) {
|
|
|
|
ThrowException("Unsupported GZIP compression method");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// http://www.gzip.org/zlib/rfc-gzip.html#header-trailer
|
2020-06-23 19:05:42 +00:00
|
|
|
stream->Seek(0L, aiOrigin_SET);
|
2016-04-05 21:23:53 +00:00
|
|
|
std::shared_ptr<StreamReaderLE> reader = std::shared_ptr<StreamReaderLE>(new StreamReaderLE(stream));
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// build a zlib stream
|
|
|
|
z_stream zstream;
|
|
|
|
zstream.opaque = Z_NULL;
|
|
|
|
zstream.zalloc = Z_NULL;
|
2020-06-23 19:05:42 +00:00
|
|
|
zstream.zfree = Z_NULL;
|
2015-05-19 03:57:13 +00:00
|
|
|
zstream.data_type = Z_BINARY;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// http://hewgill.com/journal/entries/349-how-to-decompress-gzip-stream-with-zlib
|
2020-06-23 19:05:42 +00:00
|
|
|
inflateInit2(&zstream, 16 + MAX_WBITS);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
zstream.next_in = reinterpret_cast<Bytef *>(reader->GetPtr());
|
|
|
|
zstream.avail_in = (uInt)reader->GetRemainingSize();
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
size_t total = 0l;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2018-02-06 16:52:23 +00:00
|
|
|
// TODO: be smarter about this, decompress directly into heap buffer
|
2015-05-19 03:57:13 +00:00
|
|
|
// and decompress the data .... do 1k chunks in the hope that we won't kill the stack
|
2015-05-19 03:48:29 +00:00
|
|
|
#define MYBLOCK 1024
|
2015-05-19 03:57:13 +00:00
|
|
|
Bytef block[MYBLOCK];
|
|
|
|
int ret;
|
|
|
|
do {
|
|
|
|
zstream.avail_out = MYBLOCK;
|
|
|
|
zstream.next_out = block;
|
|
|
|
ret = inflate(&zstream, Z_NO_FLUSH);
|
|
|
|
|
|
|
|
if (ret != Z_STREAM_END && ret != Z_OK) {
|
|
|
|
ThrowException("Failure decompressing this file using gzip, seemingly it is NOT a compressed .BLEND file");
|
|
|
|
}
|
|
|
|
const size_t have = MYBLOCK - zstream.avail_out;
|
|
|
|
total += have;
|
2018-02-06 16:52:23 +00:00
|
|
|
uncompressed.resize(total);
|
2020-06-23 19:05:42 +00:00
|
|
|
memcpy(uncompressed.data() + total - have, block, have);
|
|
|
|
} while (ret != Z_STREAM_END);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// terminate zlib
|
|
|
|
inflateEnd(&zstream);
|
|
|
|
|
|
|
|
// replace the input stream with a memory stream
|
2020-06-23 19:05:42 +00:00
|
|
|
stream.reset(new MemoryIOStream(reinterpret_cast<uint8_t *>(uncompressed.data()), total));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// .. and retry
|
2020-06-23 19:05:42 +00:00
|
|
|
stream->Read(magic, 7, 1);
|
|
|
|
if (strcmp(magic, "BLENDER")) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("Found no BLENDER magic word in decompressed GZIP file");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
file.i64bit = (stream->Read(magic, 1, 1), magic[0] == '-');
|
|
|
|
file.little = (stream->Read(magic, 1, 1), magic[0] == 'v');
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
stream->Read(magic, 3, 1);
|
2015-05-19 03:57:13 +00:00
|
|
|
magic[3] = '\0';
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2021-05-13 09:43:28 +00:00
|
|
|
LogInfo("Blender version is ", magic[0], ".", magic + 1,
|
2020-06-23 19:05:42 +00:00
|
|
|
" (64bit: ", file.i64bit ? "true" : "false",
|
2021-05-13 09:43:28 +00:00
|
|
|
", little endian: ", file.little ? "true" : "false", ")");
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
ParseBlendFile(file, stream);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
Scene scene;
|
2020-06-23 19:05:42 +00:00
|
|
|
ExtractScene(scene, file);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
ConvertBlendFile(pScene, scene, file);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::ParseBlendFile(FileDatabase &out, std::shared_ptr<IOStream> stream) {
|
2021-02-28 11:17:54 +00:00
|
|
|
out.reader = std::make_shared<StreamReaderAny>(stream, out.little);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
DNAParser dna_reader(out);
|
2020-06-23 19:05:42 +00:00
|
|
|
const DNA *dna = nullptr;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
out.entries.reserve(128);
|
|
|
|
{ // even small BLEND files tend to consist of many file blocks
|
|
|
|
SectionParser parser(*out.reader.get(), out.i64bit);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// first parse the file in search for the DNA and insert all other sections into the database
|
2020-06-23 19:05:42 +00:00
|
|
|
while ((parser.Next(), 1)) {
|
|
|
|
const FileBlockHead &head = parser.GetCurrent();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (head.id == "ENDB") {
|
|
|
|
break; // only valid end of the file
|
2020-06-23 19:05:42 +00:00
|
|
|
} else if (head.id == "DNA1") {
|
2015-05-19 03:57:13 +00:00
|
|
|
dna_reader.Parse();
|
|
|
|
dna = &dna_reader.GetDNA();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
out.entries.push_back(head);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!dna) {
|
|
|
|
ThrowException("SDNA not found");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::sort(out.entries.begin(), out.entries.end());
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::ExtractScene(Scene &out, const FileDatabase &file) {
|
|
|
|
const FileBlockHead *block = nullptr;
|
|
|
|
std::map<std::string, size_t>::const_iterator it = file.dna.indices.find("Scene");
|
2015-05-19 03:57:13 +00:00
|
|
|
if (it == file.dna.indices.end()) {
|
|
|
|
ThrowException("There is no `Scene` structure record");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const Structure &ss = file.dna.structures[(*it).second];
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// we need a scene somewhere to start with.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (const FileBlockHead &bl : file.entries) {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Fix: using the DNA index is more reliable to locate scenes
|
|
|
|
//if (bl.id == "SC") {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (bl.dna_index == (*it).second) {
|
|
|
|
block = &bl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!block) {
|
|
|
|
ThrowException("There is not a single `Scene` record to load");
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
file.reader->SetCurrentPos(block->start);
|
2020-06-23 19:05:42 +00:00
|
|
|
ss.Convert(out, file);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_BLENDER_NO_STATS
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_INFO(
|
2020-06-23 19:05:42 +00:00
|
|
|
"(Stats) Fields read: ", file.stats().fields_read,
|
|
|
|
", pointers resolved: ", file.stats().pointers_resolved,
|
|
|
|
", cache hits: ", file.stats().cache_hits,
|
|
|
|
", cached objects: ", file.stats().cached_objects);
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::ConvertBlendFile(aiScene *out, const Scene &in, const FileDatabase &file) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ConversionData conv(file);
|
|
|
|
|
|
|
|
// FIXME it must be possible to take the hierarchy directly from
|
|
|
|
// the file. This is terrible. Here, we're first looking for
|
|
|
|
// all objects which don't have parent objects at all -
|
2020-06-23 19:05:42 +00:00
|
|
|
std::deque<const Object *> no_parents;
|
|
|
|
for (std::shared_ptr<Base> cur = std::static_pointer_cast<Base>(in.base.first); cur; cur = cur->next) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (cur->object) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!cur->object->parent) {
|
2015-05-19 03:57:13 +00:00
|
|
|
no_parents.push_back(cur->object.get());
|
2016-09-04 18:41:20 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
conv.objects.insert(cur->object.get());
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-05 21:23:53 +00:00
|
|
|
for (std::shared_ptr<Base> cur = in.basact; cur; cur = cur->next) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (cur->object) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (cur->object->parent) {
|
2015-05-19 03:57:13 +00:00
|
|
|
conv.objects.insert(cur->object.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (no_parents.empty()) {
|
|
|
|
ThrowException("Expected at least one object with no parent");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNode *root = out->mRootNode = new aiNode("<BlenderRoot>");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
root->mNumChildren = static_cast<unsigned int>(no_parents.size());
|
2020-06-23 19:05:42 +00:00
|
|
|
root->mChildren = new aiNode *[root->mNumChildren]();
|
2015-05-19 03:57:13 +00:00
|
|
|
for (unsigned int i = 0; i < root->mNumChildren; ++i) {
|
|
|
|
root->mChildren[i] = ConvertNode(in, no_parents[i], conv, aiMatrix4x4());
|
|
|
|
root->mChildren[i]->mParent = root;
|
|
|
|
}
|
|
|
|
|
|
|
|
BuildMaterials(conv);
|
|
|
|
|
|
|
|
if (conv.meshes->size()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mMeshes = new aiMesh *[out->mNumMeshes = static_cast<unsigned int>(conv.meshes->size())];
|
|
|
|
std::copy(conv.meshes->begin(), conv.meshes->end(), out->mMeshes);
|
2015-05-19 03:57:13 +00:00
|
|
|
conv.meshes.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conv.lights->size()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mLights = new aiLight *[out->mNumLights = static_cast<unsigned int>(conv.lights->size())];
|
|
|
|
std::copy(conv.lights->begin(), conv.lights->end(), out->mLights);
|
2015-05-19 03:57:13 +00:00
|
|
|
conv.lights.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conv.cameras->size()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mCameras = new aiCamera *[out->mNumCameras = static_cast<unsigned int>(conv.cameras->size())];
|
|
|
|
std::copy(conv.cameras->begin(), conv.cameras->end(), out->mCameras);
|
2015-05-19 03:57:13 +00:00
|
|
|
conv.cameras.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conv.materials->size()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mMaterials = new aiMaterial *[out->mNumMaterials = static_cast<unsigned int>(conv.materials->size())];
|
|
|
|
std::copy(conv.materials->begin(), conv.materials->end(), out->mMaterials);
|
2015-05-19 03:57:13 +00:00
|
|
|
conv.materials.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conv.textures->size()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mTextures = new aiTexture *[out->mNumTextures = static_cast<unsigned int>(conv.textures->size())];
|
|
|
|
std::copy(conv.textures->begin(), conv.textures->end(), out->mTextures);
|
2015-05-19 03:57:13 +00:00
|
|
|
conv.textures.dismiss();
|
|
|
|
}
|
|
|
|
|
|
|
|
// acknowledge that the scene might come out incomplete
|
2016-09-12 10:41:49 +00:00
|
|
|
// by Assimp's definition of `complete`: blender scenes
|
2015-05-19 03:57:13 +00:00
|
|
|
// can consist of thousands of cameras or lights with
|
|
|
|
// not a single mesh between them.
|
|
|
|
if (!out->mNumMeshes) {
|
|
|
|
out->mFlags |= AI_SCENE_FLAGS_INCOMPLETE;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::ResolveImage(aiMaterial *out, const Material *mat, const MTex *tex, const Image *img, ConversionData &conv_data) {
|
|
|
|
(void)mat;
|
|
|
|
(void)tex;
|
|
|
|
(void)conv_data;
|
2015-05-19 03:57:13 +00:00
|
|
|
aiString name;
|
|
|
|
|
|
|
|
// check if the file contents are bundled with the BLEND file
|
|
|
|
if (img->packedfile) {
|
|
|
|
name.data[0] = '*';
|
2020-06-23 19:05:42 +00:00
|
|
|
name.length = 1 + ASSIMP_itoa10(name.data + 1, static_cast<unsigned int>(MAXLEN - 1), static_cast<int32_t>(conv_data.textures->size()));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
conv_data.textures->push_back(new aiTexture());
|
2020-06-23 19:05:42 +00:00
|
|
|
aiTexture *curTex = conv_data.textures->back();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// usually 'img->name' will be the original file name of the embedded textures,
|
|
|
|
// so we can extract the file extension from it.
|
2020-06-23 19:05:42 +00:00
|
|
|
const size_t nlen = strlen(img->name);
|
|
|
|
const char *s = img->name + nlen, *e = s;
|
|
|
|
while (s >= img->name && *s != '.') {
|
2016-09-04 18:41:20 +00:00
|
|
|
--s;
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-05-04 08:55:08 +00:00
|
|
|
curTex->achFormatHint[0] = s + 1 > e ? '\0' : (char)::tolower((unsigned char)s[1]);
|
|
|
|
curTex->achFormatHint[1] = s + 2 > e ? '\0' : (char)::tolower((unsigned char)s[2]);
|
|
|
|
curTex->achFormatHint[2] = s + 3 > e ? '\0' : (char)::tolower((unsigned char)s[3]);
|
2020-03-01 12:15:45 +00:00
|
|
|
curTex->achFormatHint[3] = '\0';
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// tex->mHeight = 0;
|
2020-03-01 12:15:45 +00:00
|
|
|
curTex->mWidth = img->packedfile->size;
|
|
|
|
uint8_t *ch = new uint8_t[curTex->mWidth];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
conv_data.db.reader->SetCurrentPos(static_cast<size_t>(img->packedfile->data->val));
|
2020-03-01 12:15:45 +00:00
|
|
|
conv_data.db.reader->CopyAndAdvance(ch, curTex->mWidth);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
curTex->pcData = reinterpret_cast<aiTexel *>(ch);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-05-13 09:43:28 +00:00
|
|
|
LogInfo("Reading embedded texture, original file was ", img->name);
|
2016-09-04 18:41:20 +00:00
|
|
|
} else {
|
2020-06-23 19:05:42 +00:00
|
|
|
name = aiString(img->name);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aiTextureType texture_type = aiTextureType_UNKNOWN;
|
|
|
|
MTex::MapType map_type = tex->mapto;
|
|
|
|
|
|
|
|
if (map_type & MTex::MapType_COL)
|
|
|
|
texture_type = aiTextureType_DIFFUSE;
|
|
|
|
else if (map_type & MTex::MapType_NORM) {
|
|
|
|
if (tex->tex->imaflag & Tex::ImageFlags_NORMALMAP) {
|
|
|
|
texture_type = aiTextureType_NORMALS;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
texture_type = aiTextureType_HEIGHT;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
out->AddProperty(&tex->norfac, 1, AI_MATKEY_BUMPSCALING);
|
|
|
|
} else if (map_type & MTex::MapType_COLSPEC)
|
2015-05-19 03:57:13 +00:00
|
|
|
texture_type = aiTextureType_SPECULAR;
|
|
|
|
else if (map_type & MTex::MapType_COLMIR)
|
|
|
|
texture_type = aiTextureType_REFLECTION;
|
|
|
|
//else if (map_type & MTex::MapType_REF)
|
|
|
|
else if (map_type & MTex::MapType_SPEC)
|
|
|
|
texture_type = aiTextureType_SHININESS;
|
|
|
|
else if (map_type & MTex::MapType_EMIT)
|
|
|
|
texture_type = aiTextureType_EMISSIVE;
|
|
|
|
//else if (map_type & MTex::MapType_ALPHA)
|
|
|
|
//else if (map_type & MTex::MapType_HAR)
|
|
|
|
//else if (map_type & MTex::MapType_RAYMIRR)
|
|
|
|
//else if (map_type & MTex::MapType_TRANSLU)
|
|
|
|
else if (map_type & MTex::MapType_AMB)
|
|
|
|
texture_type = aiTextureType_AMBIENT;
|
|
|
|
else if (map_type & MTex::MapType_DISPLACE)
|
|
|
|
texture_type = aiTextureType_DISPLACEMENT;
|
|
|
|
//else if (map_type & MTex::MapType_WARP)
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
out->AddProperty(&name, AI_MATKEY_TEXTURE(texture_type,
|
|
|
|
conv_data.next_texture[texture_type]++));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::AddSentinelTexture(aiMaterial *out, const Material *mat, const MTex *tex, ConversionData &conv_data) {
|
|
|
|
(void)mat;
|
|
|
|
(void)tex;
|
|
|
|
(void)conv_data;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
aiString name;
|
2020-06-23 19:05:42 +00:00
|
|
|
name.length = ai_snprintf(name.data, MAXLEN, "Procedural,num=%i,type=%s", conv_data.sentinel_cnt++,
|
|
|
|
GetTextureTypeDisplayString(tex->tex->type));
|
|
|
|
out->AddProperty(&name, AI_MATKEY_TEXTURE_DIFFUSE(
|
|
|
|
conv_data.next_texture[aiTextureType_DIFFUSE]++));
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::ResolveTexture(aiMaterial *out, const Material *mat, const MTex *tex, ConversionData &conv_data) {
|
|
|
|
const Tex *rtex = tex->tex.get();
|
|
|
|
if (!rtex || !rtex->type) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't support most of the texture types because they're mostly procedural.
|
|
|
|
// These are substituted by a dummy texture.
|
2020-06-23 19:05:42 +00:00
|
|
|
const char *dispnam = "";
|
|
|
|
switch (rtex->type) {
|
|
|
|
// these are listed in blender's UI
|
|
|
|
case Tex::Type_CLOUDS:
|
|
|
|
case Tex::Type_WOOD:
|
|
|
|
case Tex::Type_MARBLE:
|
|
|
|
case Tex::Type_MAGIC:
|
|
|
|
case Tex::Type_BLEND:
|
|
|
|
case Tex::Type_STUCCI:
|
|
|
|
case Tex::Type_NOISE:
|
|
|
|
case Tex::Type_PLUGIN:
|
|
|
|
case Tex::Type_MUSGRAVE:
|
|
|
|
case Tex::Type_VORONOI:
|
|
|
|
case Tex::Type_DISTNOISE:
|
|
|
|
case Tex::Type_ENVMAP:
|
|
|
|
|
|
|
|
// these do no appear in the UI, why?
|
|
|
|
case Tex::Type_POINTDENSITY:
|
|
|
|
case Tex::Type_VOXELDATA:
|
|
|
|
|
2021-05-13 09:33:16 +00:00
|
|
|
LogWarn("Encountered a texture with an unsupported type: ", dispnam);
|
2020-06-23 19:05:42 +00:00
|
|
|
AddSentinelTexture(out, mat, tex, conv_data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Tex::Type_IMAGE:
|
|
|
|
if (!rtex->ima) {
|
|
|
|
LogError("A texture claims to be an Image, but no image reference is given");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2020-06-23 19:05:42 +00:00
|
|
|
}
|
|
|
|
ResolveImage(out, mat, tex, rtex->ima.get(), conv_data);
|
|
|
|
break;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
default:
|
|
|
|
ai_assert(false);
|
2015-05-19 03:57:13 +00:00
|
|
|
};
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::BuildDefaultMaterial(Blender::ConversionData &conv_data) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// add a default material if necessary
|
2020-06-23 19:05:42 +00:00
|
|
|
unsigned int index = static_cast<unsigned int>(-1);
|
|
|
|
for (aiMesh *mesh : conv_data.meshes.get()) {
|
|
|
|
if (mesh->mMaterialIndex == static_cast<unsigned int>(-1)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (index == static_cast<unsigned int>(-1)) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Setup a default material.
|
2016-04-05 21:23:53 +00:00
|
|
|
std::shared_ptr<Material> p(new Material());
|
2020-06-23 19:05:42 +00:00
|
|
|
ai_assert(::strlen(AI_DEFAULT_MATERIAL_NAME) < sizeof(p->id.name) - 2);
|
|
|
|
strcpy(p->id.name + 2, AI_DEFAULT_MATERIAL_NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Note: MSVC11 does not zero-initialize Material here, although it should.
|
|
|
|
// Thus all relevant fields should be explicitly initialized. We cannot add
|
|
|
|
// a default constructor to Material since the DNA codegen does not support
|
|
|
|
// parsing it.
|
|
|
|
p->r = p->g = p->b = 0.6f;
|
|
|
|
p->specr = p->specg = p->specb = 0.6f;
|
|
|
|
p->ambr = p->ambg = p->ambb = 0.0f;
|
|
|
|
p->mirr = p->mirg = p->mirb = 0.0f;
|
|
|
|
p->emit = 0.f;
|
|
|
|
p->alpha = 0.f;
|
|
|
|
p->har = 0;
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
index = static_cast<unsigned int>(conv_data.materials_raw.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
conv_data.materials_raw.push_back(p);
|
|
|
|
LogInfo("Adding default material");
|
|
|
|
}
|
|
|
|
mesh->mMaterialIndex = index;
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 19:17:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::AddBlendParams(aiMaterial *result, const Material *source) {
|
2016-05-17 19:17:51 +00:00
|
|
|
aiColor3D diffuseColor(source->r, source->g, source->b);
|
|
|
|
result->AddProperty(&diffuseColor, 1, "$mat.blend.diffuse.color", 0, 0);
|
|
|
|
|
|
|
|
float diffuseIntensity = source->ref;
|
|
|
|
result->AddProperty(&diffuseIntensity, 1, "$mat.blend.diffuse.intensity", 0, 0);
|
|
|
|
|
|
|
|
int diffuseShader = source->diff_shader;
|
|
|
|
result->AddProperty(&diffuseShader, 1, "$mat.blend.diffuse.shader", 0, 0);
|
|
|
|
|
|
|
|
int diffuseRamp = 0;
|
|
|
|
result->AddProperty(&diffuseRamp, 1, "$mat.blend.diffuse.ramp", 0, 0);
|
|
|
|
|
|
|
|
aiColor3D specularColor(source->specr, source->specg, source->specb);
|
|
|
|
result->AddProperty(&specularColor, 1, "$mat.blend.specular.color", 0, 0);
|
|
|
|
|
|
|
|
float specularIntensity = source->spec;
|
|
|
|
result->AddProperty(&specularIntensity, 1, "$mat.blend.specular.intensity", 0, 0);
|
|
|
|
|
|
|
|
int specularShader = source->spec_shader;
|
|
|
|
result->AddProperty(&specularShader, 1, "$mat.blend.specular.shader", 0, 0);
|
|
|
|
|
|
|
|
int specularRamp = 0;
|
|
|
|
result->AddProperty(&specularRamp, 1, "$mat.blend.specular.ramp", 0, 0);
|
|
|
|
|
|
|
|
int specularHardness = source->har;
|
|
|
|
result->AddProperty(&specularHardness, 1, "$mat.blend.specular.hardness", 0, 0);
|
|
|
|
|
|
|
|
int transparencyUse = source->mode & MA_TRANSPARENCY ? 1 : 0;
|
|
|
|
result->AddProperty(&transparencyUse, 1, "$mat.blend.transparency.use", 0, 0);
|
|
|
|
|
|
|
|
int transparencyMethod = source->mode & MA_RAYTRANSP ? 2 : (source->mode & MA_ZTRANSP ? 1 : 0);
|
|
|
|
result->AddProperty(&transparencyMethod, 1, "$mat.blend.transparency.method", 0, 0);
|
|
|
|
|
|
|
|
float transparencyAlpha = source->alpha;
|
|
|
|
result->AddProperty(&transparencyAlpha, 1, "$mat.blend.transparency.alpha", 0, 0);
|
|
|
|
|
|
|
|
float transparencySpecular = source->spectra;
|
|
|
|
result->AddProperty(&transparencySpecular, 1, "$mat.blend.transparency.specular", 0, 0);
|
|
|
|
|
|
|
|
float transparencyFresnel = source->fresnel_tra;
|
|
|
|
result->AddProperty(&transparencyFresnel, 1, "$mat.blend.transparency.fresnel", 0, 0);
|
|
|
|
|
|
|
|
float transparencyBlend = source->fresnel_tra_i;
|
|
|
|
result->AddProperty(&transparencyBlend, 1, "$mat.blend.transparency.blend", 0, 0);
|
|
|
|
|
|
|
|
float transparencyIor = source->ang;
|
|
|
|
result->AddProperty(&transparencyIor, 1, "$mat.blend.transparency.ior", 0, 0);
|
|
|
|
|
|
|
|
float transparencyFilter = source->filter;
|
|
|
|
result->AddProperty(&transparencyFilter, 1, "$mat.blend.transparency.filter", 0, 0);
|
|
|
|
|
|
|
|
float transparencyFalloff = source->tx_falloff;
|
|
|
|
result->AddProperty(&transparencyFalloff, 1, "$mat.blend.transparency.falloff", 0, 0);
|
|
|
|
|
|
|
|
float transparencyLimit = source->tx_limit;
|
|
|
|
result->AddProperty(&transparencyLimit, 1, "$mat.blend.transparency.limit", 0, 0);
|
|
|
|
|
|
|
|
int transparencyDepth = source->ray_depth_tra;
|
|
|
|
result->AddProperty(&transparencyDepth, 1, "$mat.blend.transparency.depth", 0, 0);
|
|
|
|
|
|
|
|
float transparencyGlossAmount = source->gloss_tra;
|
|
|
|
result->AddProperty(&transparencyGlossAmount, 1, "$mat.blend.transparency.glossAmount", 0, 0);
|
|
|
|
|
|
|
|
float transparencyGlossThreshold = source->adapt_thresh_tra;
|
|
|
|
result->AddProperty(&transparencyGlossThreshold, 1, "$mat.blend.transparency.glossThreshold", 0, 0);
|
|
|
|
|
|
|
|
int transparencyGlossSamples = source->samp_gloss_tra;
|
|
|
|
result->AddProperty(&transparencyGlossSamples, 1, "$mat.blend.transparency.glossSamples", 0, 0);
|
|
|
|
|
|
|
|
int mirrorUse = source->mode & MA_RAYMIRROR ? 1 : 0;
|
|
|
|
result->AddProperty(&mirrorUse, 1, "$mat.blend.mirror.use", 0, 0);
|
|
|
|
|
|
|
|
float mirrorReflectivity = source->ray_mirror;
|
|
|
|
result->AddProperty(&mirrorReflectivity, 1, "$mat.blend.mirror.reflectivity", 0, 0);
|
|
|
|
|
|
|
|
aiColor3D mirrorColor(source->mirr, source->mirg, source->mirb);
|
|
|
|
result->AddProperty(&mirrorColor, 1, "$mat.blend.mirror.color", 0, 0);
|
|
|
|
|
|
|
|
float mirrorFresnel = source->fresnel_mir;
|
|
|
|
result->AddProperty(&mirrorFresnel, 1, "$mat.blend.mirror.fresnel", 0, 0);
|
|
|
|
|
|
|
|
float mirrorBlend = source->fresnel_mir_i;
|
|
|
|
result->AddProperty(&mirrorBlend, 1, "$mat.blend.mirror.blend", 0, 0);
|
|
|
|
|
|
|
|
int mirrorDepth = source->ray_depth;
|
|
|
|
result->AddProperty(&mirrorDepth, 1, "$mat.blend.mirror.depth", 0, 0);
|
|
|
|
|
|
|
|
float mirrorMaxDist = source->dist_mir;
|
|
|
|
result->AddProperty(&mirrorMaxDist, 1, "$mat.blend.mirror.maxDist", 0, 0);
|
|
|
|
|
|
|
|
int mirrorFadeTo = source->fadeto_mir;
|
|
|
|
result->AddProperty(&mirrorFadeTo, 1, "$mat.blend.mirror.fadeTo", 0, 0);
|
|
|
|
|
|
|
|
float mirrorGlossAmount = source->gloss_mir;
|
|
|
|
result->AddProperty(&mirrorGlossAmount, 1, "$mat.blend.mirror.glossAmount", 0, 0);
|
|
|
|
|
|
|
|
float mirrorGlossThreshold = source->adapt_thresh_mir;
|
|
|
|
result->AddProperty(&mirrorGlossThreshold, 1, "$mat.blend.mirror.glossThreshold", 0, 0);
|
|
|
|
|
|
|
|
int mirrorGlossSamples = source->samp_gloss_mir;
|
|
|
|
result->AddProperty(&mirrorGlossSamples, 1, "$mat.blend.mirror.glossSamples", 0, 0);
|
|
|
|
|
|
|
|
float mirrorGlossAnisotropic = source->aniso_gloss_mir;
|
|
|
|
result->AddProperty(&mirrorGlossAnisotropic, 1, "$mat.blend.mirror.glossAnisotropic", 0, 0);
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::BuildMaterials(ConversionData &conv_data) {
|
2016-05-17 19:17:51 +00:00
|
|
|
conv_data.materials->reserve(conv_data.materials_raw.size());
|
|
|
|
|
|
|
|
BuildDefaultMaterial(conv_data);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-06-22 16:27:15 +00:00
|
|
|
for (const std::shared_ptr<Material> &mat : conv_data.materials_raw) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// reset per material global counters
|
2020-06-23 19:05:42 +00:00
|
|
|
for (size_t i = 0; i < sizeof(conv_data.next_texture) / sizeof(conv_data.next_texture[0]); ++i) {
|
|
|
|
conv_data.next_texture[i] = 0;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *mout = new aiMaterial();
|
2015-05-19 03:57:13 +00:00
|
|
|
conv_data.materials->push_back(mout);
|
|
|
|
// For any new material field handled here, the default material above must be updated with an appropriate default value.
|
|
|
|
|
|
|
|
// set material name
|
2020-06-23 19:05:42 +00:00
|
|
|
aiString name = aiString(mat->id.name + 2); // skip over the name prefix 'MA'
|
|
|
|
mout->AddProperty(&name, AI_MATKEY_NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// basic material colors
|
2020-06-23 19:05:42 +00:00
|
|
|
aiColor3D col(mat->r, mat->g, mat->b);
|
|
|
|
if (mat->r || mat->g || mat->b) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Usually, zero diffuse color means no diffuse color at all in the equation.
|
|
|
|
// So we omit this member to express this intent.
|
2020-06-23 19:05:42 +00:00
|
|
|
mout->AddProperty(&col, 1, AI_MATKEY_COLOR_DIFFUSE);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (mat->emit) {
|
2020-06-23 19:05:42 +00:00
|
|
|
aiColor3D emit_col(mat->emit * mat->r, mat->emit * mat->g, mat->emit * mat->b);
|
|
|
|
mout->AddProperty(&emit_col, 1, AI_MATKEY_COLOR_EMISSIVE);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
col = aiColor3D(mat->specr, mat->specg, mat->specb);
|
|
|
|
mout->AddProperty(&col, 1, AI_MATKEY_COLOR_SPECULAR);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// is hardness/shininess set?
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mat->har) {
|
2015-05-19 03:57:13 +00:00
|
|
|
const float har = mat->har;
|
2020-06-23 19:05:42 +00:00
|
|
|
mout->AddProperty(&har, 1, AI_MATKEY_SHININESS);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
col = aiColor3D(mat->ambr, mat->ambg, mat->ambb);
|
|
|
|
mout->AddProperty(&col, 1, AI_MATKEY_COLOR_AMBIENT);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2016-03-29 19:10:15 +00:00
|
|
|
// is mirror enabled?
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mat->mode & MA_RAYMIRROR) {
|
2016-03-29 19:10:15 +00:00
|
|
|
const float ray_mirror = mat->ray_mirror;
|
2020-06-23 19:05:42 +00:00
|
|
|
mout->AddProperty(&ray_mirror, 1, AI_MATKEY_REFLECTIVITY);
|
2016-03-29 19:10:15 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
col = aiColor3D(mat->mirr, mat->mirg, mat->mirb);
|
|
|
|
mout->AddProperty(&col, 1, AI_MATKEY_COLOR_REFLECTIVE);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (size_t i = 0; i < sizeof(mat->mtex) / sizeof(mat->mtex[0]); ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (!mat->mtex[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
ResolveTexture(mout, mat.get(), mat->mtex[i].get(), conv_data);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2016-05-17 19:17:51 +00:00
|
|
|
|
|
|
|
AddBlendParams(mout, mat.get());
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::CheckActualType(const ElemBase *dt, const char *check) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ai_assert(dt);
|
2020-06-23 19:05:42 +00:00
|
|
|
if (strcmp(dt->dna_type, check)) {
|
2020-08-18 15:32:34 +00:00
|
|
|
ThrowException("Expected object at ", std::hex, dt, " to be of type `", check,
|
|
|
|
"`, but it claims to be a `", dt->dna_type, "`instead");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::NotSupportedObjectType(const Object *obj, const char *type) {
|
2021-05-13 09:33:16 +00:00
|
|
|
LogWarn("Object `", obj->id.name, "` - type is unsupported: `", type, "`, skipping");
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
void BlenderImporter::ConvertMesh(const Scene & /*in*/, const Object * /*obj*/, const Mesh *mesh,
|
|
|
|
ConversionData &conv_data, TempArray<std::vector, aiMesh> &temp) {
|
2016-09-12 10:41:49 +00:00
|
|
|
// TODO: Resolve various problems with BMesh triangulation before re-enabling.
|
2015-05-19 03:57:13 +00:00
|
|
|
// See issues #400, #373, #318 #315 and #132.
|
2015-05-19 03:48:29 +00:00
|
|
|
#if defined(TODO_FIX_BMESH_CONVERSION)
|
2020-06-23 19:05:42 +00:00
|
|
|
BlenderBMeshConverter BMeshConverter(mesh);
|
|
|
|
if (BMeshConverter.ContainsBMesh()) {
|
|
|
|
mesh = BMeshConverter.TriangulateBMesh();
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
typedef std::pair<const int, size_t> MyPair;
|
2015-05-19 03:57:13 +00:00
|
|
|
if ((!mesh->totface && !mesh->totloop) || !mesh->totvert) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// some sanity checks
|
2020-06-23 19:05:42 +00:00
|
|
|
if (static_cast<size_t>(mesh->totface) > mesh->mface.size()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("Number of faces is larger than the corresponding array");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (static_cast<size_t>(mesh->totvert) > mesh->mvert.size()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("Number of vertices is larger than the corresponding array");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (static_cast<size_t>(mesh->totloop) > mesh->mloop.size()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("Number of vertices is larger than the corresponding array");
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect per-submesh numbers
|
2020-06-23 19:05:42 +00:00
|
|
|
std::map<int, size_t> per_mat;
|
|
|
|
std::map<int, size_t> per_mat_verts;
|
2015-05-19 03:57:13 +00:00
|
|
|
for (int i = 0; i < mesh->totface; ++i) {
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const MFace &mf = mesh->mface[i];
|
|
|
|
per_mat[mf.mat_nr]++;
|
|
|
|
per_mat_verts[mf.mat_nr] += mf.v4 ? 4 : 3;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh->totpoly; ++i) {
|
2020-06-23 19:05:42 +00:00
|
|
|
const MPoly &mp = mesh->mpoly[i];
|
|
|
|
per_mat[mp.mat_nr]++;
|
|
|
|
per_mat_verts[mp.mat_nr] += mp.totloop;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ... and allocate the corresponding meshes
|
|
|
|
const size_t old = temp->size();
|
|
|
|
temp->reserve(temp->size() + per_mat.size());
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::map<size_t, size_t> mat_num_to_mesh_idx;
|
|
|
|
for (MyPair &it : per_mat) {
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
mat_num_to_mesh_idx[it.first] = temp->size();
|
|
|
|
temp->push_back(new aiMesh());
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *out = temp->back();
|
2015-05-19 03:57:13 +00:00
|
|
|
out->mVertices = new aiVector3D[per_mat_verts[it.first]];
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mNormals = new aiVector3D[per_mat_verts[it.first]];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
//out->mNumFaces = 0
|
|
|
|
//out->mNumVertices = 0
|
|
|
|
out->mFaces = new aiFace[it.second]();
|
|
|
|
|
2016-09-12 10:41:49 +00:00
|
|
|
// all sub-meshes created from this mesh are named equally. this allows
|
2015-05-19 03:57:13 +00:00
|
|
|
// curious users to recover the original adjacency.
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mName = aiString(mesh->id.name + 2);
|
|
|
|
// skip over the name prefix 'ME'
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// resolve the material reference and add this material to the set of
|
|
|
|
// output materials. The (temporary) material index is the index
|
|
|
|
// of the material entry within the list of resolved materials.
|
|
|
|
if (mesh->mat) {
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (static_cast<size_t>(it.first) >= mesh->mat.size()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("Material index is out of range");
|
|
|
|
}
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
std::shared_ptr<Material> mat = mesh->mat[it.first];
|
2020-06-23 19:05:42 +00:00
|
|
|
const std::deque<std::shared_ptr<Material>>::iterator has = std::find(
|
2015-05-19 03:57:13 +00:00
|
|
|
conv_data.materials_raw.begin(),
|
2020-06-23 19:05:42 +00:00
|
|
|
conv_data.materials_raw.end(), mat);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (has != conv_data.materials_raw.end()) {
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mMaterialIndex = static_cast<unsigned int>(std::distance(conv_data.materials_raw.begin(), has));
|
|
|
|
} else {
|
|
|
|
out->mMaterialIndex = static_cast<unsigned int>(conv_data.materials_raw.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
conv_data.materials_raw.push_back(mat);
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else
|
|
|
|
out->mMaterialIndex = static_cast<unsigned int>(-1);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh->totface; ++i) {
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const MFace &mf = mesh->mface[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *const out = temp[mat_num_to_mesh_idx[mf.mat_nr]];
|
|
|
|
aiFace &f = out->mFaces[out->mNumFaces++];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
f.mIndices = new unsigned int[f.mNumIndices = mf.v4 ? 4 : 3];
|
|
|
|
aiVector3D *vo = out->mVertices + out->mNumVertices;
|
|
|
|
aiVector3D *vn = out->mNormals + out->mNumVertices;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// XXX we can't fold this easily, because we are restricted
|
|
|
|
// to the member names from the BLEND file (v1,v2,v3,v4)
|
|
|
|
// which are assigned by the genblenddna.py script and
|
|
|
|
// cannot be changed without breaking the entire
|
|
|
|
// import process.
|
|
|
|
|
|
|
|
if (mf.v1 >= mesh->totvert) {
|
|
|
|
ThrowException("Vertex index v1 out of range");
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
const MVert *v = &mesh->mvert[mf.v1];
|
2015-05-19 03:57:13 +00:00
|
|
|
vo->x = v->co[0];
|
|
|
|
vo->y = v->co[1];
|
|
|
|
vo->z = v->co[2];
|
|
|
|
vn->x = v->no[0];
|
|
|
|
vn->y = v->no[1];
|
|
|
|
vn->z = v->no[2];
|
|
|
|
f.mIndices[0] = out->mNumVertices++;
|
|
|
|
++vo;
|
|
|
|
++vn;
|
|
|
|
|
|
|
|
// if (f.mNumIndices >= 2) {
|
|
|
|
if (mf.v2 >= mesh->totvert) {
|
|
|
|
ThrowException("Vertex index v2 out of range");
|
|
|
|
}
|
|
|
|
v = &mesh->mvert[mf.v2];
|
|
|
|
vo->x = v->co[0];
|
|
|
|
vo->y = v->co[1];
|
|
|
|
vo->z = v->co[2];
|
|
|
|
vn->x = v->no[0];
|
|
|
|
vn->y = v->no[1];
|
|
|
|
vn->z = v->no[2];
|
|
|
|
f.mIndices[1] = out->mNumVertices++;
|
|
|
|
++vo;
|
|
|
|
++vn;
|
|
|
|
|
|
|
|
if (mf.v3 >= mesh->totvert) {
|
|
|
|
ThrowException("Vertex index v3 out of range");
|
|
|
|
}
|
|
|
|
// if (f.mNumIndices >= 3) {
|
|
|
|
v = &mesh->mvert[mf.v3];
|
|
|
|
vo->x = v->co[0];
|
|
|
|
vo->y = v->co[1];
|
|
|
|
vo->z = v->co[2];
|
|
|
|
vn->x = v->no[0];
|
|
|
|
vn->y = v->no[1];
|
|
|
|
vn->z = v->no[2];
|
|
|
|
f.mIndices[2] = out->mNumVertices++;
|
|
|
|
++vo;
|
|
|
|
++vn;
|
|
|
|
|
|
|
|
if (mf.v4 >= mesh->totvert) {
|
|
|
|
ThrowException("Vertex index v4 out of range");
|
|
|
|
}
|
|
|
|
// if (f.mNumIndices >= 4) {
|
|
|
|
if (mf.v4) {
|
|
|
|
v = &mesh->mvert[mf.v4];
|
|
|
|
vo->x = v->co[0];
|
|
|
|
vo->y = v->co[1];
|
|
|
|
vo->z = v->co[2];
|
|
|
|
vn->x = v->no[0];
|
|
|
|
vn->y = v->no[1];
|
|
|
|
vn->z = v->no[2];
|
|
|
|
f.mIndices[3] = out->mNumVertices++;
|
|
|
|
++vo;
|
|
|
|
++vn;
|
|
|
|
|
|
|
|
out->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else
|
|
|
|
out->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh->totpoly; ++i) {
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const MPoly &mf = mesh->mpoly[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *const out = temp[mat_num_to_mesh_idx[mf.mat_nr]];
|
|
|
|
aiFace &f = out->mFaces[out->mNumFaces++];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
f.mIndices = new unsigned int[f.mNumIndices = mf.totloop];
|
|
|
|
aiVector3D *vo = out->mVertices + out->mNumVertices;
|
|
|
|
aiVector3D *vn = out->mNormals + out->mNumVertices;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// XXX we can't fold this easily, because we are restricted
|
|
|
|
// to the member names from the BLEND file (v1,v2,v3,v4)
|
|
|
|
// which are assigned by the genblenddna.py script and
|
|
|
|
// cannot be changed without breaking the entire
|
|
|
|
// import process.
|
2020-06-23 19:05:42 +00:00
|
|
|
for (int j = 0; j < mf.totloop; ++j) {
|
|
|
|
const MLoop &loop = mesh->mloop[mf.loopstart + j];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (loop.v >= mesh->totvert) {
|
|
|
|
ThrowException("Vertex index out of range");
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
const MVert &v = mesh->mvert[loop.v];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
vo->x = v.co[0];
|
|
|
|
vo->y = v.co[1];
|
|
|
|
vo->z = v.co[2];
|
|
|
|
vn->x = v.no[0];
|
|
|
|
vn->y = v.no[1];
|
|
|
|
vn->z = v.no[2];
|
|
|
|
f.mIndices[j] = out->mNumVertices++;
|
|
|
|
|
|
|
|
++vo;
|
|
|
|
++vn;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mf.totloop == 3) {
|
2015-05-19 03:57:13 +00:00
|
|
|
out->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
out->mPrimitiveTypes |= aiPrimitiveType_POLYGON;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 13:45:09 +00:00
|
|
|
// TODO should we create the TextureUVMapping map in Convert<Material> to prevent redundant processing?
|
|
|
|
|
|
|
|
// create texture <-> uvname mapping for all materials
|
|
|
|
// key is texture number, value is data *
|
|
|
|
typedef std::map<uint32_t, const MLoopUV *> TextureUVMapping;
|
|
|
|
// key is material number, value is the TextureUVMapping for the material
|
|
|
|
typedef std::map<uint32_t, TextureUVMapping> MaterialTextureUVMappings;
|
|
|
|
MaterialTextureUVMappings matTexUvMappings;
|
|
|
|
const uint32_t maxMat = static_cast<const uint32_t>(mesh->mat.size());
|
2018-06-09 13:56:26 +00:00
|
|
|
for (uint32_t m = 0; m < maxMat; ++m) {
|
2018-06-09 13:45:09 +00:00
|
|
|
// get material by index
|
|
|
|
const std::shared_ptr<Material> pMat = mesh->mat[m];
|
|
|
|
TextureUVMapping texuv;
|
|
|
|
const uint32_t maxTex = sizeof(pMat->mtex) / sizeof(pMat->mtex[0]);
|
2018-06-09 13:56:26 +00:00
|
|
|
for (uint32_t t = 0; t < maxTex; ++t) {
|
|
|
|
if (pMat->mtex[t] && pMat->mtex[t]->uvname[0]) {
|
2018-06-09 13:45:09 +00:00
|
|
|
// get the CustomData layer for given uvname and correct type
|
2018-06-10 08:35:15 +00:00
|
|
|
const ElemBase *pLoop = getCustomDataLayerData(mesh->ldata, CD_MLOOPUV, pMat->mtex[t]->uvname);
|
2018-06-09 13:56:26 +00:00
|
|
|
if (pLoop) {
|
2018-06-10 08:35:15 +00:00
|
|
|
texuv.insert(std::make_pair(t, dynamic_cast<const MLoopUV *>(pLoop)));
|
2018-06-09 13:45:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-16 13:30:22 +00:00
|
|
|
if (texuv.size()) {
|
2018-06-16 13:08:19 +00:00
|
|
|
matTexUvMappings.insert(std::make_pair(m, texuv));
|
|
|
|
}
|
2018-06-09 13:45:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// collect texture coordinates, they're stored in a separate per-face buffer
|
|
|
|
if (mesh->mtface || mesh->mloopuv) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mesh->totface > static_cast<int>(mesh->mtface.size())) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("Number of UV faces is larger than the corresponding UV face array (#1)");
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::iterator it = temp->begin() + old; it != temp->end(); ++it) {
|
|
|
|
ai_assert(0 != (*it)->mNumVertices);
|
|
|
|
ai_assert(0 != (*it)->mNumFaces);
|
2018-06-09 13:45:09 +00:00
|
|
|
const auto itMatTexUvMapping = matTexUvMappings.find((*it)->mMaterialIndex);
|
|
|
|
if (itMatTexUvMapping == matTexUvMappings.end()) {
|
|
|
|
// default behaviour like before
|
|
|
|
(*it)->mTextureCoords[0] = new aiVector3D[(*it)->mNumVertices];
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2018-06-09 13:45:09 +00:00
|
|
|
// create texture coords for every mapped tex
|
|
|
|
for (uint32_t i = 0; i < itMatTexUvMapping->second.size(); ++i) {
|
|
|
|
(*it)->mTextureCoords[i] = new aiVector3D[(*it)->mNumVertices];
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
(*it)->mNumFaces = (*it)->mNumVertices = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh->totface; ++i) {
|
2020-06-23 19:05:42 +00:00
|
|
|
const MTFace *v = &mesh->mtface[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *const out = temp[mat_num_to_mesh_idx[mesh->mface[i].mat_nr]];
|
|
|
|
const aiFace &f = out->mFaces[out->mNumFaces++];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *vo = &out->mTextureCoords[0][out->mNumVertices];
|
|
|
|
for (unsigned int j = 0; j < f.mNumIndices; ++j, ++vo, ++out->mNumVertices) {
|
2020-03-01 12:15:45 +00:00
|
|
|
vo->x = v->uv[j][0];
|
|
|
|
vo->y = v->uv[j][1];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh->totpoly; ++i) {
|
2020-06-23 19:05:42 +00:00
|
|
|
const MPoly &v = mesh->mpoly[i];
|
|
|
|
aiMesh *const out = temp[mat_num_to_mesh_idx[v.mat_nr]];
|
|
|
|
const aiFace &f = out->mFaces[out->mNumFaces++];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2018-06-09 13:45:09 +00:00
|
|
|
const auto itMatTexUvMapping = matTexUvMappings.find(v.mat_nr);
|
|
|
|
if (itMatTexUvMapping == matTexUvMappings.end()) {
|
|
|
|
// old behavior
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *vo = &out->mTextureCoords[0][out->mNumVertices];
|
2018-06-09 13:45:09 +00:00
|
|
|
for (unsigned int j = 0; j < f.mNumIndices; ++j, ++vo, ++out->mNumVertices) {
|
2020-06-23 19:05:42 +00:00
|
|
|
const MLoopUV &uv = mesh->mloopuv[v.loopstart + j];
|
2018-06-09 13:45:09 +00:00
|
|
|
vo->x = uv.uv[0];
|
|
|
|
vo->y = uv.uv[1];
|
|
|
|
}
|
2020-03-01 12:15:45 +00:00
|
|
|
} else {
|
2018-06-09 13:45:09 +00:00
|
|
|
// create textureCoords for every mapped tex
|
|
|
|
for (uint32_t m = 0; m < itMatTexUvMapping->second.size(); ++m) {
|
|
|
|
const MLoopUV *tm = itMatTexUvMapping->second[m];
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *vo = &out->mTextureCoords[m][out->mNumVertices];
|
2018-06-09 13:45:09 +00:00
|
|
|
uint32_t j = 0;
|
|
|
|
for (; j < f.mNumIndices; ++j, ++vo) {
|
2020-06-23 19:05:42 +00:00
|
|
|
const MLoopUV &uv = tm[v.loopstart + j];
|
2018-06-09 13:45:09 +00:00
|
|
|
vo->x = uv.uv[0];
|
|
|
|
vo->y = uv.uv[1];
|
|
|
|
}
|
|
|
|
// only update written mNumVertices in last loop
|
|
|
|
// TODO why must the numVertices be incremented here?
|
2018-06-09 13:56:26 +00:00
|
|
|
if (m == itMatTexUvMapping->second.size() - 1) {
|
2018-06-09 13:45:09 +00:00
|
|
|
out->mNumVertices += j;
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect texture coordinates, old-style (marked as deprecated in current blender sources)
|
|
|
|
if (mesh->tface) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mesh->totface > static_cast<int>(mesh->tface.size())) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("Number of faces is larger than the corresponding UV face array (#2)");
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::iterator it = temp->begin() + old; it != temp->end(); ++it) {
|
|
|
|
ai_assert(0 != (*it)->mNumVertices);
|
|
|
|
ai_assert(0 != (*it)->mNumFaces);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
(*it)->mTextureCoords[0] = new aiVector3D[(*it)->mNumVertices];
|
|
|
|
(*it)->mNumFaces = (*it)->mNumVertices = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh->totface; ++i) {
|
2020-06-23 19:05:42 +00:00
|
|
|
const TFace *v = &mesh->tface[i];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *const out = temp[mat_num_to_mesh_idx[mesh->mface[i].mat_nr]];
|
|
|
|
const aiFace &f = out->mFaces[out->mNumFaces++];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D *vo = &out->mTextureCoords[0][out->mNumVertices];
|
|
|
|
for (unsigned int j = 0; j < f.mNumIndices; ++j, ++vo, ++out->mNumVertices) {
|
2020-03-01 12:15:45 +00:00
|
|
|
vo->x = v->uv[j][0];
|
|
|
|
vo->y = v->uv[j][1];
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect vertex colors, stored separately as well
|
|
|
|
if (mesh->mcol || mesh->mloopcol) {
|
2020-06-23 19:05:42 +00:00
|
|
|
if (mesh->totface > static_cast<int>((mesh->mcol.size() / 4))) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ThrowException("Number of faces is larger than the corresponding color face array");
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
for (std::vector<aiMesh *>::iterator it = temp->begin() + old; it != temp->end(); ++it) {
|
|
|
|
ai_assert(0 != (*it)->mNumVertices);
|
|
|
|
ai_assert(0 != (*it)->mNumFaces);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
(*it)->mColors[0] = new aiColor4D[(*it)->mNumVertices];
|
|
|
|
(*it)->mNumFaces = (*it)->mNumVertices = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh->totface; ++i) {
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *const out = temp[mat_num_to_mesh_idx[mesh->mface[i].mat_nr]];
|
|
|
|
const aiFace &f = out->mFaces[out->mNumFaces++];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiColor4D *vo = &out->mColors[0][out->mNumVertices];
|
|
|
|
for (unsigned int n = 0; n < f.mNumIndices; ++n, ++vo, ++out->mNumVertices) {
|
|
|
|
const MCol *col = &mesh->mcol[(i << 2) + n];
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
vo->r = col->r;
|
|
|
|
vo->g = col->g;
|
|
|
|
vo->b = col->b;
|
|
|
|
vo->a = col->a;
|
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int n = f.mNumIndices; n < 4; ++n)
|
|
|
|
;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < mesh->totpoly; ++i) {
|
2020-06-23 19:05:42 +00:00
|
|
|
const MPoly &v = mesh->mpoly[i];
|
|
|
|
aiMesh *const out = temp[mat_num_to_mesh_idx[v.mat_nr]];
|
|
|
|
const aiFace &f = out->mFaces[out->mNumFaces++];
|
|
|
|
|
|
|
|
aiColor4D *vo = &out->mColors[0][out->mNumVertices];
|
|
|
|
const ai_real scaleZeroToOne = 1.f / 255.f;
|
|
|
|
for (unsigned int j = 0; j < f.mNumIndices; ++j, ++vo, ++out->mNumVertices) {
|
|
|
|
const MLoopCol &col = mesh->mloopcol[v.loopstart + j];
|
2016-07-25 00:45:41 +00:00
|
|
|
vo->r = ai_real(col.r) * scaleZeroToOne;
|
|
|
|
vo->g = ai_real(col.g) * scaleZeroToOne;
|
|
|
|
vo->b = ai_real(col.b) * scaleZeroToOne;
|
|
|
|
vo->a = ai_real(col.a) * scaleZeroToOne;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
aiCamera *BlenderImporter::ConvertCamera(const Scene & /*in*/, const Object *obj, const Camera *cam, ConversionData & /*conv_data*/) {
|
2017-12-02 15:13:43 +00:00
|
|
|
std::unique_ptr<aiCamera> out(new aiCamera());
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mName = obj->id.name + 2;
|
2015-05-19 03:57:13 +00:00
|
|
|
out->mPosition = aiVector3D(0.f, 0.f, 0.f);
|
|
|
|
out->mUp = aiVector3D(0.f, 1.f, 0.f);
|
|
|
|
out->mLookAt = aiVector3D(0.f, 0.f, -1.f);
|
2016-01-27 01:12:15 +00:00
|
|
|
if (cam->sensor_x && cam->lens) {
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mHorizontalFOV = 2.f * std::atan2(cam->sensor_x, 2.f * cam->lens);
|
2016-01-27 01:12:15 +00:00
|
|
|
}
|
|
|
|
out->mClipPlaneNear = cam->clipsta;
|
|
|
|
out->mClipPlaneFar = cam->clipend;
|
|
|
|
|
2017-12-02 15:13:43 +00:00
|
|
|
return out.release();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
aiLight *BlenderImporter::ConvertLight(const Scene & /*in*/, const Object *obj, const Lamp *lamp, ConversionData & /*conv_data*/) {
|
2017-12-02 15:13:43 +00:00
|
|
|
std::unique_ptr<aiLight> out(new aiLight());
|
2020-06-23 19:05:42 +00:00
|
|
|
out->mName = obj->id.name + 2;
|
|
|
|
|
|
|
|
switch (lamp->type) {
|
|
|
|
case Lamp::Type_Local:
|
|
|
|
out->mType = aiLightSource_POINT;
|
|
|
|
break;
|
|
|
|
case Lamp::Type_Spot:
|
|
|
|
out->mType = aiLightSource_SPOT;
|
|
|
|
|
|
|
|
// blender orients directional lights as facing toward -z
|
|
|
|
out->mDirection = aiVector3D(0.f, 0.f, -1.f);
|
|
|
|
out->mUp = aiVector3D(0.f, 1.f, 0.f);
|
|
|
|
|
|
|
|
out->mAngleInnerCone = lamp->spotsize * (1.0f - lamp->spotblend);
|
|
|
|
out->mAngleOuterCone = lamp->spotsize;
|
|
|
|
break;
|
|
|
|
case Lamp::Type_Sun:
|
|
|
|
out->mType = aiLightSource_DIRECTIONAL;
|
|
|
|
|
|
|
|
// blender orients directional lights as facing toward -z
|
|
|
|
out->mDirection = aiVector3D(0.f, 0.f, -1.f);
|
|
|
|
out->mUp = aiVector3D(0.f, 1.f, 0.f);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Lamp::Type_Area:
|
|
|
|
out->mType = aiLightSource_AREA;
|
|
|
|
|
|
|
|
if (lamp->area_shape == 0) {
|
|
|
|
out->mSize = aiVector2D(lamp->area_size, lamp->area_size);
|
|
|
|
} else {
|
|
|
|
out->mSize = aiVector2D(lamp->area_size, lamp->area_sizey);
|
|
|
|
}
|
2016-04-20 22:25:50 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
// blender orients directional lights as facing toward -z
|
|
|
|
out->mDirection = aiVector3D(0.f, 0.f, -1.f);
|
|
|
|
out->mUp = aiVector3D(0.f, 1.f, 0.f);
|
|
|
|
break;
|
2016-04-20 22:25:50 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
default:
|
|
|
|
break;
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out->mColorAmbient = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy;
|
|
|
|
out->mColorSpecular = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy;
|
|
|
|
out->mColorDiffuse = aiColor3D(lamp->r, lamp->g, lamp->b) * lamp->energy;
|
2019-04-24 22:35:14 +00:00
|
|
|
|
|
|
|
// If default values are supplied, compute the coefficients from light's max distance
|
|
|
|
// Read this: https://imdoingitwrong.wordpress.com/2011/01/31/light-attenuation/
|
|
|
|
//
|
2020-06-23 19:05:42 +00:00
|
|
|
if (lamp->constant_coefficient == 1.0f && lamp->linear_coefficient == 0.0f && lamp->quadratic_coefficient == 0.0f && lamp->dist > 0.0f) {
|
2019-04-24 22:35:14 +00:00
|
|
|
out->mAttenuationConstant = 1.0f;
|
|
|
|
out->mAttenuationLinear = 2.0f / lamp->dist;
|
|
|
|
out->mAttenuationQuadratic = 1.0f / (lamp->dist * lamp->dist);
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2019-04-24 22:35:14 +00:00
|
|
|
out->mAttenuationConstant = lamp->constant_coefficient;
|
|
|
|
out->mAttenuationLinear = lamp->linear_coefficient;
|
|
|
|
out->mAttenuationQuadratic = lamp->quadratic_coefficient;
|
|
|
|
}
|
|
|
|
|
2017-12-02 15:13:43 +00:00
|
|
|
return out.release();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNode *BlenderImporter::ConvertNode(const Scene &in, const Object *obj, ConversionData &conv_data, const aiMatrix4x4 &parentTransform) {
|
|
|
|
std::deque<const Object *> children;
|
|
|
|
for (ObjectSet::iterator it = conv_data.objects.begin(); it != conv_data.objects.end();) {
|
|
|
|
const Object *object = *it;
|
2015-05-19 03:57:13 +00:00
|
|
|
if (object->parent == obj) {
|
|
|
|
children.push_back(object);
|
|
|
|
|
|
|
|
conv_data.objects.erase(it++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
std::unique_ptr<aiNode> node(new aiNode(obj->id.name + 2)); // skip over the name prefix 'OB'
|
2015-05-19 03:57:13 +00:00
|
|
|
if (obj->data) {
|
2020-06-23 19:05:42 +00:00
|
|
|
switch (obj->type) {
|
|
|
|
case Object ::Type_EMPTY:
|
2015-05-19 03:57:13 +00:00
|
|
|
break; // do nothing
|
|
|
|
|
|
|
|
// supported object types
|
2020-06-23 19:05:42 +00:00
|
|
|
case Object ::Type_MESH: {
|
2015-05-19 03:57:13 +00:00
|
|
|
const size_t old = conv_data.meshes->size();
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
CheckActualType(obj->data.get(), "Mesh");
|
|
|
|
ConvertMesh(in, obj, static_cast<const Mesh *>(obj->data.get()), conv_data, conv_data.meshes);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (conv_data.meshes->size() > old) {
|
2020-06-23 19:05:42 +00:00
|
|
|
node->mMeshes = new unsigned int[node->mNumMeshes = static_cast<unsigned int>(conv_data.meshes->size() - old)];
|
2015-05-19 03:57:13 +00:00
|
|
|
for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
|
2016-11-18 14:54:27 +00:00
|
|
|
node->mMeshes[i] = static_cast<unsigned int>(i + old);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Object ::Type_LAMP: {
|
|
|
|
CheckActualType(obj->data.get(), "Lamp");
|
|
|
|
aiLight *mesh = ConvertLight(in, obj, static_cast<const Lamp *>(obj->data.get()), conv_data);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (mesh) {
|
|
|
|
conv_data.lights->push_back(mesh);
|
2020-06-23 19:05:42 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case Object ::Type_CAMERA: {
|
|
|
|
CheckActualType(obj->data.get(), "Camera");
|
|
|
|
aiCamera *mesh = ConvertCamera(in, obj, static_cast<const Camera *>(obj->data.get()), conv_data);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (mesh) {
|
|
|
|
conv_data.cameras->push_back(mesh);
|
2020-06-23 19:05:42 +00:00
|
|
|
}
|
|
|
|
} break;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// unsupported object types / log, but do not break
|
2020-06-23 19:05:42 +00:00
|
|
|
case Object ::Type_CURVE:
|
|
|
|
NotSupportedObjectType(obj, "Curve");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2020-06-23 19:05:42 +00:00
|
|
|
case Object ::Type_SURF:
|
|
|
|
NotSupportedObjectType(obj, "Surface");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2020-06-23 19:05:42 +00:00
|
|
|
case Object ::Type_FONT:
|
|
|
|
NotSupportedObjectType(obj, "Font");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2020-06-23 19:05:42 +00:00
|
|
|
case Object ::Type_MBALL:
|
|
|
|
NotSupportedObjectType(obj, "MetaBall");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2020-06-23 19:05:42 +00:00
|
|
|
case Object ::Type_WAVE:
|
|
|
|
NotSupportedObjectType(obj, "Wave");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
2020-06-23 19:05:42 +00:00
|
|
|
case Object ::Type_LATTICE:
|
|
|
|
NotSupportedObjectType(obj, "Lattice");
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// invalid or unknown type
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int x = 0; x < 4; ++x) {
|
|
|
|
for (unsigned int y = 0; y < 4; ++y) {
|
2015-05-19 03:57:13 +00:00
|
|
|
node->mTransformation[y][x] = obj->obmat[x][y];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aiMatrix4x4 m = parentTransform;
|
|
|
|
m = m.Inverse();
|
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
node->mTransformation = m * node->mTransformation;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
if (children.size()) {
|
|
|
|
node->mNumChildren = static_cast<unsigned int>(children.size());
|
2020-06-23 19:05:42 +00:00
|
|
|
aiNode **nd = node->mChildren = new aiNode *[node->mNumChildren]();
|
|
|
|
for (const Object *nobj : children) {
|
|
|
|
*nd = ConvertNode(in, nobj, conv_data, node->mTransformation * parentTransform);
|
2017-12-02 15:13:43 +00:00
|
|
|
(*nd++)->mParent = node.get();
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply modifiers
|
2020-06-23 19:05:42 +00:00
|
|
|
modifier_cache->ApplyModifiers(*node, conv_data, in, *obj);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2017-12-02 15:13:43 +00:00
|
|
|
return node.release();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 10:41:49 +00:00
|
|
|
#endif // ASSIMP_BUILD_NO_BLEND_IMPORTER
|