summaryrefslogtreecommitdiff
path: root/src/mesh/assimp-master/code/AssetLib/B3D/B3DImporter.cpp
blob: c4ef75be3c4d3f7722a9b35106fc63e2b3188f3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/*
---------------------------------------------------------------------------
Open Asset Import Library (assimp)
---------------------------------------------------------------------------

Copyright (c) 2006-2022, assimp team

All rights reserved.

Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the 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.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------
*/

/** @file  B3DImporter.cpp
 *  @brief Implementation of the b3d importer class
 */

#ifndef ASSIMP_BUILD_NO_B3D_IMPORTER

// internal headers
#include "AssetLib/B3D/B3DImporter.h"
#include "PostProcessing/ConvertToLHProcess.h"
#include "PostProcessing/TextureTransform.h"

#include <assimp/StringUtils.h>
#include <assimp/anim.h>
#include <assimp/importerdesc.h>
#include <assimp/scene.h>
#include <assimp/DefaultLogger.hpp>
#include <assimp/IOSystem.hpp>

#include <memory>

using namespace Assimp;
using namespace std;

static const aiImporterDesc desc = {
    "BlitzBasic 3D Importer",
    "",
    "",
    "http://www.blitzbasic.com/",
    aiImporterFlags_SupportBinaryFlavour,
    0,
    0,
    0,
    0,
    "b3d"
};

#ifdef _MSC_VER
#pragma warning(disable : 4018)
#endif

//#define DEBUG_B3D

template<typename T>
void DeleteAllBarePointers(std::vector<T> &x) {
    for (auto p : x) {
        delete p;
    }
}

B3DImporter::~B3DImporter() {
    // empty
}

// ------------------------------------------------------------------------------------------------
bool B3DImporter::CanRead(const std::string &pFile, IOSystem * /*pIOHandler*/, bool /*checkSig*/) const {
    size_t pos = pFile.find_last_of('.');
    if (pos == string::npos) {
        return false;
    }

    string ext = pFile.substr(pos + 1);
    if (ext.size() != 3) {
        return false;
    }

    return (ext[0] == 'b' || ext[0] == 'B') && (ext[1] == '3') && (ext[2] == 'd' || ext[2] == 'D');
}

// ------------------------------------------------------------------------------------------------
// Loader meta information
const aiImporterDesc *B3DImporter::GetInfo() const {
    return &desc;
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
    std::unique_ptr<IOStream> file(pIOHandler->Open(pFile));

    // Check whether we can read from the file
    if (file.get() == nullptr) {
        throw DeadlyImportError("Failed to open B3D file ", pFile, ".");
    }

    // check whether the .b3d file is large enough to contain
    // at least one chunk.
    size_t fileSize = file->FileSize();
    if (fileSize < 8) {
        throw DeadlyImportError("B3D File is too small.");
    }

    _pos = 0;
    _buf.resize(fileSize);
    file->Read(&_buf[0], 1, fileSize);
    _stack.clear();

    ReadBB3D(pScene);
}

// ------------------------------------------------------------------------------------------------
AI_WONT_RETURN void B3DImporter::Oops() {
    throw DeadlyImportError("B3D Importer - INTERNAL ERROR");
}

// ------------------------------------------------------------------------------------------------
AI_WONT_RETURN void B3DImporter::Fail(const string &str) {
#ifdef DEBUG_B3D
    ASSIMP_LOG_ERROR("Error in B3D file data: ", str);
#endif
    throw DeadlyImportError("B3D Importer - error in B3D file data: ", str);
}

// ------------------------------------------------------------------------------------------------
int B3DImporter::ReadByte() {
    if (_pos > _buf.size()) {
        Fail("EOF");
    }

    return _buf[_pos++];
}

// ------------------------------------------------------------------------------------------------
int B3DImporter::ReadInt() {
    if (_pos + 4 > _buf.size()) {
        Fail("EOF");
    }

    int n;
    memcpy(&n, &_buf[_pos], 4);
    _pos += 4;

    return n;
}

// ------------------------------------------------------------------------------------------------
float B3DImporter::ReadFloat() {
    if (_pos + 4 > _buf.size()) {
        Fail("EOF");
    }

    float n;
    memcpy(&n, &_buf[_pos], 4);
    _pos += 4;

    return n;
}

// ------------------------------------------------------------------------------------------------
aiVector2D B3DImporter::ReadVec2() {
    float x = ReadFloat();
    float y = ReadFloat();
    return aiVector2D(x, y);
}

// ------------------------------------------------------------------------------------------------
aiVector3D B3DImporter::ReadVec3() {
    float x = ReadFloat();
    float y = ReadFloat();
    float z = ReadFloat();
    return aiVector3D(x, y, z);
}

// ------------------------------------------------------------------------------------------------
aiQuaternion B3DImporter::ReadQuat() {
    // (aramis_acg) Fix to adapt the loader to changed quat orientation
    float w = -ReadFloat();
    float x = ReadFloat();
    float y = ReadFloat();
    float z = ReadFloat();
    return aiQuaternion(w, x, y, z);
}

// ------------------------------------------------------------------------------------------------
string B3DImporter::ReadString() {
    if (_pos > _buf.size()) {
        Fail("EOF");
    }
    string str;
    while (_pos < _buf.size()) {
        char c = (char)ReadByte();
        if (!c) {
            return str;
        }
        str += c;
    }
    return string();
}

// ------------------------------------------------------------------------------------------------
string B3DImporter::ReadChunk() {
    string tag;
    for (int i = 0; i < 4; ++i) {
        tag += char(ReadByte());
    }
#ifdef DEBUG_B3D
    ASSIMP_LOG_DEBUG("ReadChunk: ", tag);
#endif
    unsigned sz = (unsigned)ReadInt();
    _stack.push_back(_pos + sz);
    return tag;
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ExitChunk() {
    _pos = _stack.back();
    _stack.pop_back();
}

// ------------------------------------------------------------------------------------------------
size_t B3DImporter::ChunkSize() {
    return _stack.back() - _pos;
}
// ------------------------------------------------------------------------------------------------

template <class T>
T *B3DImporter::to_array(const vector<T> &v) {
    if (v.empty()) {
        return 0;
    }
    T *p = new T[v.size()];
    for (size_t i = 0; i < v.size(); ++i) {
        p[i] = v[i];
    }
    return p;
}

// ------------------------------------------------------------------------------------------------
template <class T>
T **unique_to_array(vector<std::unique_ptr<T>> &v) {
    if (v.empty()) {
        return 0;
    }
    T **p = new T *[v.size()];
    for (size_t i = 0; i < v.size(); ++i) {
        p[i] = v[i].release();
    }
    return p;
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadTEXS() {
    while (ChunkSize()) {
        string name = ReadString();
        /*int flags=*/ReadInt();
        /*int blend=*/ReadInt();
        /*aiVector2D pos=*/ReadVec2();
        /*aiVector2D scale=*/ReadVec2();
        /*float rot=*/ReadFloat();

        _textures.push_back(name);
    }
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadBRUS() {
    int n_texs = ReadInt();
    if (n_texs < 0 || n_texs > 8) {
        Fail("Bad texture count");
    }
    while (ChunkSize()) {
        string name = ReadString();
        aiVector3D color = ReadVec3();
        float alpha = ReadFloat();
        float shiny = ReadFloat();
        /*int blend=**/ ReadInt();
        int fx = ReadInt();

        std::unique_ptr<aiMaterial> mat(new aiMaterial);

        // Name
        aiString ainame(name);
        mat->AddProperty(&ainame, AI_MATKEY_NAME);

        // Diffuse color
        mat->AddProperty(&color, 1, AI_MATKEY_COLOR_DIFFUSE);

        // Opacity
        mat->AddProperty(&alpha, 1, AI_MATKEY_OPACITY);

        // Specular color
        aiColor3D speccolor(shiny, shiny, shiny);
        mat->AddProperty(&speccolor, 1, AI_MATKEY_COLOR_SPECULAR);

        // Specular power
        float specpow = shiny * 128;
        mat->AddProperty(&specpow, 1, AI_MATKEY_SHININESS);

        // Double sided
        if (fx & 0x10) {
            int i = 1;
            mat->AddProperty(&i, 1, AI_MATKEY_TWOSIDED);
        }

        //Textures
        for (int i = 0; i < n_texs; ++i) {
            int texid = ReadInt();
            if (texid < -1 || (texid >= 0 && texid >= static_cast<int>(_textures.size()))) {
                Fail("Bad texture id");
            }
            if (i == 0 && texid >= 0) {
                aiString texname(_textures[texid]);
                mat->AddProperty(&texname, AI_MATKEY_TEXTURE_DIFFUSE(0));
            }
        }
        _materials.emplace_back(std::move(mat));
    }
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadVRTS() {
    _vflags = ReadInt();
    _tcsets = ReadInt();
    _tcsize = ReadInt();
    if (_tcsets < 0 || _tcsets > 4 || _tcsize < 0 || _tcsize > 4) {
        Fail("Bad texcoord data");
    }

    int sz = 12 + (_vflags & 1 ? 12 : 0) + (_vflags & 2 ? 16 : 0) + (_tcsets * _tcsize * 4);
    size_t n_verts = ChunkSize() / sz;

    int v0 = static_cast<int>(_vertices.size());
    _vertices.resize(v0 + n_verts);

    for (unsigned int i = 0; i < n_verts; ++i) {
        Vertex &v = _vertices[v0 + i];

        memset(v.bones, 0, sizeof(v.bones));
        memset(v.weights, 0, sizeof(v.weights));

        v.vertex = ReadVec3();

        if (_vflags & 1) {
            v.normal = ReadVec3();
        }

        if (_vflags & 2) {
            ReadQuat(); //skip v 4bytes...
        }

        for (int j = 0; j < _tcsets; ++j) {
            float t[4] = { 0, 0, 0, 0 };
            for (int k = 0; k < _tcsize; ++k) {
                t[k] = ReadFloat();
            }
            t[1] = 1 - t[1];
            if (!j) {
                v.texcoords = aiVector3D(t[0], t[1], t[2]);
            }
        }
    }
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadTRIS(int v0) {
    int matid = ReadInt();
    if (matid == -1) {
        matid = 0;
    } else if (matid < 0 || matid >= (int)_materials.size()) {
#ifdef DEBUG_B3D
        ASSIMP_LOG_ERROR("material id=", matid);
#endif
        Fail("Bad material id");
    }

    std::unique_ptr<aiMesh> mesh(new aiMesh);

    mesh->mMaterialIndex = matid;
    mesh->mNumFaces = 0;
    mesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;

    size_t n_tris = ChunkSize() / 12;
    aiFace *face = mesh->mFaces = new aiFace[n_tris];

    for (unsigned int i = 0; i < n_tris; ++i) {
        int i0 = ReadInt() + v0;
        int i1 = ReadInt() + v0;
        int i2 = ReadInt() + v0;
        if (i0 < 0 || i0 >= (int)_vertices.size() || i1 < 0 || i1 >= (int)_vertices.size() || i2 < 0 || i2 >= (int)_vertices.size()) {
#ifdef DEBUG_B3D
            ASSIMP_LOG_ERROR("Bad triangle index: i0=", i0, ", i1=", i1, ", i2=", i2);
#endif
            Fail("Bad triangle index");
            continue;
        }
        face->mNumIndices = 3;
        face->mIndices = new unsigned[3];
        face->mIndices[0] = i0;
        face->mIndices[1] = i1;
        face->mIndices[2] = i2;
        ++mesh->mNumFaces;
        ++face;
    }

    _meshes.emplace_back(std::move(mesh));
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadMESH() {
    /*int matid=*/ReadInt();

    int v0 = static_cast<int>(_vertices.size());

    while (ChunkSize()) {
        string t = ReadChunk();
        if (t == "VRTS") {
            ReadVRTS();
        } else if (t == "TRIS") {
            ReadTRIS(v0);
        }
        ExitChunk();
    }
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadBONE(int id) {
    while (ChunkSize()) {
        int vertex = ReadInt();
        float weight = ReadFloat();
        if (vertex < 0 || vertex >= (int)_vertices.size()) {
            Fail("Bad vertex index");
        }

        Vertex &v = _vertices[vertex];
        for (int i = 0; i < 4; ++i) {
            if (!v.weights[i]) {
                v.bones[i] = static_cast<unsigned char>(id);
                v.weights[i] = weight;
                break;
            }
        }
    }
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadKEYS(aiNodeAnim *nodeAnim) {
    vector<aiVectorKey> trans, scale;
    vector<aiQuatKey> rot;
    int flags = ReadInt();
    while (ChunkSize()) {
        int frame = ReadInt();
        if (flags & 1) {
            trans.push_back(aiVectorKey(frame, ReadVec3()));
        }
        if (flags & 2) {
            scale.push_back(aiVectorKey(frame, ReadVec3()));
        }
        if (flags & 4) {
            rot.push_back(aiQuatKey(frame, ReadQuat()));
        }
    }

    if (flags & 1) {
        nodeAnim->mNumPositionKeys = static_cast<unsigned int>(trans.size());
        nodeAnim->mPositionKeys = to_array(trans);
    }

    if (flags & 2) {
        nodeAnim->mNumScalingKeys = static_cast<unsigned int>(scale.size());
        nodeAnim->mScalingKeys = to_array(scale);
    }

    if (flags & 4) {
        nodeAnim->mNumRotationKeys = static_cast<unsigned int>(rot.size());
        nodeAnim->mRotationKeys = to_array(rot);
    }
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadANIM() {
    /*int flags=*/ReadInt();
    int frames = ReadInt();
    float fps = ReadFloat();

    std::unique_ptr<aiAnimation> anim(new aiAnimation);

    anim->mDuration = frames;
    anim->mTicksPerSecond = fps;
    _animations.emplace_back(std::move(anim));
}

// ------------------------------------------------------------------------------------------------
aiNode *B3DImporter::ReadNODE(aiNode *parent) {

    string name = ReadString();
    aiVector3D t = ReadVec3();
    aiVector3D s = ReadVec3();
    aiQuaternion r = ReadQuat();

    aiMatrix4x4 trans, scale, rot;

    aiMatrix4x4::Translation(t, trans);
    aiMatrix4x4::Scaling(s, scale);
    rot = aiMatrix4x4(r.GetMatrix());

    aiMatrix4x4 tform = trans * rot * scale;

    int nodeid = static_cast<int>(_nodes.size());

    aiNode *node = new aiNode(name);
    _nodes.push_back(node);

    node->mParent = parent;
    node->mTransformation = tform;

    std::unique_ptr<aiNodeAnim> nodeAnim;
    vector<unsigned> meshes;
    vector<aiNode *> children;

    while (ChunkSize()) {
        const string chunk = ReadChunk();
        if (chunk == "MESH") {
            unsigned int n = static_cast<unsigned int>(_meshes.size());
            ReadMESH();
            for (unsigned int i = n; i < static_cast<unsigned int>(_meshes.size()); ++i) {
                meshes.push_back(i);
            }
        } else if (chunk == "BONE") {
            ReadBONE(nodeid);
        } else if (chunk == "ANIM") {
            ReadANIM();
        } else if (chunk == "KEYS") {
            if (!nodeAnim) {
                nodeAnim.reset(new aiNodeAnim);
                nodeAnim->mNodeName = node->mName;
            }
            ReadKEYS(nodeAnim.get());
        } else if (chunk == "NODE") {
            aiNode *child = ReadNODE(node);
            children.push_back(child);
        }
        ExitChunk();
    }

    if (nodeAnim) {
        _nodeAnims.emplace_back(std::move(nodeAnim));
    }

    node->mNumMeshes = static_cast<unsigned int>(meshes.size());
    node->mMeshes = to_array(meshes);

    node->mNumChildren = static_cast<unsigned int>(children.size());
    node->mChildren = to_array(children);

    return node;
}

// ------------------------------------------------------------------------------------------------
void B3DImporter::ReadBB3D(aiScene *scene) {

    _textures.clear();

    _materials.clear();

    _vertices.clear();

    _meshes.clear();

    DeleteAllBarePointers(_nodes);
    _nodes.clear();

    _nodeAnims.clear();

    _animations.clear();

    string t = ReadChunk();
    if (t == "BB3D") {
        int version = ReadInt();

        if (!DefaultLogger::isNullLogger()) {
            char dmp[128];
            ai_snprintf(dmp, 128, "B3D file format version: %i", version);
            ASSIMP_LOG_INFO(dmp);
        }

        while (ChunkSize()) {
            const string chunk = ReadChunk();
            if (chunk == "TEXS") {
                ReadTEXS();
            } else if (chunk == "BRUS") {
                ReadBRUS();
            } else if (chunk == "NODE") {
                ReadNODE(0);
            }
            ExitChunk();
        }
    }
    ExitChunk();

    if (!_nodes.size()) {
        Fail("No nodes");
    }

    if (!_meshes.size()) {
        Fail("No meshes");
    }

    // Fix nodes/meshes/bones
    for (size_t i = 0; i < _nodes.size(); ++i) {
        aiNode *node = _nodes[i];

        for (size_t j = 0; j < node->mNumMeshes; ++j) {
            aiMesh *mesh = _meshes[node->mMeshes[j]].get();

            int n_tris = mesh->mNumFaces;
            int n_verts = mesh->mNumVertices = n_tris * 3;

            aiVector3D *mv = mesh->mVertices = new aiVector3D[n_verts], *mn = 0, *mc = 0;
            if (_vflags & 1) {
                mn = mesh->mNormals = new aiVector3D[n_verts];
            }
            if (_tcsets) {
                mc = mesh->mTextureCoords[0] = new aiVector3D[n_verts];
            }

            aiFace *face = mesh->mFaces;

            vector<vector<aiVertexWeight>> vweights(_nodes.size());

            for (int vertIdx = 0; vertIdx < n_verts; vertIdx += 3) {
                for (int faceIndex = 0; faceIndex < 3; ++faceIndex) {
                    Vertex &v = _vertices[face->mIndices[faceIndex]];

                    *mv++ = v.vertex;
                    if (mn) *mn++ = v.normal;
                    if (mc) *mc++ = v.texcoords;

                    face->mIndices[faceIndex] = vertIdx + faceIndex;

                    for (int k = 0; k < 4; ++k) {
                        if (!v.weights[k])
                            break;

                        int bone = v.bones[k];
                        float weight = v.weights[k];

                        vweights[bone].push_back(aiVertexWeight(vertIdx + faceIndex, weight));
                    }
                }
                ++face;
            }

            vector<aiBone *> bones;
            for (size_t weightIndx = 0; weightIndx < vweights.size(); ++weightIndx) {
                vector<aiVertexWeight> &weights = vweights[weightIndx];
                if (!weights.size()) {
                    continue;
                }

                aiBone *bone = new aiBone;
                bones.push_back(bone);

                aiNode *bnode = _nodes[weightIndx];

                bone->mName = bnode->mName;
                bone->mNumWeights = static_cast<unsigned int>(weights.size());
                bone->mWeights = to_array(weights);

                aiMatrix4x4 mat = bnode->mTransformation;
                while (bnode->mParent) {
                    bnode = bnode->mParent;
                    mat = bnode->mTransformation * mat;
                }
                bone->mOffsetMatrix = mat.Inverse();
            }
            mesh->mNumBones = static_cast<unsigned int>(bones.size());
            mesh->mBones = to_array(bones);
        }
    }

    //nodes
    scene->mRootNode = _nodes[0];
    _nodes.clear(); // node ownership now belongs to scene

    //material
    if (!_materials.size()) {
        _materials.emplace_back(std::unique_ptr<aiMaterial>(new aiMaterial));
    }
    scene->mNumMaterials = static_cast<unsigned int>(_materials.size());
    scene->mMaterials = unique_to_array(_materials);

    //meshes
    scene->mNumMeshes = static_cast<unsigned int>(_meshes.size());
    scene->mMeshes = unique_to_array(_meshes);

    //animations
    if (_animations.size() == 1 && _nodeAnims.size()) {

        aiAnimation *anim = _animations.back().get();
        anim->mNumChannels = static_cast<unsigned int>(_nodeAnims.size());
        anim->mChannels = unique_to_array(_nodeAnims);

        scene->mNumAnimations = static_cast<unsigned int>(_animations.size());
        scene->mAnimations = unique_to_array(_animations);
    }

    // convert to RH
    MakeLeftHandedProcess makeleft;
    makeleft.Execute(scene);

    FlipWindingOrderProcess flip;
    flip.Execute(scene);
}

#endif // !! ASSIMP_BUILD_NO_B3D_IMPORTER