update sprite 3d test

This commit is contained in:
Liam 2015-11-30 14:48:19 +08:00
parent 71871bacf9
commit e0349ae975
4 changed files with 112 additions and 10 deletions

View File

@ -339,6 +339,7 @@ void Mesh::draw(Renderer* renderer, float globalZOrder, const Mat4& transform, u
if (isTransparent)
flags |= Node::FLAGS_RENDER_AS_3D;
this->checkTexture();
_meshCommand.init(globalZ,
_material,
@ -662,22 +663,37 @@ GLuint Mesh::getIndexBuffer() const
return _meshIndexData->getIndexBuffer()->getVBO();
}
GLuint Mesh::getTextureName()
void Mesh::checkTexture()
{
Texture2D* cacheTex = nullptr;
if (TextureCache::getInstance()->isDirty())
{
Texture2D* cacheTex = TextureCache::getInstance()->getTextureForKey(_texFile);
_texture = cacheTex;
cacheTex = TextureCache::getInstance()->getTextureForKey(_texFile);
if (cacheTex == nullptr)
{
cacheTex = getDummyTexture();
}
}
if (_texture == nullptr || !_texture->isValid())
else if (_texture != nullptr && !_texture->isValid())
{
_texture = nullptr;
Texture2D* dummyTex = getDummyTexture();
return dummyTex->getName();
cacheTex = getDummyTexture();
}
return _texture->getName();
if (cacheTex != nullptr && _texture != cacheTex)
{
CC_SAFE_RETAIN(cacheTex);
CC_SAFE_RELEASE(_texture);
_texture = cacheTex;
if (_material) {
auto technique = _material->_currentTechnique;
for (auto& pass : technique->_passes)
{
pass->setTexture(_texture);
}
}
bindMeshCommand();
}
}
NS_CC_END

View File

@ -194,7 +194,7 @@ public:
*/
void setForce2DQueue(bool force2D) { _force2DQueue = force2D; }
GLuint getTextureName();
void checkTexture();
CC_CONSTRUCTOR_ACCESS:

View File

@ -67,6 +67,7 @@ Sprite3DTests::Sprite3DTests()
ADD_TEST_CASE(CameraBackgroundClearTest);
ADD_TEST_CASE(Sprite3DVertexColorTest);
ADD_TEST_CASE(MotionStreak3DTest);
ADD_TEST_CASE(Sprite3DPropertyTest);
};
//------------------------------------------------------------------
@ -2512,3 +2513,72 @@ void MotionStreak3DTest::update(float delta)
_streak->setPosition3D(_sprite->getPosition3D());
_streak->setSweepAxis(Vec3(cosf(angle), 0, sinf(angle)));
}
Sprite3DPropertyTest::Sprite3DPropertyTest()
{
auto s = Director::getInstance()->getWinSize();
auto camera = Camera::createPerspective(40, s.width / s.height, 0.01f, 1000.f);
camera->setCameraFlag(CameraFlag::USER1);
camera->setPosition3D(Vec3(0.f, 50.f, 200.f));
camera->lookAt(Vec3(0.f, 0.f, 0.f));
addChild(camera);
_sprite = Sprite3D::create("Sprite3DTest/orc.c3b");
_sprite->setPosition(20.f, 0.f);
_sprite->setRotation3D(Vec3(0, 180, 0));
_meshTex = _sprite->getMesh()->getTexture();
addChild(_sprite);
setCameraMask(2);
//auto listener = EventListenerTouchAllAtOnce::create();
////listener->onTouchesEnded = CC_CALLBACK_2(Sprite3DReskinTest::onTouchesEnded, this);
//_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto label1 = Label::createWithTTF(ttfConfig, "Print Mesh Name");
auto item1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Sprite3DPropertyTest::printMeshName, this));
auto label2 = Label::createWithTTF(ttfConfig, "Remove Used Texture");
auto item2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Sprite3DPropertyTest::removeUsedTexture, this));
item1->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 4));
item2->setPosition(Vec2(VisibleRect::left().x + 100, VisibleRect::bottom().y + item1->getContentSize().height * 5));
auto pMenu1 = Menu::create(item1, item2, nullptr);
pMenu1->setPosition(Vec2(0, 0));
this->addChild(pMenu1, 10);
scheduleUpdate();
}
std::string Sprite3DPropertyTest::title() const
{
return "Sprite3DPropertyTest Test";
}
std::string Sprite3DPropertyTest::subtitle() const
{
return "";
}
void Sprite3DPropertyTest::update(float delta)
{
}
void Sprite3DPropertyTest::printMeshName(cocos2d::Ref* sender)
{
CCLOG("MeshName Begin\n");
Vector<Mesh*> meshes =_sprite->getMeshes();
for each (Mesh* mesh in meshes)
{
CCLOG("MeshName: %s \n", mesh->getName().c_str());
}
CCLOG("MeshName End\n");
}
void Sprite3DPropertyTest::removeUsedTexture(cocos2d::Ref* sender)
{
if (_meshTex != nullptr)
{
TextureCache::getInstance()->removeTexture(_meshTex);
}
}

View File

@ -584,4 +584,20 @@ protected:
cocos2d::MotionStreak3D* _streak;
};
class Sprite3DPropertyTest : public Sprite3DTestDemo
{
public:
CREATE_FUNC(Sprite3DPropertyTest);
Sprite3DPropertyTest();
virtual std::string title() const override;
virtual std::string subtitle() const override;
virtual void update(float delta) override;
void printMeshName(cocos2d::Ref* sender);
void removeUsedTexture(cocos2d::Ref* sender);
protected:
cocos2d::Sprite3D* _sprite;
cocos2d::Texture2D* _meshTex;
};
#endif