Merge pull request #8930 from super626/fixsprite

add Sprite3D::getMeshArrayByName and refactor re-skin test
This commit is contained in:
minggo 2014-11-05 18:11:51 +08:00
commit 25053b80bc
6 changed files with 88 additions and 154 deletions

View File

@ -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;

View File

@ -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<Mesh*> Sprite3D::getMeshArrayByName(const std::string& name) const
{
std::vector<Mesh*> 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) {

View File

@ -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<Mesh*> getMeshArrayByName(const std::string& name) const;
/**get mesh*/
Mesh* getMesh() const { return _meshes.at(0); }

View File

@ -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<Touch*>& 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();

View File

@ -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<SkinType, std::string> _curSkin;
cocos2d::Sprite3D* _sprite;
};
class Sprite3DWithOBBPerfromanceTest : public Sprite3DTestDemo

View File

@ -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]