From 0696041ea93494561376debcb03ee21537a13562 Mon Sep 17 00:00:00 2001 From: yangxiao Date: Sat, 25 Oct 2014 12:27:03 +0800 Subject: [PATCH 1/4] add getMeshArrayByName method --- cocos/3d/CCSprite3D.cpp | 12 +++++++++++- cocos/3d/CCSprite3D.h | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cocos/3d/CCSprite3D.cpp b/cocos/3d/CCSprite3D.cpp index a498d9b03e..66a0f0d216 100644 --- a/cocos/3d/CCSprite3D.cpp +++ b/cocos/3d/CCSprite3D.cpp @@ -684,7 +684,7 @@ Mesh* Sprite3D::getMeshByIndex(int index) const return _meshes.at(index); } -/**get SubMeshState by Name */ +/**get Mesh by Name */ Mesh* Sprite3D::getMeshByName(const std::string& name) const { for (const auto& it : _meshes) { @@ -694,6 +694,16 @@ Mesh* Sprite3D::getMeshByName(const std::string& name) const return nullptr; } +std::vector Sprite3D::getMeshArrayByName(const std::string& name) const +{ + std::vector meshes; + for (const auto& it : _meshes) { + if (it->getName() == name) + meshes.push_back(it); + } + return meshes; +} + MeshSkin* Sprite3D::getSkin() const { for (const auto& it : _meshes) { diff --git a/cocos/3d/CCSprite3D.h b/cocos/3d/CCSprite3D.h index 3b08d9bf9e..daeb83c688 100644 --- a/cocos/3d/CCSprite3D.h +++ b/cocos/3d/CCSprite3D.h @@ -60,11 +60,14 @@ public: void setTexture(const std::string& texFile); void setTexture(Texture2D* texture); - /**get SubMeshState by index*/ + /**get Mesh by index*/ Mesh* getMeshByIndex(int index) const; - /**get SubMeshState by Name */ + /**get Mesh by Name, it returns the first one if there are more than one mesh with the same name */ Mesh* getMeshByName(const std::string& name) const; + + /** get mesh array by name, returns all meshes with the given name */ + std::vector getMeshArrayByName(const std::string& name) const; /**get mesh*/ Mesh* getMesh() const { return _meshes.at(0); } From 20decd5a3930a93302372b9d5cdac5dad2bc217c Mon Sep 17 00:00:00 2001 From: yangxiao Date: Sat, 25 Oct 2014 14:11:32 +0800 Subject: [PATCH 2/4] refact reskin code --- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 195 +++++------------- .../Classes/Sprite3DTest/Sprite3DTest.h | 21 +- 2 files changed, 70 insertions(+), 146 deletions(-) diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 8e79b5b1f3..0d6c0ca196 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -1027,124 +1027,50 @@ Sprite3DReskinTest::Sprite3DReskinTest() } void Sprite3DReskinTest::menuCallback_switchHair(Ref* sender) { - _useHairId++; - if(_useHairId > 1 ) - { - _useHairId = 0; - } - if(_useHairId >= 0 && _sprite) - { - for(int i = 0; i < 2; i++ ) - { - auto subMesh = _sprite->getMeshByName(_girlHair[i]); - if(subMesh) - { - if(i == _useHairId ) - { - subMesh->setVisible(true); - } - else - { - subMesh->setVisible(false); - } - } - } - } + std::string str = _curSkin[SkinType::HAIR]; + if (str == "Girl_Hair01") + _curSkin[SkinType::HAIR] = "Girl_Hair02"; + else + _curSkin[SkinType::HAIR] = "Girl_Hair01"; + applyCurSkin(); } void Sprite3DReskinTest::menuCallback_switchGlasses(Ref* sender) { - auto subMesh = _sprite->getMeshByName("Girl_Glasses01"); - if(subMesh) - { - if(subMesh->isVisible()) - { - subMesh->setVisible(false); - } - else - { - subMesh->setVisible(true); - } - } + std::string str = _curSkin[SkinType::GLASSES]; + if (str == "Girl_Glasses01") + _curSkin[SkinType::GLASSES] = ""; + else + _curSkin[SkinType::GLASSES] = "Girl_Glasses01"; + applyCurSkin(); } void Sprite3DReskinTest::menuCallback_switchCoat(Ref* sender) { - _useUpBodyId++; - if(_useUpBodyId > 1 ) - { - _useUpBodyId = 0; - } - if(_useUpBodyId >= 0 && _sprite) - { - for(int i = 0; i < 2; i++ ) - { - auto subMesh = _sprite->getMeshByName(_girlUpperBody[i]); - if(subMesh) - { - if(i == _useUpBodyId ) - { - subMesh->setVisible(true); - } - else - { - subMesh->setVisible(false); - } - } - } - } + std::string str = _curSkin[SkinType::UPPER_BODY]; + if (str == "Girl_UpperBody01") + _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody02"; + else + _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody01"; + applyCurSkin(); } void Sprite3DReskinTest::menuCallback_switchPants(Ref* sender) { - _usePantsId++; - if(_usePantsId > 1 ) - { - _usePantsId = 0; - } - if(_usePantsId >= 0 && _sprite) - { - for(int i = 0; i < 2; i++ ) - { - auto subMesh = _sprite->getMeshByName(_girlPants[i]); - if(subMesh) - { - if(i == _usePantsId ) - { - subMesh->setVisible(true); - } - else - { - subMesh->setVisible(false); - } - } - } - } + std::string str = _curSkin[SkinType::PANTS]; + if (str == "Girl_LowerBody01") + _curSkin[SkinType::PANTS] = "Girl_LowerBody02"; + else + _curSkin[SkinType::PANTS] = "Girl_LowerBody01"; + applyCurSkin(); } void Sprite3DReskinTest::menuCallback_switchShoes(Ref* sender) { - _useShoesId++; - if(_useShoesId > 1 ) - { - _useShoesId = 0; - } - if(_useShoesId >= 0 && _sprite) - { - for(int i = 0; i < 2; i++ ) - { - auto subMesh = _sprite->getMeshByName(_girlShoes[i]); - if(subMesh) - { - if(i == _useShoesId ) - { - subMesh->setVisible(true); - } - else - { - subMesh->setVisible(false); - } - } - } - } - + std::string strShoes = _curSkin[SkinType::SHOES]; + if (strShoes == "Girl_Shoes01") + _curSkin[SkinType::SHOES] = "Girl_Shoes02"; + else + _curSkin[SkinType::SHOES] = "Girl_Shoes01"; + applyCurSkin(); } + std::string Sprite3DReskinTest::title() const { return "Testing Sprite3D Reskin"; @@ -1156,43 +1082,10 @@ std::string Sprite3DReskinTest::subtitle() const void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p) { - _girlPants[0]= "Girl_LowerBody01"; - _girlPants[1]= "Girl_LowerBody02"; - _girlUpperBody[0] = "Girl_UpperBody01"; - _girlUpperBody[1] = "Girl_UpperBody02"; - _girlShoes[0] = "Girl_Shoes01"; - _girlShoes[1] = "Girl_Shoes02"; - _girlHair[0]= "Girl_Hair01"; - _girlHair[1]= "Girl_Hair02"; - _usePantsId = 0; - _useUpBodyId = 0; - _useShoesId =0; - _useHairId = 0; - std::string fileName = "Sprite3DTest/ReskinGirl.c3b"; auto sprite = Sprite3D::create(fileName); sprite->setScale(4); sprite->setRotation3D(Vec3(0,0,0)); - auto girlPants = sprite->getMeshByName(_girlPants[1]); - if(girlPants) - { - girlPants->setVisible(false); - } - auto girlShoes = sprite->getMeshByName(_girlShoes[1]); - if(girlShoes) - { - girlShoes->setVisible(false); - } - auto girlHair = sprite->getMeshByName(_girlHair[1]); - if(girlHair) - { - girlHair->setVisible(false); - } - auto girlUpBody = sprite->getMeshByName( _girlUpperBody[1]); - if(girlUpBody) - { - girlUpBody->setVisible(false); - } addChild(sprite); sprite->setPosition( Vec2( p.x, p.y-60) ); auto animation = Animation3D::create(fileName); @@ -1203,11 +1096,37 @@ void Sprite3DReskinTest::addNewSpriteWithCoords(Vec2 p) sprite->runAction(RepeatForever::create(animate)); } _sprite = sprite; + + _curSkin[SkinType::UPPER_BODY] = "Girl_UpperBody01"; + _curSkin[SkinType::PANTS] = "Girl_LowerBody01"; + _curSkin[SkinType::SHOES] = "Girl_Shoes01"; + _curSkin[SkinType::HAIR] = "Girl_Hair01"; + _curSkin[SkinType::FACE] = "Girl_Face01"; + _curSkin[SkinType::HAND] = "Girl_Hand01"; + _curSkin[SkinType::GLASSES] = ""; + applyCurSkin(); } void Sprite3DReskinTest::onTouchesEnded(const std::vector& touches, Event* event) { } + +void Sprite3DReskinTest::applyCurSkin() +{ + for (ssize_t i = 0; i < _sprite->getMeshCount(); i++) { + auto mesh = _sprite->getMeshByIndex(i); + bool isVisible = false; + for (auto& it : _curSkin) { + if (mesh->getName() == it.second) + { + isVisible = true; + break; + } + } + _sprite->getMeshByIndex(i)->setVisible(isVisible); + } +} + Sprite3DWithOBBPerfromanceTest::Sprite3DWithOBBPerfromanceTest() { auto listener = EventListenerTouchAllAtOnce::create(); diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index 6fdd53c592..721d1ee105 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -261,14 +261,19 @@ public: void menuCallback_switchPants(Ref* sender); void menuCallback_switchShoes(Ref* sender); protected: - std::string _girlPants[2]; - int _usePantsId; - std::string _girlUpperBody[2]; - int _useUpBodyId; - std::string _girlShoes[2]; - int _useShoesId; - std::string _girlHair[2]; - int _useHairId; + void applyCurSkin(); + + enum class SkinType + { + UPPER_BODY = 0, + PANTS, + SHOES, + HAIR, + FACE, + HAND, + GLASSES, + }; + std::map _curSkin; cocos2d::Sprite3D* _sprite; }; class Sprite3DWithOBBPerfromanceTest : public Sprite3DTestDemo From 7656ff2f0e56a8f43a0063e1fb4a3f982851ad1b Mon Sep 17 00:00:00 2001 From: yangxiao Date: Mon, 27 Oct 2014 13:59:40 +0800 Subject: [PATCH 3/4] fix wrong vertex buffer size --- cocos/3d/CCMeshVertexIndexData.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/cocos/3d/CCMeshVertexIndexData.cpp b/cocos/3d/CCMeshVertexIndexData.cpp index 513805ccb2..00822071ca 100644 --- a/cocos/3d/CCMeshVertexIndexData.cpp +++ b/cocos/3d/CCMeshVertexIndexData.cpp @@ -94,15 +94,12 @@ MeshVertexData* MeshVertexData::create(const MeshData& meshdata) vertexdata->_vertexData->setStream(vertexdata->_vertexBuffer, VertexStreamAttribute(offset, it.vertexAttrib, it.type, it.size)); offset += it.attribSizeBytes; } - vertexdata->_vertexData->setStream(vertexdata->_vertexBuffer, VertexStreamAttribute(0, GLProgram::VERTEX_ATTRIB_POSITION, GL_FLOAT, 3)); - vertexdata->_vertexData->setStream(vertexdata->_vertexBuffer, VertexStreamAttribute(offsetof(V3F_C4B_T2F, colors), GLProgram::VERTEX_ATTRIB_COLOR, GL_UNSIGNED_BYTE, 4, true)); - vertexdata->_vertexData->setStream(vertexdata->_vertexBuffer, VertexStreamAttribute(offsetof(V3F_C4B_T2F, texCoords), GLProgram::VERTEX_ATTRIB_TEX_COORD, GL_FLOAT, 2)); vertexdata->_attribs = meshdata.attribs; if(vertexdata->_vertexBuffer) { - vertexdata->_vertexBuffer->updateVertices((void*)&meshdata.vertex[0], (int)meshdata.vertex.size() * 4, 0); + vertexdata->_vertexBuffer->updateVertices((void*)&meshdata.vertex[0], (int)meshdata.vertex.size() * 4 / vertexdata->_vertexBuffer->getSizePerVertex(), 0); } AABB aabb; From 4d74e7f19f8d356d5e1d1b3c053f6863de59035b Mon Sep 17 00:00:00 2001 From: yangxiao Date: Wed, 29 Oct 2014 10:38:13 +0800 Subject: [PATCH 4/4] skip getMeshArrayByName --- tools/tolua/cocos2dx_3d.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/tolua/cocos2dx_3d.ini b/tools/tolua/cocos2dx_3d.ini index 0e779cf550..f33c10f38d 100644 --- a/tools/tolua/cocos2dx_3d.ini +++ b/tools/tolua/cocos2dx_3d.ini @@ -36,7 +36,7 @@ classes = Animate3D Sprite3D Animation3D Skeleton3D ^Mesh$ AttachNode BillBoard # functions from all classes. skip = Mesh::[create getAABB getVertexBuffer hasVertexAttrib getMeshVertexAttribCount getMeshVertexAttribute getVertexSizeInBytes getSkin getMeshIndexData getGLProgramState getPrimitiveType getIndexCount getIndexFormat getIndexBuffer], - Sprite3D::[getSkin getAABB setBlendFunc], + Sprite3D::[getSkin getAABB setBlendFunc getMeshArrayByName], Skeleton3D::[create], Animation3D::[getBoneCurveByName], BillBoard::[draw]