From e0349ae9756dfe1427ad92fe4304d17d961da1ed Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 30 Nov 2015 14:48:19 +0800 Subject: [PATCH 1/2] update sprite 3d test --- cocos/3d/CCMesh.cpp | 34 ++++++--- cocos/3d/CCMesh.h | 2 +- .../Classes/Sprite3DTest/Sprite3DTest.cpp | 70 +++++++++++++++++++ .../Classes/Sprite3DTest/Sprite3DTest.h | 16 +++++ 4 files changed, 112 insertions(+), 10 deletions(-) diff --git a/cocos/3d/CCMesh.cpp b/cocos/3d/CCMesh.cpp index 30d3547e02..2bf29c3d30 100644 --- a/cocos/3d/CCMesh.cpp +++ b/cocos/3d/CCMesh.cpp @@ -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 diff --git a/cocos/3d/CCMesh.h b/cocos/3d/CCMesh.h index 964850e0e9..0338c906ad 100644 --- a/cocos/3d/CCMesh.h +++ b/cocos/3d/CCMesh.h @@ -194,7 +194,7 @@ public: */ void setForce2DQueue(bool force2D) { _force2DQueue = force2D; } - GLuint getTextureName(); + void checkTexture(); CC_CONSTRUCTOR_ACCESS: diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp index 27dfb2a546..c7a500aaec 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.cpp @@ -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 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); + } +} \ No newline at end of file diff --git a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h index f24691cd21..f8dbf0889c 100644 --- a/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h +++ b/tests/cpp-tests/Classes/Sprite3DTest/Sprite3DTest.h @@ -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 From 4d379ed9bd18f75857eb822ad630fca1efa5aeb1 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 30 Nov 2015 14:53:37 +0800 Subject: [PATCH 2/2] revert ccplane --- cocos/3d/CCPlane.cpp | 13 ------------- cocos/3d/CCPlane.h | 5 ----- 2 files changed, 18 deletions(-) diff --git a/cocos/3d/CCPlane.cpp b/cocos/3d/CCPlane.cpp index 3fa0b7f8b9..722ed2e91d 100755 --- a/cocos/3d/CCPlane.cpp +++ b/cocos/3d/CCPlane.cpp @@ -92,17 +92,4 @@ PointSide Plane::getSide(const Vec3& point) const return PointSide::IN_PLANE; } -PointSide Plane::getSide(const Vec3& point, const Vec3& tolerance) const -{ - float dist = dist2Plane(point); - float maxAbsDist = fabs(_normal.x * tolerance.x) + fabs(_normal.y * tolerance.y) + fabs(_normal.z * tolerance.z); - - if (dist > maxAbsDist) - return PointSide::FRONT_PLANE; - else if (dist < -maxAbsDist) - return PointSide::BEHIND_PLANE; - else - return PointSide::IN_PLANE; -} - NS_CC_END diff --git a/cocos/3d/CCPlane.h b/cocos/3d/CCPlane.h index 87509b32f3..cc1baf822c 100755 --- a/cocos/3d/CCPlane.h +++ b/cocos/3d/CCPlane.h @@ -100,11 +100,6 @@ public: */ PointSide getSide(const Vec3& point) const; - /** - * Return the side where the point is. - */ - PointSide getSide(const Vec3& point, const Vec3& tolerance) const; - protected: Vec3 _normal; // the normal line of the plane float _dist; // original displacement of the normal