diff --git a/cocos/3d/CCMeshSkin.cpp b/cocos/3d/CCMeshSkin.cpp index bedf24ede5..5a889039b1 100644 --- a/cocos/3d/CCMeshSkin.cpp +++ b/cocos/3d/CCMeshSkin.cpp @@ -37,6 +37,7 @@ static int PALETTE_ROWS = 3; MeshSkin::MeshSkin() : _rootBone(nullptr) , _matrixPalette(nullptr) +, _skeleton(nullptr) { } @@ -85,6 +86,23 @@ MeshSkin* MeshSkin::create(Skeleton3D* skeleton, const std::string& filename, co return nullptr; } +MeshSkin* MeshSkin::create(Skeleton3D* skeleton, const std::vector& boneNames, const std::vector& invBindPose) +{ + auto skin = new MeshSkin(); + skin->_skeleton = skeleton; + skeleton->retain(); + + CCASSERT(boneNames.size() == invBindPose.size(), "bone names' num should equals to invBindPose's num"); + for (const auto& it : boneNames) { + auto bone = skeleton->getBoneByName(it); + skin->addSkinBone(bone); + } + skin->_invBindPoses = invBindPose; + skin->autorelease(); + + return skin; +} + bool MeshSkin::initFromSkinData(const SkinData& skindata) { ssize_t i = 0; diff --git a/cocos/3d/CCMeshSkin.h b/cocos/3d/CCMeshSkin.h index bd5ff24245..83ebd8048f 100644 --- a/cocos/3d/CCMeshSkin.h +++ b/cocos/3d/CCMeshSkin.h @@ -52,6 +52,8 @@ public: /**create a new meshskin if do not want to share meshskin*/ static MeshSkin* create(Skeleton3D* skeleton, const std::string& filename, const std::string& name); + static MeshSkin* create(Skeleton3D* skeleton, const std::vector& boneNames, const std::vector& invBindPose); + /**get total bone count, skin bone + node bone*/ ssize_t getBoneCount() const; diff --git a/cocos/3d/CCSkeleton3D.cpp b/cocos/3d/CCSkeleton3D.cpp index e1a99a44f0..7009fca0e1 100644 --- a/cocos/3d/CCSkeleton3D.cpp +++ b/cocos/3d/CCSkeleton3D.cpp @@ -294,6 +294,18 @@ Skeleton3D* Skeleton3D::create(const std::string& filename, const std::string& n return nullptr; } +Skeleton3D* Skeleton3D::create(const std::vector& skeletondata) +{ + auto skeleton = new Skeleton3D(); + for (const auto& it : skeletondata) { + auto bone = createBone3D(*it); + skeleton->_rootBones.pushBack(bone); + bone->release(); + } + skeleton->autorelease(); + return skeleton; +} + bool Skeleton3D::initFromSkeletonData(const Skeleton3DData& skeletondata) { ssize_t i = 0; @@ -361,7 +373,7 @@ ssize_t Skeleton3D::getRootCount() const Bone3D* Skeleton3D::getRootBone(int index) const { - return _rootBones[index]; + return _rootBones.at(index); } int Skeleton3D::getBoneIndex(Bone3D* bone) const @@ -386,6 +398,7 @@ void Skeleton3D::removeAllBones() { _bones.clear(); CC_SAFE_RELEASE(_rootBone); + _rootBones.clear(); } void Skeleton3D::addBone(Bone3D* bone) @@ -393,6 +406,17 @@ void Skeleton3D::addBone(Bone3D* bone) _bones.pushBack(bone); } +Bone3D* Skeleton3D::createBone3D(const NodeData& nodedata) +{ + auto bone = Bone3D::create(nodedata.id); + for (const auto& it : nodedata.children) { + auto child = createBone3D(*it); + bone->addChildBone(child); + child->release(); + } + return bone; +} + //////////////////////////////////////////////////////////////////////// Skeleton3DDataCache* Skeleton3DDataCache::_cacheInstance = nullptr; diff --git a/cocos/3d/CCSkeleton3D.h b/cocos/3d/CCSkeleton3D.h index 55f31238c2..9788f73ec8 100644 --- a/cocos/3d/CCSkeleton3D.h +++ b/cocos/3d/CCSkeleton3D.h @@ -190,6 +190,8 @@ public: /**create a new meshskin if do not want to share Skeleton3D*/ static Skeleton3D* create(const std::string& filename, const std::string& name); + static Skeleton3D* create(const std::vector& skeletondata); + /**get total bone count*/ ssize_t getBoneCount() const; @@ -224,12 +226,15 @@ CC_CONSTRUCTOR_ACCESS: /**add bone*/ void addBone(Bone3D* bone); + /** create Bone3D from NodeData */ + static Bone3D* createBone3D(const NodeData& nodedata); + protected: Vector _bones; // bones Bone3D* _rootBone; - std::vector _rootBones; + Vector _rootBones; }; /** diff --git a/cocos/3d/CCSubMeshState.cpp b/cocos/3d/CCSubMeshState.cpp index 9f97a941bd..8d4aa61171 100644 --- a/cocos/3d/CCSubMeshState.cpp +++ b/cocos/3d/CCSubMeshState.cpp @@ -68,6 +68,17 @@ SubMeshState* SubMeshState::create() return state; } +SubMeshState* SubMeshState::create(const ModelNodeData& modelNodeData) +{ + auto state = new SubMeshState(); + state->autorelease(); + + state->_name = modelNodeData.id; + //not finished + + return state; +} + void SubMeshState::setTexture(Texture2D* tex) { if (tex != _texture) diff --git a/cocos/3d/CCSubMeshState.h b/cocos/3d/CCSubMeshState.h index 129a45a67b..60d23ea0dc 100644 --- a/cocos/3d/CCSubMeshState.h +++ b/cocos/3d/CCSubMeshState.h @@ -49,6 +49,8 @@ public: /**create submesh from primitivetype indexformat and indices*/ static SubMeshState* create(); + + static SubMeshState* create(const ModelNodeData& modelNodeData); /**texture getter and setter*/ void setTexture(Texture2D* tex);